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