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 | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 18 | #define DEBUG_TYPE "ppc-branch-select" |
Chris Lattner | bfca1ab | 2005-10-14 23:51:18 +0000 | [diff] [blame] | 19 | #include "PPC.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 20 | #include "MCTargetDesc/PPCPredicates.h" |
Chris Lattner | e80bf1b | 2005-10-14 23:45:43 +0000 | [diff] [blame] | 21 | #include "PPCInstrBuilder.h" |
Chris Lattner | 6f3b954 | 2005-10-14 23:59:06 +0000 | [diff] [blame] | 22 | #include "PPCInstrInfo.h" |
Chris Lattner | 96d7386 | 2006-11-16 18:13:49 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/CodeGen/MachineFunctionPass.h" |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MathExtras.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 1ef9cd4 | 2006-12-19 22:59:26 +0000 | [diff] [blame] | 29 | STATISTIC(NumExpanded, "Number of branches expanded to long format"); |
Chris Lattner | 96d7386 | 2006-11-16 18:13:49 +0000 | [diff] [blame] | 30 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 31 | namespace llvm { |
| 32 | void initializePPCBSelPass(PassRegistry&); |
| 33 | } |
| 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 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 42 | /// BlockSizes - The sizes of the basic blocks in the function. |
| 43 | std::vector<unsigned> BlockSizes; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 45 | virtual bool runOnMachineFunction(MachineFunction &Fn); |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 46 | |
| 47 | virtual const char *getPassName() const { |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 48 | return "PowerPC Branch Selector"; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 49 | } |
| 50 | }; |
Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 51 | char PPCBSel::ID = 0; |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Krzysztof Parzyszek | 2680b53 | 2013-02-13 17:40:07 +0000 | [diff] [blame] | 54 | INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector", |
| 55 | false, false) |
| 56 | |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 57 | /// createPPCBranchSelectionPass - returns an instance of the Branch Selection |
| 58 | /// Pass |
| 59 | /// |
| 60 | FunctionPass *llvm::createPPCBranchSelectionPass() { |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 61 | return new PPCBSel(); |
Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 62 | } |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 63 | |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 64 | bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) { |
Gabor Greif | 4ad72717 | 2010-07-23 13:28:47 +0000 | [diff] [blame] | 65 | const PPCInstrInfo *TII = |
| 66 | static_cast<const PPCInstrInfo*>(Fn.getTarget().getInstrInfo()); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 67 | // Give the blocks of the function a dense, in-order, numbering. |
| 68 | Fn.RenumberBlocks(); |
| 69 | BlockSizes.resize(Fn.getNumBlockIDs()); |
| 70 | |
| 71 | // Measure each MBB and compute a size for the entire function. |
| 72 | unsigned FuncSize = 0; |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 73 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 74 | ++MFI) { |
| 75 | MachineBasicBlock *MBB = MFI; |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 76 | |
| 77 | unsigned BlockSize = 0; |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 78 | for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end(); |
| 79 | MBBI != EE; ++MBBI) |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 80 | BlockSize += TII->GetInstSizeInBytes(MBBI); |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 82 | BlockSizes[MBB->getNumber()] = BlockSize; |
| 83 | FuncSize += BlockSize; |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 86 | // If the entire function is smaller than the displacement of a branch field, |
| 87 | // we know we don't need to shrink any branches in this function. This is a |
| 88 | // common case. |
| 89 | if (FuncSize < (1 << 15)) { |
| 90 | BlockSizes.clear(); |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | // For each conditional branch, if the offset to its destination is larger |
| 95 | // than the offset field allows, transform it into a long branch sequence |
| 96 | // like this: |
| 97 | // short branch: |
| 98 | // bCC MBB |
| 99 | // long branch: |
| 100 | // b!CC $PC+8 |
| 101 | // b MBB |
| 102 | // |
| 103 | bool MadeChange = true; |
| 104 | bool EverMadeChange = false; |
| 105 | while (MadeChange) { |
| 106 | // Iteratively expand branches until we reach a fixed point. |
| 107 | MadeChange = false; |
| 108 | |
| 109 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; |
| 110 | ++MFI) { |
| 111 | MachineBasicBlock &MBB = *MFI; |
| 112 | unsigned MBBStartOffset = 0; |
| 113 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 114 | I != E; ++I) { |
Hal Finkel | c521129 | 2013-05-21 14:21:09 +0000 | [diff] [blame] | 115 | MachineBasicBlock *Dest = 0; |
| 116 | if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm()) |
| 117 | Dest = I->getOperand(2).getMBB(); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 118 | else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) && |
| 119 | !I->getOperand(1).isImm()) |
| 120 | Dest = I->getOperand(1).getMBB(); |
Hal Finkel | c521129 | 2013-05-21 14:21:09 +0000 | [diff] [blame] | 121 | else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ || |
| 122 | I->getOpcode() == PPC::BDZ8 || I->getOpcode() == PPC::BDZ) && |
| 123 | !I->getOperand(0).isImm()) |
| 124 | Dest = I->getOperand(0).getMBB(); |
| 125 | |
| 126 | if (!Dest) { |
Nicolas Geoffray | ae84bbd | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 127 | MBBStartOffset += TII->GetInstSizeInBytes(I); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 128 | continue; |
| 129 | } |
| 130 | |
| 131 | // Determine the offset from the current branch to the destination |
| 132 | // block. |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 133 | int BranchSize; |
| 134 | if (Dest->getNumber() <= MBB.getNumber()) { |
| 135 | // If this is a backwards branch, the delta is the offset from the |
| 136 | // start of this block to this branch, plus the sizes of all blocks |
| 137 | // from this block to the dest. |
| 138 | BranchSize = MBBStartOffset; |
| 139 | |
| 140 | for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i) |
| 141 | BranchSize += BlockSizes[i]; |
| 142 | } else { |
| 143 | // Otherwise, add the size of the blocks between this block and the |
| 144 | // dest to the number of bytes left in this block. |
| 145 | BranchSize = -MBBStartOffset; |
| 146 | |
| 147 | for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i) |
| 148 | BranchSize += BlockSizes[i]; |
| 149 | } |
| 150 | |
| 151 | // If this branch is in range, ignore it. |
Benjamin Kramer | 2788f79 | 2010-03-29 21:13:41 +0000 | [diff] [blame] | 152 | if (isInt<16>(BranchSize)) { |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 153 | MBBStartOffset += 4; |
| 154 | continue; |
| 155 | } |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 156 | |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 157 | // Otherwise, we have to expand it to a long branch. |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 158 | MachineInstr *OldBranch = I; |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 159 | DebugLoc dl = OldBranch->getDebugLoc(); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 160 | |
| 161 | if (I->getOpcode() == PPC::BCC) { |
| 162 | // The BCC operands are: |
| 163 | // 0. PPC branch predicate |
| 164 | // 1. CR register |
| 165 | // 2. Target MBB |
| 166 | PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm(); |
| 167 | unsigned CRReg = I->getOperand(1).getReg(); |
| 168 | |
| 169 | // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition. |
| 170 | BuildMI(MBB, I, dl, TII->get(PPC::BCC)) |
| 171 | .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2); |
Hal Finkel | 940ab93 | 2014-02-28 00:27:01 +0000 | [diff] [blame] | 172 | } else if (I->getOpcode() == PPC::BC) { |
| 173 | unsigned CRBit = I->getOperand(0).getReg(); |
| 174 | BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2); |
| 175 | } else if (I->getOpcode() == PPC::BCn) { |
| 176 | unsigned CRBit = I->getOperand(0).getReg(); |
| 177 | BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2); |
Hal Finkel | 96c2d4d | 2012-06-08 15:38:21 +0000 | [diff] [blame] | 178 | } else if (I->getOpcode() == PPC::BDNZ) { |
| 179 | BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2); |
| 180 | } else if (I->getOpcode() == PPC::BDNZ8) { |
| 181 | BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2); |
| 182 | } else if (I->getOpcode() == PPC::BDZ) { |
| 183 | BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2); |
| 184 | } else if (I->getOpcode() == PPC::BDZ8) { |
| 185 | BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2); |
| 186 | } else { |
| 187 | llvm_unreachable("Unhandled branch type!"); |
| 188 | } |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 189 | |
| 190 | // Uncond branch to the real destination. |
Dale Johannesen | e9f623e | 2009-02-13 02:27:39 +0000 | [diff] [blame] | 191 | I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest); |
Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 192 | |
| 193 | // Remove the old branch from the function. |
| 194 | OldBranch->eraseFromParent(); |
| 195 | |
| 196 | // Remember that this instruction is 8-bytes, increase the size of the |
| 197 | // block by 4, remember to iterate. |
| 198 | BlockSizes[MBB.getNumber()] += 4; |
| 199 | MBBStartOffset += 8; |
| 200 | ++NumExpanded; |
| 201 | MadeChange = true; |
| 202 | } |
| 203 | } |
| 204 | EverMadeChange |= MadeChange; |
| 205 | } |
| 206 | |
| 207 | BlockSizes.clear(); |
Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 208 | return true; |
| 209 | } |
| 210 | |