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 | // |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 7 | // |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Misha Brukman | b440243 | 2005-04-21 23:30:14 +0000 | [diff] [blame] | 10 | // 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] | 11 | // conditional branches need more than 16 bits of displacement to reach their |
| 12 | // 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] | 13 | // 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] | 14 | // pass should be run last, just before the assembly printer. |
| 15 | // |
| 16 | //===----------------------------------------------------------------------===// |
| 17 | |
Chris Lattner | bfca1ab | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 18 | #include "PPC.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "MCTargetDesc/PPCPredicates.h" |
Chris Lattner | e80bf1b | 2005-10-14 23:45:43 +0000 | [diff] [blame] | 20 | #include "PPCInstrBuilder.h" |
Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 21 | #include "PPCInstrInfo.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" |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetMachine.h" |
Eric Christopher | d913448 | 2014-08-04 21:25:23 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetSubtargetInfo.h" |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chandler Carruth | 84e68b2 | 2014-04-22 02:41:26 +0000 | [diff] [blame] | 29 | #define DEBUG_TYPE "ppc-branch-select" |
| 30 | |
Chris Lattner | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 31 | STATISTIC(NumExpanded, "Number of branches expanded to long format"); |
Chris Lattner | 96d7386 | 2006-11-16 18:13:49 +0000 | [diff] [blame] | 32 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 33 | namespace llvm { |
| 34 | void initializePPCBSelPass(PassRegistry&); |
| 35 | } |
| 36 | |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 37 | namespace { |
Nick Lewycky | 02d5f77 | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 38 | struct PPCBSel : public MachineFunctionPass { |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 39 | static char ID; |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 40 | PPCBSel() : MachineFunctionPass(ID) { |
| 41 | initializePPCBSelPass(*PassRegistry::getPassRegistry()); |
| 42 | } |
Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 43 | |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 44 | // The sizes of the basic blocks in the function (the first |
| 45 | // element of the pair); the second element of the pair is the amount of the |
| 46 | // size that is due to potential padding. |
| 47 | std::vector<std::pair<unsigned, unsigned>> BlockSizes; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 48 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 49 | bool runOnMachineFunction(MachineFunction &Fn) override; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 50 | |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 51 | MachineFunctionProperties getRequiredProperties() const override { |
| 52 | return MachineFunctionProperties().set( |
Matthias Braun | 1eb4736 | 2016-08-25 01:27:13 +0000 | [diff] [blame] | 53 | MachineFunctionProperties::Property::NoVRegs); |
Derek Schuff | 1dbf7a5 | 2016-04-04 17:09:25 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Craig Topper | 0d3fa92 | 2014-04-29 07:57:37 +0000 | [diff] [blame] | 56 | const char *getPassName() const override { |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 57 | return "PowerPC Branch Selector"; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 58 | } |
| 59 | }; |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 60 | char PPCBSel::ID = 0; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 61 | } |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 62 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 63 | INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector", |
| 64 | false, false) |
| 65 | |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 66 | /// createPPCBranchSelectionPass - returns an instance of the Branch Selection |
| 67 | /// Pass |
| 68 | /// |
| 69 | FunctionPass *llvm::createPPCBranchSelectionPass() { |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 70 | return new PPCBSel(); |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 71 | } |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 72 | |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 73 | bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) { |
Eric Christopher | fc6de42 | 2014-08-05 02:39:49 +0000 | [diff] [blame] | 74 | const PPCInstrInfo *TII = |
| 75 | static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo()); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 76 | // Give the blocks of the function a dense, in-order, numbering. |
| 77 | Fn.RenumberBlocks(); |
| 78 | BlockSizes.resize(Fn.getNumBlockIDs()); |
| 79 | |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 80 | auto GetAlignmentAdjustment = |
| 81 | [TII](MachineBasicBlock &MBB, unsigned Offset) -> unsigned { |
| 82 | unsigned Align = MBB.getAlignment(); |
| 83 | if (!Align) |
| 84 | return 0; |
| 85 | |
| 86 | unsigned AlignAmt = 1 << Align; |
| 87 | unsigned ParentAlign = MBB.getParent()->getAlignment(); |
| 88 | |
| 89 | if (Align <= ParentAlign) |
| 90 | return OffsetToAlignment(Offset, AlignAmt); |
| 91 | |
| 92 | // The alignment of this MBB is larger than the function's alignment, so we |
| 93 | // can't tell whether or not it will insert nops. Assume that it will. |
| 94 | return AlignAmt + OffsetToAlignment(Offset, AlignAmt); |
| 95 | }; |
| 96 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 97 | // Measure each MBB and compute a size for the entire function. |
| 98 | unsigned FuncSize = 0; |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 99 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 100 | ++MFI) { |
Duncan P. N. Exon Smith | ac65b4c | 2015-10-20 01:07:37 +0000 | [diff] [blame] | 101 | MachineBasicBlock *MBB = &*MFI; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 102 | |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 103 | // The end of the previous block may have extra nops if this block has an |
| 104 | // alignment requirement. |
| 105 | if (MBB->getNumber() > 0) { |
| 106 | unsigned AlignExtra = GetAlignmentAdjustment(*MBB, FuncSize); |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 107 | |
| 108 | auto &BS = BlockSizes[MBB->getNumber()-1]; |
| 109 | BS.first += AlignExtra; |
| 110 | BS.second = AlignExtra; |
| 111 | |
Hal Finkel | d73bfba | 2015-01-03 14:58:25 +0000 | [diff] [blame] | 112 | FuncSize += AlignExtra; |
| 113 | } |
| 114 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 115 | unsigned BlockSize = 0; |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 116 | for (MachineInstr &MI : *MBB) |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 117 | BlockSize += TII->getInstSizeInBytes(MI); |
Duncan P. N. Exon Smith | 9cfc75c | 2016-06-30 00:01:54 +0000 | [diff] [blame] | 118 | |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 119 | BlockSizes[MBB->getNumber()].first = BlockSize; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 120 | FuncSize += BlockSize; |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 123 | // If the entire function is smaller than the displacement of a branch field, |
| 124 | // we know we don't need to shrink any branches in this function. This is a |
| 125 | // common case. |
| 126 | if (FuncSize < (1 << 15)) { |
| 127 | BlockSizes.clear(); |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | // For each conditional branch, if the offset to its destination is larger |
| 132 | // than the offset field allows, transform it into a long branch sequence |
| 133 | // like this: |
| 134 | // short branch: |
| 135 | // bCC MBB |
| 136 | // long branch: |
| 137 | // b!CC $PC+8 |
| 138 | // b MBB |
| 139 | // |
| 140 | bool MadeChange = true; |
| 141 | bool EverMadeChange = false; |
| 142 | while (MadeChange) { |
| 143 | // Iteratively expand branches until we reach a fixed point. |
| 144 | MadeChange = false; |
| 145 | |
| 146 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 147 | ++MFI) { |
| 148 | MachineBasicBlock &MBB = *MFI; |
| 149 | unsigned MBBStartOffset = 0; |
| 150 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 151 | I != E; ++I) { |
Craig Topper | 062a2ba | 2014-04-25 05:30:21 +0000 | [diff] [blame] | 152 | MachineBasicBlock *Dest = nullptr; |
Hal Finkel | c521129 | 2013-05-21 14:21:09 +0000 | [diff] [blame] | 153 | if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm()) |
| 154 | Dest = I->getOperand(2).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 155 | else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) && |
| 156 | !I->getOperand(1).isImm()) |
| 157 | Dest = I->getOperand(1).getMBB(); |
Hal Finkel | c521129 | 2013-05-21 14:21:09 +0000 | [diff] [blame] | 158 | else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ || |
| 159 | I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) && |
| 160 | !I->getOperand(0).isImm()) |
| 161 | Dest = I->getOperand(0).getMBB(); |
| 162 | |
| 163 | if (!Dest) { |
Sjoerd Meijer | 89217f8 | 2016-07-28 16:32:22 +0000 | [diff] [blame] | 164 | MBBStartOffset += TII->getInstSizeInBytes(*I); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 165 | continue; |
| 166 | } |
| 167 | |
| 168 | // Determine the offset from the current branch to the destination |
| 169 | // block. |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 170 | int BranchSize; |
| 171 | if (Dest->getNumber() <= MBB.getNumber()) { |
| 172 | // If this is a backwards branch, the delta is the offset from the |
| 173 | // start of this block to this branch, plus the sizes of all blocks |
| 174 | // from this block to the dest. |
| 175 | BranchSize = MBBStartOffset; |
| 176 | |
| 177 | for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i) |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 178 | BranchSize += BlockSizes[i].first; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 179 | } else { |
| 180 | // Otherwise, add the size of the blocks between this block and the |
| 181 | // dest to the number of bytes left in this block. |
| 182 | BranchSize = -MBBStartOffset; |
| 183 | |
| 184 | for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i) |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 185 | BranchSize += BlockSizes[i].first; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | // If this branch is in range, ignore it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 189 | if (isInt<16>(BranchSize)) { |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 190 | MBBStartOffset += 4; |
| 191 | continue; |
| 192 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 193 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 194 | // 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] | 195 | MachineInstr &OldBranch = *I; |
| 196 | DebugLoc dl = OldBranch.getDebugLoc(); |
| 197 | |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 198 | if (I->getOpcode() == PPC::BCC) { |
| 199 | // The BCC operands are: |
| 200 | // 0. PPC branch predicate |
| 201 | // 1. CR register |
| 202 | // 2. Target MBB |
| 203 | PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm(); |
| 204 | unsigned CRReg = I->getOperand(1).getReg(); |
| 205 | |
| 206 | // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition. |
| 207 | BuildMI(MBB, I, dl, TII->get(PPC::BCC)) |
| 208 | .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 209 | } else if (I->getOpcode() == PPC::BC) { |
| 210 | unsigned CRBit = I->getOperand(0).getReg(); |
| 211 | BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2); |
| 212 | } else if (I->getOpcode() == PPC::BCn) { |
| 213 | unsigned CRBit = I->getOperand(0).getReg(); |
| 214 | BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 215 | } else if (I->getOpcode() == PPC::BDNZ) { |
| 216 | BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2); |
| 217 | } else if (I->getOpcode() == PPC::BDNZ8) { |
| 218 | BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2); |
| 219 | } else if (I->getOpcode() == PPC::BDZ) { |
| 220 | BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2); |
| 221 | } else if (I->getOpcode() == PPC::BDZ8) { |
| 222 | BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2); |
| 223 | } else { |
| 224 | llvm_unreachable("Unhandled branch type!"); |
| 225 | } |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 226 | |
| 227 | // Uncond branch to the real destination. |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 228 | I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 229 | |
| 230 | // Remove the old branch from the function. |
Duncan P. N. Exon Smith | e5a22f4 | 2016-07-27 13:24:16 +0000 | [diff] [blame] | 231 | OldBranch.eraseFromParent(); |
| 232 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 233 | // Remember that this instruction is 8-bytes, increase the size of the |
| 234 | // block by 4, remember to iterate. |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 235 | BlockSizes[MBB.getNumber()].first += 4; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 236 | MBBStartOffset += 8; |
| 237 | ++NumExpanded; |
| 238 | MadeChange = true; |
| 239 | } |
| 240 | } |
Hal Finkel | f0bc9db | 2016-09-04 14:18:29 +0000 | [diff] [blame] | 241 | |
| 242 | if (MadeChange) { |
| 243 | // If we're going to iterate again, make sure we've updated our |
| 244 | // padding-based contributions to the block sizes. |
| 245 | unsigned Offset = 0; |
| 246 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 247 | ++MFI) { |
| 248 | MachineBasicBlock *MBB = &*MFI; |
| 249 | |
| 250 | if (MBB->getNumber() > 0) { |
| 251 | auto &BS = BlockSizes[MBB->getNumber()-1]; |
| 252 | BS.first -= BS.second; |
| 253 | Offset -= BS.second; |
| 254 | |
| 255 | unsigned AlignExtra = GetAlignmentAdjustment(*MBB, Offset); |
| 256 | |
| 257 | BS.first += AlignExtra; |
| 258 | BS.second = AlignExtra; |
| 259 | |
| 260 | Offset += AlignExtra; |
| 261 | } |
| 262 | |
| 263 | Offset += BlockSizes[MBB->getNumber()].first; |
| 264 | } |
| 265 | } |
| 266 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 267 | EverMadeChange |= MadeChange; |
| 268 | } |
| 269 | |
| 270 | BlockSizes.clear(); |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 271 | return true; |
| 272 | } |