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" |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | #include "llvm/ADT/Statistic.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 30 | using namespace llvm; |
| 31 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 32 | STATISTIC(NumCPEs, "Number of constpool entries"); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 33 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 34 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 35 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 36 | |
| 37 | namespace { |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 38 | /// ARMConstantIslands - Due to limited PC-relative displacements, ARM |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 39 | /// requires constant pool entries to be scattered among the instructions |
| 40 | /// inside a function. To do this, it completely ignores the normal LLVM |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 41 | /// constant pool; instead, it places constants wherever it feels like with |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 42 | /// special instructions. |
| 43 | /// |
| 44 | /// The terminology used in this pass includes: |
| 45 | /// Islands - Clumps of constants placed in the function. |
| 46 | /// Water - Potential places where an island could be formed. |
| 47 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 48 | /// tracks a list of users. |
| 49 | class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass { |
| 50 | /// NextUID - Assign unique ID's to CPE's. |
| 51 | unsigned NextUID; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 52 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 53 | /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed |
| 54 | /// by MBB Number. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 55 | std::vector<unsigned> BBSizes; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 56 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 57 | /// BBOffsets - the offset of each MBB in bytes, starting from 0. |
| 58 | std::vector<unsigned> BBOffsets; |
| 59 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 60 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 61 | /// (i.e. blocks that don't fall through to the following block, due |
| 62 | /// to a return, unreachable, or unconditional branch). |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 63 | std::vector<MachineBasicBlock*> WaterList; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 64 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 65 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 66 | /// pointer, the constant pool being referenced, and the max displacement |
| 67 | /// allowed from the instruction to the CP. |
| 68 | struct CPUser { |
| 69 | MachineInstr *MI; |
| 70 | MachineInstr *CPEMI; |
| 71 | unsigned MaxDisp; |
| 72 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp) |
| 73 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp) {} |
| 74 | }; |
| 75 | |
| 76 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 77 | /// constant pools and their max displacement. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 78 | std::vector<CPUser> CPUsers; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 79 | |
| 80 | /// CPEntry - One per constant pool entry, keeping the machine instruction |
| 81 | /// pointer, the constpool index, and the number of CPUser's which |
| 82 | /// reference this entry. |
| 83 | struct CPEntry { |
| 84 | MachineInstr *CPEMI; |
| 85 | unsigned CPI; |
| 86 | unsigned RefCount; |
| 87 | CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) |
| 88 | : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} |
| 89 | }; |
| 90 | |
| 91 | /// CPEntries - Keep track of all of the constant pool entry machine |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 92 | /// instructions. For each original constpool index (i.e. those that |
| 93 | /// existed upon entry to this pass), it keeps a vector of entries. |
| 94 | /// Original elements are cloned as we go along; the clones are |
| 95 | /// put in the vector of the original element, but have distinct CPIs. |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 96 | std::vector<std::vector<CPEntry> > CPEntries; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 97 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 98 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 99 | /// pointer, conditional or unconditional, the max displacement, |
| 100 | /// and (if isCond is true) the corresponding unconditional branch |
| 101 | /// opcode. |
| 102 | struct ImmBranch { |
| 103 | MachineInstr *MI; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 104 | unsigned MaxDisp : 31; |
| 105 | bool isCond : 1; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 106 | int UncondBr; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 107 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) |
| 108 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 109 | }; |
| 110 | |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 111 | /// Branches - Keep track of all the immediate branch instructions. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 112 | /// |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 113 | std::vector<ImmBranch> ImmBranches; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 114 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 115 | /// PushPopMIs - Keep track of all the Thumb push / pop instructions. |
| 116 | /// |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 117 | SmallVector<MachineInstr*, 4> PushPopMIs; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 118 | |
| 119 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 120 | /// the branch fix up pass. |
| 121 | bool HasFarJump; |
| 122 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 123 | const TargetInstrInfo *TII; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 124 | const ARMFunctionInfo *AFI; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 125 | bool isThumb; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 126 | public: |
| 127 | virtual bool runOnMachineFunction(MachineFunction &Fn); |
| 128 | |
| 129 | virtual const char *getPassName() const { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 130 | return "ARM constant island placement and branch shortening pass"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private: |
| 134 | void DoInitialPlacement(MachineFunction &Fn, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 135 | std::vector<MachineInstr*> &CPEMIs); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 136 | CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 137 | void InitialFunctionScan(MachineFunction &Fn, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 138 | const std::vector<MachineInstr*> &CPEMIs); |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 139 | MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 140 | void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 141 | void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 142 | bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI, unsigned Size); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 143 | int LookForExistingCPEntry(CPUser& U, unsigned UserOffset); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 144 | bool LookForWater(CPUser&U, unsigned UserOffset, bool* PadNewWater, |
| 145 | MachineBasicBlock** NewMBB); |
| 146 | void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset, |
| 147 | MachineBasicBlock** NewMBB); |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 148 | bool HandleConstantPoolUser(MachineFunction &Fn, unsigned CPUserIndex); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 149 | bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset, |
| 150 | MachineInstr *CPEMI, unsigned Disp, |
| 151 | bool DoDump); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 152 | bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water, |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 153 | unsigned Disp); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 154 | bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset, |
| 155 | unsigned Disp, bool NegativeOK); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 156 | bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 157 | bool FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br); |
| 158 | bool FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br); |
| 159 | bool FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br); |
| 160 | bool UndoLRSpillRestore(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 161 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 162 | unsigned GetOffsetOf(MachineInstr *MI) const; |
| 163 | }; |
| 164 | } |
| 165 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 166 | /// createARMConstantIslandPass - returns an instance of the constpool |
| 167 | /// island pass. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 168 | FunctionPass *llvm::createARMConstantIslandPass() { |
| 169 | return new ARMConstantIslands(); |
| 170 | } |
| 171 | |
| 172 | bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 173 | MachineConstantPool &MCP = *Fn.getConstantPool(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 174 | |
| 175 | TII = Fn.getTarget().getInstrInfo(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 176 | AFI = Fn.getInfo<ARMFunctionInfo>(); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 177 | isThumb = AFI->isThumbFunction(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 178 | |
| 179 | HasFarJump = false; |
| 180 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 181 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 182 | // the numbers agree with the position of the block in the function. |
| 183 | Fn.RenumberBlocks(); |
| 184 | |
| 185 | // Perform the initial placement of the constant pool entries. To start with, |
| 186 | // we put them all at the end of the function. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 187 | std::vector<MachineInstr*> CPEMIs; |
Evan Cheng | 7755fac | 2007-01-26 01:04:44 +0000 | [diff] [blame] | 188 | if (!MCP.isEmpty()) |
| 189 | DoInitialPlacement(Fn, CPEMIs); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 190 | |
| 191 | /// The next UID to take is the first unused one. |
| 192 | NextUID = CPEMIs.size(); |
| 193 | |
| 194 | // Do the initial scan of the function, building up information about the |
| 195 | // sizes of each block, the location of all the water, and finding all of the |
| 196 | // constant pool users. |
| 197 | InitialFunctionScan(Fn, CPEMIs); |
| 198 | CPEMIs.clear(); |
| 199 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 200 | // Iteratively place constant pool entries and fix up branches until there |
| 201 | // is no change. |
| 202 | bool MadeChange = false; |
| 203 | while (true) { |
| 204 | bool Change = false; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 205 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 206 | Change |= HandleConstantPoolUser(Fn, i); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 207 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 208 | Change |= FixUpImmediateBr(Fn, ImmBranches[i]); |
| 209 | if (!Change) |
| 210 | break; |
| 211 | MadeChange = true; |
| 212 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 213 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 214 | // If LR has been forced spilled and no far jumps (i.e. BL) has been issued. |
| 215 | // Undo the spill / restore of LR if possible. |
Evan Cheng | f49407b | 2007-03-01 08:26:31 +0000 | [diff] [blame] | 216 | if (!HasFarJump && AFI->isLRSpilledForFarJump() && isThumb) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 217 | MadeChange |= UndoLRSpillRestore(); |
| 218 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 219 | BBSizes.clear(); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 220 | BBOffsets.clear(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 221 | WaterList.clear(); |
| 222 | CPUsers.clear(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 223 | CPEntries.clear(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 224 | ImmBranches.clear(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 225 | PushPopMIs.clear(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 226 | |
| 227 | return MadeChange; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /// DoInitialPlacement - Perform the initial placement of the constant pool |
| 231 | /// entries. To start with, we put them all at the end of the function. |
| 232 | void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 233 | std::vector<MachineInstr*> &CPEMIs){ |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 234 | // Create the basic block to hold the CPE's. |
| 235 | MachineBasicBlock *BB = new MachineBasicBlock(); |
| 236 | Fn.getBasicBlockList().push_back(BB); |
| 237 | |
| 238 | // Add all of the constants from the constant pool to the end block, use an |
| 239 | // identity mapping of CPI's to CPE's. |
| 240 | const std::vector<MachineConstantPoolEntry> &CPs = |
| 241 | Fn.getConstantPool()->getConstants(); |
| 242 | |
| 243 | const TargetData &TD = *Fn.getTarget().getTargetData(); |
| 244 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
| 245 | unsigned Size = TD.getTypeSize(CPs[i].getType()); |
| 246 | // Verify that all constant pool entries are a multiple of 4 bytes. If not, |
| 247 | // we would have to pad them out or something so that instructions stay |
| 248 | // aligned. |
| 249 | assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 250 | MachineInstr *CPEMI = |
| 251 | BuildMI(BB, TII->get(ARM::CONSTPOOL_ENTRY)) |
| 252 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
| 253 | CPEMIs.push_back(CPEMI); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 254 | |
| 255 | // Add a new CPEntry, but no corresponding CPUser yet. |
| 256 | std::vector<CPEntry> CPEs; |
| 257 | CPEs.push_back(CPEntry(CPEMI, i)); |
| 258 | CPEntries.push_back(CPEs); |
| 259 | NumCPEs++; |
| 260 | DOUT << "Moved CPI#" << i << " to end of function as #" << i << "\n"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 264 | /// BBHasFallthrough - Return true if the specified basic block can fallthrough |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 265 | /// into the block immediately after it. |
| 266 | static bool BBHasFallthrough(MachineBasicBlock *MBB) { |
| 267 | // Get the next machine basic block in the function. |
| 268 | MachineFunction::iterator MBBI = MBB; |
| 269 | if (next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function. |
| 270 | return false; |
| 271 | |
| 272 | MachineBasicBlock *NextBB = next(MBBI); |
| 273 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), |
| 274 | E = MBB->succ_end(); I != E; ++I) |
| 275 | if (*I == NextBB) |
| 276 | return true; |
| 277 | |
| 278 | return false; |
| 279 | } |
| 280 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 281 | /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, |
| 282 | /// look up the corresponding CPEntry. |
| 283 | ARMConstantIslands::CPEntry |
| 284 | *ARMConstantIslands::findConstPoolEntry(unsigned CPI, |
| 285 | const MachineInstr *CPEMI) { |
| 286 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 287 | // Number of entries per constpool index should be small, just do a |
| 288 | // linear search. |
| 289 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 290 | if (CPEs[i].CPEMI == CPEMI) |
| 291 | return &CPEs[i]; |
| 292 | } |
| 293 | return NULL; |
| 294 | } |
| 295 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 296 | /// InitialFunctionScan - Do the initial scan of the function, building up |
| 297 | /// information about the sizes of each block, the location of all the water, |
| 298 | /// and finding all of the constant pool users. |
| 299 | void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 300 | const std::vector<MachineInstr*> &CPEMIs) { |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 301 | unsigned Offset = 0; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 302 | for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end(); |
| 303 | MBBI != E; ++MBBI) { |
| 304 | MachineBasicBlock &MBB = *MBBI; |
| 305 | |
| 306 | // If this block doesn't fall through into the next MBB, then this is |
| 307 | // 'water' that a constant pool island could be placed. |
| 308 | if (!BBHasFallthrough(&MBB)) |
| 309 | WaterList.push_back(&MBB); |
| 310 | |
| 311 | unsigned MBBSize = 0; |
| 312 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 313 | I != E; ++I) { |
| 314 | // Add instruction size to MBBSize. |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 315 | MBBSize += ARM::GetInstSize(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 316 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 317 | int Opc = I->getOpcode(); |
| 318 | if (TII->isBranch(Opc)) { |
| 319 | bool isCond = false; |
| 320 | unsigned Bits = 0; |
| 321 | unsigned Scale = 1; |
| 322 | int UOpc = Opc; |
| 323 | switch (Opc) { |
Evan Cheng | 743fa03 | 2007-01-25 19:43:52 +0000 | [diff] [blame] | 324 | default: |
| 325 | continue; // Ignore JT branches |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 326 | case ARM::Bcc: |
| 327 | isCond = true; |
| 328 | UOpc = ARM::B; |
| 329 | // Fallthrough |
| 330 | case ARM::B: |
| 331 | Bits = 24; |
| 332 | Scale = 4; |
| 333 | break; |
| 334 | case ARM::tBcc: |
| 335 | isCond = true; |
| 336 | UOpc = ARM::tB; |
| 337 | Bits = 8; |
| 338 | Scale = 2; |
| 339 | break; |
| 340 | case ARM::tB: |
| 341 | Bits = 11; |
| 342 | Scale = 2; |
| 343 | break; |
| 344 | } |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 345 | |
| 346 | // Record this immediate branch. |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 347 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 348 | ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc)); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 349 | } |
| 350 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 351 | if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET) |
| 352 | PushPopMIs.push_back(I); |
| 353 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 354 | // Scan the instructions for constant pool operands. |
| 355 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
| 356 | if (I->getOperand(op).isConstantPoolIndex()) { |
| 357 | // We found one. The addressing mode tells us the max displacement |
| 358 | // from the PC that this instruction permits. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 359 | |
| 360 | // Basic size info comes from the TSFlags field. |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 361 | unsigned Bits = 0; |
| 362 | unsigned Scale = 1; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 363 | unsigned TSFlags = I->getInstrDescriptor()->TSFlags; |
| 364 | switch (TSFlags & ARMII::AddrModeMask) { |
| 365 | default: |
| 366 | // Constant pool entries can reach anything. |
| 367 | if (I->getOpcode() == ARM::CONSTPOOL_ENTRY) |
| 368 | continue; |
| 369 | assert(0 && "Unknown addressing mode for CP reference!"); |
| 370 | case ARMII::AddrMode1: // AM1: 8 bits << 2 |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 371 | Bits = 8; |
| 372 | Scale = 4; // Taking the address of a CP entry. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 373 | break; |
| 374 | case ARMII::AddrMode2: |
Evan Cheng | 556f33c | 2007-02-01 20:44:52 +0000 | [diff] [blame] | 375 | Bits = 12; // +-offset_12 |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 376 | break; |
| 377 | case ARMII::AddrMode3: |
Evan Cheng | 556f33c | 2007-02-01 20:44:52 +0000 | [diff] [blame] | 378 | Bits = 8; // +-offset_8 |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 379 | break; |
| 380 | // addrmode4 has no immediate offset. |
| 381 | case ARMII::AddrMode5: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 382 | Bits = 8; |
| 383 | Scale = 4; // +-(offset_8*4) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 384 | break; |
| 385 | case ARMII::AddrModeT1: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 386 | Bits = 5; // +offset_5 |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 387 | break; |
| 388 | case ARMII::AddrModeT2: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 389 | Bits = 5; |
| 390 | Scale = 2; // +(offset_5*2) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 391 | break; |
| 392 | case ARMII::AddrModeT4: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 393 | Bits = 5; |
| 394 | Scale = 4; // +(offset_5*4) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 395 | break; |
Evan Cheng | 012f2d9 | 2007-01-24 08:53:17 +0000 | [diff] [blame] | 396 | case ARMII::AddrModeTs: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 397 | Bits = 8; |
| 398 | Scale = 4; // +(offset_8*4) |
Evan Cheng | 012f2d9 | 2007-01-24 08:53:17 +0000 | [diff] [blame] | 399 | break; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 400 | } |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 401 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 402 | // Remember that this is a user of a CP entry. |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 403 | unsigned CPI = I->getOperand(op).getConstantPoolIndex(); |
| 404 | MachineInstr *CPEMI = CPEMIs[CPI]; |
Evan Cheng | 556f33c | 2007-02-01 20:44:52 +0000 | [diff] [blame] | 405 | unsigned MaxOffs = ((1 << Bits)-1) * Scale; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 406 | CPUsers.push_back(CPUser(I, CPEMI, MaxOffs)); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 407 | |
| 408 | // Increment corresponding CPEntry reference count. |
| 409 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 410 | assert(CPE && "Cannot find a corresponding CPEntry!"); |
| 411 | CPE->RefCount++; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 412 | |
| 413 | // Instructions can only use one CP entry, don't bother scanning the |
| 414 | // rest of the operands. |
| 415 | break; |
| 416 | } |
| 417 | } |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 418 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 419 | // In thumb mode, if this block is a constpool island, pessimistically |
| 420 | // assume it needs to be padded by two byte so it's aligned on 4 byte |
| 421 | // boundary. |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 422 | if (isThumb && |
Evan Cheng | 05cc424 | 2007-02-02 19:09:19 +0000 | [diff] [blame] | 423 | !MBB.empty() && |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 424 | MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) |
| 425 | MBBSize += 2; |
| 426 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 427 | BBSizes.push_back(MBBSize); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 428 | BBOffsets.push_back(Offset); |
| 429 | Offset += MBBSize; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 433 | /// GetOffsetOf - Return the current offset of the specified machine instruction |
| 434 | /// from the start of the function. This offset changes as stuff is moved |
| 435 | /// around inside the function. |
| 436 | unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const { |
| 437 | MachineBasicBlock *MBB = MI->getParent(); |
| 438 | |
| 439 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 440 | // before this instruction's block, and the offset from the start of the block |
| 441 | // it is in. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 442 | unsigned Offset = BBOffsets[MBB->getNumber()]; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 443 | |
| 444 | // Sum instructions before MI in MBB. |
| 445 | for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) { |
| 446 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
| 447 | if (&*I == MI) return Offset; |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 448 | Offset += ARM::GetInstSize(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 449 | } |
| 450 | } |
| 451 | |
| 452 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 453 | /// ID. |
| 454 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 455 | const MachineBasicBlock *RHS) { |
| 456 | return LHS->getNumber() < RHS->getNumber(); |
| 457 | } |
| 458 | |
| 459 | /// UpdateForInsertedWaterBlock - When a block is newly inserted into the |
| 460 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 461 | /// and update the arrays that parallel this numbering. |
| 462 | void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) { |
| 463 | // Renumber the MBB's to keep them consequtive. |
| 464 | NewBB->getParent()->RenumberBlocks(NewBB); |
| 465 | |
| 466 | // Insert a size into BBSizes to align it properly with the (newly |
| 467 | // renumbered) block numbers. |
| 468 | BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 469 | |
| 470 | // Likewise for BBOffsets. |
| 471 | BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 472 | |
| 473 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
| 474 | // available water after it. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 475 | std::vector<MachineBasicBlock*>::iterator IP = |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 476 | std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, |
| 477 | CompareMBBNumbers); |
| 478 | WaterList.insert(IP, NewBB); |
| 479 | } |
| 480 | |
| 481 | |
| 482 | /// Split the basic block containing MI into two blocks, which are joined by |
| 483 | /// an unconditional branch. Update datastructures and renumber blocks to |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 484 | /// account for this change and returns the newly created block. |
| 485 | MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 486 | MachineBasicBlock *OrigBB = MI->getParent(); |
| 487 | |
| 488 | // Create a new MBB for the code after the OrigBB. |
| 489 | MachineBasicBlock *NewBB = new MachineBasicBlock(OrigBB->getBasicBlock()); |
| 490 | MachineFunction::iterator MBBI = OrigBB; ++MBBI; |
| 491 | OrigBB->getParent()->getBasicBlockList().insert(MBBI, NewBB); |
| 492 | |
| 493 | // Splice the instructions starting with MI over to NewBB. |
| 494 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
| 495 | |
| 496 | // Add an unconditional branch from OrigBB to NewBB. |
Evan Cheng | a9b8b8d | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 497 | // Note the new unconditional branch is not being recorded. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 498 | BuildMI(OrigBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 499 | NumSplit++; |
| 500 | |
| 501 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
| 502 | while (!OrigBB->succ_empty()) { |
| 503 | MachineBasicBlock *Succ = *OrigBB->succ_begin(); |
| 504 | OrigBB->removeSuccessor(Succ); |
| 505 | NewBB->addSuccessor(Succ); |
| 506 | |
| 507 | // This pass should be run after register allocation, so there should be no |
| 508 | // PHI nodes to update. |
| 509 | assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI) |
| 510 | && "PHI nodes should be eliminated by now!"); |
| 511 | } |
| 512 | |
| 513 | // OrigBB branches to NewBB. |
| 514 | OrigBB->addSuccessor(NewBB); |
| 515 | |
| 516 | // Update internal data structures to account for the newly inserted MBB. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 517 | // This is almost the same as UpdateForInsertedWaterBlock, except that |
| 518 | // the Water goes after OrigBB, not NewBB. |
| 519 | NewBB->getParent()->RenumberBlocks(NewBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 520 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 521 | // Insert a size into BBSizes to align it properly with the (newly |
| 522 | // renumbered) block numbers. |
| 523 | BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0); |
| 524 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 525 | // Likewise for BBOffsets. |
| 526 | BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0); |
| 527 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 528 | // Next, update WaterList. Specifically, we need to add OrigMBB as having |
| 529 | // available water after it (but not if it's already there, which happens |
| 530 | // when splitting before a conditional branch that is followed by an |
| 531 | // unconditional branch - in that case we want to insert NewBB). |
| 532 | std::vector<MachineBasicBlock*>::iterator IP = |
| 533 | std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB, |
| 534 | CompareMBBNumbers); |
| 535 | MachineBasicBlock* WaterBB = *IP; |
| 536 | if (WaterBB == OrigBB) |
| 537 | WaterList.insert(next(IP), NewBB); |
| 538 | else |
| 539 | WaterList.insert(IP, OrigBB); |
| 540 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 541 | // Figure out how large the first NewMBB is. |
| 542 | unsigned NewBBSize = 0; |
| 543 | for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end(); |
| 544 | I != E; ++I) |
Evan Cheng | 29836c3 | 2007-01-29 23:45:17 +0000 | [diff] [blame] | 545 | NewBBSize += ARM::GetInstSize(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 546 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 547 | unsigned OrigBBI = OrigBB->getNumber(); |
| 548 | unsigned NewBBI = NewBB->getNumber(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 549 | // Set the size of NewBB in BBSizes. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 550 | BBSizes[NewBBI] = NewBBSize; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 551 | |
| 552 | // We removed instructions from UserMBB, subtract that off from its size. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 553 | // Add 2 or 4 to the block to count the unconditional branch we added to it. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 554 | unsigned delta = isThumb ? 2 : 4; |
| 555 | BBSizes[OrigBBI] -= NewBBSize - delta; |
| 556 | |
| 557 | // ...and adjust BBOffsets for NewBB accordingly. |
| 558 | BBOffsets[NewBBI] = BBOffsets[OrigBBI] + BBSizes[OrigBBI]; |
| 559 | |
| 560 | // All BBOffsets following these blocks must be modified. |
| 561 | AdjustBBOffsetsAfter(NewBB, delta); |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 562 | |
| 563 | return NewBB; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 564 | } |
| 565 | |
Dale Johannesen | ce74de4 | 2007-02-25 18:31:31 +0000 | [diff] [blame] | 566 | /// OffsetIsInRange - Checks whether UserOffset is within MaxDisp of |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 567 | /// TrialOffset. |
| 568 | bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset, |
| 569 | unsigned TrialOffset, unsigned MaxDisp, bool NegativeOK) { |
| 570 | if (UserOffset <= TrialOffset) { |
| 571 | // User before the Trial. |
| 572 | if (TrialOffset-UserOffset <= MaxDisp) |
| 573 | return true; |
| 574 | } else if (NegativeOK) { |
| 575 | if (UserOffset-TrialOffset <= MaxDisp) |
| 576 | return true; |
| 577 | } |
| 578 | return false; |
| 579 | } |
| 580 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 581 | /// WaterIsInRange - Returns true if a CPE placed after the specified |
| 582 | /// Water (a basic block) will be in range for the specific MI. |
| 583 | |
| 584 | bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset, |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 585 | MachineBasicBlock* Water, unsigned MaxDisp) |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 586 | { |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 587 | unsigned CPEOffset = BBOffsets[Water->getNumber()] + |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 588 | BBSizes[Water->getNumber()]; |
| 589 | // If the Water is a constpool island, it has already been aligned. |
| 590 | // If not, align it. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 591 | if (isThumb && |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 592 | (Water->empty() || |
| 593 | Water->begin()->getOpcode() != ARM::CONSTPOOL_ENTRY)) |
| 594 | CPEOffset += 2; |
| 595 | |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame^] | 596 | // If the CPE is to be inserted before the instruction, that will raise |
| 597 | // the offset of the instruction. |
| 598 | if (CPEOffset < UserOffset) |
| 599 | UserOffset += isThumb ? 2 : 4; |
| 600 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 601 | return OffsetIsInRange (UserOffset, CPEOffset, MaxDisp, !isThumb); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /// CPEIsInRange - Returns true if the distance between specific MI and |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 605 | /// specific ConstPool entry instruction can fit in MI's displacement field. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 606 | bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset, |
| 607 | MachineInstr *CPEMI, |
| 608 | unsigned MaxDisp, bool DoDump) { |
| 609 | // In thumb mode, pessimistically assumes the .align 2 before the first CPE |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 610 | // in the island adds two byte padding. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 611 | unsigned AlignAdj = isThumb ? 2 : 0; |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 612 | unsigned CPEOffset = GetOffsetOf(CPEMI) + AlignAdj; |
| 613 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 614 | if (DoDump) { |
| 615 | DOUT << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 616 | << " max delta=" << MaxDisp |
| 617 | << " insn address=" << UserOffset |
| 618 | << " CPE address=" << CPEOffset |
| 619 | << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI; |
| 620 | } |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 621 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 622 | return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, !isThumb); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 625 | /// BBIsJumpedOver - Return true of the specified basic block's only predecessor |
| 626 | /// unconditionally branches to its only successor. |
| 627 | static bool BBIsJumpedOver(MachineBasicBlock *MBB) { |
| 628 | if (MBB->pred_size() != 1 || MBB->succ_size() != 1) |
| 629 | return false; |
| 630 | |
| 631 | MachineBasicBlock *Succ = *MBB->succ_begin(); |
| 632 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 633 | MachineInstr *PredMI = &Pred->back(); |
| 634 | if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB) |
| 635 | return PredMI->getOperand(0).getMBB() == Succ; |
| 636 | return false; |
| 637 | } |
| 638 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 639 | void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta) |
| 640 | { |
| 641 | MachineFunction::iterator MBBI = BB->getParent()->end(); |
Dale Johannesen | ce74de4 | 2007-02-25 18:31:31 +0000 | [diff] [blame] | 642 | for(unsigned i=BB->getNumber()+1; i<BB->getParent()->getNumBlockIDs(); i++) |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 643 | BBOffsets[i] += delta; |
| 644 | } |
| 645 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 646 | /// DecrementOldEntry - find the constant pool entry with index CPI |
| 647 | /// and instruction CPEMI, and decrement its refcount. If the refcount |
| 648 | /// becomes 0 remove the entry and instruction. Returns true if we removed |
| 649 | /// the entry, false if we didn't. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 650 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 651 | bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI, |
| 652 | unsigned Size) { |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 653 | // Find the old entry. Eliminate it if it is no longer used. |
| 654 | CPEntry *OldCPE = findConstPoolEntry(CPI, CPEMI); |
| 655 | assert(OldCPE && "Unexpected!"); |
| 656 | if (--OldCPE->RefCount == 0) { |
| 657 | MachineBasicBlock *OldCPEBB = OldCPE->CPEMI->getParent(); |
| 658 | if (OldCPEBB->empty()) { |
| 659 | // In thumb mode, the size of island is padded by two to compensate for |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 660 | // the alignment requirement. Thus it will now be 2 when the block is |
| 661 | // empty, so fix this. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 662 | // All succeeding offsets have the current size value added in, fix this. |
| 663 | if (BBSizes[OldCPEBB->getNumber()] != 0) { |
| 664 | AdjustBBOffsetsAfter(OldCPEBB, -BBSizes[OldCPEBB->getNumber()]); |
| 665 | BBSizes[OldCPEBB->getNumber()] = 0; |
| 666 | } |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 667 | // An island has only one predecessor BB and one successor BB. Check if |
| 668 | // this BB's predecessor jumps directly to this BB's successor. This |
| 669 | // shouldn't happen currently. |
| 670 | assert(!BBIsJumpedOver(OldCPEBB) && "How did this happen?"); |
| 671 | // FIXME: remove the empty blocks after all the work is done? |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 672 | } else { |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 673 | BBSizes[OldCPEBB->getNumber()] -= Size; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 674 | // All succeeding offsets have the current size value added in, fix this. |
| 675 | AdjustBBOffsetsAfter(OldCPEBB, -Size); |
| 676 | } |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 677 | OldCPE->CPEMI->eraseFromParent(); |
| 678 | OldCPE->CPEMI = NULL; |
| 679 | NumCPEs--; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 680 | return true; |
| 681 | } |
| 682 | return false; |
| 683 | } |
| 684 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 685 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 686 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 687 | /// change the data structures so the user references the clone. Returns: |
| 688 | /// 0 = no existing entry found |
| 689 | /// 1 = entry found, and there were no code insertions or deletions |
| 690 | /// 2 = entry found, and there were code insertions or deletions |
| 691 | int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset) |
| 692 | { |
| 693 | MachineInstr *UserMI = U.MI; |
| 694 | MachineInstr *CPEMI = U.CPEMI; |
| 695 | |
| 696 | // Check to see if the CPE is already in-range. |
| 697 | if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, true)) { |
| 698 | DOUT << "In range\n"; |
| 699 | return 1; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 700 | } |
| 701 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 702 | // No. Look for previously created clones of the CPE that are in range. |
| 703 | unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex(); |
| 704 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 705 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 706 | // We already tried this one |
| 707 | if (CPEs[i].CPEMI == CPEMI) |
| 708 | continue; |
| 709 | // Removing CPEs can leave empty entries, skip |
| 710 | if (CPEs[i].CPEMI == NULL) |
| 711 | continue; |
| 712 | if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, false)) { |
| 713 | DOUT << "Replacing CPE#" << CPI << " with CPE#" << CPEs[i].CPI << "\n"; |
| 714 | // Point the CPUser node to the replacement |
| 715 | U.CPEMI = CPEs[i].CPEMI; |
| 716 | // Change the CPI in the instruction operand to refer to the clone. |
| 717 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
| 718 | if (UserMI->getOperand(j).isConstantPoolIndex()) { |
| 719 | UserMI->getOperand(j).setConstantPoolIndex(CPEs[i].CPI); |
| 720 | break; |
| 721 | } |
| 722 | // Adjust the refcount of the clone... |
| 723 | CPEs[i].RefCount++; |
| 724 | // ...and the original. If we didn't remove the old entry, none of the |
| 725 | // addresses changed, so we don't need another pass. |
| 726 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 727 | return DecrementOldEntry(CPI, CPEMI, Size) ? 2 : 1; |
| 728 | } |
| 729 | } |
| 730 | return 0; |
| 731 | } |
| 732 | |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 733 | /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in |
| 734 | /// the specific unconditional branch instruction. |
| 735 | static inline unsigned getUnconditionalBrDisp(int Opc) { |
| 736 | return (Opc == ARM::tB) ? ((1<<10)-1)*2 : ((1<<23)-1)*4; |
| 737 | } |
| 738 | |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 739 | /// LookForWater - look for an existing entry in the WaterList in which |
| 740 | /// we can place the CPE referenced from U so it's within range of U's MI. |
| 741 | /// Returns true if found, false if not. If it returns true, *NewMBB |
| 742 | /// is set to the WaterList entry, and *PadNewWater is set to false if |
| 743 | /// the WaterList entry is an island. |
| 744 | |
| 745 | bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset, |
| 746 | bool *PadNewWater, MachineBasicBlock** NewMBB) { |
| 747 | if (!WaterList.empty()) { |
| 748 | for (std::vector<MachineBasicBlock*>::iterator IP = prior(WaterList.end()), |
| 749 | B = WaterList.begin();; --IP) { |
| 750 | MachineBasicBlock* WaterBB = *IP; |
| 751 | if (WaterIsInRange(UserOffset, WaterBB, U.MaxDisp)) { |
| 752 | DOUT << "found water in range\n"; |
| 753 | // CPE goes before following block (NewMBB). |
| 754 | *NewMBB = next(MachineFunction::iterator(WaterBB)); |
| 755 | // If WaterBB is an island, don't pad the new island. |
| 756 | // If WaterBB is empty, go backwards until we find something that |
| 757 | // isn't. WaterBB may become empty if it's an island whose |
| 758 | // contents were moved farther back. |
| 759 | if (isThumb) { |
| 760 | MachineBasicBlock* BB = WaterBB; |
| 761 | while (BB->empty()) |
| 762 | BB = prior(MachineFunction::iterator(BB)); |
| 763 | if (BB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) |
| 764 | *PadNewWater = false; |
| 765 | } |
| 766 | // Remove the original WaterList entry; we want subsequent |
| 767 | // insertions in this vicinity to go after the one we're |
| 768 | // about to insert. This considerably reduces the number |
| 769 | // of times we have to move the same CPE more than once. |
| 770 | WaterList.erase(IP); |
| 771 | return true; |
| 772 | } |
| 773 | if (IP == B) |
| 774 | break; |
| 775 | } |
| 776 | } |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | /// CreateNewWater - No existing WaterList entry will work for |
| 781 | /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the |
| 782 | /// block is used if in range, and the conditional branch munged so control |
| 783 | /// flow is correct. Otherwise the block is split to create a hole with an |
| 784 | /// unconditional branch around it. In either case *NewMBB is set to a |
| 785 | /// block following which the new island can be inserted (the WaterList |
| 786 | /// is not adjusted). |
| 787 | |
| 788 | void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex, |
| 789 | unsigned UserOffset, MachineBasicBlock** NewMBB) { |
| 790 | CPUser &U = CPUsers[CPUserIndex]; |
| 791 | MachineInstr *UserMI = U.MI; |
| 792 | MachineInstr *CPEMI = U.CPEMI; |
| 793 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
| 794 | unsigned OffsetOfNextBlock = BBOffsets[UserMBB->getNumber()] + |
| 795 | BBSizes[UserMBB->getNumber()]; |
| 796 | assert(OffsetOfNextBlock = BBOffsets[UserMBB->getNumber()+1]); |
| 797 | |
| 798 | // If the use is at the end of the block, or the end of the block |
| 799 | // is within range, make new water there. (The +2 or 4 below is |
| 800 | // for the unconditional branch we will be adding. If the block ends in |
| 801 | // an unconditional branch already, it is water, and is known to |
| 802 | // be out of range, so we'll always be adding one.) |
| 803 | if (&UserMBB->back() == UserMI || |
| 804 | OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb ? 2 : 4), |
| 805 | U.MaxDisp, !isThumb)) { |
| 806 | DOUT << "Split at end of block\n"; |
| 807 | if (&UserMBB->back() == UserMI) |
| 808 | assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!"); |
| 809 | *NewMBB = next(MachineFunction::iterator(UserMBB)); |
| 810 | // Add an unconditional branch from UserMBB to fallthrough block. |
| 811 | // Record it for branch lengthening; this new branch will not get out of |
| 812 | // range, but if the preceding conditional branch is out of range, the |
| 813 | // targets will be exchanged, and the altered branch may be out of |
| 814 | // range, so the machinery has to know about it. |
| 815 | int UncondBr = isThumb ? ARM::tB : ARM::B; |
| 816 | BuildMI(UserMBB, TII->get(UncondBr)).addMBB(*NewMBB); |
| 817 | unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); |
| 818 | ImmBranches.push_back(ImmBranch(&UserMBB->back(), |
| 819 | MaxDisp, false, UncondBr)); |
| 820 | int delta = isThumb ? 2 : 4; |
| 821 | BBSizes[UserMBB->getNumber()] += delta; |
| 822 | AdjustBBOffsetsAfter(UserMBB, delta); |
| 823 | } else { |
| 824 | // What a big block. Find a place within the block to split it. |
| 825 | // This is a little tricky on Thumb since instructions are 2 bytes |
| 826 | // and constant pool entries are 4 bytes: if instruction I references |
| 827 | // island CPE, and instruction I+1 references CPE', it will |
| 828 | // not work well to put CPE as far forward as possible, since then |
| 829 | // CPE' cannot immediately follow it (that location is 2 bytes |
| 830 | // farther away from I+1 than CPE was from I) and we'd need to create |
| 831 | // a new island. |
| 832 | // The 4 in the following is for the unconditional branch we'll be |
| 833 | // inserting (allows for long branch on Thumb). The 2 or 0 is for |
| 834 | // alignment of the island. |
| 835 | unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4 + (isThumb ? 2 : 0); |
| 836 | // This could point off the end of the block if we've already got |
| 837 | // constant pool entries following this block; only the last one is |
| 838 | // in the water list. Back past any possible branches (allow for a |
| 839 | // conditional and a maximally long unconditional). |
| 840 | if (BaseInsertOffset >= BBOffsets[UserMBB->getNumber()+1]) |
| 841 | BaseInsertOffset = BBOffsets[UserMBB->getNumber()+1] - |
| 842 | (isThumb ? 6 : 8); |
| 843 | unsigned EndInsertOffset = BaseInsertOffset + |
| 844 | CPEMI->getOperand(2).getImm(); |
| 845 | MachineBasicBlock::iterator MI = UserMI; |
| 846 | ++MI; |
| 847 | unsigned CPUIndex = CPUserIndex+1; |
| 848 | for (unsigned Offset = UserOffset+ARM::GetInstSize(UserMI); |
| 849 | Offset < BaseInsertOffset; |
| 850 | Offset += ARM::GetInstSize(MI), |
| 851 | MI = next(MI)) { |
| 852 | if (CPUIndex < CPUsers.size() && CPUsers[CPUIndex].MI == MI) { |
| 853 | if (!OffsetIsInRange(Offset, EndInsertOffset, |
| 854 | CPUsers[CPUIndex].MaxDisp, !isThumb)) { |
| 855 | BaseInsertOffset -= (isThumb ? 2 : 4); |
| 856 | EndInsertOffset -= (isThumb ? 2 : 4); |
| 857 | } |
| 858 | // This is overly conservative, as we don't account for CPEMIs |
| 859 | // being reused within the block, but it doesn't matter much. |
| 860 | EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm(); |
| 861 | CPUIndex++; |
| 862 | } |
| 863 | } |
| 864 | DOUT << "Split in middle of big block\n"; |
| 865 | *NewMBB = SplitBlockBeforeInstr(prior(MI)); |
| 866 | } |
| 867 | } |
| 868 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 869 | /// HandleConstantPoolUser - Analyze the specified user, checking to see if it |
| 870 | /// is out-of-range. If so, pick it up the constant pool value and move it some |
| 871 | /// place in-range. Return true if we changed any addresses (thus must run |
| 872 | /// another pass of branch lengthening), false otherwise. |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 873 | bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn, |
| 874 | unsigned CPUserIndex){ |
| 875 | CPUser &U = CPUsers[CPUserIndex]; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 876 | MachineInstr *UserMI = U.MI; |
| 877 | MachineInstr *CPEMI = U.CPEMI; |
| 878 | unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex(); |
| 879 | unsigned Size = CPEMI->getOperand(2).getImm(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 880 | MachineBasicBlock *NewMBB; |
| 881 | // Compute this only once, it's expensive |
| 882 | unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8); |
| 883 | |
| 884 | // See if the current entry is within range, or there is a clone of it |
| 885 | // in range. |
| 886 | int result = LookForExistingCPEntry(U, UserOffset); |
| 887 | if (result==1) return false; |
| 888 | else if (result==2) return true; |
| 889 | |
| 890 | // No existing clone of this CPE is within range. |
| 891 | // We will be generating a new clone. Get a UID for it. |
| 892 | unsigned ID = NextUID++; |
| 893 | |
| 894 | // Look for water where we can place this CPE. We look for the farthest one |
| 895 | // away that will work. Forward references only for now (although later |
| 896 | // we might find some that are backwards). |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 897 | bool PadNewWater = true; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 898 | |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 899 | if (!LookForWater(U, UserOffset, &PadNewWater, &NewMBB)) { |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 900 | // No water found. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 901 | DOUT << "No water found\n"; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 902 | CreateNewWater(CPUserIndex, UserOffset, &NewMBB); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 903 | } |
| 904 | |
| 905 | // Okay, we know we can put an island before NewMBB now, do it! |
| 906 | MachineBasicBlock *NewIsland = new MachineBasicBlock(); |
| 907 | Fn.getBasicBlockList().insert(NewMBB, NewIsland); |
| 908 | |
| 909 | // Update internal data structures to account for the newly inserted MBB. |
| 910 | UpdateForInsertedWaterBlock(NewIsland); |
| 911 | |
| 912 | // Decrement the old entry, and remove it if refcount becomes 0. |
| 913 | DecrementOldEntry(CPI, CPEMI, Size); |
| 914 | |
| 915 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 916 | // add it to the island. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 917 | U.CPEMI = BuildMI(NewIsland, TII->get(ARM::CONSTPOOL_ENTRY)) |
| 918 | .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 919 | CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 920 | NumCPEs++; |
| 921 | |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 922 | // Compensate for .align 2 in thumb mode. |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 923 | if (isThumb && PadNewWater) Size += 2; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 924 | // Increase the size of the island block to account for the new entry. |
| 925 | BBSizes[NewIsland->getNumber()] += Size; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 926 | BBOffsets[NewIsland->getNumber()] = BBOffsets[NewMBB->getNumber()]; |
| 927 | AdjustBBOffsetsAfter(NewIsland, Size); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 928 | |
| 929 | // Finally, change the CPI in the instruction operand to be ID. |
| 930 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
| 931 | if (UserMI->getOperand(i).isConstantPoolIndex()) { |
| 932 | UserMI->getOperand(i).setConstantPoolIndex(ID); |
| 933 | break; |
| 934 | } |
| 935 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 936 | DOUT << " Moved CPE to #" << ID << " CPI=" << CPI << "\t" << *UserMI; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 937 | |
| 938 | return true; |
| 939 | } |
| 940 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 941 | /// BBIsInRange - Returns true if the distance between specific MI and |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 942 | /// specific BB can fit in MI's displacement field. |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 943 | bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB, |
| 944 | unsigned MaxDisp) { |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 945 | unsigned PCAdj = isThumb ? 4 : 8; |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 946 | unsigned BrOffset = GetOffsetOf(MI) + PCAdj; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 947 | unsigned DestOffset = BBOffsets[DestBB->getNumber()]; |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 948 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 949 | DOUT << "Branch of destination BB#" << DestBB->getNumber() |
| 950 | << " from BB#" << MI->getParent()->getNumber() |
| 951 | << " max delta=" << MaxDisp |
| 952 | << " at offset " << int(DestOffset-BrOffset) << "\t" << *MI; |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 953 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 954 | return OffsetIsInRange(BrOffset, DestOffset, MaxDisp, true); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 955 | } |
| 956 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 957 | /// FixUpImmediateBr - Fix up an immediate branch whose destination is too far |
| 958 | /// away to fit in its displacement field. |
| 959 | bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 960 | MachineInstr *MI = Br.MI; |
| 961 | MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock(); |
| 962 | |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 963 | // Check to see if the DestBB is already in-range. |
| 964 | if (BBIsInRange(MI, DestBB, Br.MaxDisp)) |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 965 | return false; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 966 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 967 | if (!Br.isCond) |
| 968 | return FixUpUnconditionalBr(Fn, Br); |
| 969 | return FixUpConditionalBr(Fn, Br); |
| 970 | } |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 971 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 972 | /// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is |
| 973 | /// too far away to fit in its displacement field. If the LR register has been |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 974 | /// spilled in the epilogue, then we can use BL to implement a far jump. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 975 | /// Otherwise, add an intermediate branch instruction to to a branch. |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 976 | bool |
| 977 | ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br) { |
| 978 | MachineInstr *MI = Br.MI; |
| 979 | MachineBasicBlock *MBB = MI->getParent(); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 980 | assert(isThumb && "Expected a Thumb function!"); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 981 | |
| 982 | // Use BL to implement far jump. |
| 983 | Br.MaxDisp = (1 << 21) * 2; |
| 984 | MI->setInstrDescriptor(TII->get(ARM::tBfar)); |
| 985 | BBSizes[MBB->getNumber()] += 2; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 986 | AdjustBBOffsetsAfter(MBB, 2); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 987 | HasFarJump = true; |
| 988 | NumUBrFixed++; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 989 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 990 | DOUT << " Changed B to long jump " << *MI; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 991 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 992 | return true; |
| 993 | } |
| 994 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 995 | /// FixUpConditionalBr - Fix up a conditional branch whose destination is too |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 996 | /// far away to fit in its displacement field. It is converted to an inverse |
| 997 | /// conditional branch + an unconditional branch to the destination. |
| 998 | bool |
| 999 | ARMConstantIslands::FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br) { |
| 1000 | MachineInstr *MI = Br.MI; |
| 1001 | MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock(); |
| 1002 | |
| 1003 | // Add a unconditional branch to the destination and invert the branch |
| 1004 | // condition to jump over it: |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1005 | // blt L1 |
| 1006 | // => |
| 1007 | // bge L2 |
| 1008 | // b L1 |
| 1009 | // L2: |
| 1010 | ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImmedValue(); |
| 1011 | CC = ARMCC::getOppositeCondition(CC); |
| 1012 | |
| 1013 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 1014 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 1015 | // split the MBB before the next instruction. |
| 1016 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1017 | MachineInstr *BMI = &MBB->back(); |
| 1018 | bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1019 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1020 | NumCBrFixed++; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1021 | if (BMI != MI) { |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1022 | if (next(MachineBasicBlock::iterator(MI)) == MBB->back() && |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1023 | BMI->getOpcode() == Br.UncondBr) { |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1024 | // Last MI in the BB is a unconditional branch. Can we simply invert the |
| 1025 | // condition and swap destinations: |
| 1026 | // beq L1 |
| 1027 | // b L2 |
| 1028 | // => |
| 1029 | // bne L2 |
| 1030 | // b L1 |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1031 | MachineBasicBlock *NewDest = BMI->getOperand(0).getMachineBasicBlock(); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1032 | if (BBIsInRange(MI, NewDest, Br.MaxDisp)) { |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1033 | DOUT << " Invert Bcc condition and swap its destination with " << *BMI; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1034 | BMI->getOperand(0).setMachineBasicBlock(DestBB); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1035 | MI->getOperand(0).setMachineBasicBlock(NewDest); |
| 1036 | MI->getOperand(1).setImm(CC); |
| 1037 | return true; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | |
| 1042 | if (NeedSplit) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1043 | SplitBlockBeforeInstr(MI); |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1044 | // No need for the branch to the next block. We're adding a unconditional |
| 1045 | // branch to the destination. |
| 1046 | MBB->back().eraseFromParent(); |
| 1047 | } |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1048 | MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB)); |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1049 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1050 | DOUT << " Insert B to BB#" << DestBB->getNumber() |
| 1051 | << " also invert condition and change dest. to BB#" |
| 1052 | << NextBB->getNumber() << "\n"; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1053 | |
| 1054 | // Insert a unconditional branch and replace the conditional branch. |
| 1055 | // 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] | 1056 | BuildMI(MBB, TII->get(MI->getOpcode())).addMBB(NextBB).addImm(CC); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1057 | Br.MI = &MBB->back(); |
| 1058 | BuildMI(MBB, TII->get(Br.UncondBr)).addMBB(DestBB); |
Evan Cheng | a9b8b8d | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 1059 | unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); |
Evan Cheng | a0bf794 | 2007-01-25 23:31:04 +0000 | [diff] [blame] | 1060 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1061 | MI->eraseFromParent(); |
| 1062 | |
| 1063 | // Increase the size of MBB to account for the new unconditional branch. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1064 | int delta = ARM::GetInstSize(&MBB->back()); |
| 1065 | BBSizes[MBB->getNumber()] += delta; |
| 1066 | AdjustBBOffsetsAfter(MBB, delta); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1067 | return true; |
| 1068 | } |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1069 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1070 | /// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills |
| 1071 | /// LR / restores LR to pc. |
| 1072 | bool ARMConstantIslands::UndoLRSpillRestore() { |
| 1073 | bool MadeChange = false; |
| 1074 | for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { |
| 1075 | MachineInstr *MI = PushPopMIs[i]; |
| 1076 | if (MI->getNumOperands() == 1) { |
| 1077 | if (MI->getOpcode() == ARM::tPOP_RET && |
| 1078 | MI->getOperand(0).getReg() == ARM::PC) |
| 1079 | BuildMI(MI->getParent(), TII->get(ARM::tBX_RET)); |
| 1080 | MI->eraseFromParent(); |
| 1081 | MadeChange = true; |
| 1082 | } |
| 1083 | } |
| 1084 | return MadeChange; |
| 1085 | } |