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