1 //===-- BranchRelaxation.cpp ----------------------------------------------===//
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 #include "llvm/CodeGen/Passes.h"
11 #include "llvm/ADT/SmallVector.h"
12 #include "llvm/ADT/Statistic.h"
13 #include "llvm/CodeGen/MachineFunctionPass.h"
14 #include "llvm/CodeGen/RegisterScavenging.h"
15 #include "llvm/Target/TargetInstrInfo.h"
16 #include "llvm/Target/TargetSubtargetInfo.h"
17 #include "llvm/Support/Debug.h"
18 #include "llvm/Support/Format.h"
19 #include "llvm/Support/raw_ostream.h"
20 
21 using namespace llvm;
22 
23 #define DEBUG_TYPE "branch-relaxation"
24 
25 STATISTIC(NumSplit, "Number of basic blocks split");
26 STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
27 STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed");
28 
29 #define BRANCH_RELAX_NAME "Branch relaxation pass"
30 
31 namespace {
32 class BranchRelaxation : public MachineFunctionPass {
33   /// BasicBlockInfo - Information about the offset and size of a single
34   /// basic block.
35   struct BasicBlockInfo {
36     /// Offset - Distance from the beginning of the function to the beginning
37     /// of this basic block.
38     ///
39     /// The offset is always aligned as required by the basic block.
40     unsigned Offset;
41 
42     /// Size - Size of the basic block in bytes.  If the block contains
43     /// inline assembly, this is a worst case estimate.
44     ///
45     /// The size does not include any alignment padding whether from the
46     /// beginning of the block, or from an aligned jump table at the end.
47     unsigned Size;
48 
49     BasicBlockInfo() : Offset(0), Size(0) {}
50 
51     /// Compute the offset immediately following this block. \p MBB is the next
52     /// block.
53     unsigned postOffset(const MachineBasicBlock &MBB) const {
54       unsigned PO = Offset + Size;
55       unsigned Align = MBB.getAlignment();
56       if (Align == 0)
57         return PO;
58 
59       unsigned AlignAmt = 1 << Align;
60       unsigned ParentAlign = MBB.getParent()->getAlignment();
61       if (Align <= ParentAlign)
62         return PO + OffsetToAlignment(PO, AlignAmt);
63 
64       // The alignment of this MBB is larger than the function's alignment, so we
65       // can't tell whether or not it will insert nops. Assume that it will.
66       return PO + AlignAmt + OffsetToAlignment(PO, AlignAmt);
67     }
68   };
69 
70   SmallVector<BasicBlockInfo, 16> BlockInfo;
71   std::unique_ptr<RegScavenger> RS;
72 
73   MachineFunction *MF;
74   const TargetInstrInfo *TII;
75 
76   bool relaxBranchInstructions();
77   void scanFunction();
78 
79   MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &BB);
80 
81   MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI);
82   void adjustBlockOffsets(MachineBasicBlock &MBB);
83   bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
84 
85   bool fixupConditionalBranch(MachineInstr &MI);
86   bool fixupUnconditionalBranch(MachineInstr &MI);
87   uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
88   unsigned getInstrOffset(const MachineInstr &MI) const;
89   void dumpBBs();
90   void verify();
91 
92 public:
93   static char ID;
94   BranchRelaxation() : MachineFunctionPass(ID) { }
95 
96   bool runOnMachineFunction(MachineFunction &MF) override;
97 
98   StringRef getPassName() const override {
99     return BRANCH_RELAX_NAME;
100   }
101 };
102 
103 }
104 
105 char BranchRelaxation::ID = 0;
106 char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
107 
108 INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
109 
110 /// verify - check BBOffsets, BBSizes, alignment of islands
111 void BranchRelaxation::verify() {
112 #ifndef NDEBUG
113   unsigned PrevNum = MF->begin()->getNumber();
114   for (MachineBasicBlock &MBB : *MF) {
115     unsigned Align = MBB.getAlignment();
116     unsigned Num = MBB.getNumber();
117     assert(BlockInfo[Num].Offset % (1u << Align) == 0);
118     assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
119     PrevNum = Num;
120   }
121 #endif
122 }
123 
124 /// print block size and offset information - debugging
125 void BranchRelaxation::dumpBBs() {
126   for (auto &MBB : *MF) {
127     const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
128     dbgs() << format("BB#%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
129            << format("size=%#x\n", BBI.Size);
130   }
131 }
132 
133 /// scanFunction - Do the initial scan of the function, building up
134 /// information about each block.
135 void BranchRelaxation::scanFunction() {
136   BlockInfo.clear();
137   BlockInfo.resize(MF->getNumBlockIDs());
138 
139   // First thing, compute the size of all basic blocks, and see if the function
140   // has any inline assembly in it. If so, we have to be conservative about
141   // alignment assumptions, as we don't know for sure the size of any
142   // instructions in the inline assembly.
143   for (MachineBasicBlock &MBB : *MF)
144     BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
145 
146   // Compute block offsets and known bits.
147   adjustBlockOffsets(*MF->begin());
148 }
149 
150 /// computeBlockSize - Compute the size for MBB.
151 uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
152   uint64_t Size = 0;
153   for (const MachineInstr &MI : MBB)
154     Size += TII->getInstSizeInBytes(MI);
155   return Size;
156 }
157 
158 /// getInstrOffset - Return the current offset of the specified machine
159 /// instruction from the start of the function.  This offset changes as stuff is
160 /// moved around inside the function.
161 unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
162   const MachineBasicBlock *MBB = MI.getParent();
163 
164   // The offset is composed of two things: the sum of the sizes of all MBB's
165   // before this instruction's block, and the offset from the start of the block
166   // it is in.
167   unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
168 
169   // Sum instructions before MI in MBB.
170   for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
171     assert(I != MBB->end() && "Didn't find MI in its own basic block?");
172     Offset += TII->getInstSizeInBytes(*I);
173   }
174 
175   return Offset;
176 }
177 
178 void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
179   unsigned PrevNum = Start.getNumber();
180   for (auto &MBB : make_range(MachineFunction::iterator(Start), MF->end())) {
181     unsigned Num = MBB.getNumber();
182     if (!Num) // block zero is never changed from offset zero.
183       continue;
184     // Get the offset and known bits at the end of the layout predecessor.
185     // Include the alignment of the current block.
186     BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
187 
188     PrevNum = Num;
189   }
190 }
191 
192   /// Insert a new empty basic block and insert it after \BB
193 MachineBasicBlock *BranchRelaxation::createNewBlockAfter(MachineBasicBlock &BB) {
194   // Create a new MBB for the code after the OrigBB.
195   MachineBasicBlock *NewBB =
196       MF->CreateMachineBasicBlock(BB.getBasicBlock());
197   MF->insert(++BB.getIterator(), NewBB);
198 
199   // Insert an entry into BlockInfo to align it properly with the block numbers.
200   BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
201 
202   return NewBB;
203 }
204 
205 /// Split the basic block containing MI into two blocks, which are joined by
206 /// an unconditional branch.  Update data structures and renumber blocks to
207 /// account for this change and returns the newly created block.
208 /// NOTE: Successor list of the original BB is out of date after this function,
209 /// and must be updated by the caller! Other transforms follow using this
210 /// utility function, so no point updating now rather than waiting.
211 MachineBasicBlock *BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI) {
212   MachineBasicBlock *OrigBB = MI.getParent();
213 
214   // Create a new MBB for the code after the OrigBB.
215   MachineBasicBlock *NewBB =
216       MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
217   MF->insert(++OrigBB->getIterator(), NewBB);
218 
219   // Splice the instructions starting with MI over to NewBB.
220   NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
221 
222   // Add an unconditional branch from OrigBB to NewBB.
223   // Note the new unconditional branch is not being recorded.
224   // There doesn't seem to be meaningful DebugInfo available; this doesn't
225   // correspond to anything in the source.
226   TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
227 
228   // Insert an entry into BlockInfo to align it properly with the block numbers.
229   BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
230 
231   // Figure out how large the OrigBB is.  As the first half of the original
232   // block, it cannot contain a tablejump.  The size includes
233   // the new jump we added.  (It should be possible to do this without
234   // recounting everything, but it's very confusing, and this is rarely
235   // executed.)
236   BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
237 
238   // Figure out how large the NewMBB is. As the second half of the original
239   // block, it may contain a tablejump.
240   BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
241 
242   // All BBOffsets following these blocks must be modified.
243   adjustBlockOffsets(*OrigBB);
244 
245   ++NumSplit;
246 
247   return NewBB;
248 }
249 
250 /// isBlockInRange - Returns true if the distance between specific MI and
251 /// specific BB can fit in MI's displacement field.
252 bool BranchRelaxation::isBlockInRange(
253   const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
254   int64_t BrOffset = getInstrOffset(MI);
255   int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
256 
257   if (TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - BrOffset))
258     return true;
259 
260   DEBUG(
261     dbgs() << "Out of range branch to destination BB#" << DestBB.getNumber()
262            << " from BB#" << MI.getParent()->getNumber()
263            << " to " << DestOffset
264            << " offset " << DestOffset - BrOffset
265            << '\t' << MI
266   );
267 
268   return false;
269 }
270 
271 /// fixupConditionalBranch - Fix up a conditional branch whose destination is
272 /// too far away to fit in its displacement field. It is converted to an inverse
273 /// conditional branch + an unconditional branch to the destination.
274 bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
275   DebugLoc DL = MI.getDebugLoc();
276   MachineBasicBlock *MBB = MI.getParent();
277   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
278   SmallVector<MachineOperand, 4> Cond;
279 
280   bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
281   assert(!Fail && "branches to be relaxed must be analyzable");
282   (void)Fail;
283 
284   // Add an unconditional branch to the destination and invert the branch
285   // condition to jump over it:
286   // tbz L1
287   // =>
288   // tbnz L2
289   // b   L1
290   // L2:
291 
292   if (FBB && isBlockInRange(MI, *FBB)) {
293     // Last MI in the BB is an unconditional branch. We can simply invert the
294     // condition and swap destinations:
295     // beq L1
296     // b   L2
297     // =>
298     // bne L2
299     // b   L1
300     DEBUG(dbgs() << "  Invert condition and swap "
301                     "its destination with " << MBB->back());
302 
303     TII->reverseBranchCondition(Cond);
304     int OldSize = 0, NewSize = 0;
305     TII->removeBranch(*MBB, &OldSize);
306     TII->insertBranch(*MBB, FBB, TBB, Cond, DL, &NewSize);
307 
308     BlockInfo[MBB->getNumber()].Size += (NewSize - OldSize);
309     return true;
310   } else if (FBB) {
311     // We need to split the basic block here to obtain two long-range
312     // unconditional branches.
313     auto &NewBB = *MF->CreateMachineBasicBlock(MBB->getBasicBlock());
314     MF->insert(++MBB->getIterator(), &NewBB);
315 
316     // Insert an entry into BlockInfo to align it properly with the block
317     // numbers.
318     BlockInfo.insert(BlockInfo.begin() + NewBB.getNumber(), BasicBlockInfo());
319 
320     unsigned &NewBBSize = BlockInfo[NewBB.getNumber()].Size;
321     int NewBrSize;
322     TII->insertUnconditionalBranch(NewBB, FBB, DL, &NewBrSize);
323     NewBBSize += NewBrSize;
324 
325     // Update the successor lists according to the transformation to follow.
326     // Do it here since if there's no split, no update is needed.
327     MBB->replaceSuccessor(FBB, &NewBB);
328     NewBB.addSuccessor(FBB);
329   }
330 
331   // We now have an appropriate fall-through block in place (either naturally or
332   // just created), so we can invert the condition.
333   MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
334 
335   DEBUG(dbgs() << "  Insert B to BB#" << TBB->getNumber()
336                << ", invert condition and change dest. to BB#"
337                << NextBB.getNumber() << '\n');
338 
339   unsigned &MBBSize = BlockInfo[MBB->getNumber()].Size;
340 
341   // Insert a new conditional branch and a new unconditional branch.
342   int RemovedSize = 0;
343   TII->reverseBranchCondition(Cond);
344   TII->removeBranch(*MBB, &RemovedSize);
345   MBBSize -= RemovedSize;
346 
347   int AddedSize = 0;
348   TII->insertBranch(*MBB, &NextBB, TBB, Cond, DL, &AddedSize);
349   MBBSize += AddedSize;
350 
351   // Finally, keep the block offsets up to date.
352   adjustBlockOffsets(*MBB);
353   return true;
354 }
355 
356 bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
357   MachineBasicBlock *MBB = MI.getParent();
358 
359   unsigned OldBrSize = TII->getInstSizeInBytes(MI);
360   MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
361 
362   int64_t DestOffset = BlockInfo[DestBB->getNumber()].Offset;
363   int64_t SrcOffset = getInstrOffset(MI);
364 
365   assert(!TII->isBranchOffsetInRange(MI.getOpcode(), DestOffset - SrcOffset));
366 
367   BlockInfo[MBB->getNumber()].Size -= OldBrSize;
368 
369   MachineBasicBlock *BranchBB = MBB;
370 
371   // If this was an expanded conditional branch, there is already a single
372   // unconditional branch in a block.
373   if (!MBB->empty()) {
374     BranchBB = createNewBlockAfter(*MBB);
375 
376     // Add live outs.
377     for (const MachineBasicBlock *Succ : MBB->successors()) {
378       for (const MachineBasicBlock::RegisterMaskPair &LiveIn : Succ->liveins())
379         BranchBB->addLiveIn(LiveIn);
380     }
381 
382     BranchBB->sortUniqueLiveIns();
383     BranchBB->addSuccessor(DestBB);
384     MBB->replaceSuccessor(DestBB, BranchBB);
385   }
386 
387   DebugLoc DL = MI.getDebugLoc();
388   MI.eraseFromParent();
389 
390   // insertUnconditonalBranch may have inserted a new block.
391   BlockInfo[MBB->getNumber()].Size += TII->insertIndirectBranch(
392     *BranchBB, *DestBB, DL, DestOffset - SrcOffset, RS.get());
393 
394   computeBlockSize(*BranchBB);
395   adjustBlockOffsets(*MBB);
396   return true;
397 }
398 
399 bool BranchRelaxation::relaxBranchInstructions() {
400   bool Changed = false;
401 
402   // Relaxing branches involves creating new basic blocks, so re-eval
403   // end() for termination.
404   for (MachineFunction::iterator I = MF->begin(); I != MF->end(); ++I) {
405     MachineBasicBlock &MBB = *I;
406 
407     MachineBasicBlock::iterator Next;
408     for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
409          J != MBB.end(); J = Next) {
410       Next = std::next(J);
411       MachineInstr &MI = *J;
412 
413       if (MI.isConditionalBranch()) {
414         MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
415         if (!isBlockInRange(MI, *DestBB)) {
416           if (Next != MBB.end() && Next->isConditionalBranch()) {
417             // If there are multiple conditional branches, this isn't an
418             // analyzable block. Split later terminators into a new block so
419             // each one will be analyzable.
420 
421             MachineBasicBlock *NewBB = splitBlockBeforeInstr(*Next);
422             NewBB->transferSuccessors(&MBB);
423             MBB.addSuccessor(NewBB);
424             MBB.addSuccessor(DestBB);
425 
426             // Cleanup potential unconditional branch to successor block.
427             NewBB->updateTerminator();
428             MBB.updateTerminator();
429           } else {
430             fixupConditionalBranch(MI);
431             ++NumConditionalRelaxed;
432           }
433 
434           Changed = true;
435 
436           // This may have modified all of the terminators, so start over.
437           Next = MBB.getFirstTerminator();
438         }
439       }
440 
441       if (MI.isUnconditionalBranch()) {
442         // Unconditional branch destination might be unanalyzable, assume these
443         // are OK.
444         if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI)) {
445           if (!isBlockInRange(MI, *DestBB)) {
446             fixupUnconditionalBranch(MI);
447             ++NumUnconditionalRelaxed;
448             Changed = true;
449           }
450         }
451 
452         // Unconditional branch is the last terminator.
453         break;
454       }
455     }
456   }
457 
458   return Changed;
459 }
460 
461 bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
462   MF = &mf;
463 
464   DEBUG(dbgs() << "***** BranchRelaxation *****\n");
465 
466   const TargetSubtargetInfo &ST = MF->getSubtarget();
467   TII = ST.getInstrInfo();
468 
469   const TargetRegisterInfo *TRI = ST.getRegisterInfo();
470   if (TRI->trackLivenessAfterRegAlloc(*MF))
471     RS.reset(new RegScavenger());
472 
473   // Renumber all of the machine basic blocks in the function, guaranteeing that
474   // the numbers agree with the position of the block in the function.
475   MF->RenumberBlocks();
476 
477   // Do the initial scan of the function, building up information about the
478   // sizes of each block.
479   scanFunction();
480 
481   DEBUG(dbgs() << "  Basic blocks before relaxation\n"; dumpBBs(););
482 
483   bool MadeChange = false;
484   while (relaxBranchInstructions())
485     MadeChange = true;
486 
487   // After a while, this might be made debug-only, but it is not expensive.
488   verify();
489 
490   DEBUG(dbgs() << "  Basic blocks after relaxation\n\n"; dumpBBs());
491 
492   BlockInfo.clear();
493 
494   return MadeChange;
495 }
496