| Nate Begeman | 6cca84e | 2005-10-16 05:39:50 +0000 | [diff] [blame] | 1 | //===-- PPCBranchSelector.cpp - Emit long conditional branches-----*- C++ -*-=// | 
| 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 | 
|  | 13 | // positions pass, and a branch psuedo op to machine branch opcode pass.  This | 
|  | 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" | 
| 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 | 8c6a41e | 2006-11-17 22:10:59 +0000 | [diff] [blame] | 22 | #include "PPCPredicates.h" | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/MachineFunctionPass.h" | 
| Chris Lattner | 4dc4f30 | 2006-10-13 17:56:02 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetMachine.h" | 
|  | 25 | #include "llvm/Target/TargetAsmInfo.h" | 
| Chris Lattner | 96d7386 | 2006-11-16 18:13:49 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/Statistic.h" | 
|  | 27 | #include "llvm/Support/Compiler.h" | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 28 | #include "llvm/Support/MathExtras.h" | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 29 | using namespace llvm; | 
|  | 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 |  | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 33 | namespace { | 
| Chris Lattner | 996795b | 2006-06-28 23:17:24 +0000 | [diff] [blame] | 34 | struct VISIBILITY_HIDDEN PPCBSel : public MachineFunctionPass { | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 35 | static char ID; | 
| Devang Patel | 09f162c | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 36 | PPCBSel() : MachineFunctionPass((intptr_t)&ID) {} | 
|  | 37 |  | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 38 | /// BlockSizes - The sizes of the basic blocks in the function. | 
|  | 39 | std::vector<unsigned> BlockSizes; | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 40 |  | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 41 | virtual bool runOnMachineFunction(MachineFunction &Fn); | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 42 |  | 
|  | 43 | virtual const char *getPassName() const { | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 44 | return "PowerPC Branch Selector"; | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 45 | } | 
|  | 46 | }; | 
| Devang Patel | 8c78a0b | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 47 | char PPCBSel::ID = 0; | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 48 | } | 
|  | 49 |  | 
|  | 50 | /// createPPCBranchSelectionPass - returns an instance of the Branch Selection | 
|  | 51 | /// Pass | 
|  | 52 | /// | 
|  | 53 | FunctionPass *llvm::createPPCBranchSelectionPass() { | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 54 | return new PPCBSel(); | 
| Misha Brukman | ef8cf02 | 2004-07-27 18:33:06 +0000 | [diff] [blame] | 55 | } | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 56 |  | 
|  | 57 | /// getNumBytesForInstruction - Return the number of bytes of code the specified | 
|  | 58 | /// instruction may be.  This returns the maximum number of bytes. | 
|  | 59 | /// | 
|  | 60 | static unsigned getNumBytesForInstruction(MachineInstr *MI) { | 
|  | 61 | switch (MI->getOpcode()) { | 
| Chris Lattner | 4dc4f30 | 2006-10-13 17:56:02 +0000 | [diff] [blame] | 62 | case PPC::INLINEASM: {       // Inline Asm: Variable size. | 
|  | 63 | MachineFunction *MF = MI->getParent()->getParent(); | 
|  | 64 | const char *AsmStr = MI->getOperand(0).getSymbolName(); | 
|  | 65 | return MF->getTarget().getTargetAsmInfo()->getInlineAsmLength(AsmStr); | 
|  | 66 | } | 
| Jim Laskey | f9e5445 | 2007-01-26 14:34:52 +0000 | [diff] [blame] | 67 | case PPC::LABEL: { | 
|  | 68 | return 0; | 
|  | 69 | } | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 70 | default: | 
|  | 71 | return 4; // PowerPC instructions are all 4 bytes | 
|  | 72 | } | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 |  | 
|  | 76 | bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) { | 
| Evan Cheng | 20350c4 | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 77 | const TargetInstrInfo *TII = Fn.getTarget().getInstrInfo(); | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 78 | // Give the blocks of the function a dense, in-order, numbering. | 
|  | 79 | Fn.RenumberBlocks(); | 
|  | 80 | BlockSizes.resize(Fn.getNumBlockIDs()); | 
|  | 81 |  | 
|  | 82 | // Measure each MBB and compute a size for the entire function. | 
|  | 83 | unsigned FuncSize = 0; | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 84 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; | 
|  | 85 | ++MFI) { | 
|  | 86 | MachineBasicBlock *MBB = MFI; | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 87 |  | 
|  | 88 | unsigned BlockSize = 0; | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 89 | for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end(); | 
|  | 90 | MBBI != EE; ++MBBI) | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 91 | BlockSize += getNumBytesForInstruction(MBBI); | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 92 |  | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 93 | BlockSizes[MBB->getNumber()] = BlockSize; | 
|  | 94 | FuncSize += BlockSize; | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 95 | } | 
|  | 96 |  | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 97 | // If the entire function is smaller than the displacement of a branch field, | 
|  | 98 | // we know we don't need to shrink any branches in this function.  This is a | 
|  | 99 | // common case. | 
|  | 100 | if (FuncSize < (1 << 15)) { | 
|  | 101 | BlockSizes.clear(); | 
|  | 102 | return false; | 
|  | 103 | } | 
|  | 104 |  | 
|  | 105 | // For each conditional branch, if the offset to its destination is larger | 
|  | 106 | // than the offset field allows, transform it into a long branch sequence | 
|  | 107 | // like this: | 
|  | 108 | //   short branch: | 
|  | 109 | //     bCC MBB | 
|  | 110 | //   long branch: | 
|  | 111 | //     b!CC $PC+8 | 
|  | 112 | //     b MBB | 
|  | 113 | // | 
|  | 114 | bool MadeChange = true; | 
|  | 115 | bool EverMadeChange = false; | 
|  | 116 | while (MadeChange) { | 
|  | 117 | // Iteratively expand branches until we reach a fixed point. | 
|  | 118 | MadeChange = false; | 
|  | 119 |  | 
|  | 120 | for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E; | 
|  | 121 | ++MFI) { | 
|  | 122 | MachineBasicBlock &MBB = *MFI; | 
|  | 123 | unsigned MBBStartOffset = 0; | 
|  | 124 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); | 
|  | 125 | I != E; ++I) { | 
| Dan Gohman | 9da02f5 | 2007-09-14 20:33:02 +0000 | [diff] [blame] | 126 | if (I->getOpcode() != PPC::BCC || I->getOperand(2).isImmediate()) { | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 127 | MBBStartOffset += getNumBytesForInstruction(I); | 
|  | 128 | continue; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | // Determine the offset from the current branch to the destination | 
|  | 132 | // block. | 
| Chris Lattner | a5bb370 | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 133 | MachineBasicBlock *Dest = I->getOperand(2).getMBB(); | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 134 |  | 
|  | 135 | int BranchSize; | 
|  | 136 | if (Dest->getNumber() <= MBB.getNumber()) { | 
|  | 137 | // If this is a backwards branch, the delta is the offset from the | 
|  | 138 | // start of this block to this branch, plus the sizes of all blocks | 
|  | 139 | // from this block to the dest. | 
|  | 140 | BranchSize = MBBStartOffset; | 
|  | 141 |  | 
|  | 142 | for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i) | 
|  | 143 | BranchSize += BlockSizes[i]; | 
|  | 144 | } else { | 
|  | 145 | // Otherwise, add the size of the blocks between this block and the | 
|  | 146 | // dest to the number of bytes left in this block. | 
|  | 147 | BranchSize = -MBBStartOffset; | 
|  | 148 |  | 
|  | 149 | for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i) | 
|  | 150 | BranchSize += BlockSizes[i]; | 
|  | 151 | } | 
|  | 152 |  | 
|  | 153 | // If this branch is in range, ignore it. | 
|  | 154 | if (isInt16(BranchSize)) { | 
|  | 155 | MBBStartOffset += 4; | 
|  | 156 | continue; | 
|  | 157 | } | 
|  | 158 |  | 
|  | 159 | // Otherwise, we have to expand it to a long branch. | 
|  | 160 | // The BCC operands are: | 
|  | 161 | // 0. PPC branch predicate | 
|  | 162 | // 1. CR register | 
|  | 163 | // 2. Target MBB | 
|  | 164 | PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm(); | 
|  | 165 | unsigned CRReg = I->getOperand(1).getReg(); | 
|  | 166 |  | 
|  | 167 | MachineInstr *OldBranch = I; | 
|  | 168 |  | 
|  | 169 | // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition. | 
| Evan Cheng | 20350c4 | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 170 | BuildMI(MBB, I, TII->get(PPC::BCC)) | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 171 | .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2); | 
|  | 172 |  | 
|  | 173 | // Uncond branch to the real destination. | 
| Evan Cheng | 20350c4 | 2006-11-27 23:37:22 +0000 | [diff] [blame] | 174 | I = BuildMI(MBB, I, TII->get(PPC::B)).addMBB(Dest); | 
| Chris Lattner | 542dfd5 | 2006-11-18 00:32:03 +0000 | [diff] [blame] | 175 |  | 
|  | 176 | // Remove the old branch from the function. | 
|  | 177 | OldBranch->eraseFromParent(); | 
|  | 178 |  | 
|  | 179 | // Remember that this instruction is 8-bytes, increase the size of the | 
|  | 180 | // block by 4, remember to iterate. | 
|  | 181 | BlockSizes[MBB.getNumber()] += 4; | 
|  | 182 | MBBStartOffset += 8; | 
|  | 183 | ++NumExpanded; | 
|  | 184 | MadeChange = true; | 
|  | 185 | } | 
|  | 186 | } | 
|  | 187 | EverMadeChange |= MadeChange; | 
|  | 188 | } | 
|  | 189 |  | 
|  | 190 | BlockSizes.clear(); | 
| Chris Lattner | 26e385a | 2006-02-08 19:33:26 +0000 | [diff] [blame] | 191 | return true; | 
|  | 192 | } | 
|  | 193 |  |