Bill Wendling | 9a4d2e4 | 2010-12-21 01:54:40 +0000 | [diff] [blame] | 1 | //===-- ARMConstantIslandPass.cpp - ARM constant islands ------------------===// |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file contains a pass that splits the constant pool up into 'islands' |
| 11 | // which are scattered through-out the function. This is required due to the |
| 12 | // limited pc-relative displacements that ARM has. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #define DEBUG_TYPE "arm-cp-islands" |
| 17 | #include "ARM.h" |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 18 | #include "ARMMachineFunctionInfo.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 19 | #include "ARMInstrInfo.h" |
Evan Cheng | 719510a | 2010-08-12 20:30:05 +0000 | [diff] [blame] | 20 | #include "Thumb2InstrInfo.h" |
Evan Cheng | ee04a6d | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 21 | #include "MCTargetDesc/ARMAddressingModes.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 23 | #include "llvm/CodeGen/MachineFunctionPass.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" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallSet.h" |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallVector.h" |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/STLExtras.h" |
| 33 | #include "llvm/ADT/Statistic.h" |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 34 | #include "llvm/Support/CommandLine.h" |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 35 | #include <algorithm> |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 38 | STATISTIC(NumCPEs, "Number of constpool entries"); |
| 39 | STATISTIC(NumSplit, "Number of uncond branches inserted"); |
| 40 | STATISTIC(NumCBrFixed, "Number of cond branches fixed"); |
| 41 | STATISTIC(NumUBrFixed, "Number of uncond branches fixed"); |
| 42 | STATISTIC(NumTBs, "Number of table branches generated"); |
| 43 | STATISTIC(NumT2CPShrunk, "Number of Thumb2 constantpool instructions shrunk"); |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 44 | STATISTIC(NumT2BrShrunk, "Number of Thumb2 immediate branches shrunk"); |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 45 | STATISTIC(NumCBZ, "Number of CBZ / CBNZ formed"); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 46 | STATISTIC(NumJTMoved, "Number of jump table destination blocks moved"); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 47 | STATISTIC(NumJTInserted, "Number of jump table intermediate blocks inserted"); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | static cl::opt<bool> |
Jim Grosbach | f04777b | 2009-11-17 21:24:11 +0000 | [diff] [blame] | 51 | AdjustJumpTableBlocks("arm-adjust-jump-tables", cl::Hidden, cl::init(true), |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 52 | cl::desc("Adjust basic block layout to better use TB[BH]")); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 53 | |
| 54 | namespace { |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 55 | /// ARMConstantIslands - Due to limited PC-relative displacements, ARM |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 56 | /// requires constant pool entries to be scattered among the instructions |
| 57 | /// inside a function. To do this, it completely ignores the normal LLVM |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 58 | /// constant pool; instead, it places constants wherever it feels like with |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 59 | /// special instructions. |
| 60 | /// |
| 61 | /// The terminology used in this pass includes: |
| 62 | /// Islands - Clumps of constants placed in the function. |
| 63 | /// Water - Potential places where an island could be formed. |
| 64 | /// CPE - A constant pool entry that has been placed somewhere, which |
| 65 | /// tracks a list of users. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 66 | class ARMConstantIslands : public MachineFunctionPass { |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 67 | /// BasicBlockInfo - Information about the offset and size of a single |
| 68 | /// basic block. |
| 69 | struct BasicBlockInfo { |
| 70 | /// Offset - Distance from the beginning of the function to the beginning |
| 71 | /// of this basic block. |
| 72 | /// |
| 73 | /// The two-byte pads required for Thumb alignment are counted as part of |
| 74 | /// the following block. |
| 75 | unsigned Offset; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 76 | |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 77 | /// Size - Size of the basic block in bytes. If the block contains |
| 78 | /// inline assembly, this is a worst case estimate. |
| 79 | /// |
| 80 | /// The two-byte pads required for Thumb alignment are counted as part of |
| 81 | /// the following block (i.e., the offset and size for a padded block |
| 82 | /// will both be ==2 mod 4). |
| 83 | unsigned Size; |
| 84 | |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 85 | /// Unalign - When non-zero, the block contains instructions (inline asm) |
| 86 | /// of unknown size. The real size may be smaller than Size bytes by a |
| 87 | /// multiple of 1 << Unalign. |
| 88 | uint8_t Unalign; |
| 89 | |
| 90 | /// PostAlign - When non-zero, the block terminator contains a .align |
| 91 | /// directive, so the end of the block is aligned to 1 << PostAlign |
| 92 | /// bytes. |
| 93 | uint8_t PostAlign; |
| 94 | |
| 95 | BasicBlockInfo() : Offset(0), Size(0), Unalign(0), PostAlign(0) {} |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 96 | |
| 97 | /// Compute the offset immediately following this block. |
| 98 | unsigned postOffset() const { return Offset + Size; } |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
| 101 | std::vector<BasicBlockInfo> BBInfo; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 102 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 103 | /// WaterList - A sorted list of basic blocks where islands could be placed |
| 104 | /// (i.e. blocks that don't fall through to the following block, due |
| 105 | /// to a return, unreachable, or unconditional branch). |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 106 | std::vector<MachineBasicBlock*> WaterList; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 107 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 108 | /// NewWaterList - The subset of WaterList that was created since the |
| 109 | /// previous iteration by inserting unconditional branches. |
| 110 | SmallSet<MachineBasicBlock*, 4> NewWaterList; |
| 111 | |
Bob Wilson | 034de5f | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 112 | typedef std::vector<MachineBasicBlock*>::iterator water_iterator; |
| 113 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 114 | /// CPUser - One user of a constant pool, keeping the machine instruction |
| 115 | /// pointer, the constant pool being referenced, and the max displacement |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 116 | /// allowed from the instruction to the CP. The HighWaterMark records the |
| 117 | /// highest basic block where a new CPEntry can be placed. To ensure this |
| 118 | /// pass terminates, the CP entries are initially placed at the end of the |
| 119 | /// function and then move monotonically to lower addresses. The |
| 120 | /// exception to this rule is when the current CP entry for a particular |
| 121 | /// CPUser is out of range, but there is another CP entry for the same |
| 122 | /// constant value in range. We want to use the existing in-range CP |
| 123 | /// entry, but if it later moves out of range, the search for new water |
| 124 | /// should resume where it left off. The HighWaterMark is used to record |
| 125 | /// that point. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 126 | struct CPUser { |
| 127 | MachineInstr *MI; |
| 128 | MachineInstr *CPEMI; |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 129 | MachineBasicBlock *HighWaterMark; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 130 | unsigned MaxDisp; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 131 | bool NegOk; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 132 | bool IsSoImm; |
| 133 | CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp, |
| 134 | bool neg, bool soimm) |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 135 | : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) { |
| 136 | HighWaterMark = CPEMI->getParent(); |
| 137 | } |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 138 | }; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 139 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 140 | /// CPUsers - Keep track of all of the machine instructions that use various |
| 141 | /// constant pools and their max displacement. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 142 | std::vector<CPUser> CPUsers; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 143 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 144 | /// CPEntry - One per constant pool entry, keeping the machine instruction |
| 145 | /// pointer, the constpool index, and the number of CPUser's which |
| 146 | /// reference this entry. |
| 147 | struct CPEntry { |
| 148 | MachineInstr *CPEMI; |
| 149 | unsigned CPI; |
| 150 | unsigned RefCount; |
| 151 | CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0) |
| 152 | : CPEMI(cpemi), CPI(cpi), RefCount(rc) {} |
| 153 | }; |
| 154 | |
| 155 | /// CPEntries - Keep track of all of the constant pool entry machine |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 156 | /// instructions. For each original constpool index (i.e. those that |
| 157 | /// existed upon entry to this pass), it keeps a vector of entries. |
| 158 | /// Original elements are cloned as we go along; the clones are |
| 159 | /// put in the vector of the original element, but have distinct CPIs. |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 160 | std::vector<std::vector<CPEntry> > CPEntries; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 161 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 162 | /// ImmBranch - One per immediate branch, keeping the machine instruction |
| 163 | /// pointer, conditional or unconditional, the max displacement, |
| 164 | /// and (if isCond is true) the corresponding unconditional branch |
| 165 | /// opcode. |
| 166 | struct ImmBranch { |
| 167 | MachineInstr *MI; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 168 | unsigned MaxDisp : 31; |
| 169 | bool isCond : 1; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 170 | int UncondBr; |
Evan Cheng | c285414 | 2007-01-25 23:18:59 +0000 | [diff] [blame] | 171 | ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr) |
| 172 | : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {} |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 173 | }; |
| 174 | |
Evan Cheng | 2706f97 | 2007-05-16 05:14:06 +0000 | [diff] [blame] | 175 | /// ImmBranches - Keep track of all the immediate branch instructions. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 176 | /// |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 177 | std::vector<ImmBranch> ImmBranches; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 178 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 179 | /// PushPopMIs - Keep track of all the Thumb push / pop instructions. |
| 180 | /// |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 181 | SmallVector<MachineInstr*, 4> PushPopMIs; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 182 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 183 | /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions. |
| 184 | SmallVector<MachineInstr*, 4> T2JumpTables; |
| 185 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 186 | /// HasFarJump - True if any far jump instruction has been emitted during |
| 187 | /// the branch fix up pass. |
| 188 | bool HasFarJump; |
| 189 | |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 190 | /// HasInlineAsm - True if the function contains inline assembly. |
| 191 | bool HasInlineAsm; |
| 192 | |
Chris Lattner | 2062875 | 2010-07-22 21:27:00 +0000 | [diff] [blame] | 193 | const ARMInstrInfo *TII; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 194 | const ARMSubtarget *STI; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 195 | ARMFunctionInfo *AFI; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 196 | bool isThumb; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 197 | bool isThumb1; |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 198 | bool isThumb2; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 199 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 200 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 201 | ARMConstantIslands() : MachineFunctionPass(ID) {} |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 202 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 203 | virtual bool runOnMachineFunction(MachineFunction &MF); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 204 | |
| 205 | virtual const char *getPassName() const { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 206 | return "ARM constant island placement and branch shortening pass"; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 207 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 208 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 209 | private: |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 210 | void DoInitialPlacement(MachineFunction &MF, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 211 | std::vector<MachineInstr*> &CPEMIs); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 212 | CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 213 | void JumpTableFunctionScan(MachineFunction &MF); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 214 | void InitialFunctionScan(MachineFunction &MF, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 215 | const std::vector<MachineInstr*> &CPEMIs); |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 216 | MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 217 | void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 218 | void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta); |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 219 | bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 220 | int LookForExistingCPEntry(CPUser& U, unsigned UserOffset); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 221 | bool LookForWater(CPUser&U, unsigned UserOffset, water_iterator &WaterIter); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 222 | void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset, |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 223 | MachineBasicBlock *&NewMBB); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 224 | bool HandleConstantPoolUser(MachineFunction &MF, unsigned CPUserIndex); |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 225 | void RemoveDeadCPEMI(MachineInstr *CPEMI); |
| 226 | bool RemoveUnusedCPEntries(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 227 | bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset, |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 228 | MachineInstr *CPEMI, unsigned Disp, bool NegOk, |
| 229 | bool DoDump = false); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 230 | bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water, |
Dale Johannesen | 5d9c4b6 | 2007-07-11 18:32:38 +0000 | [diff] [blame] | 231 | CPUser &U); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 232 | bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset, |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 233 | unsigned Disp, bool NegativeOK, bool IsSoImm = false); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 234 | bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 235 | bool FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br); |
| 236 | bool FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br); |
| 237 | bool FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 238 | bool UndoLRSpillRestore(); |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 239 | bool OptimizeThumb2Instructions(MachineFunction &MF); |
| 240 | bool OptimizeThumb2Branches(MachineFunction &MF); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 241 | bool ReorderThumb2JumpTables(MachineFunction &MF); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 242 | bool OptimizeThumb2JumpTables(MachineFunction &MF); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 243 | MachineBasicBlock *AdjustJTTargetBlockForward(MachineBasicBlock *BB, |
| 244 | MachineBasicBlock *JTBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 245 | |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 246 | void ComputeBlockSize(const MachineBasicBlock *MBB); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 247 | unsigned GetOffsetOf(MachineInstr *MI) const; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 248 | void dumpBBs(); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 249 | void verify(MachineFunction &MF); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 250 | }; |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 251 | char ARMConstantIslands::ID = 0; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 252 | } |
| 253 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 254 | /// verify - check BBOffsets, BBSizes, alignment of islands |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 255 | void ARMConstantIslands::verify(MachineFunction &MF) { |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 256 | for (unsigned i = 1, e = BBInfo.size(); i != e; ++i) |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 257 | assert(BBInfo[i-1].postOffset() == BBInfo[i].Offset); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 258 | if (!isThumb) |
| 259 | return; |
| 260 | #ifndef NDEBUG |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 261 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 262 | MBBI != E; ++MBBI) { |
| 263 | MachineBasicBlock *MBB = MBBI; |
| 264 | if (!MBB->empty() && |
| 265 | MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) { |
| 266 | unsigned MBBId = MBB->getNumber(); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 267 | assert(HasInlineAsm || |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 268 | (BBInfo[MBBId].Offset%4 == 0 && BBInfo[MBBId].Size%4 == 0) || |
| 269 | (BBInfo[MBBId].Offset%4 != 0 && BBInfo[MBBId].Size%4 != 0)); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 270 | } |
| 271 | } |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 272 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { |
| 273 | CPUser &U = CPUsers[i]; |
| 274 | unsigned UserOffset = GetOffsetOf(U.MI) + (isThumb ? 4 : 8); |
Jim Grosbach | a956256 | 2009-11-20 19:37:38 +0000 | [diff] [blame] | 275 | unsigned CPEOffset = GetOffsetOf(U.CPEMI); |
| 276 | unsigned Disp = UserOffset < CPEOffset ? CPEOffset - UserOffset : |
| 277 | UserOffset - CPEOffset; |
| 278 | assert(Disp <= U.MaxDisp || "Constant pool entry out of range!"); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 279 | } |
Jim Grosbach | a956256 | 2009-11-20 19:37:38 +0000 | [diff] [blame] | 280 | #endif |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | /// print block size and offset information - debugging |
| 284 | void ARMConstantIslands::dumpBBs() { |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 285 | for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) { |
| 286 | DEBUG(errs() << "block " << J << " offset " << BBInfo[J].Offset |
| 287 | << " size " << BBInfo[J].Size << "\n"); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 288 | } |
| 289 | } |
| 290 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 291 | /// createARMConstantIslandPass - returns an instance of the constpool |
| 292 | /// island pass. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 293 | FunctionPass *llvm::createARMConstantIslandPass() { |
| 294 | return new ARMConstantIslands(); |
| 295 | } |
| 296 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 297 | bool ARMConstantIslands::runOnMachineFunction(MachineFunction &MF) { |
| 298 | MachineConstantPool &MCP = *MF.getConstantPool(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 299 | |
Chris Lattner | 2062875 | 2010-07-22 21:27:00 +0000 | [diff] [blame] | 300 | TII = (const ARMInstrInfo*)MF.getTarget().getInstrInfo(); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 301 | AFI = MF.getInfo<ARMFunctionInfo>(); |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 302 | STI = &MF.getTarget().getSubtarget<ARMSubtarget>(); |
| 303 | |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 304 | isThumb = AFI->isThumbFunction(); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 305 | isThumb1 = AFI->isThumb1OnlyFunction(); |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 306 | isThumb2 = AFI->isThumb2Function(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 307 | |
| 308 | HasFarJump = false; |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 309 | HasInlineAsm = false; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 310 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 311 | // Renumber all of the machine basic blocks in the function, guaranteeing that |
| 312 | // the numbers agree with the position of the block in the function. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 313 | MF.RenumberBlocks(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 314 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 315 | // Try to reorder and otherwise adjust the block layout to make good use |
| 316 | // of the TB[BH] instructions. |
| 317 | bool MadeChange = false; |
| 318 | if (isThumb2 && AdjustJumpTableBlocks) { |
| 319 | JumpTableFunctionScan(MF); |
| 320 | MadeChange |= ReorderThumb2JumpTables(MF); |
| 321 | // Data is out of date, so clear it. It'll be re-computed later. |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 322 | T2JumpTables.clear(); |
| 323 | // Blocks may have shifted around. Keep the numbering up to date. |
| 324 | MF.RenumberBlocks(); |
| 325 | } |
| 326 | |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 327 | // Thumb1 functions containing constant pools get 4-byte alignment. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 328 | // This is so we can keep exact track of where the alignment padding goes. |
| 329 | |
Chris Lattner | 7d7dab0 | 2010-01-27 23:37:36 +0000 | [diff] [blame] | 330 | // ARM and Thumb2 functions need to be 4-byte aligned. |
| 331 | if (!isThumb1) |
| 332 | MF.EnsureAlignment(2); // 2 = log2(4) |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 333 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 334 | // Perform the initial placement of the constant pool entries. To start with, |
| 335 | // we put them all at the end of the function. |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 336 | std::vector<MachineInstr*> CPEMIs; |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 337 | if (!MCP.isEmpty()) { |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 338 | DoInitialPlacement(MF, CPEMIs); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 339 | if (isThumb1) |
Chris Lattner | 7d7dab0 | 2010-01-27 23:37:36 +0000 | [diff] [blame] | 340 | MF.EnsureAlignment(2); // 2 = log2(4) |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 341 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 342 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 343 | /// The next UID to take is the first unused one. |
Evan Cheng | 5de5d4b | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 344 | AFI->initPICLabelUId(CPEMIs.size()); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 345 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 346 | // Do the initial scan of the function, building up information about the |
| 347 | // sizes of each block, the location of all the water, and finding all of the |
| 348 | // constant pool users. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 349 | InitialFunctionScan(MF, CPEMIs); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 350 | CPEMIs.clear(); |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 351 | DEBUG(dumpBBs()); |
| 352 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 353 | |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 354 | /// Remove dead constant pool entries. |
Bill Wendling | cd08024 | 2010-12-18 01:53:06 +0000 | [diff] [blame] | 355 | MadeChange |= RemoveUnusedCPEntries(); |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 356 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 357 | // Iteratively place constant pool entries and fix up branches until there |
| 358 | // is no change. |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 359 | unsigned NoCPIters = 0, NoBRIters = 0; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 360 | while (true) { |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 361 | bool CPChange = false; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 362 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 363 | CPChange |= HandleConstantPoolUser(MF, i); |
| 364 | if (CPChange && ++NoCPIters > 30) |
| 365 | llvm_unreachable("Constant Island pass failed to converge!"); |
Evan Cheng | 8202010 | 2007-07-10 22:00:16 +0000 | [diff] [blame] | 366 | DEBUG(dumpBBs()); |
Jim Grosbach | 26b8ef5 | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 367 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 368 | // Clear NewWaterList now. If we split a block for branches, it should |
| 369 | // appear as "new water" for the next iteration of constant pool placement. |
| 370 | NewWaterList.clear(); |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 371 | |
| 372 | bool BRChange = false; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 373 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 374 | BRChange |= FixUpImmediateBr(MF, ImmBranches[i]); |
| 375 | if (BRChange && ++NoBRIters > 30) |
| 376 | llvm_unreachable("Branch Fix Up pass failed to converge!"); |
Evan Cheng | 8202010 | 2007-07-10 22:00:16 +0000 | [diff] [blame] | 377 | DEBUG(dumpBBs()); |
Evan Cheng | b6879b2 | 2009-08-07 07:35:21 +0000 | [diff] [blame] | 378 | |
| 379 | if (!CPChange && !BRChange) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 380 | break; |
| 381 | MadeChange = true; |
| 382 | } |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 383 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 384 | // Shrink 32-bit Thumb2 branch, load, and store instructions. |
Evan Cheng | e44be63 | 2010-08-09 18:35:19 +0000 | [diff] [blame] | 385 | if (isThumb2 && !STI->prefers32BitThumb()) |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 386 | MadeChange |= OptimizeThumb2Instructions(MF); |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 387 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 388 | // 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] | 389 | verify(MF); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 390 | |
Jim Grosbach | 26b8ef5 | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 391 | // If LR has been forced spilled and no far jump (i.e. BL) has been issued, |
| 392 | // undo the spill / restore of LR if possible. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 393 | if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump()) |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 394 | MadeChange |= UndoLRSpillRestore(); |
| 395 | |
Anton Korobeynikov | 98b928e | 2011-01-30 22:07:39 +0000 | [diff] [blame] | 396 | // Save the mapping between original and cloned constpool entries. |
| 397 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 398 | for (unsigned j = 0, je = CPEntries[i].size(); j != je; ++j) { |
| 399 | const CPEntry & CPE = CPEntries[i][j]; |
| 400 | AFI->recordCPEClone(i, CPE.CPI); |
| 401 | } |
| 402 | } |
| 403 | |
Evan Cheng | b1c857b | 2010-07-22 02:09:47 +0000 | [diff] [blame] | 404 | DEBUG(errs() << '\n'; dumpBBs()); |
| 405 | |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 406 | BBInfo.clear(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 407 | WaterList.clear(); |
| 408 | CPUsers.clear(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 409 | CPEntries.clear(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 410 | ImmBranches.clear(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 411 | PushPopMIs.clear(); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 412 | T2JumpTables.clear(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 413 | |
| 414 | return MadeChange; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /// DoInitialPlacement - Perform the initial placement of the constant pool |
| 418 | /// 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] | 419 | void ARMConstantIslands::DoInitialPlacement(MachineFunction &MF, |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 420 | std::vector<MachineInstr*> &CPEMIs) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 421 | // Create the basic block to hold the CPE's. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 422 | MachineBasicBlock *BB = MF.CreateMachineBasicBlock(); |
| 423 | MF.push_back(BB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 424 | |
Jakob Stoklund Olesen | 3e572ac | 2011-12-06 01:43:02 +0000 | [diff] [blame] | 425 | // Mark the basic block as 4-byte aligned as required by the const-pool. |
| 426 | BB->setAlignment(2); |
| 427 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 428 | // Add all of the constants from the constant pool to the end block, use an |
| 429 | // identity mapping of CPI's to CPE's. |
| 430 | const std::vector<MachineConstantPoolEntry> &CPs = |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 431 | MF.getConstantPool()->getConstants(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 432 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 433 | const TargetData &TD = *MF.getTarget().getTargetData(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 434 | for (unsigned i = 0, e = CPs.size(); i != e; ++i) { |
Duncan Sands | 777d230 | 2009-05-09 07:06:46 +0000 | [diff] [blame] | 435 | unsigned Size = TD.getTypeAllocSize(CPs[i].getType()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 436 | // Verify that all constant pool entries are a multiple of 4 bytes. If not, |
| 437 | // we would have to pad them out or something so that instructions stay |
| 438 | // aligned. |
| 439 | assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!"); |
| 440 | MachineInstr *CPEMI = |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 441 | BuildMI(BB, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY)) |
| 442 | .addImm(i).addConstantPoolIndex(i).addImm(Size); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 443 | CPEMIs.push_back(CPEMI); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 444 | |
| 445 | // Add a new CPEntry, but no corresponding CPUser yet. |
| 446 | std::vector<CPEntry> CPEs; |
| 447 | CPEs.push_back(CPEntry(CPEMI, i)); |
| 448 | CPEntries.push_back(CPEs); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 449 | ++NumCPEs; |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 450 | DEBUG(errs() << "Moved CPI#" << i << " to end of function as #" << i |
| 451 | << "\n"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 452 | } |
| 453 | } |
| 454 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 455 | /// BBHasFallthrough - Return true if the specified basic block can fallthrough |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 456 | /// into the block immediately after it. |
| 457 | static bool BBHasFallthrough(MachineBasicBlock *MBB) { |
| 458 | // Get the next machine basic block in the function. |
| 459 | MachineFunction::iterator MBBI = MBB; |
Jim Grosbach | 18f30e6 | 2010-06-02 21:53:11 +0000 | [diff] [blame] | 460 | // Can't fall off end of function. |
| 461 | if (llvm::next(MBBI) == MBB->getParent()->end()) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 462 | return false; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 463 | |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 464 | MachineBasicBlock *NextBB = llvm::next(MBBI); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 465 | for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(), |
| 466 | E = MBB->succ_end(); I != E; ++I) |
| 467 | if (*I == NextBB) |
| 468 | return true; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 469 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 470 | return false; |
| 471 | } |
| 472 | |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 473 | /// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI, |
| 474 | /// look up the corresponding CPEntry. |
| 475 | ARMConstantIslands::CPEntry |
| 476 | *ARMConstantIslands::findConstPoolEntry(unsigned CPI, |
| 477 | const MachineInstr *CPEMI) { |
| 478 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 479 | // Number of entries per constpool index should be small, just do a |
| 480 | // linear search. |
| 481 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 482 | if (CPEs[i].CPEMI == CPEMI) |
| 483 | return &CPEs[i]; |
| 484 | } |
| 485 | return NULL; |
| 486 | } |
| 487 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 488 | /// JumpTableFunctionScan - Do a scan of the function, building up |
| 489 | /// information about the sizes of each block and the locations of all |
| 490 | /// the jump tables. |
| 491 | void ARMConstantIslands::JumpTableFunctionScan(MachineFunction &MF) { |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 492 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 493 | MBBI != E; ++MBBI) { |
| 494 | MachineBasicBlock &MBB = *MBBI; |
| 495 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 496 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 497 | I != E; ++I) |
| 498 | if (I->getDesc().isBranch() && I->getOpcode() == ARM::t2BR_JT) |
| 499 | T2JumpTables.push_back(I); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 503 | /// InitialFunctionScan - Do the initial scan of the function, building up |
| 504 | /// information about the sizes of each block, the location of all the water, |
| 505 | /// and finding all of the constant pool users. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 506 | void ARMConstantIslands::InitialFunctionScan(MachineFunction &MF, |
Evan Cheng | e03cff6 | 2007-02-09 23:59:14 +0000 | [diff] [blame] | 507 | const std::vector<MachineInstr*> &CPEMIs) { |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 508 | // First thing, see if the function has any inline assembly in it. If so, |
| 509 | // we have to be conservative about alignment assumptions, as we don't |
| 510 | // know for sure the size of any instructions in the inline assembly. |
| 511 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
| 512 | MBBI != E; ++MBBI) { |
| 513 | MachineBasicBlock &MBB = *MBBI; |
| 514 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 515 | I != E; ++I) |
| 516 | if (I->getOpcode() == ARM::INLINEASM) |
| 517 | HasInlineAsm = true; |
| 518 | } |
| 519 | |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 520 | BBInfo.clear(); |
| 521 | BBInfo.resize(MF.getNumBlockIDs()); |
| 522 | |
Bill Wendling | 9a4d2e4 | 2010-12-21 01:54:40 +0000 | [diff] [blame] | 523 | // Now go back through the instructions and build up our data structures. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 524 | unsigned Offset = 0; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 525 | for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 526 | MBBI != E; ++MBBI) { |
| 527 | MachineBasicBlock &MBB = *MBBI; |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 528 | BasicBlockInfo &BBI = BBInfo[MBB.getNumber()]; |
| 529 | BBI.Offset = Offset; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 530 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 531 | // If this block doesn't fall through into the next MBB, then this is |
| 532 | // 'water' that a constant pool island could be placed. |
| 533 | if (!BBHasFallthrough(&MBB)) |
| 534 | WaterList.push_back(&MBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 535 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 536 | unsigned MBBSize = 0; |
| 537 | for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); |
| 538 | I != E; ++I) { |
Jim Grosbach | 9cfcfeb | 2010-06-21 17:49:23 +0000 | [diff] [blame] | 539 | if (I->isDebugValue()) |
| 540 | continue; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 541 | // Add instruction size to MBBSize. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 542 | MBBSize += TII->GetInstSizeInBytes(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 543 | |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 544 | // For inline asm, GetInstSizeInBytes returns a conservative estimate. |
| 545 | // The actual size may be smaller, but still a multiple of the instr size. |
| 546 | if (I->isInlineAsm()) |
| 547 | BBI.Unalign = isThumb ? 1 : 2; |
| 548 | |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 549 | int Opc = I->getOpcode(); |
Chris Lattner | 749c6f6 | 2008-01-07 07:27:27 +0000 | [diff] [blame] | 550 | if (I->getDesc().isBranch()) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 551 | bool isCond = false; |
| 552 | unsigned Bits = 0; |
| 553 | unsigned Scale = 1; |
| 554 | int UOpc = Opc; |
| 555 | switch (Opc) { |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 556 | default: |
| 557 | continue; // Ignore other JT branches |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 558 | case ARM::tBR_JTr: |
Evan Cheng | 66ac531 | 2009-07-25 00:33:29 +0000 | [diff] [blame] | 559 | // A Thumb1 table jump may involve padding; for the offsets to |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 560 | // be right, functions containing these must be 4-byte aligned. |
Evan Cheng | b1c857b | 2010-07-22 02:09:47 +0000 | [diff] [blame] | 561 | // tBR_JTr expands to a mov pc followed by .align 2 and then the jump |
Jakob Stoklund Olesen | 6fbea43 | 2011-12-06 22:41:31 +0000 | [diff] [blame] | 562 | // table entries. So this code checks whether offset of tBR_JTr + 2 |
| 563 | // is aligned. That is held in Offset+MBBSize, which already has |
| 564 | // 2 added in for the size of the mov pc instruction. |
Chris Lattner | 7d7dab0 | 2010-01-27 23:37:36 +0000 | [diff] [blame] | 565 | MF.EnsureAlignment(2U); |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 566 | BBI.PostAlign = 2; |
Jakob Stoklund Olesen | 6fbea43 | 2011-12-06 22:41:31 +0000 | [diff] [blame] | 567 | if ((Offset+MBBSize)%4 != 0 || HasInlineAsm) |
| 568 | // FIXME: Add a pseudo ALIGN instruction instead. |
| 569 | MBBSize += 2; // padding |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 570 | continue; // Does not get an entry in ImmBranches |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 571 | case ARM::t2BR_JT: |
| 572 | T2JumpTables.push_back(I); |
| 573 | continue; // Does not get an entry in ImmBranches |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 574 | case ARM::Bcc: |
| 575 | isCond = true; |
| 576 | UOpc = ARM::B; |
| 577 | // Fallthrough |
| 578 | case ARM::B: |
| 579 | Bits = 24; |
| 580 | Scale = 4; |
| 581 | break; |
| 582 | case ARM::tBcc: |
| 583 | isCond = true; |
| 584 | UOpc = ARM::tB; |
| 585 | Bits = 8; |
| 586 | Scale = 2; |
| 587 | break; |
| 588 | case ARM::tB: |
| 589 | Bits = 11; |
| 590 | Scale = 2; |
| 591 | break; |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 592 | case ARM::t2Bcc: |
| 593 | isCond = true; |
| 594 | UOpc = ARM::t2B; |
| 595 | Bits = 20; |
| 596 | Scale = 2; |
| 597 | break; |
| 598 | case ARM::t2B: |
| 599 | Bits = 24; |
| 600 | Scale = 2; |
| 601 | break; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 602 | } |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 603 | |
| 604 | // Record this immediate branch. |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 605 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 606 | ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc)); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 607 | } |
| 608 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 609 | if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET) |
| 610 | PushPopMIs.push_back(I); |
| 611 | |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 612 | if (Opc == ARM::CONSTPOOL_ENTRY) |
| 613 | continue; |
| 614 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 615 | // Scan the instructions for constant pool operands. |
| 616 | for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 617 | if (I->getOperand(op).isCPI()) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 618 | // We found one. The addressing mode tells us the max displacement |
| 619 | // from the PC that this instruction permits. |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 620 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 621 | // Basic size info comes from the TSFlags field. |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 622 | unsigned Bits = 0; |
| 623 | unsigned Scale = 1; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 624 | bool NegOk = false; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 625 | bool IsSoImm = false; |
| 626 | |
| 627 | switch (Opc) { |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 628 | default: |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 629 | llvm_unreachable("Unknown addressing mode for CP reference!"); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 630 | break; |
| 631 | |
| 632 | // Taking the address of a CP entry. |
| 633 | case ARM::LEApcrel: |
| 634 | // This takes a SoImm, which is 8 bit immediate rotated. We'll |
| 635 | // pretend the maximum offset is 255 * 4. Since each instruction |
Jim Grosbach | dec6de9 | 2009-11-19 18:23:19 +0000 | [diff] [blame] | 636 | // 4 byte wide, this is always correct. We'll check for other |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 637 | // displacements that fits in a SoImm as well. |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 638 | Bits = 8; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 639 | Scale = 4; |
| 640 | NegOk = true; |
| 641 | IsSoImm = true; |
| 642 | break; |
Owen Anderson | 6b8719f | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 643 | case ARM::t2LEApcrel: |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 644 | Bits = 12; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 645 | NegOk = true; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 646 | break; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 647 | case ARM::tLEApcrel: |
| 648 | Bits = 8; |
| 649 | Scale = 4; |
| 650 | break; |
| 651 | |
Jim Grosbach | 3e55612 | 2010-10-26 22:37:02 +0000 | [diff] [blame] | 652 | case ARM::LDRi12: |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 653 | case ARM::LDRcp: |
Owen Anderson | 971b83b | 2011-02-08 22:39:40 +0000 | [diff] [blame] | 654 | case ARM::t2LDRpci: |
Evan Cheng | 556f33c | 2007-02-01 20:44:52 +0000 | [diff] [blame] | 655 | Bits = 12; // +-offset_12 |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 656 | NegOk = true; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 657 | break; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 658 | |
| 659 | case ARM::tLDRpci: |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 660 | Bits = 8; |
| 661 | Scale = 4; // +(offset_8*4) |
Evan Cheng | 012f2d9 | 2007-01-24 08:53:17 +0000 | [diff] [blame] | 662 | break; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 663 | |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 664 | case ARM::VLDRD: |
| 665 | case ARM::VLDRS: |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 666 | Bits = 8; |
| 667 | Scale = 4; // +-(offset_8*4) |
| 668 | NegOk = true; |
Evan Cheng | 055b031 | 2009-06-29 07:51:04 +0000 | [diff] [blame] | 669 | break; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 670 | } |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 671 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 672 | // Remember that this is a user of a CP entry. |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 673 | unsigned CPI = I->getOperand(op).getIndex(); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 674 | MachineInstr *CPEMI = CPEMIs[CPI]; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 675 | unsigned MaxOffs = ((1 << Bits)-1) * Scale; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 676 | CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm)); |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 677 | |
| 678 | // Increment corresponding CPEntry reference count. |
| 679 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 680 | assert(CPE && "Cannot find a corresponding CPEntry!"); |
| 681 | CPE->RefCount++; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 682 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 683 | // Instructions can only use one CP entry, don't bother scanning the |
| 684 | // rest of the operands. |
| 685 | break; |
| 686 | } |
| 687 | } |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 688 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 689 | // In thumb mode, if this block is a constpool island, we may need padding |
| 690 | // so it's aligned on 4 byte boundary. |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 691 | if (isThumb && |
Evan Cheng | 05cc424 | 2007-02-02 19:09:19 +0000 | [diff] [blame] | 692 | !MBB.empty() && |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 693 | MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY && |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 694 | ((Offset%4) != 0 || HasInlineAsm)) |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 695 | MBBSize += 2; |
| 696 | |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 697 | BBI.Size = MBBSize; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 698 | Offset += MBBSize; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 702 | /// ComputeBlockSize - Compute the size and some alignment information for MBB. |
| 703 | /// This function updates BBInfo directly. |
| 704 | void ARMConstantIslands::ComputeBlockSize(const MachineBasicBlock *MBB) { |
| 705 | BasicBlockInfo &BBI = BBInfo[MBB->getNumber()]; |
| 706 | BBI.Size = 0; |
| 707 | BBI.Unalign = 0; |
| 708 | BBI.PostAlign = 0; |
| 709 | |
| 710 | for (MachineBasicBlock::const_iterator I = MBB->begin(), E = MBB->end(); |
| 711 | I != E; ++I) { |
| 712 | BBI.Size += TII->GetInstSizeInBytes(I); |
| 713 | // For inline asm, GetInstSizeInBytes returns a conservative estimate. |
| 714 | // The actual size may be smaller, but still a multiple of the instr size. |
| 715 | if (I->isInlineAsm()) |
| 716 | BBI.Unalign = isThumb ? 1 : 2; |
| 717 | } |
| 718 | |
| 719 | // tBR_JTr contains a .align 2 directive. |
| 720 | if (!MBB->empty() && MBB->back().getOpcode() == ARM::tBR_JTr) |
| 721 | BBI.PostAlign = 2; |
| 722 | } |
| 723 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 724 | /// GetOffsetOf - Return the current offset of the specified machine instruction |
| 725 | /// from the start of the function. This offset changes as stuff is moved |
| 726 | /// around inside the function. |
| 727 | unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const { |
| 728 | MachineBasicBlock *MBB = MI->getParent(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 729 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 730 | // The offset is composed of two things: the sum of the sizes of all MBB's |
| 731 | // before this instruction's block, and the offset from the start of the block |
| 732 | // it is in. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 733 | unsigned Offset = BBInfo[MBB->getNumber()].Offset; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 734 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 735 | // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has |
| 736 | // alignment padding, and compensate if so. |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 737 | if (isThumb && |
| 738 | MI->getOpcode() == ARM::CONSTPOOL_ENTRY && |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 739 | (Offset%4 != 0 || HasInlineAsm)) |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 740 | Offset += 2; |
| 741 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 742 | // Sum instructions before MI in MBB. |
| 743 | for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) { |
| 744 | assert(I != MBB->end() && "Didn't find MI in its own basic block?"); |
| 745 | if (&*I == MI) return Offset; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 746 | Offset += TII->GetInstSizeInBytes(I); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
| 750 | /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB |
| 751 | /// ID. |
| 752 | static bool CompareMBBNumbers(const MachineBasicBlock *LHS, |
| 753 | const MachineBasicBlock *RHS) { |
| 754 | return LHS->getNumber() < RHS->getNumber(); |
| 755 | } |
| 756 | |
| 757 | /// UpdateForInsertedWaterBlock - When a block is newly inserted into the |
| 758 | /// machine function, it upsets all of the block numbers. Renumber the blocks |
| 759 | /// and update the arrays that parallel this numbering. |
| 760 | void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) { |
Duncan Sands | ab4c366 | 2011-02-15 09:23:02 +0000 | [diff] [blame] | 761 | // Renumber the MBB's to keep them consecutive. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 762 | NewBB->getParent()->RenumberBlocks(NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 763 | |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 764 | // Insert an entry into BBInfo to align it properly with the (newly |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 765 | // renumbered) block numbers. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 766 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 767 | |
| 768 | // Next, update WaterList. Specifically, we need to add NewMBB as having |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 769 | // available water after it. |
Bob Wilson | 034de5f | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 770 | water_iterator IP = |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 771 | std::lower_bound(WaterList.begin(), WaterList.end(), NewBB, |
| 772 | CompareMBBNumbers); |
| 773 | WaterList.insert(IP, NewBB); |
| 774 | } |
| 775 | |
| 776 | |
| 777 | /// Split the basic block containing MI into two blocks, which are joined by |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 778 | /// an unconditional branch. Update data structures and renumber blocks to |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 779 | /// account for this change and returns the newly created block. |
| 780 | MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 781 | MachineBasicBlock *OrigBB = MI->getParent(); |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 782 | MachineFunction &MF = *OrigBB->getParent(); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 783 | |
| 784 | // Create a new MBB for the code after the OrigBB. |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 785 | MachineBasicBlock *NewBB = |
| 786 | MF.CreateMachineBasicBlock(OrigBB->getBasicBlock()); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 787 | MachineFunction::iterator MBBI = OrigBB; ++MBBI; |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 788 | MF.insert(MBBI, NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 789 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 790 | // Splice the instructions starting with MI over to NewBB. |
| 791 | NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end()); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 792 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 793 | // Add an unconditional branch from OrigBB to NewBB. |
Evan Cheng | a9b8b8d | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 794 | // Note the new unconditional branch is not being recorded. |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 795 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 796 | // correspond to anything in the source. |
Evan Cheng | 58541fd | 2009-07-07 01:16:41 +0000 | [diff] [blame] | 797 | unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B; |
Owen Anderson | 51f6a7a | 2011-09-09 21:48:23 +0000 | [diff] [blame] | 798 | if (!isThumb) |
| 799 | BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB); |
| 800 | else |
| 801 | BuildMI(OrigBB, DebugLoc(), TII->get(Opc)).addMBB(NewBB) |
| 802 | .addImm(ARMCC::AL).addReg(0); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 803 | ++NumSplit; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 804 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 805 | // Update the CFG. All succs of OrigBB are now succs of NewBB. |
Jakob Stoklund Olesen | e80fba0 | 2011-12-06 00:51:12 +0000 | [diff] [blame] | 806 | NewBB->transferSuccessors(OrigBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 807 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 808 | // OrigBB branches to NewBB. |
| 809 | OrigBB->addSuccessor(NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 810 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 811 | // Update internal data structures to account for the newly inserted MBB. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 812 | // This is almost the same as UpdateForInsertedWaterBlock, except that |
| 813 | // the Water goes after OrigBB, not NewBB. |
Dan Gohman | 8e5f2c6 | 2008-07-07 23:14:23 +0000 | [diff] [blame] | 814 | MF.RenumberBlocks(NewBB); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 815 | |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 816 | // Insert an entry into BBInfo to align it properly with the (newly |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 817 | // renumbered) block numbers. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 818 | BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo()); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 819 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 820 | // Next, update WaterList. Specifically, we need to add OrigMBB as having |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 821 | // available water after it (but not if it's already there, which happens |
| 822 | // when splitting before a conditional branch that is followed by an |
| 823 | // unconditional branch - in that case we want to insert NewBB). |
Bob Wilson | 034de5f | 2009-10-12 18:52:13 +0000 | [diff] [blame] | 824 | water_iterator IP = |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 825 | std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB, |
| 826 | CompareMBBNumbers); |
| 827 | MachineBasicBlock* WaterBB = *IP; |
| 828 | if (WaterBB == OrigBB) |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 829 | WaterList.insert(llvm::next(IP), NewBB); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 830 | else |
| 831 | WaterList.insert(IP, OrigBB); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 832 | NewWaterList.insert(OrigBB); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 833 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 834 | unsigned OrigBBI = OrigBB->getNumber(); |
| 835 | unsigned NewBBI = NewBB->getNumber(); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 836 | |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 837 | int delta = isThumb1 ? 2 : 4; |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 838 | |
| 839 | // Figure out how large the OrigBB is. As the first half of the original |
| 840 | // block, it cannot contain a tablejump. The size includes |
| 841 | // the new jump we added. (It should be possible to do this without |
| 842 | // recounting everything, but it's very confusing, and this is rarely |
| 843 | // executed.) |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 844 | ComputeBlockSize(OrigBB); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 845 | |
| 846 | // ...and adjust BBOffsets for NewBB accordingly. |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 847 | BBInfo[NewBBI].Offset = BBInfo[OrigBBI].postOffset(); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 848 | |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 849 | // Figure out how large the NewMBB is. As the second half of the original |
| 850 | // block, it may contain a tablejump. |
Jakob Stoklund Olesen | a26811e | 2011-12-07 04:17:35 +0000 | [diff] [blame^] | 851 | ComputeBlockSize(NewBB); |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 852 | |
Jakob Stoklund Olesen | 6fbea43 | 2011-12-06 22:41:31 +0000 | [diff] [blame] | 853 | MachineInstr* ThumbJTMI = prior(NewBB->end()); |
| 854 | if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) { |
| 855 | // We've added another 2-byte instruction before this tablejump, which |
| 856 | // means we will always need padding if we didn't before, and vice versa. |
| 857 | |
| 858 | // The original offset of the jump instruction was: |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 859 | unsigned OrigOffset = BBInfo[OrigBBI].postOffset() - delta; |
Jakob Stoklund Olesen | 6fbea43 | 2011-12-06 22:41:31 +0000 | [diff] [blame] | 860 | if (OrigOffset%4 == 0) { |
| 861 | // We had padding before and now we don't. No net change in code size. |
| 862 | delta = 0; |
| 863 | } else { |
| 864 | // We didn't have padding before and now we do. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 865 | BBInfo[NewBBI].Size += 2; |
Jakob Stoklund Olesen | 6fbea43 | 2011-12-06 22:41:31 +0000 | [diff] [blame] | 866 | delta = 4; |
| 867 | } |
| 868 | } |
| 869 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 870 | // All BBOffsets following these blocks must be modified. |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 871 | if (delta) |
| 872 | AdjustBBOffsetsAfter(NewBB, delta); |
Evan Cheng | 0c61584 | 2007-01-31 02:22:22 +0000 | [diff] [blame] | 873 | |
| 874 | return NewBB; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 875 | } |
| 876 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 877 | /// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 878 | /// reference) is within MaxDisp of TrialOffset (a proposed location of a |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 879 | /// constant pool entry). |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 880 | bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset, |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 881 | unsigned TrialOffset, unsigned MaxDisp, |
| 882 | bool NegativeOK, bool IsSoImm) { |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 883 | // On Thumb offsets==2 mod 4 are rounded down by the hardware for |
| 884 | // purposes of the displacement computation; compensate for that here. |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 885 | // Effectively, the valid range of displacements is 2 bytes smaller for such |
| 886 | // references. |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 887 | unsigned TotalAdj = 0; |
| 888 | if (isThumb && UserOffset%4 !=0) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 889 | UserOffset -= 2; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 890 | TotalAdj = 2; |
| 891 | } |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 892 | // CPEs will be rounded up to a multiple of 4. |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 893 | if (isThumb && TrialOffset%4 != 0) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 894 | TrialOffset += 2; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 895 | TotalAdj += 2; |
| 896 | } |
| 897 | |
| 898 | // In Thumb2 mode, later branch adjustments can shift instructions up and |
| 899 | // cause alignment change. In the worst case scenario this can cause the |
| 900 | // user's effective address to be subtracted by 2 and the CPE's address to |
| 901 | // be plus 2. |
| 902 | if (isThumb2 && TotalAdj != 4) |
| 903 | MaxDisp -= (4 - TotalAdj); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 904 | |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 905 | if (UserOffset <= TrialOffset) { |
| 906 | // User before the Trial. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 907 | if (TrialOffset - UserOffset <= MaxDisp) |
| 908 | return true; |
Evan Cheng | 40efc25 | 2009-07-24 19:31:03 +0000 | [diff] [blame] | 909 | // FIXME: Make use full range of soimm values. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 910 | } else if (NegativeOK) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 911 | if (UserOffset - TrialOffset <= MaxDisp) |
| 912 | return true; |
Evan Cheng | 40efc25 | 2009-07-24 19:31:03 +0000 | [diff] [blame] | 913 | // FIXME: Make use full range of soimm values. |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 914 | } |
| 915 | return false; |
| 916 | } |
| 917 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 918 | /// WaterIsInRange - Returns true if a CPE placed after the specified |
| 919 | /// Water (a basic block) will be in range for the specific MI. |
| 920 | |
| 921 | bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset, |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 922 | MachineBasicBlock* Water, CPUser &U) { |
Dale Johannesen | 5d9c4b6 | 2007-07-11 18:32:38 +0000 | [diff] [blame] | 923 | unsigned MaxDisp = U.MaxDisp; |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 924 | unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 925 | |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 926 | // If the CPE is to be inserted before the instruction, that will raise |
Bob Wilson | af4b735 | 2009-10-12 22:49:05 +0000 | [diff] [blame] | 927 | // the offset of the instruction. |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 928 | if (CPEOffset < UserOffset) |
Dale Johannesen | 5d9c4b6 | 2007-07-11 18:32:38 +0000 | [diff] [blame] | 929 | UserOffset += U.CPEMI->getOperand(2).getImm(); |
Dale Johannesen | d959aa4 | 2007-04-02 20:31:06 +0000 | [diff] [blame] | 930 | |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 931 | return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | /// CPEIsInRange - Returns true if the distance between specific MI and |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 935 | /// specific ConstPool entry instruction can fit in MI's displacement field. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 936 | bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset, |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 937 | MachineInstr *CPEMI, unsigned MaxDisp, |
| 938 | bool NegOk, bool DoDump) { |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 939 | unsigned CPEOffset = GetOffsetOf(CPEMI); |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 940 | assert((CPEOffset%4 == 0 || HasInlineAsm) && "Misaligned CPE"); |
Evan Cheng | 2021abe | 2007-02-01 01:09:47 +0000 | [diff] [blame] | 941 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 942 | if (DoDump) { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 943 | DEBUG(errs() << "User of CPE#" << CPEMI->getOperand(0).getImm() |
| 944 | << " max delta=" << MaxDisp |
| 945 | << " insn address=" << UserOffset |
| 946 | << " CPE address=" << CPEOffset |
| 947 | << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 948 | } |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 949 | |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 950 | return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Evan Cheng | d1e7d9a | 2009-01-28 00:53:34 +0000 | [diff] [blame] | 953 | #ifndef NDEBUG |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 954 | /// BBIsJumpedOver - Return true of the specified basic block's only predecessor |
| 955 | /// unconditionally branches to its only successor. |
| 956 | static bool BBIsJumpedOver(MachineBasicBlock *MBB) { |
| 957 | if (MBB->pred_size() != 1 || MBB->succ_size() != 1) |
| 958 | return false; |
| 959 | |
| 960 | MachineBasicBlock *Succ = *MBB->succ_begin(); |
| 961 | MachineBasicBlock *Pred = *MBB->pred_begin(); |
| 962 | MachineInstr *PredMI = &Pred->back(); |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 963 | if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB |
| 964 | || PredMI->getOpcode() == ARM::t2B) |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 965 | return PredMI->getOperand(0).getMBB() == Succ; |
| 966 | return false; |
| 967 | } |
Evan Cheng | d1e7d9a | 2009-01-28 00:53:34 +0000 | [diff] [blame] | 968 | #endif // NDEBUG |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 969 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 970 | void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB, |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 971 | int delta) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 972 | MachineFunction::iterator MBBI = BB; MBBI = llvm::next(MBBI); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 973 | for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs(); |
| 974 | i < e; ++i) { |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 975 | BBInfo[i].Offset += delta; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 976 | // If some existing blocks have padding, adjust the padding as needed, a |
| 977 | // bit tricky. delta can be negative so don't use % on that. |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 978 | if (!isThumb) |
| 979 | continue; |
| 980 | MachineBasicBlock *MBB = MBBI; |
Jim Grosbach | 4d8e90a | 2009-11-19 23:10:28 +0000 | [diff] [blame] | 981 | if (!MBB->empty() && !HasInlineAsm) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 982 | // Constant pool entries require padding. |
| 983 | if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) { |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 984 | unsigned OldOffset = BBInfo[i].Offset - delta; |
| 985 | if ((OldOffset%4) == 0 && (BBInfo[i].Offset%4) != 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 986 | // add new padding |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 987 | BBInfo[i].Size += 2; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 988 | delta += 2; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 989 | } else if ((OldOffset%4) != 0 && (BBInfo[i].Offset%4) == 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 990 | // remove existing padding |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 991 | BBInfo[i].Size -= 2; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 992 | delta -= 2; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 993 | } |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 994 | } |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 995 | // Thumb1 jump tables require padding. They should be at the end; |
| 996 | // following unconditional branches are removed by AnalyzeBranch. |
Evan Cheng | b1c857b | 2010-07-22 02:09:47 +0000 | [diff] [blame] | 997 | // tBR_JTr expands to a mov pc followed by .align 2 and then the jump |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 998 | // table entries. So this code checks whether offset of tBR_JTr |
| 999 | // is aligned; if it is, the offset of the jump table following the |
| 1000 | // instruction will not be aligned, and we need padding. |
Evan Cheng | 7894762 | 2009-07-24 18:20:44 +0000 | [diff] [blame] | 1001 | MachineInstr *ThumbJTMI = prior(MBB->end()); |
Evan Cheng | 66ac531 | 2009-07-25 00:33:29 +0000 | [diff] [blame] | 1002 | if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) { |
Dale Johannesen | 8086d58 | 2010-07-23 22:50:23 +0000 | [diff] [blame] | 1003 | unsigned NewMIOffset = GetOffsetOf(ThumbJTMI); |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 1004 | unsigned OldMIOffset = NewMIOffset - delta; |
| 1005 | if ((OldMIOffset%4) == 0 && (NewMIOffset%4) != 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1006 | // remove existing padding |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1007 | BBInfo[i].Size -= 2; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1008 | delta -= 2; |
Evan Cheng | 4a8ea21 | 2009-08-11 07:36:14 +0000 | [diff] [blame] | 1009 | } else if ((OldMIOffset%4) != 0 && (NewMIOffset%4) == 0) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1010 | // add new padding |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1011 | BBInfo[i].Size += 2; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1012 | delta += 2; |
| 1013 | } |
| 1014 | } |
| 1015 | if (delta==0) |
| 1016 | return; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1017 | } |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1018 | MBBI = llvm::next(MBBI); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1019 | } |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1022 | /// DecrementOldEntry - find the constant pool entry with index CPI |
| 1023 | /// and instruction CPEMI, and decrement its refcount. If the refcount |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1024 | /// becomes 0 remove the entry and instruction. Returns true if we removed |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1025 | /// the entry, false if we didn't. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1026 | |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1027 | bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) { |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1028 | // Find the old entry. Eliminate it if it is no longer used. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1029 | CPEntry *CPE = findConstPoolEntry(CPI, CPEMI); |
| 1030 | assert(CPE && "Unexpected!"); |
| 1031 | if (--CPE->RefCount == 0) { |
| 1032 | RemoveDeadCPEMI(CPEMI); |
| 1033 | CPE->CPEMI = NULL; |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1034 | --NumCPEs; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1035 | return true; |
| 1036 | } |
| 1037 | return false; |
| 1038 | } |
| 1039 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1040 | /// LookForCPEntryInRange - see if the currently referenced CPE is in range; |
| 1041 | /// if not, see if an in-range clone of the CPE is in range, and if so, |
| 1042 | /// change the data structures so the user references the clone. Returns: |
| 1043 | /// 0 = no existing entry found |
| 1044 | /// 1 = entry found, and there were no code insertions or deletions |
| 1045 | /// 2 = entry found, and there were code insertions or deletions |
| 1046 | int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset) |
| 1047 | { |
| 1048 | MachineInstr *UserMI = U.MI; |
| 1049 | MachineInstr *CPEMI = U.CPEMI; |
| 1050 | |
| 1051 | // Check to see if the CPE is already in-range. |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 1052 | if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1053 | DEBUG(errs() << "In range\n"); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1054 | return 1; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1057 | // 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] | 1058 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1059 | std::vector<CPEntry> &CPEs = CPEntries[CPI]; |
| 1060 | for (unsigned i = 0, e = CPEs.size(); i != e; ++i) { |
| 1061 | // We already tried this one |
| 1062 | if (CPEs[i].CPEMI == CPEMI) |
| 1063 | continue; |
| 1064 | // Removing CPEs can leave empty entries, skip |
| 1065 | if (CPEs[i].CPEMI == NULL) |
| 1066 | continue; |
Evan Cheng | 5d8f1ca | 2009-07-21 23:56:01 +0000 | [diff] [blame] | 1067 | if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1068 | DEBUG(errs() << "Replacing CPE#" << CPI << " with CPE#" |
| 1069 | << CPEs[i].CPI << "\n"); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1070 | // Point the CPUser node to the replacement |
| 1071 | U.CPEMI = CPEs[i].CPEMI; |
| 1072 | // Change the CPI in the instruction operand to refer to the clone. |
| 1073 | for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1074 | if (UserMI->getOperand(j).isCPI()) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1075 | UserMI->getOperand(j).setIndex(CPEs[i].CPI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1076 | break; |
| 1077 | } |
| 1078 | // Adjust the refcount of the clone... |
| 1079 | CPEs[i].RefCount++; |
| 1080 | // ...and the original. If we didn't remove the old entry, none of the |
| 1081 | // addresses changed, so we don't need another pass. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1082 | return DecrementOldEntry(CPI, CPEMI) ? 2 : 1; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1083 | } |
| 1084 | } |
| 1085 | return 0; |
| 1086 | } |
| 1087 | |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1088 | /// getUnconditionalBrDisp - Returns the maximum displacement that can fit in |
| 1089 | /// the specific unconditional branch instruction. |
| 1090 | static inline unsigned getUnconditionalBrDisp(int Opc) { |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1091 | switch (Opc) { |
| 1092 | case ARM::tB: |
| 1093 | return ((1<<10)-1)*2; |
| 1094 | case ARM::t2B: |
| 1095 | return ((1<<23)-1)*2; |
| 1096 | default: |
| 1097 | break; |
| 1098 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 1099 | |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1100 | return ((1<<23)-1)*4; |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1101 | } |
| 1102 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1103 | /// LookForWater - Look for an existing entry in the WaterList in which |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1104 | /// we can place the CPE referenced from U so it's within range of U's MI. |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1105 | /// Returns true if found, false if not. If it returns true, WaterIter |
Bob Wilson | f98032e | 2009-10-12 21:23:15 +0000 | [diff] [blame] | 1106 | /// is set to the WaterList entry. For Thumb, prefer water that will not |
| 1107 | /// introduce padding to water that will. To ensure that this pass |
| 1108 | /// terminates, the CPE location for a particular CPUser is only allowed to |
| 1109 | /// move to a lower address, so search backward from the end of the list and |
| 1110 | /// prefer the first water that is in range. |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1111 | bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset, |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1112 | water_iterator &WaterIter) { |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1113 | if (WaterList.empty()) |
| 1114 | return false; |
| 1115 | |
Bob Wilson | 32c50e8 | 2009-10-12 20:45:53 +0000 | [diff] [blame] | 1116 | bool FoundWaterThatWouldPad = false; |
| 1117 | water_iterator IPThatWouldPad; |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1118 | for (water_iterator IP = prior(WaterList.end()), |
| 1119 | B = WaterList.begin();; --IP) { |
| 1120 | MachineBasicBlock* WaterBB = *IP; |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1121 | // Check if water is in range and is either at a lower address than the |
| 1122 | // current "high water mark" or a new water block that was created since |
| 1123 | // the previous iteration by inserting an unconditional branch. In the |
| 1124 | // latter case, we want to allow resetting the high water mark back to |
| 1125 | // this new water since we haven't seen it before. Inserting branches |
| 1126 | // should be relatively uncommon and when it does happen, we want to be |
| 1127 | // sure to take advantage of it for all the CPEs near that block, so that |
| 1128 | // we don't insert more branches than necessary. |
| 1129 | if (WaterIsInRange(UserOffset, WaterBB, U) && |
| 1130 | (WaterBB->getNumber() < U.HighWaterMark->getNumber() || |
| 1131 | NewWaterList.count(WaterBB))) { |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1132 | unsigned WBBId = WaterBB->getNumber(); |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 1133 | if (isThumb && BBInfo[WBBId].postOffset()%4 != 0) { |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1134 | // This is valid Water, but would introduce padding. Remember |
| 1135 | // it in case we don't find any Water that doesn't do this. |
Bob Wilson | 32c50e8 | 2009-10-12 20:45:53 +0000 | [diff] [blame] | 1136 | if (!FoundWaterThatWouldPad) { |
| 1137 | FoundWaterThatWouldPad = true; |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1138 | IPThatWouldPad = IP; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1139 | } |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1140 | } else { |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1141 | WaterIter = IP; |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1142 | return true; |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1143 | } |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1144 | } |
Bob Wilson | 3b75735 | 2009-10-12 19:04:03 +0000 | [diff] [blame] | 1145 | if (IP == B) |
| 1146 | break; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1147 | } |
Bob Wilson | 32c50e8 | 2009-10-12 20:45:53 +0000 | [diff] [blame] | 1148 | if (FoundWaterThatWouldPad) { |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1149 | WaterIter = IPThatWouldPad; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1150 | return true; |
| 1151 | } |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1152 | return false; |
| 1153 | } |
| 1154 | |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1155 | /// CreateNewWater - No existing WaterList entry will work for |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1156 | /// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the |
| 1157 | /// block is used if in range, and the conditional branch munged so control |
| 1158 | /// flow is correct. Otherwise the block is split to create a hole with an |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1159 | /// unconditional branch around it. In either case NewMBB is set to a |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1160 | /// block following which the new island can be inserted (the WaterList |
| 1161 | /// is not adjusted). |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1162 | void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex, |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1163 | unsigned UserOffset, |
| 1164 | MachineBasicBlock *&NewMBB) { |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1165 | CPUser &U = CPUsers[CPUserIndex]; |
| 1166 | MachineInstr *UserMI = U.MI; |
| 1167 | MachineInstr *CPEMI = U.CPEMI; |
| 1168 | MachineBasicBlock *UserMBB = UserMI->getParent(); |
Jakob Stoklund Olesen | 5bb3253 | 2011-12-07 01:22:52 +0000 | [diff] [blame] | 1169 | unsigned OffsetOfNextBlock = BBInfo[UserMBB->getNumber()].postOffset(); |
| 1170 | assert(OffsetOfNextBlock == BBInfo[UserMBB->getNumber()+1].Offset); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1171 | |
Bob Wilson | 36fa532 | 2009-10-15 05:10:36 +0000 | [diff] [blame] | 1172 | // If the block does not end in an unconditional branch already, and if the |
| 1173 | // end of the block is within range, make new water there. (The addition |
| 1174 | // below is for the unconditional branch we will be adding: 4 bytes on ARM + |
| 1175 | // Thumb2, 2 on Thumb1. Possible Thumb1 alignment padding is allowed for |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1176 | // inside OffsetIsInRange. |
Bob Wilson | 36fa532 | 2009-10-15 05:10:36 +0000 | [diff] [blame] | 1177 | if (BBHasFallthrough(UserMBB) && |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1178 | OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4), |
| 1179 | U.MaxDisp, U.NegOk, U.IsSoImm)) { |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1180 | DEBUG(errs() << "Split at end of block\n"); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1181 | if (&UserMBB->back() == UserMI) |
| 1182 | assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!"); |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1183 | NewMBB = llvm::next(MachineFunction::iterator(UserMBB)); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1184 | // Add an unconditional branch from UserMBB to fallthrough block. |
| 1185 | // Record it for branch lengthening; this new branch will not get out of |
| 1186 | // range, but if the preceding conditional branch is out of range, the |
| 1187 | // targets will be exchanged, and the altered branch may be out of |
| 1188 | // range, so the machinery has to know about it. |
David Goodwin | 5e47a9a | 2009-06-30 18:04:13 +0000 | [diff] [blame] | 1189 | int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B; |
Owen Anderson | 51f6a7a | 2011-09-09 21:48:23 +0000 | [diff] [blame] | 1190 | if (!isThumb) |
| 1191 | BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB); |
| 1192 | else |
| 1193 | BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB) |
| 1194 | .addImm(ARMCC::AL).addReg(0); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1195 | unsigned MaxDisp = getUnconditionalBrDisp(UncondBr); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1196 | ImmBranches.push_back(ImmBranch(&UserMBB->back(), |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1197 | MaxDisp, false, UncondBr)); |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1198 | int delta = isThumb1 ? 2 : 4; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1199 | BBInfo[UserMBB->getNumber()].Size += delta; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1200 | AdjustBBOffsetsAfter(UserMBB, delta); |
| 1201 | } else { |
| 1202 | // 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] | 1203 | // This is a little tricky on Thumb1 since instructions are 2 bytes |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1204 | // and constant pool entries are 4 bytes: if instruction I references |
| 1205 | // island CPE, and instruction I+1 references CPE', it will |
| 1206 | // not work well to put CPE as far forward as possible, since then |
| 1207 | // CPE' cannot immediately follow it (that location is 2 bytes |
| 1208 | // 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] | 1209 | // a new island. So, we make a first guess, then walk through the |
| 1210 | // instructions between the one currently being looked at and the |
| 1211 | // possible insertion point, and make sure any other instructions |
| 1212 | // that reference CPEs will be able to use the same island area; |
| 1213 | // if not, we back up the insertion point. |
| 1214 | |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1215 | // 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] | 1216 | // inserting (allows for long branch on Thumb1). Alignment of the |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1217 | // island is handled inside OffsetIsInRange. |
| 1218 | unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1219 | // This could point off the end of the block if we've already got |
| 1220 | // constant pool entries following this block; only the last one is |
| 1221 | // in the water list. Back past any possible branches (allow for a |
| 1222 | // conditional and a maximally long unconditional). |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1223 | if (BaseInsertOffset >= BBInfo[UserMBB->getNumber()+1].Offset) |
| 1224 | BaseInsertOffset = BBInfo[UserMBB->getNumber()+1].Offset - |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1225 | (isThumb1 ? 6 : 8); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1226 | unsigned EndInsertOffset = BaseInsertOffset + |
| 1227 | CPEMI->getOperand(2).getImm(); |
| 1228 | MachineBasicBlock::iterator MI = UserMI; |
| 1229 | ++MI; |
| 1230 | unsigned CPUIndex = CPUserIndex+1; |
Evan Cheng | 719510a | 2010-08-12 20:30:05 +0000 | [diff] [blame] | 1231 | unsigned NumCPUsers = CPUsers.size(); |
| 1232 | MachineInstr *LastIT = 0; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1233 | for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1234 | Offset < BaseInsertOffset; |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1235 | Offset += TII->GetInstSizeInBytes(MI), |
Evan Cheng | 719510a | 2010-08-12 20:30:05 +0000 | [diff] [blame] | 1236 | MI = llvm::next(MI)) { |
| 1237 | if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1238 | CPUser &U = CPUsers[CPUIndex]; |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1239 | if (!OffsetIsInRange(Offset, EndInsertOffset, |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1240 | U.MaxDisp, U.NegOk, U.IsSoImm)) { |
| 1241 | BaseInsertOffset -= (isThumb1 ? 2 : 4); |
| 1242 | EndInsertOffset -= (isThumb1 ? 2 : 4); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1243 | } |
| 1244 | // This is overly conservative, as we don't account for CPEMIs |
| 1245 | // being reused within the block, but it doesn't matter much. |
| 1246 | EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm(); |
| 1247 | CPUIndex++; |
| 1248 | } |
Evan Cheng | 719510a | 2010-08-12 20:30:05 +0000 | [diff] [blame] | 1249 | |
| 1250 | // Remember the last IT instruction. |
| 1251 | if (MI->getOpcode() == ARM::t2IT) |
| 1252 | LastIT = MI; |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1253 | } |
Evan Cheng | 719510a | 2010-08-12 20:30:05 +0000 | [diff] [blame] | 1254 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1255 | DEBUG(errs() << "Split in middle of big block\n"); |
Evan Cheng | 719510a | 2010-08-12 20:30:05 +0000 | [diff] [blame] | 1256 | --MI; |
| 1257 | |
| 1258 | // Avoid splitting an IT block. |
| 1259 | if (LastIT) { |
| 1260 | unsigned PredReg = 0; |
| 1261 | ARMCC::CondCodes CC = llvm::getITInstrPredicate(MI, PredReg); |
| 1262 | if (CC != ARMCC::AL) |
| 1263 | MI = LastIT; |
| 1264 | } |
| 1265 | NewMBB = SplitBlockBeforeInstr(MI); |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1266 | } |
| 1267 | } |
| 1268 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1269 | /// HandleConstantPoolUser - Analyze the specified user, checking to see if it |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1270 | /// 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] | 1271 | /// place in-range. Return true if we changed any addresses (thus must run |
| 1272 | /// another pass of branch lengthening), false otherwise. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1273 | bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &MF, |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1274 | unsigned CPUserIndex) { |
Dale Johannesen | f1b214d | 2007-02-28 18:41:23 +0000 | [diff] [blame] | 1275 | CPUser &U = CPUsers[CPUserIndex]; |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1276 | MachineInstr *UserMI = U.MI; |
| 1277 | MachineInstr *CPEMI = U.CPEMI; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1278 | unsigned CPI = CPEMI->getOperand(1).getIndex(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1279 | unsigned Size = CPEMI->getOperand(2).getImm(); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1280 | // Compute this only once, it's expensive. The 4 or 8 is the value the |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1281 | // hardware keeps in the PC. |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1282 | unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8); |
Evan Cheng | 768c9f7 | 2007-04-27 08:14:15 +0000 | [diff] [blame] | 1283 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1284 | // See if the current entry is within range, or there is a clone of it |
| 1285 | // in range. |
| 1286 | int result = LookForExistingCPEntry(U, UserOffset); |
| 1287 | if (result==1) return false; |
| 1288 | else if (result==2) return true; |
| 1289 | |
| 1290 | // No existing clone of this CPE is within range. |
| 1291 | // We will be generating a new clone. Get a UID for it. |
Evan Cheng | 5de5d4b | 2011-01-17 08:03:18 +0000 | [diff] [blame] | 1292 | unsigned ID = AFI->createPICLabelUId(); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1293 | |
Bob Wilson | f98032e | 2009-10-12 21:23:15 +0000 | [diff] [blame] | 1294 | // Look for water where we can place this CPE. |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1295 | MachineBasicBlock *NewIsland = MF.CreateMachineBasicBlock(); |
| 1296 | MachineBasicBlock *NewMBB; |
| 1297 | water_iterator IP; |
| 1298 | if (LookForWater(U, UserOffset, IP)) { |
| 1299 | DEBUG(errs() << "found water in range\n"); |
| 1300 | MachineBasicBlock *WaterBB = *IP; |
| 1301 | |
| 1302 | // If the original WaterList entry was "new water" on this iteration, |
| 1303 | // propagate that to the new island. This is just keeping NewWaterList |
| 1304 | // updated to match the WaterList, which will be updated below. |
| 1305 | if (NewWaterList.count(WaterBB)) { |
| 1306 | NewWaterList.erase(WaterBB); |
| 1307 | NewWaterList.insert(NewIsland); |
| 1308 | } |
| 1309 | // The new CPE goes before the following block (NewMBB). |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1310 | NewMBB = llvm::next(MachineFunction::iterator(WaterBB)); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1311 | |
| 1312 | } else { |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1313 | // No water found. |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1314 | DEBUG(errs() << "No water found\n"); |
Bob Wilson | 757652c | 2009-10-12 21:39:43 +0000 | [diff] [blame] | 1315 | CreateNewWater(CPUserIndex, UserOffset, NewMBB); |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1316 | |
| 1317 | // SplitBlockBeforeInstr adds to WaterList, which is important when it is |
| 1318 | // called while handling branches so that the water will be seen on the |
| 1319 | // next iteration for constant pools, but in this context, we don't want |
| 1320 | // it. Check for this so it will be removed from the WaterList. |
| 1321 | // Also remove any entry from NewWaterList. |
| 1322 | MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB)); |
| 1323 | IP = std::find(WaterList.begin(), WaterList.end(), WaterBB); |
| 1324 | if (IP != WaterList.end()) |
| 1325 | NewWaterList.erase(WaterBB); |
| 1326 | |
| 1327 | // We are adding new water. Update NewWaterList. |
| 1328 | NewWaterList.insert(NewIsland); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
Bob Wilson | b923953 | 2009-10-15 20:49:47 +0000 | [diff] [blame] | 1331 | // Remove the original WaterList entry; we want subsequent insertions in |
| 1332 | // this vicinity to go after the one we're about to insert. This |
| 1333 | // considerably reduces the number of times we have to move the same CPE |
| 1334 | // more than once and is also important to ensure the algorithm terminates. |
| 1335 | if (IP != WaterList.end()) |
| 1336 | WaterList.erase(IP); |
| 1337 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1338 | // 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] | 1339 | MF.insert(NewMBB, NewIsland); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1340 | |
| 1341 | // Update internal data structures to account for the newly inserted MBB. |
| 1342 | UpdateForInsertedWaterBlock(NewIsland); |
| 1343 | |
| 1344 | // Decrement the old entry, and remove it if refcount becomes 0. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1345 | DecrementOldEntry(CPI, CPEMI); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Now that we have an island to add the CPE to, clone the original CPE and |
| 1348 | // add it to the island. |
Bob Wilson | 549dda9 | 2009-10-15 05:52:29 +0000 | [diff] [blame] | 1349 | U.HighWaterMark = NewIsland; |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1350 | U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(ARM::CONSTPOOL_ENTRY)) |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1351 | .addImm(ID).addConstantPoolIndex(CPI).addImm(Size); |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1352 | CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1)); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1353 | ++NumCPEs; |
Evan Cheng | c99ef08 | 2007-02-09 20:54:44 +0000 | [diff] [blame] | 1354 | |
Jakob Stoklund Olesen | 3e572ac | 2011-12-06 01:43:02 +0000 | [diff] [blame] | 1355 | // Mark the basic block as 4-byte aligned as required by the const-pool entry. |
| 1356 | NewIsland->setAlignment(2); |
| 1357 | |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1358 | BBInfo[NewIsland->getNumber()].Offset = BBInfo[NewMBB->getNumber()].Offset; |
Evan Cheng | b43216e | 2007-02-01 10:16:15 +0000 | [diff] [blame] | 1359 | // Compensate for .align 2 in thumb mode. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1360 | if (isThumb && (BBInfo[NewIsland->getNumber()].Offset%4 != 0 || HasInlineAsm)) |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1361 | Size += 2; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1362 | // Increase the size of the island block to account for the new entry. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1363 | BBInfo[NewIsland->getNumber()].Size += Size; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1364 | AdjustBBOffsetsAfter(NewIsland, Size); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1365 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1366 | // Finally, change the CPI in the instruction operand to be ID. |
| 1367 | for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i) |
Dan Gohman | d735b80 | 2008-10-03 15:45:36 +0000 | [diff] [blame] | 1368 | if (UserMI->getOperand(i).isCPI()) { |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1369 | UserMI->getOperand(i).setIndex(ID); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1370 | break; |
| 1371 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1372 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1373 | DEBUG(errs() << " Moved CPE to #" << ID << " CPI=" << CPI |
| 1374 | << '\t' << *UserMI); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1375 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1376 | return true; |
| 1377 | } |
| 1378 | |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1379 | /// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update |
| 1380 | /// sizes and offsets of impacted basic blocks. |
| 1381 | void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) { |
| 1382 | MachineBasicBlock *CPEBB = CPEMI->getParent(); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1383 | unsigned Size = CPEMI->getOperand(2).getImm(); |
| 1384 | CPEMI->eraseFromParent(); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1385 | BBInfo[CPEBB->getNumber()].Size -= Size; |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1386 | // All succeeding offsets have the current size value added in, fix this. |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1387 | if (CPEBB->empty()) { |
Evan Cheng | d3d9d66 | 2009-07-23 18:27:47 +0000 | [diff] [blame] | 1388 | // 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] | 1389 | // 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] | 1390 | // empty, so fix this. |
| 1391 | // All succeeding offsets have the current size value added in, fix this. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1392 | if (BBInfo[CPEBB->getNumber()].Size != 0) { |
| 1393 | Size += BBInfo[CPEBB->getNumber()].Size; |
| 1394 | BBInfo[CPEBB->getNumber()].Size = 0; |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1395 | } |
Jakob Stoklund Olesen | 305e5fe | 2011-12-06 21:55:35 +0000 | [diff] [blame] | 1396 | |
| 1397 | // This block no longer needs to be aligned. <rdar://problem/10534709>. |
| 1398 | CPEBB->setAlignment(0); |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1399 | } |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1400 | AdjustBBOffsetsAfter(CPEBB, -Size); |
| 1401 | // An island has only one predecessor BB and one successor BB. Check if |
| 1402 | // this BB's predecessor jumps directly to this BB's successor. This |
| 1403 | // shouldn't happen currently. |
| 1404 | assert(!BBIsJumpedOver(CPEBB) && "How did this happen?"); |
| 1405 | // FIXME: remove the empty blocks after all the work is done? |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | /// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts |
| 1409 | /// are zero. |
| 1410 | bool ARMConstantIslands::RemoveUnusedCPEntries() { |
| 1411 | unsigned MadeChange = false; |
| 1412 | for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) { |
| 1413 | std::vector<CPEntry> &CPEs = CPEntries[i]; |
| 1414 | for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) { |
| 1415 | if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) { |
| 1416 | RemoveDeadCPEMI(CPEs[j].CPEMI); |
| 1417 | CPEs[j].CPEMI = NULL; |
| 1418 | MadeChange = true; |
| 1419 | } |
| 1420 | } |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1421 | } |
Evan Cheng | ed884f3 | 2007-04-03 23:39:48 +0000 | [diff] [blame] | 1422 | return MadeChange; |
| 1423 | } |
| 1424 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1425 | /// BBIsInRange - Returns true if the distance between specific MI and |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1426 | /// specific BB can fit in MI's displacement field. |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1427 | bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB, |
| 1428 | unsigned MaxDisp) { |
Dale Johannesen | b71aa2b | 2007-02-28 23:20:38 +0000 | [diff] [blame] | 1429 | unsigned PCAdj = isThumb ? 4 : 8; |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1430 | unsigned BrOffset = GetOffsetOf(MI) + PCAdj; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1431 | unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1432 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1433 | DEBUG(errs() << "Branch of destination BB#" << DestBB->getNumber() |
| 1434 | << " from BB#" << MI->getParent()->getNumber() |
| 1435 | << " max delta=" << MaxDisp |
| 1436 | << " from " << GetOffsetOf(MI) << " to " << DestOffset |
| 1437 | << " offset " << int(DestOffset-BrOffset) << "\t" << *MI); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1438 | |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1439 | if (BrOffset <= DestOffset) { |
| 1440 | // Branch before the Dest. |
| 1441 | if (DestOffset-BrOffset <= MaxDisp) |
| 1442 | return true; |
| 1443 | } else { |
| 1444 | if (BrOffset-DestOffset <= MaxDisp) |
| 1445 | return true; |
| 1446 | } |
| 1447 | return false; |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1450 | /// FixUpImmediateBr - Fix up an immediate branch whose destination is too far |
| 1451 | /// away to fit in its displacement field. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1452 | bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1453 | MachineInstr *MI = Br.MI; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1454 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1455 | |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1456 | // Check to see if the DestBB is already in-range. |
| 1457 | if (BBIsInRange(MI, DestBB, Br.MaxDisp)) |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1458 | return false; |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1459 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1460 | if (!Br.isCond) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1461 | return FixUpUnconditionalBr(MF, Br); |
| 1462 | return FixUpConditionalBr(MF, Br); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1463 | } |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1464 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1465 | /// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is |
| 1466 | /// 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] | 1467 | /// 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] | 1468 | /// Otherwise, add an intermediate branch instruction to a branch. |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1469 | bool |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1470 | ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br) { |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1471 | MachineInstr *MI = Br.MI; |
| 1472 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | 53c67c0 | 2009-08-07 05:45:07 +0000 | [diff] [blame] | 1473 | if (!isThumb1) |
| 1474 | llvm_unreachable("FixUpUnconditionalBr is Thumb1 only!"); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1475 | |
| 1476 | // Use BL to implement far jump. |
| 1477 | Br.MaxDisp = (1 << 21) * 2; |
Chris Lattner | 5080f4d | 2008-01-11 18:10:50 +0000 | [diff] [blame] | 1478 | MI->setDesc(TII->get(ARM::tBfar)); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1479 | BBInfo[MBB->getNumber()].Size += 2; |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1480 | AdjustBBOffsetsAfter(MBB, 2); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1481 | HasFarJump = true; |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1482 | ++NumUBrFixed; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1483 | |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1484 | DEBUG(errs() << " Changed B to long jump " << *MI); |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1485 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1486 | return true; |
| 1487 | } |
| 1488 | |
Dale Johannesen | 88e37ae | 2007-02-23 05:02:36 +0000 | [diff] [blame] | 1489 | /// FixUpConditionalBr - Fix up a conditional branch whose destination is too |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1490 | /// far away to fit in its displacement field. It is converted to an inverse |
| 1491 | /// conditional branch + an unconditional branch to the destination. |
| 1492 | bool |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1493 | ARMConstantIslands::FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br) { |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1494 | MachineInstr *MI = Br.MI; |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1495 | MachineBasicBlock *DestBB = MI->getOperand(0).getMBB(); |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1496 | |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1497 | // Add an unconditional branch to the destination and invert the branch |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1498 | // condition to jump over it: |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1499 | // blt L1 |
| 1500 | // => |
| 1501 | // bge L2 |
| 1502 | // b L1 |
| 1503 | // L2: |
Chris Lattner | 9a1ceae | 2007-12-30 20:49:49 +0000 | [diff] [blame] | 1504 | ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1505 | CC = ARMCC::getOppositeCondition(CC); |
Evan Cheng | 0e1d379 | 2007-07-05 07:18:20 +0000 | [diff] [blame] | 1506 | unsigned CCReg = MI->getOperand(2).getReg(); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1507 | |
| 1508 | // If the branch is at the end of its MBB and that has a fall-through block, |
| 1509 | // direct the updated conditional branch to the fall-through block. Otherwise, |
| 1510 | // split the MBB before the next instruction. |
| 1511 | MachineBasicBlock *MBB = MI->getParent(); |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1512 | MachineInstr *BMI = &MBB->back(); |
| 1513 | bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1514 | |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame] | 1515 | ++NumCBrFixed; |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1516 | if (BMI != MI) { |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1517 | if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) && |
Evan Cheng | bd5d3db | 2007-02-03 02:08:34 +0000 | [diff] [blame] | 1518 | BMI->getOpcode() == Br.UncondBr) { |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1519 | // 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] | 1520 | // condition and swap destinations: |
| 1521 | // beq L1 |
| 1522 | // b L2 |
| 1523 | // => |
| 1524 | // bne L2 |
| 1525 | // b L1 |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1526 | MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB(); |
Evan Cheng | c0dbec7 | 2007-01-31 19:57:44 +0000 | [diff] [blame] | 1527 | if (BBIsInRange(MI, NewDest, Br.MaxDisp)) { |
Chris Lattner | 705e07f | 2009-08-23 03:41:05 +0000 | [diff] [blame] | 1528 | DEBUG(errs() << " Invert Bcc condition and swap its destination with " |
| 1529 | << *BMI); |
Chris Lattner | 8aa797a | 2007-12-30 23:10:15 +0000 | [diff] [blame] | 1530 | BMI->getOperand(0).setMBB(DestBB); |
| 1531 | MI->getOperand(0).setMBB(NewDest); |
Evan Cheng | 43aeab6 | 2007-01-26 20:38:26 +0000 | [diff] [blame] | 1532 | MI->getOperand(1).setImm(CC); |
| 1533 | return true; |
| 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | if (NeedSplit) { |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1539 | SplitBlockBeforeInstr(MI); |
Bob Wilson | 39bf051 | 2009-05-12 17:35:29 +0000 | [diff] [blame] | 1540 | // 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] | 1541 | // branch to the destination. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1542 | int delta = TII->GetInstSizeInBytes(&MBB->back()); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1543 | BBInfo[MBB->getNumber()].Size -= delta; |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1544 | MachineBasicBlock* SplitBB = llvm::next(MachineFunction::iterator(MBB)); |
Dale Johannesen | 8593e41 | 2007-04-29 19:19:30 +0000 | [diff] [blame] | 1545 | AdjustBBOffsetsAfter(SplitBB, -delta); |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1546 | MBB->back().eraseFromParent(); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1547 | // BBInfo[SplitBB].Offset is wrong temporarily, fixed below |
Evan Cheng | dd353b8 | 2007-01-26 02:02:39 +0000 | [diff] [blame] | 1548 | } |
Chris Lattner | 7896c9f | 2009-12-03 00:50:42 +0000 | [diff] [blame] | 1549 | MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB)); |
Bob Wilson | 8494526 | 2009-05-12 17:09:30 +0000 | [diff] [blame] | 1550 | |
Chris Lattner | 893e1c9 | 2009-08-23 06:49:22 +0000 | [diff] [blame] | 1551 | DEBUG(errs() << " Insert B to BB#" << DestBB->getNumber() |
| 1552 | << " also invert condition and change dest. to BB#" |
| 1553 | << NextBB->getNumber() << "\n"); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1554 | |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1555 | // Insert a new conditional branch and a new unconditional branch. |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1556 | // Also update the ImmBranch as well as adding a new entry for the new branch. |
Chris Lattner | c7f3ace | 2010-04-02 20:16:16 +0000 | [diff] [blame] | 1557 | BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode())) |
Dale Johannesen | b672840 | 2009-02-13 02:25:56 +0000 | [diff] [blame] | 1558 | .addMBB(NextBB).addImm(CC).addReg(CCReg); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1559 | Br.MI = &MBB->back(); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1560 | BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back()); |
Owen Anderson | cd4338f | 2011-09-09 23:05:14 +0000 | [diff] [blame] | 1561 | if (isThumb) |
| 1562 | BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB) |
| 1563 | .addImm(ARMCC::AL).addReg(0); |
| 1564 | else |
| 1565 | BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1566 | BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back()); |
Evan Cheng | a9b8b8d | 2007-01-31 18:29:27 +0000 | [diff] [blame] | 1567 | unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr); |
Evan Cheng | a0bf794 | 2007-01-25 23:31:04 +0000 | [diff] [blame] | 1568 | ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr)); |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1569 | |
| 1570 | // Remove the old conditional branch. It may or may not still be in MBB. |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1571 | BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1572 | MI->eraseFromParent(); |
| 1573 | |
Dale Johannesen | 56c42ef | 2007-04-23 20:09:04 +0000 | [diff] [blame] | 1574 | // The net size change is an addition of one unconditional branch. |
Nicolas Geoffray | 52e724a | 2008-04-16 20:10:13 +0000 | [diff] [blame] | 1575 | int delta = TII->GetInstSizeInBytes(&MBB->back()); |
Dale Johannesen | 99c49a4 | 2007-02-25 00:47:03 +0000 | [diff] [blame] | 1576 | AdjustBBOffsetsAfter(MBB, delta); |
Evan Cheng | af5cbcb | 2007-01-25 03:12:46 +0000 | [diff] [blame] | 1577 | return true; |
| 1578 | } |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1579 | |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1580 | /// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills |
Evan Cheng | 4b322e5 | 2009-08-11 21:11:32 +0000 | [diff] [blame] | 1581 | /// LR / restores LR to pc. FIXME: This is done here because it's only possible |
| 1582 | /// to do this if tBfar is not used. |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1583 | bool ARMConstantIslands::UndoLRSpillRestore() { |
| 1584 | bool MadeChange = false; |
| 1585 | for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) { |
| 1586 | MachineInstr *MI = PushPopMIs[i]; |
Bob Wilson | 815baeb | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 1587 | // First two operands are predicates. |
Evan Cheng | 44bec52 | 2007-05-15 01:29:07 +0000 | [diff] [blame] | 1588 | if (MI->getOpcode() == ARM::tPOP_RET && |
Bob Wilson | 815baeb | 2010-03-13 01:08:20 +0000 | [diff] [blame] | 1589 | MI->getOperand(2).getReg() == ARM::PC && |
| 1590 | MI->getNumExplicitOperands() == 3) { |
Jim Grosbach | 25e6d48 | 2011-07-08 21:50:04 +0000 | [diff] [blame] | 1591 | // Create the new insn and copy the predicate from the old. |
| 1592 | BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET)) |
| 1593 | .addOperand(MI->getOperand(0)) |
| 1594 | .addOperand(MI->getOperand(1)); |
Evan Cheng | 44bec52 | 2007-05-15 01:29:07 +0000 | [diff] [blame] | 1595 | MI->eraseFromParent(); |
| 1596 | MadeChange = true; |
Evan Cheng | d1b2c1e | 2007-01-30 01:18:38 +0000 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | return MadeChange; |
| 1600 | } |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1601 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1602 | bool ARMConstantIslands::OptimizeThumb2Instructions(MachineFunction &MF) { |
| 1603 | bool MadeChange = false; |
| 1604 | |
| 1605 | // Shrink ADR and LDR from constantpool. |
| 1606 | for (unsigned i = 0, e = CPUsers.size(); i != e; ++i) { |
| 1607 | CPUser &U = CPUsers[i]; |
| 1608 | unsigned Opcode = U.MI->getOpcode(); |
| 1609 | unsigned NewOpc = 0; |
| 1610 | unsigned Scale = 1; |
| 1611 | unsigned Bits = 0; |
| 1612 | switch (Opcode) { |
| 1613 | default: break; |
Owen Anderson | 6b8719f | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 1614 | case ARM::t2LEApcrel: |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1615 | if (isARMLowRegister(U.MI->getOperand(0).getReg())) { |
| 1616 | NewOpc = ARM::tLEApcrel; |
| 1617 | Bits = 8; |
| 1618 | Scale = 4; |
| 1619 | } |
| 1620 | break; |
| 1621 | case ARM::t2LDRpci: |
| 1622 | if (isARMLowRegister(U.MI->getOperand(0).getReg())) { |
| 1623 | NewOpc = ARM::tLDRpci; |
| 1624 | Bits = 8; |
| 1625 | Scale = 4; |
| 1626 | } |
| 1627 | break; |
| 1628 | } |
| 1629 | |
| 1630 | if (!NewOpc) |
| 1631 | continue; |
| 1632 | |
| 1633 | unsigned UserOffset = GetOffsetOf(U.MI) + 4; |
| 1634 | unsigned MaxOffs = ((1 << Bits) - 1) * Scale; |
| 1635 | // FIXME: Check if offset is multiple of scale if scale is not 4. |
| 1636 | if (CPEIsInRange(U.MI, UserOffset, U.CPEMI, MaxOffs, false, true)) { |
| 1637 | U.MI->setDesc(TII->get(NewOpc)); |
| 1638 | MachineBasicBlock *MBB = U.MI->getParent(); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1639 | BBInfo[MBB->getNumber()].Size -= 2; |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1640 | AdjustBBOffsetsAfter(MBB, -2); |
| 1641 | ++NumT2CPShrunk; |
| 1642 | MadeChange = true; |
| 1643 | } |
| 1644 | } |
| 1645 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1646 | MadeChange |= OptimizeThumb2Branches(MF); |
Jim Grosbach | 01dec0e | 2009-11-12 03:28:35 +0000 | [diff] [blame] | 1647 | MadeChange |= OptimizeThumb2JumpTables(MF); |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1648 | return MadeChange; |
| 1649 | } |
| 1650 | |
| 1651 | bool ARMConstantIslands::OptimizeThumb2Branches(MachineFunction &MF) { |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1652 | bool MadeChange = false; |
| 1653 | |
| 1654 | for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i) { |
| 1655 | ImmBranch &Br = ImmBranches[i]; |
| 1656 | unsigned Opcode = Br.MI->getOpcode(); |
| 1657 | unsigned NewOpc = 0; |
| 1658 | unsigned Scale = 1; |
| 1659 | unsigned Bits = 0; |
| 1660 | switch (Opcode) { |
| 1661 | default: break; |
| 1662 | case ARM::t2B: |
| 1663 | NewOpc = ARM::tB; |
| 1664 | Bits = 11; |
| 1665 | Scale = 2; |
| 1666 | break; |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1667 | case ARM::t2Bcc: { |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1668 | NewOpc = ARM::tBcc; |
| 1669 | Bits = 8; |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1670 | Scale = 2; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1671 | break; |
| 1672 | } |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1673 | } |
| 1674 | if (NewOpc) { |
| 1675 | unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale; |
| 1676 | MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); |
| 1677 | if (BBIsInRange(Br.MI, DestBB, MaxOffs)) { |
| 1678 | Br.MI->setDesc(TII->get(NewOpc)); |
| 1679 | MachineBasicBlock *MBB = Br.MI->getParent(); |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1680 | BBInfo[MBB->getNumber()].Size -= 2; |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1681 | AdjustBBOffsetsAfter(MBB, -2); |
| 1682 | ++NumT2BrShrunk; |
| 1683 | MadeChange = true; |
| 1684 | } |
| 1685 | } |
| 1686 | |
| 1687 | Opcode = Br.MI->getOpcode(); |
| 1688 | if (Opcode != ARM::tBcc) |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1689 | continue; |
| 1690 | |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1691 | NewOpc = 0; |
| 1692 | unsigned PredReg = 0; |
| 1693 | ARMCC::CondCodes Pred = llvm::getInstrPredicate(Br.MI, PredReg); |
| 1694 | if (Pred == ARMCC::EQ) |
| 1695 | NewOpc = ARM::tCBZ; |
| 1696 | else if (Pred == ARMCC::NE) |
| 1697 | NewOpc = ARM::tCBNZ; |
| 1698 | if (!NewOpc) |
| 1699 | continue; |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1700 | MachineBasicBlock *DestBB = Br.MI->getOperand(0).getMBB(); |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1701 | // Check if the distance is within 126. Subtract starting offset by 2 |
| 1702 | // because the cmp will be eliminated. |
| 1703 | unsigned BrOffset = GetOffsetOf(Br.MI) + 4 - 2; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1704 | unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset; |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1705 | if (BrOffset < DestOffset && (DestOffset - BrOffset) <= 126) { |
Evan Cheng | 0539c15 | 2011-04-01 22:09:28 +0000 | [diff] [blame] | 1706 | MachineBasicBlock::iterator CmpMI = Br.MI; |
| 1707 | if (CmpMI != Br.MI->getParent()->begin()) { |
| 1708 | --CmpMI; |
| 1709 | if (CmpMI->getOpcode() == ARM::tCMPi8) { |
| 1710 | unsigned Reg = CmpMI->getOperand(0).getReg(); |
| 1711 | Pred = llvm::getInstrPredicate(CmpMI, PredReg); |
| 1712 | if (Pred == ARMCC::AL && |
| 1713 | CmpMI->getOperand(1).getImm() == 0 && |
| 1714 | isARMLowRegister(Reg)) { |
| 1715 | MachineBasicBlock *MBB = Br.MI->getParent(); |
| 1716 | MachineInstr *NewBR = |
| 1717 | BuildMI(*MBB, CmpMI, Br.MI->getDebugLoc(), TII->get(NewOpc)) |
| 1718 | .addReg(Reg).addMBB(DestBB,Br.MI->getOperand(0).getTargetFlags()); |
| 1719 | CmpMI->eraseFromParent(); |
| 1720 | Br.MI->eraseFromParent(); |
| 1721 | Br.MI = NewBR; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1722 | BBInfo[MBB->getNumber()].Size -= 2; |
Evan Cheng | 0539c15 | 2011-04-01 22:09:28 +0000 | [diff] [blame] | 1723 | AdjustBBOffsetsAfter(MBB, -2); |
| 1724 | ++NumCBZ; |
| 1725 | MadeChange = true; |
| 1726 | } |
Evan Cheng | de17fb6 | 2009-10-31 23:46:45 +0000 | [diff] [blame] | 1727 | } |
| 1728 | } |
Evan Cheng | 31b99dd | 2009-08-14 18:31:44 +0000 | [diff] [blame] | 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | return MadeChange; |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1733 | } |
| 1734 | |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1735 | /// OptimizeThumb2JumpTables - Use tbb / tbh instructions to generate smaller |
| 1736 | /// jumptables when it's possible. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1737 | bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction &MF) { |
| 1738 | bool MadeChange = false; |
| 1739 | |
| 1740 | // FIXME: After the tables are shrunk, can we get rid some of the |
| 1741 | // constantpool tables? |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1742 | MachineJumpTableInfo *MJTI = MF.getJumpTableInfo(); |
Chris Lattner | b1e8039 | 2010-01-25 23:22:00 +0000 | [diff] [blame] | 1743 | if (MJTI == 0) return false; |
Jim Grosbach | 26b8ef5 | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 1744 | |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1745 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1746 | for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { |
| 1747 | MachineInstr *MI = T2JumpTables[i]; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1748 | const MCInstrDesc &MCID = MI->getDesc(); |
| 1749 | unsigned NumOps = MCID.getNumOperands(); |
| 1750 | unsigned JTOpIdx = NumOps - (MCID.isPredicable() ? 3 : 2); |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1751 | MachineOperand JTOP = MI->getOperand(JTOpIdx); |
| 1752 | unsigned JTI = JTOP.getIndex(); |
| 1753 | assert(JTI < JT.size()); |
| 1754 | |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1755 | bool ByteOk = true; |
| 1756 | bool HalfWordOk = true; |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1757 | unsigned JTOffset = GetOffsetOf(MI) + 4; |
| 1758 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1759 | for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { |
| 1760 | MachineBasicBlock *MBB = JTBBs[j]; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1761 | unsigned DstOffset = BBInfo[MBB->getNumber()].Offset; |
Evan Cheng | 8770f74 | 2009-07-29 23:20:20 +0000 | [diff] [blame] | 1762 | // Negative offset is not ok. FIXME: We should change BB layout to make |
| 1763 | // sure all the branches are forward. |
Evan Cheng | d26b14c | 2009-07-31 18:28:05 +0000 | [diff] [blame] | 1764 | if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1765 | ByteOk = false; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1766 | unsigned TBHLimit = ((1<<16)-1)*2; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1767 | if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1768 | HalfWordOk = false; |
| 1769 | if (!ByteOk && !HalfWordOk) |
| 1770 | break; |
| 1771 | } |
| 1772 | |
| 1773 | if (ByteOk || HalfWordOk) { |
| 1774 | MachineBasicBlock *MBB = MI->getParent(); |
| 1775 | unsigned BaseReg = MI->getOperand(0).getReg(); |
| 1776 | bool BaseRegKill = MI->getOperand(0).isKill(); |
| 1777 | if (!BaseRegKill) |
| 1778 | continue; |
| 1779 | unsigned IdxReg = MI->getOperand(1).getReg(); |
| 1780 | bool IdxRegKill = MI->getOperand(1).isKill(); |
Jim Grosbach | c7937ae | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 1781 | |
| 1782 | // Scan backwards to find the instruction that defines the base |
| 1783 | // register. Due to post-RA scheduling, we can't count on it |
| 1784 | // immediately preceding the branch instruction. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1785 | MachineBasicBlock::iterator PrevI = MI; |
Jim Grosbach | c7937ae | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 1786 | MachineBasicBlock::iterator B = MBB->begin(); |
| 1787 | while (PrevI != B && !PrevI->definesRegister(BaseReg)) |
| 1788 | --PrevI; |
| 1789 | |
| 1790 | // If for some reason we didn't find it, we can't do anything, so |
| 1791 | // just skip this one. |
| 1792 | if (!PrevI->definesRegister(BaseReg)) |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1793 | continue; |
| 1794 | |
Jim Grosbach | c7937ae | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 1795 | MachineInstr *AddrMI = PrevI; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1796 | bool OptOk = true; |
Jim Grosbach | 26b8ef5 | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 1797 | // Examine the instruction that calculates the jumptable entry address. |
Jim Grosbach | c7937ae | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 1798 | // Make sure it only defines the base register and kills any uses |
| 1799 | // other than the index register. |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1800 | for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) { |
| 1801 | const MachineOperand &MO = AddrMI->getOperand(k); |
| 1802 | if (!MO.isReg() || !MO.getReg()) |
| 1803 | continue; |
| 1804 | if (MO.isDef() && MO.getReg() != BaseReg) { |
| 1805 | OptOk = false; |
| 1806 | break; |
| 1807 | } |
| 1808 | if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) { |
| 1809 | OptOk = false; |
| 1810 | break; |
| 1811 | } |
| 1812 | } |
| 1813 | if (!OptOk) |
| 1814 | continue; |
| 1815 | |
Owen Anderson | 6b8719f | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 1816 | // Now scan back again to find the tLEApcrel or t2LEApcrelJT instruction |
Jim Grosbach | c7937ae | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 1817 | // that gave us the initial base register definition. |
| 1818 | for (--PrevI; PrevI != B && !PrevI->definesRegister(BaseReg); --PrevI) |
| 1819 | ; |
| 1820 | |
Owen Anderson | 6b8719f | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 1821 | // The instruction should be a tLEApcrel or t2LEApcrelJT; we want |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1822 | // to delete it as well. |
Jim Grosbach | c7937ae | 2010-07-07 22:51:22 +0000 | [diff] [blame] | 1823 | MachineInstr *LeaMI = PrevI; |
Evan Cheng | a1efbbd | 2009-08-14 00:32:16 +0000 | [diff] [blame] | 1824 | if ((LeaMI->getOpcode() != ARM::tLEApcrelJT && |
Owen Anderson | 6b8719f | 2010-12-13 22:51:08 +0000 | [diff] [blame] | 1825 | LeaMI->getOpcode() != ARM::t2LEApcrelJT) || |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1826 | LeaMI->getOperand(0).getReg() != BaseReg) |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1827 | OptOk = false; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1828 | |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1829 | if (!OptOk) |
| 1830 | continue; |
| 1831 | |
Jim Grosbach | d092a87 | 2010-11-29 21:28:32 +0000 | [diff] [blame] | 1832 | unsigned Opc = ByteOk ? ARM::t2TBB_JT : ARM::t2TBH_JT; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1833 | MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc)) |
| 1834 | .addReg(IdxReg, getKillRegState(IdxRegKill)) |
| 1835 | .addJumpTableIndex(JTI, JTOP.getTargetFlags()) |
| 1836 | .addImm(MI->getOperand(JTOpIdx+1).getImm()); |
| 1837 | // FIXME: Insert an "ALIGN" instruction to ensure the next instruction |
| 1838 | // is 2-byte aligned. For now, asm printer will fix it up. |
| 1839 | unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI); |
| 1840 | unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI); |
| 1841 | OrigSize += TII->GetInstSizeInBytes(LeaMI); |
| 1842 | OrigSize += TII->GetInstSizeInBytes(MI); |
| 1843 | |
| 1844 | AddrMI->eraseFromParent(); |
| 1845 | LeaMI->eraseFromParent(); |
| 1846 | MI->eraseFromParent(); |
| 1847 | |
| 1848 | int delta = OrigSize - NewSize; |
Jakob Stoklund Olesen | a3f331b | 2011-12-07 01:08:25 +0000 | [diff] [blame] | 1849 | BBInfo[MBB->getNumber()].Size -= delta; |
Evan Cheng | 25f7cfc | 2009-08-01 06:13:52 +0000 | [diff] [blame] | 1850 | AdjustBBOffsetsAfter(MBB, -delta); |
| 1851 | |
| 1852 | ++NumTBs; |
| 1853 | MadeChange = true; |
Evan Cheng | 5657c01 | 2009-07-29 02:18:14 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | return MadeChange; |
| 1858 | } |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1859 | |
Jim Grosbach | 9249efe | 2009-11-16 18:55:47 +0000 | [diff] [blame] | 1860 | /// ReorderThumb2JumpTables - Adjust the function's block layout to ensure that |
| 1861 | /// jump tables always branch forwards, since that's what tbb and tbh need. |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1862 | bool ARMConstantIslands::ReorderThumb2JumpTables(MachineFunction &MF) { |
| 1863 | bool MadeChange = false; |
| 1864 | |
| 1865 | MachineJumpTableInfo *MJTI = MF.getJumpTableInfo(); |
Chris Lattner | b1e8039 | 2010-01-25 23:22:00 +0000 | [diff] [blame] | 1866 | if (MJTI == 0) return false; |
Jim Grosbach | 26b8ef5 | 2010-07-07 21:06:51 +0000 | [diff] [blame] | 1867 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1868 | const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables(); |
| 1869 | for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) { |
| 1870 | MachineInstr *MI = T2JumpTables[i]; |
Evan Cheng | e837dea | 2011-06-28 19:10:37 +0000 | [diff] [blame] | 1871 | const MCInstrDesc &MCID = MI->getDesc(); |
| 1872 | unsigned NumOps = MCID.getNumOperands(); |
| 1873 | unsigned JTOpIdx = NumOps - (MCID.isPredicable() ? 3 : 2); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1874 | MachineOperand JTOP = MI->getOperand(JTOpIdx); |
| 1875 | unsigned JTI = JTOP.getIndex(); |
| 1876 | assert(JTI < JT.size()); |
| 1877 | |
| 1878 | // We prefer if target blocks for the jump table come after the jump |
| 1879 | // instruction so we can use TB[BH]. Loop through the target blocks |
| 1880 | // and try to adjust them such that that's true. |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1881 | int JTNumber = MI->getParent()->getNumber(); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1882 | const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs; |
| 1883 | for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) { |
| 1884 | MachineBasicBlock *MBB = JTBBs[j]; |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1885 | int DTNumber = MBB->getNumber(); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1886 | |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1887 | if (DTNumber < JTNumber) { |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1888 | // The destination precedes the switch. Try to move the block forward |
| 1889 | // so we have a positive offset. |
| 1890 | MachineBasicBlock *NewBB = |
| 1891 | AdjustJTTargetBlockForward(MBB, MI->getParent()); |
| 1892 | if (NewBB) |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1893 | MJTI->ReplaceMBBInJumpTable(JTI, JTBBs[j], NewBB); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1894 | MadeChange = true; |
| 1895 | } |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | return MadeChange; |
| 1900 | } |
| 1901 | |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1902 | MachineBasicBlock *ARMConstantIslands:: |
| 1903 | AdjustJTTargetBlockForward(MachineBasicBlock *BB, MachineBasicBlock *JTBB) |
| 1904 | { |
| 1905 | MachineFunction &MF = *BB->getParent(); |
| 1906 | |
Jim Grosbach | 03e2d44 | 2010-07-07 22:53:35 +0000 | [diff] [blame] | 1907 | // If the destination block is terminated by an unconditional branch, |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1908 | // try to move it; otherwise, create a new block following the jump |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1909 | // table that branches back to the actual target. This is a very simple |
| 1910 | // heuristic. FIXME: We can definitely improve it. |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1911 | MachineBasicBlock *TBB = 0, *FBB = 0; |
| 1912 | SmallVector<MachineOperand, 4> Cond; |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1913 | SmallVector<MachineOperand, 4> CondPrior; |
| 1914 | MachineFunction::iterator BBi = BB; |
| 1915 | MachineFunction::iterator OldPrior = prior(BBi); |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1916 | |
Jim Grosbach | ca215e7 | 2009-11-16 17:10:56 +0000 | [diff] [blame] | 1917 | // If the block terminator isn't analyzable, don't try to move the block |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1918 | bool B = TII->AnalyzeBranch(*BB, TBB, FBB, Cond); |
Jim Grosbach | ca215e7 | 2009-11-16 17:10:56 +0000 | [diff] [blame] | 1919 | |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1920 | // If the block ends in an unconditional branch, move it. The prior block |
| 1921 | // has to have an analyzable terminator for us to move this one. Be paranoid |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1922 | // and make sure we're not trying to move the entry block of the function. |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1923 | if (!B && Cond.empty() && BB != MF.begin() && |
| 1924 | !TII->AnalyzeBranch(*OldPrior, TBB, FBB, CondPrior)) { |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1925 | BB->moveAfter(JTBB); |
| 1926 | OldPrior->updateTerminator(); |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1927 | BB->updateTerminator(); |
Jim Grosbach | 08cbda5 | 2009-11-16 18:58:52 +0000 | [diff] [blame] | 1928 | // Update numbering to account for the block being moved. |
Jim Grosbach | a0a95a3 | 2009-11-17 01:21:04 +0000 | [diff] [blame] | 1929 | MF.RenumberBlocks(); |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1930 | ++NumJTMoved; |
| 1931 | return NULL; |
| 1932 | } |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1933 | |
| 1934 | // Create a new MBB for the code after the jump BB. |
| 1935 | MachineBasicBlock *NewBB = |
| 1936 | MF.CreateMachineBasicBlock(JTBB->getBasicBlock()); |
| 1937 | MachineFunction::iterator MBBI = JTBB; ++MBBI; |
| 1938 | MF.insert(MBBI, NewBB); |
| 1939 | |
| 1940 | // Add an unconditional branch from NewBB to BB. |
| 1941 | // There doesn't seem to be meaningful DebugInfo available; this doesn't |
| 1942 | // correspond directly to anything in the source. |
| 1943 | assert (isThumb2 && "Adjusting for TB[BH] but not in Thumb2?"); |
Owen Anderson | 51f6a7a | 2011-09-09 21:48:23 +0000 | [diff] [blame] | 1944 | BuildMI(NewBB, DebugLoc(), TII->get(ARM::t2B)).addMBB(BB) |
| 1945 | .addImm(ARMCC::AL).addReg(0); |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1946 | |
Jim Grosbach | 00a6a1f | 2009-11-14 20:10:18 +0000 | [diff] [blame] | 1947 | // Update internal data structures to account for the newly inserted MBB. |
| 1948 | MF.RenumberBlocks(NewBB); |
| 1949 | |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1950 | // Update the CFG. |
| 1951 | NewBB->addSuccessor(BB); |
| 1952 | JTBB->removeSuccessor(BB); |
| 1953 | JTBB->addSuccessor(NewBB); |
| 1954 | |
Jim Grosbach | 80697d1 | 2009-11-12 17:25:07 +0000 | [diff] [blame] | 1955 | ++NumJTInserted; |
Jim Grosbach | 1fc7d71 | 2009-11-11 02:47:19 +0000 | [diff] [blame] | 1956 | return NewBB; |
| 1957 | } |