xref: /llvm-project/llvm/lib/CodeGen/BranchRelaxation.cpp (revision e223e4567722661f1b32aa052cd13d9f47b896d9)
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/ErrorHandling.h"
27 #include "llvm/Support/Format.h"
28 #include "llvm/Support/raw_ostream.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include <cassert>
31 #include <cstdint>
32 #include <iterator>
33 #include <memory>
34 
35 using namespace llvm;
36 
37 #define DEBUG_TYPE "branch-relaxation"
38 
39 STATISTIC(NumSplit, "Number of basic blocks split");
40 STATISTIC(NumConditionalRelaxed, "Number of conditional branches relaxed");
41 STATISTIC(NumUnconditionalRelaxed, "Number of unconditional branches relaxed");
42 
43 #define BRANCH_RELAX_NAME "Branch relaxation pass"
44 
45 namespace {
46 
47 class BranchRelaxation : public MachineFunctionPass {
48   /// BasicBlockInfo - Information about the offset and size of a single
49   /// basic block.
50   struct BasicBlockInfo {
51     /// Offset - Distance from the beginning of the function to the beginning
52     /// of this basic block.
53     ///
54     /// The offset is always aligned as required by the basic block.
55     unsigned Offset = 0;
56 
57     /// Size - Size of the basic block in bytes.  If the block contains
58     /// inline assembly, this is a worst case estimate.
59     ///
60     /// The size does not include any alignment padding whether from the
61     /// beginning of the block, or from an aligned jump table at the end.
62     unsigned Size = 0;
63 
64     BasicBlockInfo() = default;
65 
66     /// Compute the offset immediately following this block. \p MBB is the next
67     /// block.
68     unsigned postOffset(const MachineBasicBlock &MBB) const {
69       const unsigned PO = Offset + Size;
70       const Align Alignment = MBB.getAlignment();
71       const Align ParentAlign = MBB.getParent()->getAlignment();
72       if (Alignment <= ParentAlign)
73         return alignTo(PO, Alignment);
74 
75       // The alignment of this MBB is larger than the function's alignment, so we
76       // can't tell whether or not it will insert nops. Assume that it will.
77       return alignTo(PO, Alignment) + Alignment.value() - ParentAlign.value();
78     }
79   };
80 
81   SmallVector<BasicBlockInfo, 16> BlockInfo;
82 
83   // The basic block after which trampolines are inserted. This is the last
84   // basic block that isn't in the cold section.
85   MachineBasicBlock *TrampolineInsertionPoint = nullptr;
86   std::unique_ptr<RegScavenger> RS;
87   LivePhysRegs LiveRegs;
88 
89   MachineFunction *MF = nullptr;
90   const TargetRegisterInfo *TRI = nullptr;
91   const TargetInstrInfo *TII = nullptr;
92   const TargetMachine *TM = nullptr;
93 
94   bool relaxBranchInstructions();
95   void scanFunction();
96 
97   MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &OrigMBB);
98   MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &OrigMBB,
99                                          const BasicBlock *BB);
100 
101   MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI,
102                                            MachineBasicBlock *DestBB);
103   void adjustBlockOffsets(MachineBasicBlock &Start);
104   bool isBlockInRange(const MachineInstr &MI, const MachineBasicBlock &BB) const;
105 
106   bool fixupConditionalBranch(MachineInstr &MI);
107   bool fixupUnconditionalBranch(MachineInstr &MI);
108   uint64_t computeBlockSize(const MachineBasicBlock &MBB) const;
109   unsigned getInstrOffset(const MachineInstr &MI) const;
110   void dumpBBs();
111   void verify();
112 
113 public:
114   static char ID;
115 
116   BranchRelaxation() : MachineFunctionPass(ID) {}
117 
118   bool runOnMachineFunction(MachineFunction &MF) override;
119 
120   StringRef getPassName() const override { return BRANCH_RELAX_NAME; }
121 };
122 
123 } // end anonymous namespace
124 
125 char BranchRelaxation::ID = 0;
126 
127 char &llvm::BranchRelaxationPassID = BranchRelaxation::ID;
128 
129 INITIALIZE_PASS(BranchRelaxation, DEBUG_TYPE, BRANCH_RELAX_NAME, false, false)
130 
131 /// verify - check BBOffsets, BBSizes, alignment of islands
132 void BranchRelaxation::verify() {
133 #ifndef NDEBUG
134   unsigned PrevNum = MF->begin()->getNumber();
135   for (MachineBasicBlock &MBB : *MF) {
136     const unsigned Num = MBB.getNumber();
137     assert(!Num || BlockInfo[PrevNum].postOffset(MBB) <= BlockInfo[Num].Offset);
138     assert(BlockInfo[Num].Size == computeBlockSize(MBB));
139     PrevNum = Num;
140   }
141 
142   for (MachineBasicBlock &MBB : *MF) {
143     for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
144          J != MBB.end(); J = std::next(J)) {
145       MachineInstr &MI = *J;
146       if (!MI.isConditionalBranch() && !MI.isUnconditionalBranch())
147         continue;
148       if (MI.getOpcode() == TargetOpcode::FAULTING_OP)
149         continue;
150       MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
151       assert(isBlockInRange(MI, *DestBB));
152     }
153   }
154 #endif
155 }
156 
157 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
158 /// print block size and offset information - debugging
159 LLVM_DUMP_METHOD void BranchRelaxation::dumpBBs() {
160   for (auto &MBB : *MF) {
161     const BasicBlockInfo &BBI = BlockInfo[MBB.getNumber()];
162     dbgs() << format("%%bb.%u\toffset=%08x\t", MBB.getNumber(), BBI.Offset)
163            << format("size=%#x\n", BBI.Size);
164   }
165 }
166 #endif
167 
168 /// scanFunction - Do the initial scan of the function, building up
169 /// information about each block.
170 void BranchRelaxation::scanFunction() {
171   BlockInfo.clear();
172   BlockInfo.resize(MF->getNumBlockIDs());
173   TrampolineInsertionPoint = nullptr;
174 
175   // First thing, compute the size of all basic blocks, and see if the function
176   // has any inline assembly in it. If so, we have to be conservative about
177   // alignment assumptions, as we don't know for sure the size of any
178   // instructions in the inline assembly. At the same time, place the
179   // trampoline insertion point at the end of the hot portion of the function.
180   for (MachineBasicBlock &MBB : *MF) {
181     BlockInfo[MBB.getNumber()].Size = computeBlockSize(MBB);
182 
183     if (MBB.getSectionID() != MBBSectionID::ColdSectionID)
184       TrampolineInsertionPoint = &MBB;
185   }
186 
187   // Compute block offsets and known bits.
188   adjustBlockOffsets(*MF->begin());
189 
190   if (TrampolineInsertionPoint == nullptr) {
191     LLVM_DEBUG(dbgs() << "  No suitable trampoline insertion point found in "
192                       << MF->getName() << ".\n");
193   }
194 }
195 
196 /// computeBlockSize - Compute the size for MBB.
197 uint64_t BranchRelaxation::computeBlockSize(const MachineBasicBlock &MBB) const {
198   uint64_t Size = 0;
199   for (const MachineInstr &MI : MBB)
200     Size += TII->getInstSizeInBytes(MI);
201   return Size;
202 }
203 
204 /// getInstrOffset - Return the current offset of the specified machine
205 /// instruction from the start of the function.  This offset changes as stuff is
206 /// moved around inside the function.
207 unsigned BranchRelaxation::getInstrOffset(const MachineInstr &MI) const {
208   const MachineBasicBlock *MBB = MI.getParent();
209 
210   // The offset is composed of two things: the sum of the sizes of all MBB's
211   // before this instruction's block, and the offset from the start of the block
212   // it is in.
213   unsigned Offset = BlockInfo[MBB->getNumber()].Offset;
214 
215   // Sum instructions before MI in MBB.
216   for (MachineBasicBlock::const_iterator I = MBB->begin(); &*I != &MI; ++I) {
217     assert(I != MBB->end() && "Didn't find MI in its own basic block?");
218     Offset += TII->getInstSizeInBytes(*I);
219   }
220 
221   return Offset;
222 }
223 
224 void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) {
225   unsigned PrevNum = Start.getNumber();
226   for (auto &MBB :
227        make_range(std::next(MachineFunction::iterator(Start)), MF->end())) {
228     unsigned Num = MBB.getNumber();
229     // Get the offset and known bits at the end of the layout predecessor.
230     // Include the alignment of the current block.
231     BlockInfo[Num].Offset = BlockInfo[PrevNum].postOffset(MBB);
232 
233     PrevNum = Num;
234   }
235 }
236 
237 /// Insert a new empty MachineBasicBlock and insert it after \p OrigMBB
238 MachineBasicBlock *
239 BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigBB) {
240   return createNewBlockAfter(OrigBB, OrigBB.getBasicBlock());
241 }
242 
243 /// Insert a new empty MachineBasicBlock with \p BB as its BasicBlock
244 /// and insert it after \p OrigMBB
245 MachineBasicBlock *
246 BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigMBB,
247                                       const BasicBlock *BB) {
248   // Create a new MBB for the code after the OrigBB.
249   MachineBasicBlock *NewBB = MF->CreateMachineBasicBlock(BB);
250   MF->insert(++OrigMBB.getIterator(), NewBB);
251 
252   // Place the new block in the same section as OrigBB
253   NewBB->setSectionID(OrigMBB.getSectionID());
254   NewBB->setIsEndSection(OrigMBB.isEndSection());
255   OrigMBB.setIsEndSection(false);
256 
257   // Insert an entry into BlockInfo to align it properly with the block numbers.
258   BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
259 
260   return NewBB;
261 }
262 
263 /// Split the basic block containing MI into two blocks, which are joined by
264 /// an unconditional branch.  Update data structures and renumber blocks to
265 /// account for this change and returns the newly created block.
266 MachineBasicBlock *
267 BranchRelaxation::splitBlockBeforeInstr(MachineInstr &MI,
268                                         MachineBasicBlock *DestBB) {
269   MachineBasicBlock *OrigBB = MI.getParent();
270 
271   // Create a new MBB for the code after the OrigBB.
272   MachineBasicBlock *NewBB =
273       MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
274   MF->insert(++OrigBB->getIterator(), NewBB);
275 
276   // Place the new block in the same section as OrigBB.
277   NewBB->setSectionID(OrigBB->getSectionID());
278   NewBB->setIsEndSection(OrigBB->isEndSection());
279   OrigBB->setIsEndSection(false);
280 
281   // Splice the instructions starting with MI over to NewBB.
282   NewBB->splice(NewBB->end(), OrigBB, MI.getIterator(), OrigBB->end());
283 
284   // Add an unconditional branch from OrigBB to NewBB.
285   // Note the new unconditional branch is not being recorded.
286   // There doesn't seem to be meaningful DebugInfo available; this doesn't
287   // correspond to anything in the source.
288   TII->insertUnconditionalBranch(*OrigBB, NewBB, DebugLoc());
289 
290   // Insert an entry into BlockInfo to align it properly with the block numbers.
291   BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
292 
293   NewBB->transferSuccessors(OrigBB);
294   OrigBB->addSuccessor(NewBB);
295   OrigBB->addSuccessor(DestBB);
296 
297   // Cleanup potential unconditional branch to successor block.
298   // Note that updateTerminator may change the size of the blocks.
299   OrigBB->updateTerminator(NewBB);
300 
301   // Figure out how large the OrigBB is.  As the first half of the original
302   // block, it cannot contain a tablejump.  The size includes
303   // the new jump we added.  (It should be possible to do this without
304   // recounting everything, but it's very confusing, and this is rarely
305   // executed.)
306   BlockInfo[OrigBB->getNumber()].Size = computeBlockSize(*OrigBB);
307 
308   // Figure out how large the NewMBB is. As the second half of the original
309   // block, it may contain a tablejump.
310   BlockInfo[NewBB->getNumber()].Size = computeBlockSize(*NewBB);
311 
312   // All BBOffsets following these blocks must be modified.
313   adjustBlockOffsets(*OrigBB);
314 
315   // Need to fix live-in lists if we track liveness.
316   if (TRI->trackLivenessAfterRegAlloc(*MF))
317     computeAndAddLiveIns(LiveRegs, *NewBB);
318 
319   ++NumSplit;
320 
321   return NewBB;
322 }
323 
324 /// isBlockInRange - Returns true if the distance between specific MI and
325 /// specific BB can fit in MI's displacement field.
326 bool BranchRelaxation::isBlockInRange(
327   const MachineInstr &MI, const MachineBasicBlock &DestBB) const {
328   int64_t BrOffset = getInstrOffset(MI);
329   int64_t DestOffset = BlockInfo[DestBB.getNumber()].Offset;
330 
331   const MachineBasicBlock *SrcBB = MI.getParent();
332 
333   if (TII->isBranchOffsetInRange(MI.getOpcode(),
334                                  SrcBB->getSectionID() != DestBB.getSectionID()
335                                      ? TM->getMaxCodeSize()
336                                      : DestOffset - BrOffset))
337     return true;
338 
339   LLVM_DEBUG(dbgs() << "Out of range branch to destination "
340                     << printMBBReference(DestBB) << " from "
341                     << printMBBReference(*MI.getParent()) << " to "
342                     << DestOffset << " offset " << DestOffset - BrOffset << '\t'
343                     << MI);
344 
345   return false;
346 }
347 
348 /// fixupConditionalBranch - Fix up a conditional branch whose destination is
349 /// too far away to fit in its displacement field. It is converted to an inverse
350 /// conditional branch + an unconditional branch to the destination.
351 bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) {
352   DebugLoc DL = MI.getDebugLoc();
353   MachineBasicBlock *MBB = MI.getParent();
354   MachineBasicBlock *TBB = nullptr, *FBB = nullptr;
355   MachineBasicBlock *NewBB = nullptr;
356   SmallVector<MachineOperand, 4> Cond;
357 
358   auto insertUncondBranch = [&](MachineBasicBlock *MBB,
359                                 MachineBasicBlock *DestBB) {
360     unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
361     int NewBrSize = 0;
362     TII->insertUnconditionalBranch(*MBB, DestBB, DL, &NewBrSize);
363     BBSize += NewBrSize;
364   };
365   auto insertBranch = [&](MachineBasicBlock *MBB, MachineBasicBlock *TBB,
366                           MachineBasicBlock *FBB,
367                           SmallVectorImpl<MachineOperand>& Cond) {
368     unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
369     int NewBrSize = 0;
370     TII->insertBranch(*MBB, TBB, FBB, Cond, DL, &NewBrSize);
371     BBSize += NewBrSize;
372   };
373   auto removeBranch = [&](MachineBasicBlock *MBB) {
374     unsigned &BBSize = BlockInfo[MBB->getNumber()].Size;
375     int RemovedSize = 0;
376     TII->removeBranch(*MBB, &RemovedSize);
377     BBSize -= RemovedSize;
378   };
379 
380   auto finalizeBlockChanges = [&](MachineBasicBlock *MBB,
381                                   MachineBasicBlock *NewBB) {
382     // Keep the block offsets up to date.
383     adjustBlockOffsets(*MBB);
384 
385     // Need to fix live-in lists if we track liveness.
386     if (NewBB && TRI->trackLivenessAfterRegAlloc(*MF))
387       computeAndAddLiveIns(LiveRegs, *NewBB);
388   };
389 
390   bool Fail = TII->analyzeBranch(*MBB, TBB, FBB, Cond);
391   assert(!Fail && "branches to be relaxed must be analyzable");
392   (void)Fail;
393 
394   // Since cross-section conditional branches to the cold section are rarely
395   // taken, try to avoid inverting the condition. Instead, add a "trampoline
396   // branch", which unconditionally branches to the branch destination. Place
397   // the trampoline branch at the end of the function and retarget the
398   // conditional branch to the trampoline.
399   // tbz L1
400   // =>
401   // tbz L1Trampoline
402   // ...
403   // L1Trampoline: b  L1
404   if (MBB->getSectionID() != TBB->getSectionID() &&
405       TBB->getSectionID() == MBBSectionID::ColdSectionID &&
406       TrampolineInsertionPoint != nullptr) {
407     // If the insertion point is out of range, we can't put a trampoline there.
408     NewBB =
409         createNewBlockAfter(*TrampolineInsertionPoint, MBB->getBasicBlock());
410 
411     if (isBlockInRange(MI, *NewBB)) {
412       LLVM_DEBUG(dbgs() << "  Retarget destination to trampoline at "
413                         << NewBB->back());
414 
415       insertUncondBranch(NewBB, TBB);
416 
417       // Update the successor lists to include the trampoline.
418       MBB->replaceSuccessor(TBB, NewBB);
419       NewBB->addSuccessor(TBB);
420 
421       // Replace branch in the current (MBB) block.
422       removeBranch(MBB);
423       insertBranch(MBB, NewBB, FBB, Cond);
424 
425       TrampolineInsertionPoint = NewBB;
426       finalizeBlockChanges(MBB, NewBB);
427       return true;
428     }
429 
430     LLVM_DEBUG(
431         dbgs() << "  Trampoline insertion point out of range for Bcc from "
432                << printMBBReference(*MBB) << " to " << printMBBReference(*TBB)
433                << ".\n");
434     TrampolineInsertionPoint->setIsEndSection(NewBB->isEndSection());
435     MF->erase(NewBB);
436   }
437 
438   // Add an unconditional branch to the destination and invert the branch
439   // condition to jump over it:
440   // tbz L1
441   // =>
442   // tbnz L2
443   // b   L1
444   // L2:
445 
446   bool ReversedCond = !TII->reverseBranchCondition(Cond);
447   if (ReversedCond) {
448     if (FBB && isBlockInRange(MI, *FBB)) {
449       // Last MI in the BB is an unconditional branch. We can simply invert the
450       // condition and swap destinations:
451       // beq L1
452       // b   L2
453       // =>
454       // bne L2
455       // b   L1
456       LLVM_DEBUG(dbgs() << "  Invert condition and swap "
457                            "its destination with "
458                         << MBB->back());
459 
460       removeBranch(MBB);
461       insertBranch(MBB, FBB, TBB, Cond);
462       finalizeBlockChanges(MBB, nullptr);
463       return true;
464     }
465     if (FBB) {
466       // We need to split the basic block here to obtain two long-range
467       // unconditional branches.
468       NewBB = createNewBlockAfter(*MBB);
469 
470       insertUncondBranch(NewBB, FBB);
471       // Update the succesor lists according to the transformation to follow.
472       // Do it here since if there's no split, no update is needed.
473       MBB->replaceSuccessor(FBB, NewBB);
474       NewBB->addSuccessor(FBB);
475     }
476 
477     // We now have an appropriate fall-through block in place (either naturally or
478     // just created), so we can use the inverted the condition.
479     MachineBasicBlock &NextBB = *std::next(MachineFunction::iterator(MBB));
480 
481     LLVM_DEBUG(dbgs() << "  Insert B to " << printMBBReference(*TBB)
482                       << ", invert condition and change dest. to "
483                       << printMBBReference(NextBB) << '\n');
484 
485     removeBranch(MBB);
486     // Insert a new conditional branch and a new unconditional branch.
487     insertBranch(MBB, &NextBB, TBB, Cond);
488 
489     finalizeBlockChanges(MBB, NewBB);
490     return true;
491   }
492   // Branch cond can't be inverted.
493   // In this case we always add a block after the MBB.
494   LLVM_DEBUG(dbgs() << "  The branch condition can't be inverted. "
495                     << "  Insert a new BB after " << MBB->back());
496 
497   if (!FBB)
498     FBB = &(*std::next(MachineFunction::iterator(MBB)));
499 
500   // This is the block with cond. branch and the distance to TBB is too long.
501   //    beq L1
502   // L2:
503 
504   // We do the following transformation:
505   //    beq NewBB
506   //    b L2
507   // NewBB:
508   //    b L1
509   // L2:
510 
511   NewBB = createNewBlockAfter(*MBB);
512   insertUncondBranch(NewBB, TBB);
513 
514   LLVM_DEBUG(dbgs() << "  Insert cond B to the new BB "
515                     << printMBBReference(*NewBB)
516                     << "  Keep the exiting condition.\n"
517                     << "  Insert B to " << printMBBReference(*FBB) << ".\n"
518                     << "  In the new BB: Insert B to "
519                     << printMBBReference(*TBB) << ".\n");
520 
521   // Update the successor lists according to the transformation to follow.
522   MBB->replaceSuccessor(TBB, NewBB);
523   NewBB->addSuccessor(TBB);
524 
525   // Replace branch in the current (MBB) block.
526   removeBranch(MBB);
527   insertBranch(MBB, NewBB, FBB, Cond);
528 
529   finalizeBlockChanges(MBB, NewBB);
530   return true;
531 }
532 
533 bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) {
534   MachineBasicBlock *MBB = MI.getParent();
535   SmallVector<MachineOperand, 4> Cond;
536   unsigned OldBrSize = TII->getInstSizeInBytes(MI);
537   MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
538 
539   int64_t DestOffset = BlockInfo[DestBB->getNumber()].Offset;
540   int64_t SrcOffset = getInstrOffset(MI);
541 
542   assert(!TII->isBranchOffsetInRange(
543       MI.getOpcode(), MBB->getSectionID() != DestBB->getSectionID()
544                           ? TM->getMaxCodeSize()
545                           : DestOffset - SrcOffset));
546 
547   BlockInfo[MBB->getNumber()].Size -= OldBrSize;
548 
549   MachineBasicBlock *BranchBB = MBB;
550 
551   // If this was an expanded conditional branch, there is already a single
552   // unconditional branch in a block.
553   if (!MBB->empty()) {
554     BranchBB = createNewBlockAfter(*MBB);
555 
556     // Add live outs.
557     for (const MachineBasicBlock *Succ : MBB->successors()) {
558       for (const MachineBasicBlock::RegisterMaskPair &LiveIn : Succ->liveins())
559         BranchBB->addLiveIn(LiveIn);
560     }
561 
562     BranchBB->sortUniqueLiveIns();
563     BranchBB->addSuccessor(DestBB);
564     MBB->replaceSuccessor(DestBB, BranchBB);
565   }
566 
567   DebugLoc DL = MI.getDebugLoc();
568   MI.eraseFromParent();
569 
570   // Create the optional restore block and, initially, place it at the end of
571   // function. That block will be placed later if it's used; otherwise, it will
572   // be erased.
573   MachineBasicBlock *RestoreBB = createNewBlockAfter(MF->back(),
574                                                      DestBB->getBasicBlock());
575   std::prev(RestoreBB->getIterator())
576       ->setIsEndSection(RestoreBB->isEndSection());
577   RestoreBB->setIsEndSection(false);
578 
579   TII->insertIndirectBranch(*BranchBB, *DestBB, *RestoreBB, DL,
580                             BranchBB->getSectionID() != DestBB->getSectionID()
581                                 ? TM->getMaxCodeSize()
582                                 : DestOffset - SrcOffset,
583                             RS.get());
584 
585   BlockInfo[BranchBB->getNumber()].Size = computeBlockSize(*BranchBB);
586   adjustBlockOffsets(*MBB);
587 
588   // If RestoreBB is required, try to place just before DestBB.
589   if (!RestoreBB->empty()) {
590     // TODO: For multiple far branches to the same destination, there are
591     // chances that some restore blocks could be shared if they clobber the
592     // same registers and share the same restore sequence. So far, those
593     // restore blocks are just duplicated for each far branch.
594     assert(!DestBB->isEntryBlock());
595     MachineBasicBlock *PrevBB = &*std::prev(DestBB->getIterator());
596     // Fall through only if PrevBB has no unconditional branch as one of its
597     // terminators.
598     if (auto *FT = PrevBB->getLogicalFallThrough()) {
599       assert(FT == DestBB);
600       TII->insertUnconditionalBranch(*PrevBB, FT, DebugLoc());
601       BlockInfo[PrevBB->getNumber()].Size = computeBlockSize(*PrevBB);
602     }
603     // Now, RestoreBB could be placed directly before DestBB.
604     MF->splice(DestBB->getIterator(), RestoreBB->getIterator());
605     // Update successors and predecessors.
606     RestoreBB->addSuccessor(DestBB);
607     BranchBB->replaceSuccessor(DestBB, RestoreBB);
608     if (TRI->trackLivenessAfterRegAlloc(*MF))
609       computeAndAddLiveIns(LiveRegs, *RestoreBB);
610     // Compute the restore block size.
611     BlockInfo[RestoreBB->getNumber()].Size = computeBlockSize(*RestoreBB);
612     // Update the offset starting from the previous block.
613     adjustBlockOffsets(*PrevBB);
614 
615     // Fix up section information for RestoreBB and DestBB
616     RestoreBB->setSectionID(DestBB->getSectionID());
617     RestoreBB->setIsBeginSection(DestBB->isBeginSection());
618     DestBB->setIsBeginSection(false);
619   } else {
620     // Remove restore block if it's not required.
621     MF->erase(RestoreBB);
622   }
623 
624   return true;
625 }
626 
627 bool BranchRelaxation::relaxBranchInstructions() {
628   bool Changed = false;
629 
630   // Relaxing branches involves creating new basic blocks, so re-eval
631   // end() for termination.
632   for (MachineBasicBlock &MBB : *MF) {
633     // Empty block?
634     MachineBasicBlock::iterator Last = MBB.getLastNonDebugInstr();
635     if (Last == MBB.end())
636       continue;
637 
638     // Expand the unconditional branch first if necessary. If there is a
639     // conditional branch, this will end up changing the branch destination of
640     // it to be over the newly inserted indirect branch block, which may avoid
641     // the need to try expanding the conditional branch first, saving an extra
642     // jump.
643     if (Last->isUnconditionalBranch()) {
644       // Unconditional branch destination might be unanalyzable, assume these
645       // are OK.
646       if (MachineBasicBlock *DestBB = TII->getBranchDestBlock(*Last)) {
647         if (!isBlockInRange(*Last, *DestBB) && !TII->isTailCall(*Last)) {
648           fixupUnconditionalBranch(*Last);
649           ++NumUnconditionalRelaxed;
650           Changed = true;
651         }
652       }
653     }
654 
655     // Loop over the conditional branches.
656     MachineBasicBlock::iterator Next;
657     for (MachineBasicBlock::iterator J = MBB.getFirstTerminator();
658          J != MBB.end(); J = Next) {
659       Next = std::next(J);
660       MachineInstr &MI = *J;
661 
662       if (!MI.isConditionalBranch())
663         continue;
664 
665       if (MI.getOpcode() == TargetOpcode::FAULTING_OP)
666         // FAULTING_OP's destination is not encoded in the instruction stream
667         // and thus never needs relaxed.
668         continue;
669 
670       MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI);
671       if (!isBlockInRange(MI, *DestBB)) {
672         if (Next != MBB.end() && Next->isConditionalBranch()) {
673           // If there are multiple conditional branches, this isn't an
674           // analyzable block. Split later terminators into a new block so
675           // each one will be analyzable.
676 
677           splitBlockBeforeInstr(*Next, DestBB);
678         } else {
679           fixupConditionalBranch(MI);
680           ++NumConditionalRelaxed;
681         }
682 
683         Changed = true;
684 
685         // This may have modified all of the terminators, so start over.
686         Next = MBB.getFirstTerminator();
687       }
688     }
689   }
690 
691   return Changed;
692 }
693 
694 bool BranchRelaxation::runOnMachineFunction(MachineFunction &mf) {
695   MF = &mf;
696 
697   LLVM_DEBUG(dbgs() << "***** BranchRelaxation *****\n");
698 
699   const TargetSubtargetInfo &ST = MF->getSubtarget();
700   TII = ST.getInstrInfo();
701   TM = &MF->getTarget();
702 
703   TRI = ST.getRegisterInfo();
704   if (TRI->trackLivenessAfterRegAlloc(*MF))
705     RS.reset(new RegScavenger());
706 
707   // Renumber all of the machine basic blocks in the function, guaranteeing that
708   // the numbers agree with the position of the block in the function.
709   MF->RenumberBlocks();
710 
711   // Do the initial scan of the function, building up information about the
712   // sizes of each block.
713   scanFunction();
714 
715   LLVM_DEBUG(dbgs() << "  Basic blocks before relaxation\n"; dumpBBs(););
716 
717   bool MadeChange = false;
718   while (relaxBranchInstructions())
719     MadeChange = true;
720 
721   // After a while, this might be made debug-only, but it is not expensive.
722   verify();
723 
724   LLVM_DEBUG(dbgs() << "  Basic blocks after relaxation\n\n"; dumpBBs());
725 
726   BlockInfo.clear();
727 
728   return MadeChange;
729 }
730