1 //===-- MipsLongBranch.cpp - Emit long branches ---------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This pass expands a branch or jump instruction into a long branch if its
11 // offset is too large to fit into its immediate field.
12 //
13 // FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries.
14 //===----------------------------------------------------------------------===//
15
16 #include "Mips.h"
17 #include "MCTargetDesc/MipsBaseInfo.h"
18 #include "MCTargetDesc/MipsMCNaCl.h"
19 #include "MipsMachineFunction.h"
20 #include "MipsTargetMachine.h"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/CodeGen/MachineFunctionPass.h"
23 #include "llvm/CodeGen/MachineInstrBuilder.h"
24 #include "llvm/IR/Function.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/MathExtras.h"
27 #include "llvm/Target/TargetInstrInfo.h"
28 #include "llvm/Target/TargetMachine.h"
29 #include "llvm/Target/TargetRegisterInfo.h"
30
31 using namespace llvm;
32
33 #define DEBUG_TYPE "mips-long-branch"
34
35 STATISTIC(LongBranches, "Number of long branches.");
36
37 static cl::opt<bool> SkipLongBranch(
38 "skip-mips-long-branch",
39 cl::init(false),
40 cl::desc("MIPS: Skip long branch pass."),
41 cl::Hidden);
42
43 static cl::opt<bool> ForceLongBranch(
44 "force-mips-long-branch",
45 cl::init(false),
46 cl::desc("MIPS: Expand all branches to long format."),
47 cl::Hidden);
48
49 namespace {
50 typedef MachineBasicBlock::iterator Iter;
51 typedef MachineBasicBlock::reverse_iterator ReverseIter;
52
53 struct MBBInfo {
54 uint64_t Size, Address;
55 bool HasLongBranch;
56 MachineInstr *Br;
57
MBBInfo__anond6f6da100111::MBBInfo58 MBBInfo() : Size(0), HasLongBranch(false), Br(nullptr) {}
59 };
60
61 class MipsLongBranch : public MachineFunctionPass {
62
63 public:
64 static char ID;
MipsLongBranch(TargetMachine & tm)65 MipsLongBranch(TargetMachine &tm)
66 : MachineFunctionPass(ID), TM(tm), IsPIC(TM.isPositionIndependent()),
67 ABI(static_cast<const MipsTargetMachine &>(TM).getABI()) {}
68
getPassName() const69 const char *getPassName() const override {
70 return "Mips Long Branch";
71 }
72
73 bool runOnMachineFunction(MachineFunction &F) override;
74
getRequiredProperties() const75 MachineFunctionProperties getRequiredProperties() const override {
76 return MachineFunctionProperties().set(
77 MachineFunctionProperties::Property::AllVRegsAllocated);
78 }
79
80 private:
81 void splitMBB(MachineBasicBlock *MBB);
82 void initMBBInfo();
83 int64_t computeOffset(const MachineInstr *Br);
84 void replaceBranch(MachineBasicBlock &MBB, Iter Br, const DebugLoc &DL,
85 MachineBasicBlock *MBBOpnd);
86 void expandToLongBranch(MBBInfo &Info);
87
88 const TargetMachine &TM;
89 MachineFunction *MF;
90 SmallVector<MBBInfo, 16> MBBInfos;
91 bool IsPIC;
92 MipsABIInfo ABI;
93 unsigned LongBranchSeqSize;
94 };
95
96 char MipsLongBranch::ID = 0;
97 } // end of anonymous namespace
98
99 /// createMipsLongBranchPass - Returns a pass that converts branches to long
100 /// branches.
createMipsLongBranchPass(MipsTargetMachine & tm)101 FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) {
102 return new MipsLongBranch(tm);
103 }
104
105 /// Iterate over list of Br's operands and search for a MachineBasicBlock
106 /// operand.
getTargetMBB(const MachineInstr & Br)107 static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) {
108 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) {
109 const MachineOperand &MO = Br.getOperand(I);
110
111 if (MO.isMBB())
112 return MO.getMBB();
113 }
114
115 llvm_unreachable("This instruction does not have an MBB operand.");
116 }
117
118 // Traverse the list of instructions backwards until a non-debug instruction is
119 // found or it reaches E.
getNonDebugInstr(ReverseIter B,const ReverseIter & E)120 static ReverseIter getNonDebugInstr(ReverseIter B, const ReverseIter &E) {
121 for (; B != E; ++B)
122 if (!B->isDebugValue())
123 return B;
124
125 return E;
126 }
127
128 // Split MBB if it has two direct jumps/branches.
splitMBB(MachineBasicBlock * MBB)129 void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) {
130 ReverseIter End = MBB->rend();
131 ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End);
132
133 // Return if MBB has no branch instructions.
134 if ((LastBr == End) ||
135 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch()))
136 return;
137
138 ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End);
139
140 // MBB has only one branch instruction if FirstBr is not a branch
141 // instruction.
142 if ((FirstBr == End) ||
143 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch()))
144 return;
145
146 assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found.");
147
148 // Create a new MBB. Move instructions in MBB to the newly created MBB.
149 MachineBasicBlock *NewMBB =
150 MF->CreateMachineBasicBlock(MBB->getBasicBlock());
151
152 // Insert NewMBB and fix control flow.
153 MachineBasicBlock *Tgt = getTargetMBB(*FirstBr);
154 NewMBB->transferSuccessors(MBB);
155 NewMBB->removeSuccessor(Tgt, true);
156 MBB->addSuccessor(NewMBB);
157 MBB->addSuccessor(Tgt);
158 MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB);
159
160 NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end());
161 }
162
163 // Fill MBBInfos.
initMBBInfo()164 void MipsLongBranch::initMBBInfo() {
165 // Split the MBBs if they have two branches. Each basic block should have at
166 // most one branch after this loop is executed.
167 for (auto &MBB : *MF)
168 splitMBB(&MBB);
169
170 MF->RenumberBlocks();
171 MBBInfos.clear();
172 MBBInfos.resize(MF->size());
173
174 const MipsInstrInfo *TII =
175 static_cast<const MipsInstrInfo *>(MF->getSubtarget().getInstrInfo());
176 for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) {
177 MachineBasicBlock *MBB = MF->getBlockNumbered(I);
178
179 // Compute size of MBB.
180 for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin();
181 MI != MBB->instr_end(); ++MI)
182 MBBInfos[I].Size += TII->GetInstSizeInBytes(*MI);
183
184 // Search for MBB's branch instruction.
185 ReverseIter End = MBB->rend();
186 ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End);
187
188 if ((Br != End) && !Br->isIndirectBranch() &&
189 (Br->isConditionalBranch() || (Br->isUnconditionalBranch() && IsPIC)))
190 MBBInfos[I].Br = &*(++Br).base();
191 }
192 }
193
194 // Compute offset of branch in number of bytes.
computeOffset(const MachineInstr * Br)195 int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) {
196 int64_t Offset = 0;
197 int ThisMBB = Br->getParent()->getNumber();
198 int TargetMBB = getTargetMBB(*Br)->getNumber();
199
200 // Compute offset of a forward branch.
201 if (ThisMBB < TargetMBB) {
202 for (int N = ThisMBB + 1; N < TargetMBB; ++N)
203 Offset += MBBInfos[N].Size;
204
205 return Offset + 4;
206 }
207
208 // Compute offset of a backward branch.
209 for (int N = ThisMBB; N >= TargetMBB; --N)
210 Offset += MBBInfos[N].Size;
211
212 return -Offset + 4;
213 }
214
215 // Replace Br with a branch which has the opposite condition code and a
216 // MachineBasicBlock operand MBBOpnd.
replaceBranch(MachineBasicBlock & MBB,Iter Br,const DebugLoc & DL,MachineBasicBlock * MBBOpnd)217 void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br,
218 const DebugLoc &DL,
219 MachineBasicBlock *MBBOpnd) {
220 const MipsInstrInfo *TII = static_cast<const MipsInstrInfo *>(
221 MBB.getParent()->getSubtarget().getInstrInfo());
222 unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode());
223 const MCInstrDesc &NewDesc = TII->get(NewOpc);
224
225 MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc);
226
227 for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) {
228 MachineOperand &MO = Br->getOperand(I);
229
230 if (!MO.isReg()) {
231 assert(MO.isMBB() && "MBB operand expected.");
232 break;
233 }
234
235 MIB.addReg(MO.getReg());
236 }
237
238 MIB.addMBB(MBBOpnd);
239
240 if (Br->hasDelaySlot()) {
241 // Bundle the instruction in the delay slot to the newly created branch
242 // and erase the original branch.
243 assert(Br->isBundledWithSucc());
244 MachineBasicBlock::instr_iterator II = Br.getInstrIterator();
245 MIBundleBuilder(&*MIB).append((++II)->removeFromBundle());
246 }
247 Br->eraseFromParent();
248 }
249
250 // Expand branch instructions to long branches.
251 // TODO: This function has to be fixed for beqz16 and bnez16, because it
252 // currently assumes that all branches have 16-bit offsets, and will produce
253 // wrong code if branches whose allowed offsets are [-128, -126, ..., 126]
254 // are present.
expandToLongBranch(MBBInfo & I)255 void MipsLongBranch::expandToLongBranch(MBBInfo &I) {
256 MachineBasicBlock::iterator Pos;
257 MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br);
258 DebugLoc DL = I.Br->getDebugLoc();
259 const BasicBlock *BB = MBB->getBasicBlock();
260 MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB);
261 MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB);
262 const MipsSubtarget &Subtarget =
263 static_cast<const MipsSubtarget &>(MF->getSubtarget());
264 const MipsInstrInfo *TII =
265 static_cast<const MipsInstrInfo *>(Subtarget.getInstrInfo());
266
267 MF->insert(FallThroughMBB, LongBrMBB);
268 MBB->replaceSuccessor(TgtMBB, LongBrMBB);
269
270 if (IsPIC) {
271 MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB);
272 MF->insert(FallThroughMBB, BalTgtMBB);
273 LongBrMBB->addSuccessor(BalTgtMBB);
274 BalTgtMBB->addSuccessor(TgtMBB);
275
276 // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal
277 // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an
278 // pseudo-instruction wrapping BGEZAL).
279 unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR;
280
281 if (!ABI.IsN64()) {
282 // $longbr:
283 // addiu $sp, $sp, -8
284 // sw $ra, 0($sp)
285 // lui $at, %hi($tgt - $baltgt)
286 // bal $baltgt
287 // addiu $at, $at, %lo($tgt - $baltgt)
288 // $baltgt:
289 // addu $at, $ra, $at
290 // lw $ra, 0($sp)
291 // jr $at
292 // addiu $sp, $sp, 8
293 // $fallthrough:
294 //
295
296 Pos = LongBrMBB->begin();
297
298 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
299 .addReg(Mips::SP).addImm(-8);
300 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA)
301 .addReg(Mips::SP).addImm(0);
302
303 // LUi and ADDiu instructions create 32-bit offset of the target basic
304 // block from the target of BAL instruction. We cannot use immediate
305 // value for this offset because it cannot be determined accurately when
306 // the program has inline assembly statements. We therefore use the
307 // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which
308 // are resolved during the fixup, so the values will always be correct.
309 //
310 // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt)
311 // expressions at this point (it is possible only at the MC layer),
312 // we replace LUi and ADDiu with pseudo instructions
313 // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic
314 // blocks as operands to these instructions. When lowering these pseudo
315 // instructions to LUi and ADDiu in the MC layer, we will create
316 // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as
317 // operands to lowered instructions.
318
319 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT)
320 .addMBB(TgtMBB).addMBB(BalTgtMBB);
321 MIBundleBuilder(*LongBrMBB, Pos)
322 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
323 .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT)
324 .addReg(Mips::AT)
325 .addMBB(TgtMBB)
326 .addMBB(BalTgtMBB));
327
328 Pos = BalTgtMBB->begin();
329
330 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT)
331 .addReg(Mips::RA).addReg(Mips::AT);
332 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA)
333 .addReg(Mips::SP).addImm(0);
334
335 // In NaCl, modifying the sp is not allowed in branch delay slot.
336 if (Subtarget.isTargetNaCl())
337 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
338 .addReg(Mips::SP).addImm(8);
339
340 if (Subtarget.hasMips32r6())
341 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR))
342 .addReg(Mips::ZERO).addReg(Mips::AT);
343 else
344 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR)).addReg(Mips::AT);
345
346 if (Subtarget.isTargetNaCl()) {
347 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::NOP));
348 // Bundle-align the target of indirect branch JR.
349 TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN);
350 } else
351 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP)
352 .addReg(Mips::SP).addImm(8);
353
354 BalTgtMBB->rbegin()->bundleWithPred();
355 } else {
356 // $longbr:
357 // daddiu $sp, $sp, -16
358 // sd $ra, 0($sp)
359 // daddiu $at, $zero, %hi($tgt - $baltgt)
360 // dsll $at, $at, 16
361 // bal $baltgt
362 // daddiu $at, $at, %lo($tgt - $baltgt)
363 // $baltgt:
364 // daddu $at, $ra, $at
365 // ld $ra, 0($sp)
366 // jr64 $at
367 // daddiu $sp, $sp, 16
368 // $fallthrough:
369 //
370
371 // We assume the branch is within-function, and that offset is within
372 // +/- 2GB. High 32 bits will therefore always be zero.
373
374 // Note that this will work even if the offset is negative, because
375 // of the +1 modification that's added in that case. For example, if the
376 // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is
377 //
378 // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000
379 //
380 // and the bits [47:32] are zero. For %highest
381 //
382 // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000
383 //
384 // and the bits [63:48] are zero.
385
386 Pos = LongBrMBB->begin();
387
388 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
389 .addReg(Mips::SP_64).addImm(-16);
390 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64)
391 .addReg(Mips::SP_64).addImm(0);
392 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu),
393 Mips::AT_64).addReg(Mips::ZERO_64)
394 .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB);
395 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64)
396 .addReg(Mips::AT_64).addImm(16);
397
398 MIBundleBuilder(*LongBrMBB, Pos)
399 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB))
400 .append(
401 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64)
402 .addReg(Mips::AT_64)
403 .addMBB(TgtMBB, MipsII::MO_ABS_LO)
404 .addMBB(BalTgtMBB));
405
406 Pos = BalTgtMBB->begin();
407
408 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64)
409 .addReg(Mips::RA_64).addReg(Mips::AT_64);
410 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64)
411 .addReg(Mips::SP_64).addImm(0);
412
413 if (Subtarget.hasMips64r6())
414 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JALR64))
415 .addReg(Mips::ZERO_64).addReg(Mips::AT_64);
416 else
417 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64);
418
419 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64)
420 .addReg(Mips::SP_64).addImm(16);
421 BalTgtMBB->rbegin()->bundleWithPred();
422 }
423
424 assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize);
425 } else {
426 // $longbr:
427 // j $tgt
428 // nop
429 // $fallthrough:
430 //
431 Pos = LongBrMBB->begin();
432 LongBrMBB->addSuccessor(TgtMBB);
433 MIBundleBuilder(*LongBrMBB, Pos)
434 .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB))
435 .append(BuildMI(*MF, DL, TII->get(Mips::NOP)));
436
437 assert(LongBrMBB->size() == LongBranchSeqSize);
438 }
439
440 if (I.Br->isUnconditionalBranch()) {
441 // Change branch destination.
442 assert(I.Br->getDesc().getNumOperands() == 1);
443 I.Br->RemoveOperand(0);
444 I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB));
445 } else
446 // Change branch destination and reverse condition.
447 replaceBranch(*MBB, I.Br, DL, &*FallThroughMBB);
448 }
449
emitGPDisp(MachineFunction & F,const MipsInstrInfo * TII)450 static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) {
451 MachineBasicBlock &MBB = F.front();
452 MachineBasicBlock::iterator I = MBB.begin();
453 DebugLoc DL = MBB.findDebugLoc(MBB.begin());
454 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0)
455 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI);
456 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0)
457 .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO);
458 MBB.removeLiveIn(Mips::V0);
459 }
460
runOnMachineFunction(MachineFunction & F)461 bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) {
462 const MipsSubtarget &STI =
463 static_cast<const MipsSubtarget &>(F.getSubtarget());
464 const MipsInstrInfo *TII =
465 static_cast<const MipsInstrInfo *>(STI.getInstrInfo());
466 LongBranchSeqSize =
467 !IsPIC ? 2 : (ABI.IsN64() ? 10 : (!STI.isTargetNaCl() ? 9 : 10));
468
469 if (STI.inMips16Mode() || !STI.enableLongBranchPass())
470 return false;
471 if (IsPIC && static_cast<const MipsTargetMachine &>(TM).getABI().IsO32() &&
472 F.getInfo<MipsFunctionInfo>()->globalBaseRegSet())
473 emitGPDisp(F, TII);
474
475 if (SkipLongBranch)
476 return true;
477
478 MF = &F;
479 initMBBInfo();
480
481 SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end();
482 bool EverMadeChange = false, MadeChange = true;
483
484 while (MadeChange) {
485 MadeChange = false;
486
487 for (I = MBBInfos.begin(); I != E; ++I) {
488 // Skip if this MBB doesn't have a branch or the branch has already been
489 // converted to a long branch.
490 if (!I->Br || I->HasLongBranch)
491 continue;
492
493 int ShVal = STI.inMicroMipsMode() ? 2 : 4;
494 int64_t Offset = computeOffset(I->Br) / ShVal;
495
496 if (STI.isTargetNaCl()) {
497 // The offset calculation does not include sandboxing instructions
498 // that will be added later in the MC layer. Since at this point we
499 // don't know the exact amount of code that "sandboxing" will add, we
500 // conservatively estimate that code will not grow more than 100%.
501 Offset *= 2;
502 }
503
504 // Check if offset fits into 16-bit immediate field of branches.
505 if (!ForceLongBranch && isInt<16>(Offset))
506 continue;
507
508 I->HasLongBranch = true;
509 I->Size += LongBranchSeqSize * 4;
510 ++LongBranches;
511 EverMadeChange = MadeChange = true;
512 }
513 }
514
515 if (!EverMadeChange)
516 return true;
517
518 // Compute basic block addresses.
519 if (IsPIC) {
520 uint64_t Address = 0;
521
522 for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I)
523 I->Address = Address;
524 }
525
526 // Do the expansion.
527 for (I = MBBInfos.begin(); I != E; ++I)
528 if (I->HasLongBranch)
529 expandToLongBranch(*I);
530
531 MF->RenumberBlocks();
532
533 return true;
534 }
535