Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1 | //===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a pass that splits the constant pool up into 'islands' |
| 11 | // which are scattered through-out the function. This is required due to the |
| 12 | // limited pc-relative displacements that ARM has. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "arm-cp-islands" |
| 17 | #include "ARM.h" |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 19 | #include "ARMInstrInfo.h" |
| 20 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 21 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 22 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
| 24 | #include "llvm/Target/TargetMachine.h" |
| 25 | #include "llvm/Support/Compiler.h" |
| 26 | #include "llvm/Support/Debug.h" |
| 27 | #include "llvm/ADT/STLExtras.h" |
| 28 | #include "llvm/ADT/Statistic.h" |
| 29 | #include <iostream> |
| 30 | using namespace llvm; |
| 31 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 32 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 33 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 34 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 35 | |
| 36 | namespace { |
| 37 | /// ARMConstantIslands - Due to limited pc-relative displacements, ARM |
| 38 | /// requires constant pool entries to be scattered among the instructions |
| 39 | /// inside a function. To do this, it completely ignores the normal LLVM |
| 40 | /// constant pool, instead, it places constants where-ever it feels like with |
| 41 | /// special instructions. |
| 42 | /// |
| 43 | /// The terminology used in this pass includes: |
| 44 | /// Islands - Clumps of constants placed in the function. |
| 45 | /// Water - Potential places where an island could be formed. |
| 46 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 47 | /// tracks a list of users. |
| 48 | class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass { |
| 49 | /// NextUID - Assign unique ID's to CPE's. |
| 50 | unsigned NextUID; |
| 51 | |
| 52 | /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed |
| 53 | /// by MBB Number. |
| 54 | std::vector<unsigned> BBSizes; |
| 55 | |
| 56 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 57 | /// (i.e. blocks that don't fall through to the following block, due |
| 58 | /// to a return, unreachable, or unconditional branch). |
| 59 | std::vector<MachineBasicBlock*> WaterList; |
| 60 | |
| 61 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 62 | /// pointer, the constant pool being referenced, and the max displacement |
| 63 | /// allowed from the instruction to the CP. |
| 64 | struct CPUser { |
| 65 | MachineInstr *MI; |
| 66 | MachineInstr *CPEMI; |
| 67 | unsigned MaxDisp; |
| 68 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp) |
| 69 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp) {} |
| 70 | }; |
| 71 | |
| 72 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 73 | /// constant pools and their max displacement. |
| 74 | std::vector<CPUser> CPUsers; |
| 75 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 76 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 77 | /// pointer, conditional or unconditional, the max displacement, |
| 78 | /// and (if isCond is true) the corresponding unconditional branch |
| 79 | /// opcode. |
| 80 | struct ImmBranch { |
| 81 | MachineInstr *MI; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 82 | unsigned MaxDisp : 31; |
| 83 | bool isCond : 1; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 84 | int UncondBr; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 85 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) |
| 86 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 89 | /// Branches - Keep track of all the immediate branch instructions. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 90 | /// |
| 91 | std::vector<ImmBranch> ImmBranches; |
| 92 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 93 | /// PushPopMIs - Keep track of all the Thumb push / pop instructions. |
| 94 | /// |
| 95 | std::vector<MachineInstr*> PushPopMIs; |
| 96 | |
| 97 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 98 | /// the branch fix up pass. |
| 99 | bool HasFarJump; |
| 100 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 101 | const TargetInstrInfo *TII; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 102 | const ARMFunctionInfo *AFI; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 103 | public: |
| 104 | virtual bool runOnMachineFunction(MachineFunction &Fn); |
| 105 | |
| 106 | virtual const char *getPassName() const { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 107 | return "ARM constant island placement and branch shortening pass"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | private: |
| 111 | void DoInitialPlacement(MachineFunction &Fn, |
| 112 | std::vector<MachineInstr*> &CPEMIs); |
| 113 | void InitialFunctionScan(MachineFunction &Fn, |
| 114 | const std::vector<MachineInstr*> &CPEMIs); |
| 115 | void SplitBlockBeforeInstr(MachineInstr *MI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 116 | void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 117 | bool HandleConstantPoolUser(MachineFunction &Fn, CPUser &U); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 118 | bool BBIsInBranchRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned D); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 119 | bool FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br); |
| 120 | bool FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br); |
| 121 | bool FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br); |
| 122 | bool UndoLRSpillRestore(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 123 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 124 | unsigned GetOffsetOf(MachineInstr *MI) const; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 125 | unsigned GetOffsetOf(MachineBasicBlock *MBB) const; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 126 | }; |
| 127 | } |
| 128 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 129 | /// createARMConstantIslandPass - returns an instance of the constpool |
| 130 | /// island pass. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 131 | FunctionPass *llvm::createARMConstantIslandPass() { |
| 132 | return new ARMConstantIslands(); |
| 133 | } |
| 134 | |
| 135 | bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 136 | MachineConstantPool &MCP = *Fn.getConstantPool(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 137 | |
| 138 | TII = Fn.getTarget().getInstrInfo(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 139 | AFI = Fn.getInfo<ARMFunctionInfo>(); |
| 140 | |
| 141 | HasFarJump = false; |
| 142 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 143 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 144 | // the numbers agree with the position of the block in the function. |
| 145 | Fn.RenumberBlocks(); |
| 146 | |
| 147 | // Perform the initial placement of the constant pool entries. To start with, |
| 148 | // we put them all at the end of the function. |
| 149 | std::vector<MachineInstr*> CPEMIs; |
Evan Cheng | 7755fac | 2007-01-26 01:04:44 +0000 | [diff] [blame] | 150 | if (!MCP.isEmpty()) |
| 151 | DoInitialPlacement(Fn, CPEMIs); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 152 | |
| 153 | /// The next UID to take is the first unused one. |
| 154 | NextUID = CPEMIs.size(); |
| 155 | |
| 156 | // Do the initial scan of the function, building up information about the |
| 157 | // sizes of each block, the location of all the water, and finding all of the |
| 158 | // constant pool users. |
| 159 | InitialFunctionScan(Fn, CPEMIs); |
| 160 | CPEMIs.clear(); |
| 161 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 162 | // Iteratively place constant pool entries and fix up branches until there |
| 163 | // is no change. |
| 164 | bool MadeChange = false; |
| 165 | while (true) { |
| 166 | bool Change = false; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 167 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 168 | Change |= HandleConstantPoolUser(Fn, CPUsers[i]); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 169 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 170 | Change |= FixUpImmediateBr(Fn, ImmBranches[i]); |
| 171 | if (!Change) |
| 172 | break; |
| 173 | MadeChange = true; |
| 174 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 175 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 176 | // If LR has been forced spilled and no far jumps (i.e. BL) has been issued. |
| 177 | // Undo the spill / restore of LR if possible. |
| 178 | if (!HasFarJump && AFI->isLRForceSpilled() && AFI->isThumbFunction()) |
| 179 | MadeChange |= UndoLRSpillRestore(); |
| 180 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 181 | BBSizes.clear(); |
| 182 | WaterList.clear(); |
| 183 | CPUsers.clear(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 184 | ImmBranches.clear(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 185 | |
| 186 | return MadeChange; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | /// DoInitialPlacement - Perform the initial placement of the constant pool |
| 190 | /// entries. To start with, we put them all at the end of the function. |
| 191 | void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn, |
| 192 | std::vector<MachineInstr*> &CPEMIs){ |
| 193 | // Create the basic block to hold the CPE's. |
| 194 | MachineBasicBlock *BB = new MachineBasicBlock(); |
| 195 | Fn.getBasicBlockList().push_back(BB); |
| 196 | |
| 197 | // Add all of the constants from the constant pool to the end block, use an |
| 198 | // identity mapping of CPI's to CPE's. |
| 199 | const std::vector<MachineConstantPoolEntry> &CPs = |
| 200 | Fn.getConstantPool()->getConstants(); |
| 201 | |
| 202 | const TargetData &TD = *Fn.getTarget().getTargetData(); |
| 203 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
| 204 | unsigned Size = TD.getTypeSize(CPs[i].getType()); |
| 205 | // Verify that all constant pool entries are a multiple of 4 bytes. If not, |
| 206 | // we would have to pad them out or something so that instructions stay |
| 207 | // aligned. |
| 208 | assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 209 | MachineInstr *CPEMI = |
| 210 | BuildMI(BB, TII->get(ARM::CONSTPOOL_ENTRY)) |
| 211 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
| 212 | CPEMIs.push_back(CPEMI); |
| 213 | DEBUG(std::cerr << "Moved CPI#" << i << " to end of function as #" |
| 214 | << i << "\n"); |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | /// BBHasFallthrough - Return true of the specified basic block can fallthrough |
| 219 | /// into the block immediately after it. |
| 220 | static bool BBHasFallthrough(MachineBasicBlock *MBB) { |
| 221 | // Get the next machine basic block in the function. |
| 222 | MachineFunction::iterator MBBI = MBB; |
| 223 | if (next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function. |
| 224 | return false; |
| 225 | |
| 226 | MachineBasicBlock *NextBB = next(MBBI); |
| 227 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), |
| 228 | E = MBB->succ_end(); I != E; ++I) |
| 229 | if (*I == NextBB) |
| 230 | return true; |
| 231 | |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | /// InitialFunctionScan - Do the initial scan of the function, building up |
| 236 | /// information about the sizes of each block, the location of all the water, |
| 237 | /// and finding all of the constant pool users. |
| 238 | void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn, |
| 239 | const std::vector<MachineInstr*> &CPEMIs) { |
| 240 | for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end(); |
| 241 | MBBI != E; ++MBBI) { |
| 242 | MachineBasicBlock &MBB = *MBBI; |
| 243 | |
| 244 | // If this block doesn't fall through into the next MBB, then this is |
| 245 | // 'water' that a constant pool island could be placed. |
| 246 | if (!BBHasFallthrough(&MBB)) |
| 247 | WaterList.push_back(&MBB); |
| 248 | |
| 249 | unsigned MBBSize = 0; |
| 250 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 251 | I != E; ++I) { |
| 252 | // Add instruction size to MBBSize. |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 253 | MBBSize += ARM::GetInstSize(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 254 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 255 | int Opc = I->getOpcode(); |
| 256 | if (TII->isBranch(Opc)) { |
| 257 | bool isCond = false; |
| 258 | unsigned Bits = 0; |
| 259 | unsigned Scale = 1; |
| 260 | int UOpc = Opc; |
| 261 | switch (Opc) { |
Evan Cheng | 743fa03 | 2007-01-25 19:43:52 +0000 | [diff] [blame] | 262 | default: |
| 263 | continue; // Ignore JT branches |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 264 | case ARM::Bcc: |
| 265 | isCond = true; |
| 266 | UOpc = ARM::B; |
| 267 | // Fallthrough |
| 268 | case ARM::B: |
| 269 | Bits = 24; |
| 270 | Scale = 4; |
| 271 | break; |
| 272 | case ARM::tBcc: |
| 273 | isCond = true; |
| 274 | UOpc = ARM::tB; |
| 275 | Bits = 8; |
| 276 | Scale = 2; |
| 277 | break; |
| 278 | case ARM::tB: |
| 279 | Bits = 11; |
| 280 | Scale = 2; |
| 281 | break; |
| 282 | } |
| 283 | unsigned MaxDisp = (1 << (Bits-1)) * Scale; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 284 | ImmBranches.push_back(ImmBranch(I, MaxDisp, isCond, UOpc)); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 287 | if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET) |
| 288 | PushPopMIs.push_back(I); |
| 289 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 290 | // Scan the instructions for constant pool operands. |
| 291 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
| 292 | if (I->getOperand(op).isConstantPoolIndex()) { |
| 293 | // We found one. The addressing mode tells us the max displacement |
| 294 | // from the PC that this instruction permits. |
| 295 | unsigned MaxOffs = 0; |
| 296 | |
| 297 | // Basic size info comes from the TSFlags field. |
| 298 | unsigned TSFlags = I->getInstrDescriptor()->TSFlags; |
| 299 | switch (TSFlags & ARMII::AddrModeMask) { |
| 300 | default: |
| 301 | // Constant pool entries can reach anything. |
| 302 | if (I->getOpcode() == ARM::CONSTPOOL_ENTRY) |
| 303 | continue; |
| 304 | assert(0 && "Unknown addressing mode for CP reference!"); |
| 305 | case ARMII::AddrMode1: // AM1: 8 bits << 2 |
| 306 | MaxOffs = 1 << (8+2); // Taking the address of a CP entry. |
| 307 | break; |
| 308 | case ARMII::AddrMode2: |
| 309 | MaxOffs = 1 << 12; // +-offset_12 |
| 310 | break; |
| 311 | case ARMII::AddrMode3: |
| 312 | MaxOffs = 1 << 8; // +-offset_8 |
| 313 | break; |
| 314 | // addrmode4 has no immediate offset. |
| 315 | case ARMII::AddrMode5: |
| 316 | MaxOffs = 1 << (8+2); // +-(offset_8*4) |
| 317 | break; |
| 318 | case ARMII::AddrModeT1: |
| 319 | MaxOffs = 1 << 5; |
| 320 | break; |
| 321 | case ARMII::AddrModeT2: |
| 322 | MaxOffs = 1 << (5+1); |
| 323 | break; |
| 324 | case ARMII::AddrModeT4: |
| 325 | MaxOffs = 1 << (5+2); |
| 326 | break; |
Evan Cheng | 012f2d9 | 2007-01-24 08:53:17 +0000 | [diff] [blame] | 327 | case ARMII::AddrModeTs: |
| 328 | MaxOffs = 1 << (8+2); |
| 329 | break; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | // Remember that this is a user of a CP entry. |
| 333 | MachineInstr *CPEMI =CPEMIs[I->getOperand(op).getConstantPoolIndex()]; |
| 334 | CPUsers.push_back(CPUser(I, CPEMI, MaxOffs)); |
| 335 | |
| 336 | // Instructions can only use one CP entry, don't bother scanning the |
| 337 | // rest of the operands. |
| 338 | break; |
| 339 | } |
| 340 | } |
| 341 | BBSizes.push_back(MBBSize); |
| 342 | } |
| 343 | } |
| 344 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 345 | /// GetOffsetOf - Return the current offset of the specified machine instruction |
| 346 | /// from the start of the function. This offset changes as stuff is moved |
| 347 | /// around inside the function. |
| 348 | unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const { |
| 349 | MachineBasicBlock *MBB = MI->getParent(); |
| 350 | |
| 351 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 352 | // before this instruction's block, and the offset from the start of the block |
| 353 | // it is in. |
| 354 | unsigned Offset = 0; |
| 355 | |
| 356 | // Sum block sizes before MBB. |
| 357 | for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB) |
| 358 | Offset += BBSizes[BB]; |
| 359 | |
| 360 | // Sum instructions before MI in MBB. |
| 361 | for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) { |
| 362 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
| 363 | if (&*I == MI) return Offset; |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 364 | Offset += ARM::GetInstSize(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 365 | } |
| 366 | } |
| 367 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 368 | /// GetOffsetOf - Return the current offset of the specified machine BB |
| 369 | /// from the start of the function. This offset changes as stuff is moved |
| 370 | /// around inside the function. |
| 371 | unsigned ARMConstantIslands::GetOffsetOf(MachineBasicBlock *MBB) const { |
| 372 | // Sum block sizes before MBB. |
| 373 | unsigned Offset = 0; |
| 374 | for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB) |
| 375 | Offset += BBSizes[BB]; |
| 376 | |
| 377 | return Offset; |
| 378 | } |
| 379 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 380 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 381 | /// ID. |
| 382 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 383 | const MachineBasicBlock *RHS) { |
| 384 | return LHS->getNumber() < RHS->getNumber(); |
| 385 | } |
| 386 | |
| 387 | /// UpdateForInsertedWaterBlock - When a block is newly inserted into the |
| 388 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 389 | /// and update the arrays that parallel this numbering. |
| 390 | void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) { |
| 391 | // Renumber the MBB's to keep them consequtive. |
| 392 | NewBB->getParent()->RenumberBlocks(NewBB); |
| 393 | |
| 394 | // Insert a size into BBSizes to align it properly with the (newly |
| 395 | // renumbered) block numbers. |
| 396 | BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0); |
| 397 | |
| 398 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
| 399 | // available water after it. |
| 400 | std::vector<MachineBasicBlock*>::iterator IP = |
| 401 | std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, |
| 402 | CompareMBBNumbers); |
| 403 | WaterList.insert(IP, NewBB); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | /// Split the basic block containing MI into two blocks, which are joined by |
| 408 | /// an unconditional branch. Update datastructures and renumber blocks to |
| 409 | /// account for this change. |
| 410 | void ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) { |
| 411 | MachineBasicBlock *OrigBB = MI->getParent(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 412 | bool isThumb = AFI->isThumbFunction(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 413 | |
| 414 | // Create a new MBB for the code after the OrigBB. |
| 415 | MachineBasicBlock *NewBB = new MachineBasicBlock(OrigBB->getBasicBlock()); |
| 416 | MachineFunction::iterator MBBI = OrigBB; ++MBBI; |
| 417 | OrigBB->getParent()->getBasicBlockList().insert(MBBI, NewBB); |
| 418 | |
| 419 | // Splice the instructions starting with MI over to NewBB. |
| 420 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
| 421 | |
| 422 | // Add an unconditional branch from OrigBB to NewBB. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 423 | BuildMI(OrigBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 424 | NumSplit++; |
| 425 | |
| 426 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
| 427 | while (!OrigBB->succ_empty()) { |
| 428 | MachineBasicBlock *Succ = *OrigBB->succ_begin(); |
| 429 | OrigBB->removeSuccessor(Succ); |
| 430 | NewBB->addSuccessor(Succ); |
| 431 | |
| 432 | // This pass should be run after register allocation, so there should be no |
| 433 | // PHI nodes to update. |
| 434 | assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI) |
| 435 | && "PHI nodes should be eliminated by now!"); |
| 436 | } |
| 437 | |
| 438 | // OrigBB branches to NewBB. |
| 439 | OrigBB->addSuccessor(NewBB); |
| 440 | |
| 441 | // Update internal data structures to account for the newly inserted MBB. |
| 442 | UpdateForInsertedWaterBlock(NewBB); |
| 443 | |
| 444 | // Figure out how large the first NewMBB is. |
| 445 | unsigned NewBBSize = 0; |
| 446 | for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end(); |
| 447 | I != E; ++I) |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 448 | NewBBSize += ARM::GetInstSize(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 449 | |
| 450 | // Set the size of NewBB in BBSizes. |
| 451 | BBSizes[NewBB->getNumber()] = NewBBSize; |
| 452 | |
| 453 | // We removed instructions from UserMBB, subtract that off from its size. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 454 | // Add 2 or 4 to the block to count the unconditional branch we added to it. |
| 455 | BBSizes[OrigBB->getNumber()] -= NewBBSize - (isThumb ? 2 : 4); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | /// HandleConstantPoolUser - Analyze the specified user, checking to see if it |
| 459 | /// is out-of-range. If so, pick it up the constant pool value and move it some |
| 460 | /// place in-range. |
| 461 | bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn, CPUser &U){ |
| 462 | MachineInstr *UserMI = U.MI; |
| 463 | MachineInstr *CPEMI = U.CPEMI; |
| 464 | |
| 465 | unsigned UserOffset = GetOffsetOf(UserMI); |
| 466 | unsigned CPEOffset = GetOffsetOf(CPEMI); |
| 467 | |
| 468 | DEBUG(std::cerr << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 469 | << " max delta=" << U.MaxDisp |
| 470 | << " at offset " << int(UserOffset-CPEOffset) << "\t" |
| 471 | << *UserMI); |
| 472 | |
| 473 | // Check to see if the CPE is already in-range. |
| 474 | if (UserOffset < CPEOffset) { |
| 475 | // User before the CPE. |
| 476 | if (CPEOffset-UserOffset <= U.MaxDisp) |
| 477 | return false; |
| 478 | } else { |
| 479 | if (UserOffset-CPEOffset <= U.MaxDisp) |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | |
| 484 | // Solution guaranteed to work: split the user's MBB right before the user and |
| 485 | // insert a clone the CPE into the newly created water. |
| 486 | |
| 487 | // If the user isn't at the start of its MBB, or if there is a fall-through |
| 488 | // into the user's MBB, split the MBB before the User. |
| 489 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
| 490 | if (&UserMBB->front() != UserMI || |
| 491 | UserMBB == &Fn.front() || // entry MBB of function. |
| 492 | BBHasFallthrough(prior(MachineFunction::iterator(UserMBB)))) { |
| 493 | // TODO: Search for the best place to split the code. In practice, using |
| 494 | // loop nesting information to insert these guys outside of loops would be |
| 495 | // sufficient. |
| 496 | SplitBlockBeforeInstr(UserMI); |
| 497 | |
| 498 | // UserMI's BB may have changed. |
| 499 | UserMBB = UserMI->getParent(); |
| 500 | } |
| 501 | |
| 502 | // Okay, we know we can put an island before UserMBB now, do it! |
| 503 | MachineBasicBlock *NewIsland = new MachineBasicBlock(); |
| 504 | Fn.getBasicBlockList().insert(UserMBB, NewIsland); |
| 505 | |
| 506 | // Update internal data structures to account for the newly inserted MBB. |
| 507 | UpdateForInsertedWaterBlock(NewIsland); |
| 508 | |
| 509 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 510 | // add it to the island. |
| 511 | unsigned ID = NextUID++; |
| 512 | unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex(); |
| 513 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 514 | |
| 515 | // Build a new CPE for this user. |
| 516 | U.CPEMI = BuildMI(NewIsland, TII->get(ARM::CONSTPOOL_ENTRY)) |
| 517 | .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); |
| 518 | |
| 519 | // Increase the size of the island block to account for the new entry. |
| 520 | BBSizes[NewIsland->getNumber()] += Size; |
| 521 | |
| 522 | // Finally, change the CPI in the instruction operand to be ID. |
| 523 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
| 524 | if (UserMI->getOperand(i).isConstantPoolIndex()) { |
| 525 | UserMI->getOperand(i).setConstantPoolIndex(ID); |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | DEBUG(std::cerr << " Moved CPE to #" << ID << " CPI=" << CPI << "\t" |
| 530 | << *UserMI); |
| 531 | |
| 532 | |
| 533 | return true; |
| 534 | } |
| 535 | |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 536 | /// BBIsInBranchRange - Returns true is the distance between specific MI and |
| 537 | /// specific BB can fit in MI's displacement field. |
| 538 | bool ARMConstantIslands::BBIsInBranchRange(MachineInstr *MI, |
| 539 | MachineBasicBlock *DestBB, |
| 540 | unsigned MaxDisp) { |
| 541 | unsigned BrOffset = GetOffsetOf(MI); |
| 542 | unsigned DestOffset = GetOffsetOf(DestBB); |
| 543 | |
| 544 | // Check to see if the destination BB is in range. |
| 545 | if (BrOffset < DestOffset) { |
| 546 | if (DestOffset - BrOffset < MaxDisp) |
| 547 | return true; |
| 548 | } else { |
| 549 | if (BrOffset - DestOffset <= MaxDisp) |
| 550 | return true; |
| 551 | } |
| 552 | return false; |
| 553 | } |
| 554 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 555 | /// FixUpImmediateBr - Fix up an immediate branch whose destination is too far |
| 556 | /// away to fit in its displacement field. |
| 557 | bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 558 | MachineInstr *MI = Br.MI; |
| 559 | MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock(); |
| 560 | |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 561 | if (BBIsInBranchRange(MI, DestBB, Br.MaxDisp)) |
| 562 | return false; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 563 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 564 | if (!Br.isCond) |
| 565 | return FixUpUnconditionalBr(Fn, Br); |
| 566 | return FixUpConditionalBr(Fn, Br); |
| 567 | } |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 568 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 569 | /// FixUpUnconditionalBr - Fix up an unconditional branches whose destination is |
| 570 | /// too far away to fit in its displacement field. If LR register has been |
| 571 | /// spilled in the epilogue, then we can use BL to implement a far jump. |
| 572 | /// Otherwise, add a intermediate branch instruction to to a branch. |
| 573 | bool |
| 574 | ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br) { |
| 575 | MachineInstr *MI = Br.MI; |
| 576 | MachineBasicBlock *MBB = MI->getParent(); |
| 577 | assert(AFI->isThumbFunction() && "Expected a Thumb function!"); |
| 578 | |
| 579 | // Use BL to implement far jump. |
| 580 | Br.MaxDisp = (1 << 21) * 2; |
| 581 | MI->setInstrDescriptor(TII->get(ARM::tBfar)); |
| 582 | BBSizes[MBB->getNumber()] += 2; |
| 583 | HasFarJump = true; |
| 584 | NumUBrFixed++; |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | static inline unsigned getUncondBranchDisp(int Opc) { |
| 589 | return (Opc == ARM::tB) ? (1<<10)*2 : (1<<23)*4; |
| 590 | } |
| 591 | |
| 592 | /// FixUpConditionalBr - Fix up a conditional branches whose destination is too |
| 593 | /// far away to fit in its displacement field. It is converted to an inverse |
| 594 | /// conditional branch + an unconditional branch to the destination. |
| 595 | bool |
| 596 | ARMConstantIslands::FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br) { |
| 597 | MachineInstr *MI = Br.MI; |
| 598 | MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock(); |
| 599 | |
| 600 | // Add a unconditional branch to the destination and invert the branch |
| 601 | // condition to jump over it: |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 602 | // blt L1 |
| 603 | // => |
| 604 | // bge L2 |
| 605 | // b L1 |
| 606 | // L2: |
| 607 | ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImmedValue(); |
| 608 | CC = ARMCC::getOppositeCondition(CC); |
| 609 | |
| 610 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 611 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 612 | // split the MBB before the next instruction. |
| 613 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 614 | MachineInstr *BackMI = &MBB->back(); |
| 615 | bool NeedSplit = (BackMI != MI) || !BBHasFallthrough(MBB); |
| 616 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 617 | NumCBrFixed++; |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 618 | if (BackMI != MI) { |
| 619 | if (next(MachineBasicBlock::iterator(MI)) == MBB->back() && |
| 620 | BackMI->getOpcode() == Br.UncondBr) { |
| 621 | // Last MI in the BB is a unconditional branch. Can we simply invert the |
| 622 | // condition and swap destinations: |
| 623 | // beq L1 |
| 624 | // b L2 |
| 625 | // => |
| 626 | // bne L2 |
| 627 | // b L1 |
| 628 | MachineBasicBlock *NewDest = BackMI->getOperand(0).getMachineBasicBlock(); |
| 629 | if (BBIsInBranchRange(MI, NewDest, Br.MaxDisp)) { |
| 630 | BackMI->getOperand(0).setMachineBasicBlock(DestBB); |
| 631 | MI->getOperand(0).setMachineBasicBlock(NewDest); |
| 632 | MI->getOperand(1).setImm(CC); |
| 633 | return true; |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | if (NeedSplit) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 639 | SplitBlockBeforeInstr(MI); |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 640 | // No need for the branch to the next block. We're adding a unconditional |
| 641 | // branch to the destination. |
| 642 | MBB->back().eraseFromParent(); |
| 643 | } |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 644 | MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB)); |
| 645 | |
| 646 | // Insert a unconditional branch and replace the conditional branch. |
| 647 | // Also update the ImmBranch as well as adding a new entry for the new branch. |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 648 | BuildMI(MBB, TII->get(MI->getOpcode())).addMBB(NextBB).addImm(CC); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 649 | Br.MI = &MBB->back(); |
| 650 | BuildMI(MBB, TII->get(Br.UncondBr)).addMBB(DestBB); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 651 | unsigned MaxDisp = getUncondBranchDisp(Br.UncondBr); |
Evan Cheng | a0bf794 | 2007-01-25 23:31:04 +0000 | [diff] [blame] | 652 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 653 | MI->eraseFromParent(); |
| 654 | |
| 655 | // Increase the size of MBB to account for the new unconditional branch. |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 656 | BBSizes[MBB->getNumber()] += ARM::GetInstSize(&MBB->back()); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 657 | return true; |
| 658 | } |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame^] | 659 | |
| 660 | |
| 661 | /// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills |
| 662 | /// LR / restores LR to pc. |
| 663 | bool ARMConstantIslands::UndoLRSpillRestore() { |
| 664 | bool MadeChange = false; |
| 665 | for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { |
| 666 | MachineInstr *MI = PushPopMIs[i]; |
| 667 | if (MI->getNumOperands() == 1) { |
| 668 | if (MI->getOpcode() == ARM::tPOP_RET && |
| 669 | MI->getOperand(0).getReg() == ARM::PC) |
| 670 | BuildMI(MI->getParent(), TII->get(ARM::tBX_RET)); |
| 671 | MI->eraseFromParent(); |
| 672 | MadeChange = true; |
| 673 | } |
| 674 | } |
| 675 | return MadeChange; |
| 676 | } |