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