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