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