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