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