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