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