Jia Liu | b22310f | 2012-02-18 12:03:15 +0000 | [diff] [blame] | 1 | //===-- PPCBranchSelector.cpp - Emit long conditional branches ------------===// |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 6 | // |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 9 | // This file contains a pass that scans a machine function to determine which |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 10 | // conditional branches need more than 16 bits of displacement to reach their |
| 11 | // target basic block. It does this in two passes; a calculation of basic block |
Gabor Greif | 21fed66 | 2010-08-23 20:30:51 +0000 | [diff] [blame] | 12 | // positions pass, and a branch pseudo op to machine branch opcode pass. This |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 13 | // pass should be run last, just before the assembly printer. |
| 14 | // |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "MCTargetDesc/PPCPredicates.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 18 | #include "PPC.h" |
Chris Lattner | e80bf1b | 2005-10-14 23:45:43 +0000 | [diff] [blame] | 19 | #include "PPCInstrBuilder.h" |
Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 20 | #include "PPCInstrInfo.h" |
Hal Finkel | 530fa5f | 2016-10-03 04:06:44 +0000 | [diff] [blame] | 21 | #include "PPCSubtarget.h" |
Chris Lattner | 96d7386 | 2006-11-16 18:13:49 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Hal Finkel | 530fa5f | 2016-10-03 04:06:44 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
David Blaikie | b3bde2e | 2017-11-17 01:07:10 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/TargetSubtargetInfo.h" |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetMachine.h" |
Guozhi Wei | 11308bd | 2019-03-06 18:22:22 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "ppc-branch-select" |
| 32 | |
Chris Lattner | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 33 | STATISTIC(NumExpanded, "Number of branches expanded to long format"); |
Chris Lattner | 96d7386 | 2006-11-16 18:13:49 +0000 | [diff] [blame] | 34 | |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 35 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 36 | struct PPCBSel : public MachineFunctionPass { |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 37 | static char ID; |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 38 | PPCBSel() : MachineFunctionPass(ID) { |
| 39 | initializePPCBSelPass(*PassRegistry::getPassRegistry()); |
| 40 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 41 | |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 42 | // The sizes of the basic blocks in the function (the first |
| 43 | // element of the pair); the second element of the pair is the amount of the |
| 44 | // size that is due to potential padding. |
| 45 | std::vector<std::pair<unsigned, unsigned>> BlockSizes; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 46 | |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 47 | // The first block number which has imprecise instruction address. |
| 48 | int FirstImpreciseBlock = -1; |
| 49 | |
| 50 | unsigned GetAlignmentAdjustment(MachineBasicBlock &MBB, unsigned Offset); |
| 51 | unsigned ComputeBlockSizes(MachineFunction &Fn); |
| 52 | void modifyAdjustment(MachineFunction &Fn); |
| 53 | int computeBranchSize(MachineFunction &Fn, |
| 54 | const MachineBasicBlock *Src, |
| 55 | const MachineBasicBlock *Dest, |
| 56 | unsigned BrOffset); |
| 57 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 58 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 59 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 60 | MachineFunctionProperties getRequiredProperties() const override { |
| 61 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 62 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 63 | } |
| 64 | |
Mehdi Amini | 117296c | 2016-10-01 02:56:57 +0000 | [diff] [blame] | 65 | StringRef getPassName() const override { return "PowerPC Branch Selector"; } |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 66 | }; |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 67 | char PPCBSel::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 68 | } |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 69 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 70 | INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector", |
| 71 | false, false) |
| 72 | |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 73 | /// createPPCBranchSelectionPass - returns an instance of the Branch Selection |
| 74 | /// Pass |
| 75 | /// |
| 76 | FunctionPass *llvm::createPPCBranchSelectionPass() { |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 77 | return new PPCBSel(); |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 78 | } |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 79 | |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 80 | /// In order to make MBB aligned, we need to add an adjustment value to the |
| 81 | /// original Offset. |
| 82 | unsigned PPCBSel::GetAlignmentAdjustment(MachineBasicBlock &MBB, |
| 83 | unsigned Offset) { |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 84 | unsigned LogAlign = MBB.getLogAlignment(); |
| 85 | if (!LogAlign) |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 86 | return 0; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 87 | |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 88 | unsigned AlignAmt = 1 << LogAlign; |
| 89 | unsigned ParentLogAlign = MBB.getParent()->getLogAlignment(); |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 90 | |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 91 | if (LogAlign <= ParentLogAlign) |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 92 | return OffsetToAlignment(Offset, AlignAmt); |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 93 | |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 94 | // The alignment of this MBB is larger than the function's alignment, so we |
| 95 | // can't tell whether or not it will insert nops. Assume that it will. |
| 96 | if (FirstImpreciseBlock < 0) |
| 97 | FirstImpreciseBlock = MBB.getNumber(); |
| 98 | return AlignAmt + OffsetToAlignment(Offset, AlignAmt); |
| 99 | } |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 100 | |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 101 | /// We need to be careful about the offset of the first block in the function |
| 102 | /// because it might not have the function's alignment. This happens because, |
| 103 | /// under the ELFv2 ABI, for functions which require a TOC pointer, we add a |
| 104 | /// two-instruction sequence to the start of the function. |
| 105 | /// Note: This needs to be synchronized with the check in |
| 106 | /// PPCLinuxAsmPrinter::EmitFunctionBodyStart. |
| 107 | static inline unsigned GetInitialOffset(MachineFunction &Fn) { |
Hal Finkel | 530fa5f | 2016-10-03 04:06:44 +0000 | [diff] [blame] | 108 | unsigned InitialOffset = 0; |
| 109 | if (Fn.getSubtarget<PPCSubtarget>().isELFv2ABI() && |
| 110 | !Fn.getRegInfo().use_empty(PPC::X2)) |
| 111 | InitialOffset = 8; |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 112 | return InitialOffset; |
| 113 | } |
Hal Finkel | 530fa5f | 2016-10-03 04:06:44 +0000 | [diff] [blame] | 114 | |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 115 | /// Measure each MBB and compute a size for the entire function. |
| 116 | unsigned PPCBSel::ComputeBlockSizes(MachineFunction &Fn) { |
| 117 | const PPCInstrInfo *TII = |
| 118 | static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo()); |
| 119 | unsigned FuncSize = GetInitialOffset(Fn); |
| 120 | |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 121 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 122 | ++MFI) { |
Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 123 | MachineBasicBlock *MBB = &*MFI; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 124 | |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 125 | // The end of the previous block may have extra nops if this block has an |
| 126 | // alignment requirement. |
| 127 | if (MBB->getNumber() > 0) { |
| 128 | unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize); |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 129 | |
| 130 | auto &BS = BlockSizes[MBB->getNumber()-1]; |
| 131 | BS.first += AlignExtra; |
| 132 | BS.second = AlignExtra; |
| 133 | |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 134 | FuncSize += AlignExtra; |
| 135 | } |
| 136 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 137 | unsigned BlockSize = 0; |
Guozhi Wei | 11308bd | 2019-03-06 18:22:22 +0000 | [diff] [blame] | 138 | for (MachineInstr &MI : *MBB) { |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 139 | BlockSize += TII->getInstSizeInBytes(MI); |
Guozhi Wei | 11308bd | 2019-03-06 18:22:22 +0000 | [diff] [blame] | 140 | if (MI.isInlineAsm() && (FirstImpreciseBlock < 0)) |
| 141 | FirstImpreciseBlock = MBB->getNumber(); |
| 142 | } |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 143 | |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 144 | BlockSizes[MBB->getNumber()].first = BlockSize; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 145 | FuncSize += BlockSize; |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 146 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 147 | |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 148 | return FuncSize; |
| 149 | } |
| 150 | |
| 151 | /// Modify the basic block align adjustment. |
| 152 | void PPCBSel::modifyAdjustment(MachineFunction &Fn) { |
| 153 | unsigned Offset = GetInitialOffset(Fn); |
| 154 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 155 | ++MFI) { |
| 156 | MachineBasicBlock *MBB = &*MFI; |
| 157 | |
| 158 | if (MBB->getNumber() > 0) { |
| 159 | auto &BS = BlockSizes[MBB->getNumber()-1]; |
| 160 | BS.first -= BS.second; |
| 161 | Offset -= BS.second; |
| 162 | |
| 163 | unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset); |
| 164 | |
| 165 | BS.first += AlignExtra; |
| 166 | BS.second = AlignExtra; |
| 167 | |
| 168 | Offset += AlignExtra; |
| 169 | } |
| 170 | |
| 171 | Offset += BlockSizes[MBB->getNumber()].first; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | /// Determine the offset from the branch in Src block to the Dest block. |
| 176 | /// BrOffset is the offset of the branch instruction inside Src block. |
| 177 | int PPCBSel::computeBranchSize(MachineFunction &Fn, |
| 178 | const MachineBasicBlock *Src, |
| 179 | const MachineBasicBlock *Dest, |
| 180 | unsigned BrOffset) { |
| 181 | int BranchSize; |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 182 | unsigned MaxLogAlign = 2; |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 183 | bool NeedExtraAdjustment = false; |
| 184 | if (Dest->getNumber() <= Src->getNumber()) { |
| 185 | // If this is a backwards branch, the delta is the offset from the |
| 186 | // start of this block to this branch, plus the sizes of all blocks |
| 187 | // from this block to the dest. |
| 188 | BranchSize = BrOffset; |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 189 | MaxLogAlign = std::max(MaxLogAlign, Src->getLogAlignment()); |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 190 | |
| 191 | int DestBlock = Dest->getNumber(); |
| 192 | BranchSize += BlockSizes[DestBlock].first; |
| 193 | for (unsigned i = DestBlock+1, e = Src->getNumber(); i < e; ++i) { |
| 194 | BranchSize += BlockSizes[i].first; |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 195 | MaxLogAlign = |
| 196 | std::max(MaxLogAlign, Fn.getBlockNumbered(i)->getLogAlignment()); |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | NeedExtraAdjustment = (FirstImpreciseBlock >= 0) && |
| 200 | (DestBlock >= FirstImpreciseBlock); |
| 201 | } else { |
| 202 | // Otherwise, add the size of the blocks between this block and the |
| 203 | // dest to the number of bytes left in this block. |
| 204 | unsigned StartBlock = Src->getNumber(); |
| 205 | BranchSize = BlockSizes[StartBlock].first - BrOffset; |
| 206 | |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 207 | MaxLogAlign = std::max(MaxLogAlign, Dest->getLogAlignment()); |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 208 | for (unsigned i = StartBlock+1, e = Dest->getNumber(); i != e; ++i) { |
| 209 | BranchSize += BlockSizes[i].first; |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 210 | MaxLogAlign = |
| 211 | std::max(MaxLogAlign, Fn.getBlockNumbered(i)->getLogAlignment()); |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | NeedExtraAdjustment = (FirstImpreciseBlock >= 0) && |
| 215 | (Src->getNumber() >= FirstImpreciseBlock); |
| 216 | } |
| 217 | |
| 218 | // We tend to over estimate code size due to large alignment and |
| 219 | // inline assembly. Usually it causes larger computed branch offset. |
| 220 | // But sometimes it may also causes smaller computed branch offset |
| 221 | // than actual branch offset. If the offset is close to the limit of |
| 222 | // encoding, it may cause problem at run time. |
| 223 | // Following is a simplified example. |
| 224 | // |
| 225 | // actual estimated |
| 226 | // address address |
| 227 | // ... |
| 228 | // bne Far 100 10c |
| 229 | // .p2align 4 |
| 230 | // Near: 110 110 |
| 231 | // ... |
| 232 | // Far: 8108 8108 |
| 233 | // |
| 234 | // Actual offset: 0x8108 - 0x100 = 0x8008 |
| 235 | // Computed offset: 0x8108 - 0x10c = 0x7ffc |
| 236 | // |
| 237 | // This example also shows when we can get the largest gap between |
| 238 | // estimated offset and actual offset. If there is an aligned block |
| 239 | // ABB between branch and target, assume its alignment is <align> |
| 240 | // bits. Now consider the accumulated function size FSIZE till the end |
| 241 | // of previous block PBB. If the estimated FSIZE is multiple of |
| 242 | // 2^<align>, we don't need any padding for the estimated address of |
| 243 | // ABB. If actual FSIZE at the end of PBB is 4 bytes more than |
| 244 | // multiple of 2^<align>, then we need (2^<align> - 4) bytes of |
| 245 | // padding. It also means the actual branch offset is (2^<align> - 4) |
| 246 | // larger than computed offset. Other actual FSIZE needs less padding |
| 247 | // bytes, so causes smaller gap between actual and computed offset. |
| 248 | // |
| 249 | // On the other hand, if the inline asm or large alignment occurs |
| 250 | // between the branch block and destination block, the estimated address |
| 251 | // can be <delta> larger than actual address. If padding bytes are |
| 252 | // needed for a later aligned block, the actual number of padding bytes |
| 253 | // is at most <delta> more than estimated padding bytes. So the actual |
| 254 | // aligned block address is less than or equal to the estimated aligned |
| 255 | // block address. So the actual branch offset is less than or equal to |
| 256 | // computed branch offset. |
| 257 | // |
| 258 | // The computed offset is at most ((1 << alignment) - 4) bytes smaller |
| 259 | // than actual offset. So we add this number to the offset for safety. |
| 260 | if (NeedExtraAdjustment) |
Guillaume Chatelet | aff45e4 | 2019-09-05 10:00:22 +0000 | [diff] [blame] | 261 | BranchSize += (1 << MaxLogAlign) - 4; |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 262 | |
| 263 | return BranchSize; |
| 264 | } |
| 265 | |
| 266 | bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) { |
| 267 | const PPCInstrInfo *TII = |
| 268 | static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo()); |
| 269 | // Give the blocks of the function a dense, in-order, numbering. |
| 270 | Fn.RenumberBlocks(); |
| 271 | BlockSizes.resize(Fn.getNumBlockIDs()); |
| 272 | FirstImpreciseBlock = -1; |
| 273 | |
| 274 | // Measure each MBB and compute a size for the entire function. |
| 275 | unsigned FuncSize = ComputeBlockSizes(Fn); |
| 276 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 277 | // If the entire function is smaller than the displacement of a branch field, |
| 278 | // we know we don't need to shrink any branches in this function. This is a |
| 279 | // common case. |
| 280 | if (FuncSize < (1 << 15)) { |
| 281 | BlockSizes.clear(); |
| 282 | return false; |
| 283 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 284 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 285 | // For each conditional branch, if the offset to its destination is larger |
| 286 | // than the offset field allows, transform it into a long branch sequence |
| 287 | // like this: |
| 288 | // short branch: |
| 289 | // bCC MBB |
| 290 | // long branch: |
| 291 | // b!CC $PC+8 |
| 292 | // b MBB |
| 293 | // |
| 294 | bool MadeChange = true; |
| 295 | bool EverMadeChange = false; |
| 296 | while (MadeChange) { |
| 297 | // Iteratively expand branches until we reach a fixed point. |
| 298 | MadeChange = false; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 300 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 301 | ++MFI) { |
| 302 | MachineBasicBlock &MBB = *MFI; |
| 303 | unsigned MBBStartOffset = 0; |
| 304 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 305 | I != E; ++I) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 306 | MachineBasicBlock *Dest = nullptr; |
Hal Finkel | c521129 | 2013-05-21 14:21:09 +0000 | [diff] [blame] | 307 | if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm()) |
| 308 | Dest = I->getOperand(2).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 309 | else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) && |
| 310 | !I->getOperand(1).isImm()) |
| 311 | Dest = I->getOperand(1).getMBB(); |
Hal Finkel | c521129 | 2013-05-21 14:21:09 +0000 | [diff] [blame] | 312 | else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ || |
| 313 | I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) && |
| 314 | !I->getOperand(0).isImm()) |
| 315 | Dest = I->getOperand(0).getMBB(); |
| 316 | |
| 317 | if (!Dest) { |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 318 | MBBStartOffset += TII->getInstSizeInBytes(*I); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 319 | continue; |
| 320 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 321 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 322 | // Determine the offset from the current branch to the destination |
| 323 | // block. |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 324 | int BranchSize = computeBranchSize(Fn, &MBB, Dest, MBBStartOffset); |
Guozhi Wei | 11308bd | 2019-03-06 18:22:22 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 326 | // If this branch is in range, ignore it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 327 | if (isInt<16>(BranchSize)) { |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 328 | MBBStartOffset += 4; |
| 329 | continue; |
| 330 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 332 | // Otherwise, we have to expand it to a long branch. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 333 | MachineInstr &OldBranch = *I; |
| 334 | DebugLoc dl = OldBranch.getDebugLoc(); |
| 335 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 336 | if (I->getOpcode() == PPC::BCC) { |
| 337 | // The BCC operands are: |
| 338 | // 0. PPC branch predicate |
| 339 | // 1. CR register |
| 340 | // 2. Target MBB |
| 341 | PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm(); |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 342 | Register CRReg = I->getOperand(1).getReg(); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 343 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 344 | // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition. |
| 345 | BuildMI(MBB, I, dl, TII->get(PPC::BCC)) |
| 346 | .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 347 | } else if (I->getOpcode() == PPC::BC) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 348 | Register CRBit = I->getOperand(0).getReg(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 349 | BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2); |
| 350 | } else if (I->getOpcode() == PPC::BCn) { |
Daniel Sanders | 0c47611 | 2019-08-15 19:22:08 +0000 | [diff] [blame] | 351 | Register CRBit = I->getOperand(0).getReg(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 352 | BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 353 | } else if (I->getOpcode() == PPC::BDNZ) { |
| 354 | BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2); |
| 355 | } else if (I->getOpcode() == PPC::BDNZ8) { |
| 356 | BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2); |
| 357 | } else if (I->getOpcode() == PPC::BDZ) { |
| 358 | BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2); |
| 359 | } else if (I->getOpcode() == PPC::BDZ8) { |
| 360 | BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2); |
| 361 | } else { |
| 362 | llvm_unreachable("Unhandled branch type!"); |
| 363 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 365 | // Uncond branch to the real destination. |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 366 | I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 367 | |
| 368 | // Remove the old branch from the function. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 369 | OldBranch.eraseFromParent(); |
| 370 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 371 | // Remember that this instruction is 8-bytes, increase the size of the |
| 372 | // block by 4, remember to iterate. |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 373 | BlockSizes[MBB.getNumber()].first += 4; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 374 | MBBStartOffset += 8; |
| 375 | ++NumExpanded; |
| 376 | MadeChange = true; |
| 377 | } |
| 378 | } |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 379 | |
| 380 | if (MadeChange) { |
| 381 | // If we're going to iterate again, make sure we've updated our |
| 382 | // padding-based contributions to the block sizes. |
Guozhi Wei | 330dcd9 | 2019-03-26 21:27:38 +0000 | [diff] [blame] | 383 | modifyAdjustment(Fn); |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 386 | EverMadeChange |= MadeChange; |
| 387 | } |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 388 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 389 | BlockSizes.clear(); |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 390 | return true; |
| 391 | } |