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