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