diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:04 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2023-02-11 12:38:11 +0000 |
commit | e3b557809604d036af6e00c60f012c2025b59a5e (patch) | |
tree | 8a11ba2269a3b669601e2fd41145b174008f4da8 /llvm/lib/CodeGen/BranchRelaxation.cpp | |
parent | 08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff) |
Diffstat (limited to 'llvm/lib/CodeGen/BranchRelaxation.cpp')
-rw-r--r-- | llvm/lib/CodeGen/BranchRelaxation.cpp | 33 |
1 files changed, 23 insertions, 10 deletions
diff --git a/llvm/lib/CodeGen/BranchRelaxation.cpp b/llvm/lib/CodeGen/BranchRelaxation.cpp index 29508f8f35a6..016c81dc5aa4 100644 --- a/llvm/lib/CodeGen/BranchRelaxation.cpp +++ b/llvm/lib/CodeGen/BranchRelaxation.cpp @@ -23,6 +23,7 @@ #include "llvm/Pass.h" #include "llvm/Support/Compiler.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" #include <cassert> @@ -87,7 +88,9 @@ class BranchRelaxation : public MachineFunctionPass { bool relaxBranchInstructions(); void scanFunction(); - MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &BB); + MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &OrigMBB); + MachineBasicBlock *createNewBlockAfter(MachineBasicBlock &OrigMBB, + const BasicBlock *BB); MachineBasicBlock *splitBlockBeforeInstr(MachineInstr &MI, MachineBasicBlock *DestBB); @@ -201,12 +204,20 @@ void BranchRelaxation::adjustBlockOffsets(MachineBasicBlock &Start) { } } -/// Insert a new empty basic block and insert it after \BB -MachineBasicBlock *BranchRelaxation::createNewBlockAfter(MachineBasicBlock &BB) { +/// Insert a new empty MachineBasicBlock and insert it after \p OrigMBB +MachineBasicBlock * +BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigBB) { + return createNewBlockAfter(OrigBB, OrigBB.getBasicBlock()); +} + +/// Insert a new empty MachineBasicBlock with \p BB as its BasicBlock +/// and insert it after \p OrigMBB +MachineBasicBlock * +BranchRelaxation::createNewBlockAfter(MachineBasicBlock &OrigMBB, + const BasicBlock *BB) { // Create a new MBB for the code after the OrigBB. - MachineBasicBlock *NewBB = - MF->CreateMachineBasicBlock(BB.getBasicBlock()); - MF->insert(++BB.getIterator(), NewBB); + MachineBasicBlock *NewBB = MF->CreateMachineBasicBlock(BB); + MF->insert(++OrigMBB.getIterator(), NewBB); // Insert an entry into BlockInfo to align it properly with the block numbers. BlockInfo.insert(BlockInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); @@ -431,7 +442,7 @@ bool BranchRelaxation::fixupConditionalBranch(MachineInstr &MI) { bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) { MachineBasicBlock *MBB = MI.getParent(); - + SmallVector<MachineOperand, 4> Cond; unsigned OldBrSize = TII->getInstSizeInBytes(MI); MachineBasicBlock *DestBB = TII->getBranchDestBlock(MI); @@ -466,7 +477,8 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) { // Create the optional restore block and, initially, place it at the end of // function. That block will be placed later if it's used; otherwise, it will // be erased. - MachineBasicBlock *RestoreBB = createNewBlockAfter(MF->back()); + MachineBasicBlock *RestoreBB = createNewBlockAfter(MF->back(), + DestBB->getBasicBlock()); TII->insertIndirectBranch(*BranchBB, *DestBB, *RestoreBB, DL, DestOffset - SrcOffset, RS.get()); @@ -482,10 +494,11 @@ bool BranchRelaxation::fixupUnconditionalBranch(MachineInstr &MI) { // restore blocks are just duplicated for each far branch. assert(!DestBB->isEntryBlock()); MachineBasicBlock *PrevBB = &*std::prev(DestBB->getIterator()); - if (auto *FT = PrevBB->getFallThrough()) { + // Fall through only if PrevBB has no unconditional branch as one of its + // terminators. + if (auto *FT = PrevBB->getLogicalFallThrough()) { assert(FT == DestBB); TII->insertUnconditionalBranch(*PrevBB, FT, DebugLoc()); - // Recalculate the block size. BlockInfo[PrevBB->getNumber()].Size = computeBlockSize(*PrevBB); } // Now, RestoreBB could be placed directly before DestBB. |