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