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