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