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