blob: b0744d29c600f1e5115f4b42615ce7d5902140dd [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Chenga8e29892007-01-19 07:51:42 +00007//
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 Chengd3d9d662009-07-23 18:27:47 +000018#include "ARMAddressingModes.h"
Evan Chengaf5cbcb2007-01-25 03:12:46 +000019#include "ARMMachineFunctionInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000020#include "ARMInstrInfo.h"
21#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga8e29892007-01-19 07:51:42 +000024#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Evan Chengc99ef082007-02-09 20:54:44 +000029#include "llvm/ADT/SmallVector.h"
Evan Chenga8e29892007-01-19 07:51:42 +000030#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/Statistic.h"
Evan Chenga8e29892007-01-19 07:51:42 +000032using namespace llvm;
33
Evan Chengc99ef082007-02-09 20:54:44 +000034STATISTIC(NumCPEs, "Number of constpool entries");
Evan Chengd1b2c1e2007-01-30 01:18:38 +000035STATISTIC(NumSplit, "Number of uncond branches inserted");
36STATISTIC(NumCBrFixed, "Number of cond branches fixed");
37STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
Evan Chenga8e29892007-01-19 07:51:42 +000038
39namespace {
Dale Johannesen88e37ae2007-02-23 05:02:36 +000040 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
Evan Chenga8e29892007-01-19 07:51:42 +000041 /// requires constant pool entries to be scattered among the instructions
42 /// inside a function. To do this, it completely ignores the normal LLVM
Dale Johannesen88e37ae2007-02-23 05:02:36 +000043 /// constant pool; instead, it places constants wherever it feels like with
Evan Chenga8e29892007-01-19 07:51:42 +000044 /// special instructions.
45 ///
46 /// The terminology used in this pass includes:
47 /// Islands - Clumps of constants placed in the function.
48 /// Water - Potential places where an island could be formed.
49 /// CPE - A constant pool entry that has been placed somewhere, which
50 /// tracks a list of users.
51 class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass {
Evan Chenga8e29892007-01-19 07:51:42 +000052 /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
Dale Johannesen8593e412007-04-29 19:19:30 +000053 /// by MBB Number. The two-byte pads required for Thumb alignment are
54 /// counted as part of the following block (i.e., the offset and size for
55 /// a padded block will both be ==2 mod 4).
Evan Chenge03cff62007-02-09 23:59:14 +000056 std::vector<unsigned> BBSizes;
Bob Wilson84945262009-05-12 17:09:30 +000057
Dale Johannesen99c49a42007-02-25 00:47:03 +000058 /// BBOffsets - the offset of each MBB in bytes, starting from 0.
Dale Johannesen8593e412007-04-29 19:19:30 +000059 /// The two-byte pads required for Thumb alignment are counted as part of
60 /// the following block.
Dale Johannesen99c49a42007-02-25 00:47:03 +000061 std::vector<unsigned> BBOffsets;
62
Evan Chenga8e29892007-01-19 07:51:42 +000063 /// WaterList - A sorted list of basic blocks where islands could be placed
64 /// (i.e. blocks that don't fall through to the following block, due
65 /// to a return, unreachable, or unconditional branch).
Evan Chenge03cff62007-02-09 23:59:14 +000066 std::vector<MachineBasicBlock*> WaterList;
Evan Chengc99ef082007-02-09 20:54:44 +000067
Evan Chenga8e29892007-01-19 07:51:42 +000068 /// CPUser - One user of a constant pool, keeping the machine instruction
69 /// pointer, the constant pool being referenced, and the max displacement
70 /// allowed from the instruction to the CP.
71 struct CPUser {
72 MachineInstr *MI;
73 MachineInstr *CPEMI;
74 unsigned MaxDisp;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +000075 bool NegOk;
Evan Chengd3d9d662009-07-23 18:27:47 +000076 bool IsSoImm;
77 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
78 bool neg, bool soimm)
79 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) {}
Evan Chenga8e29892007-01-19 07:51:42 +000080 };
Bob Wilson84945262009-05-12 17:09:30 +000081
Evan Chenga8e29892007-01-19 07:51:42 +000082 /// CPUsers - Keep track of all of the machine instructions that use various
83 /// constant pools and their max displacement.
Evan Chenge03cff62007-02-09 23:59:14 +000084 std::vector<CPUser> CPUsers;
Bob Wilson84945262009-05-12 17:09:30 +000085
Evan Chengc99ef082007-02-09 20:54:44 +000086 /// CPEntry - One per constant pool entry, keeping the machine instruction
87 /// pointer, the constpool index, and the number of CPUser's which
88 /// reference this entry.
89 struct CPEntry {
90 MachineInstr *CPEMI;
91 unsigned CPI;
92 unsigned RefCount;
93 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
94 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
95 };
96
97 /// CPEntries - Keep track of all of the constant pool entry machine
Dale Johannesen88e37ae2007-02-23 05:02:36 +000098 /// instructions. For each original constpool index (i.e. those that
99 /// existed upon entry to this pass), it keeps a vector of entries.
100 /// Original elements are cloned as we go along; the clones are
101 /// put in the vector of the original element, but have distinct CPIs.
Evan Chengc99ef082007-02-09 20:54:44 +0000102 std::vector<std::vector<CPEntry> > CPEntries;
Bob Wilson84945262009-05-12 17:09:30 +0000103
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000104 /// ImmBranch - One per immediate branch, keeping the machine instruction
105 /// pointer, conditional or unconditional, the max displacement,
106 /// and (if isCond is true) the corresponding unconditional branch
107 /// opcode.
108 struct ImmBranch {
109 MachineInstr *MI;
Evan Chengc2854142007-01-25 23:18:59 +0000110 unsigned MaxDisp : 31;
111 bool isCond : 1;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000112 int UncondBr;
Evan Chengc2854142007-01-25 23:18:59 +0000113 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
114 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000115 };
116
Evan Cheng2706f972007-05-16 05:14:06 +0000117 /// ImmBranches - Keep track of all the immediate branch instructions.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000118 ///
Evan Chenge03cff62007-02-09 23:59:14 +0000119 std::vector<ImmBranch> ImmBranches;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000120
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000121 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
122 ///
Evan Chengc99ef082007-02-09 20:54:44 +0000123 SmallVector<MachineInstr*, 4> PushPopMIs;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000124
125 /// HasFarJump - True if any far jump instruction has been emitted during
126 /// the branch fix up pass.
127 bool HasFarJump;
128
Evan Chenga8e29892007-01-19 07:51:42 +0000129 const TargetInstrInfo *TII;
Dale Johannesen8593e412007-04-29 19:19:30 +0000130 ARMFunctionInfo *AFI;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000131 bool isThumb;
Evan Chengd3d9d662009-07-23 18:27:47 +0000132 bool isThumb1;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000133 bool isThumb2;
Evan Chenga8e29892007-01-19 07:51:42 +0000134 public:
Devang Patel19974732007-05-03 01:11:54 +0000135 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +0000136 ARMConstantIslands() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000137
Evan Chenga8e29892007-01-19 07:51:42 +0000138 virtual bool runOnMachineFunction(MachineFunction &Fn);
139
140 virtual const char *getPassName() const {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000141 return "ARM constant island placement and branch shortening pass";
Evan Chenga8e29892007-01-19 07:51:42 +0000142 }
Bob Wilson84945262009-05-12 17:09:30 +0000143
Evan Chenga8e29892007-01-19 07:51:42 +0000144 private:
145 void DoInitialPlacement(MachineFunction &Fn,
Evan Chenge03cff62007-02-09 23:59:14 +0000146 std::vector<MachineInstr*> &CPEMIs);
Evan Chengc99ef082007-02-09 20:54:44 +0000147 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Evan Chenga8e29892007-01-19 07:51:42 +0000148 void InitialFunctionScan(MachineFunction &Fn,
Evan Chenge03cff62007-02-09 23:59:14 +0000149 const std::vector<MachineInstr*> &CPEMIs);
Evan Cheng0c615842007-01-31 02:22:22 +0000150 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
Evan Chenga8e29892007-01-19 07:51:42 +0000151 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000152 void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta);
Evan Chenged884f32007-04-03 23:39:48 +0000153 bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000154 int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
Bob Wilson84945262009-05-12 17:09:30 +0000155 bool LookForWater(CPUser&U, unsigned UserOffset,
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000156 MachineBasicBlock** NewMBB);
Bob Wilson84945262009-05-12 17:09:30 +0000157 MachineBasicBlock* AcceptWater(MachineBasicBlock *WaterBB,
Dale Johannesen8593e412007-04-29 19:19:30 +0000158 std::vector<MachineBasicBlock*>::iterator IP);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000159 void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset,
160 MachineBasicBlock** NewMBB);
Dale Johannesenf1b214d2007-02-28 18:41:23 +0000161 bool HandleConstantPoolUser(MachineFunction &Fn, unsigned CPUserIndex);
Evan Chenged884f32007-04-03 23:39:48 +0000162 void RemoveDeadCPEMI(MachineInstr *CPEMI);
163 bool RemoveUnusedCPEntries();
Bob Wilson84945262009-05-12 17:09:30 +0000164 bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000165 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
166 bool DoDump = false);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000167 bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water,
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000168 CPUser &U);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000169 bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000170 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
Evan Chengc0dbec72007-01-31 19:57:44 +0000171 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000172 bool FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br);
173 bool FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br);
174 bool FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br);
175 bool UndoLRSpillRestore();
Evan Chenga8e29892007-01-19 07:51:42 +0000176
Evan Chenga8e29892007-01-19 07:51:42 +0000177 unsigned GetOffsetOf(MachineInstr *MI) const;
Dale Johannesen8593e412007-04-29 19:19:30 +0000178 void dumpBBs();
179 void verify(MachineFunction &Fn);
Evan Chenga8e29892007-01-19 07:51:42 +0000180 };
Devang Patel19974732007-05-03 01:11:54 +0000181 char ARMConstantIslands::ID = 0;
Evan Chenga8e29892007-01-19 07:51:42 +0000182}
183
Dale Johannesen8593e412007-04-29 19:19:30 +0000184/// verify - check BBOffsets, BBSizes, alignment of islands
185void ARMConstantIslands::verify(MachineFunction &Fn) {
186 assert(BBOffsets.size() == BBSizes.size());
187 for (unsigned i = 1, e = BBOffsets.size(); i != e; ++i)
188 assert(BBOffsets[i-1]+BBSizes[i-1] == BBOffsets[i]);
Evan Chengd3d9d662009-07-23 18:27:47 +0000189 if (!isThumb)
190 return;
191#ifndef NDEBUG
192 for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
193 MBBI != E; ++MBBI) {
194 MachineBasicBlock *MBB = MBBI;
195 if (!MBB->empty() &&
196 MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
197 unsigned MBBId = MBB->getNumber();
198 assert((BBOffsets[MBBId]%4 == 0 && BBSizes[MBBId]%4 == 0) ||
199 (BBOffsets[MBBId]%4 != 0 && BBSizes[MBBId]%4 != 0));
Dale Johannesen8593e412007-04-29 19:19:30 +0000200 }
201 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000202#endif
Dale Johannesen8593e412007-04-29 19:19:30 +0000203}
204
205/// print block size and offset information - debugging
206void ARMConstantIslands::dumpBBs() {
207 for (unsigned J = 0, E = BBOffsets.size(); J !=E; ++J) {
Bob Wilson84945262009-05-12 17:09:30 +0000208 DOUT << "block " << J << " offset " << BBOffsets[J] <<
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000209 " size " << BBSizes[J] << "\n";
Dale Johannesen8593e412007-04-29 19:19:30 +0000210 }
211}
212
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000213/// createARMConstantIslandPass - returns an instance of the constpool
214/// island pass.
Evan Chenga8e29892007-01-19 07:51:42 +0000215FunctionPass *llvm::createARMConstantIslandPass() {
216 return new ARMConstantIslands();
217}
218
219bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) {
Evan Chenga8e29892007-01-19 07:51:42 +0000220 MachineConstantPool &MCP = *Fn.getConstantPool();
Bob Wilson84945262009-05-12 17:09:30 +0000221
Evan Chenga8e29892007-01-19 07:51:42 +0000222 TII = Fn.getTarget().getInstrInfo();
Dale Johannesen8593e412007-04-29 19:19:30 +0000223 AFI = Fn.getInfo<ARMFunctionInfo>();
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000224 isThumb = AFI->isThumbFunction();
Evan Chengd3d9d662009-07-23 18:27:47 +0000225 isThumb1 = AFI->isThumb1OnlyFunction();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000226 isThumb2 = AFI->isThumb2Function();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000227
228 HasFarJump = false;
229
Evan Chenga8e29892007-01-19 07:51:42 +0000230 // Renumber all of the machine basic blocks in the function, guaranteeing that
231 // the numbers agree with the position of the block in the function.
232 Fn.RenumberBlocks();
233
Evan Chengd3d9d662009-07-23 18:27:47 +0000234 // Thumb1 functions containing constant pools get 2-byte alignment.
235 // This is so we can keep exact track of where the alignment padding goes.
236
237 // Set default. Thumb1 function is 1-byte aligned, ARM and Thumb2 are 2-byte
238 // aligned.
239 AFI->setAlign(isThumb1 ? 1U : 2U);
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000240
Evan Chenga8e29892007-01-19 07:51:42 +0000241 // Perform the initial placement of the constant pool entries. To start with,
242 // we put them all at the end of the function.
Evan Chenge03cff62007-02-09 23:59:14 +0000243 std::vector<MachineInstr*> CPEMIs;
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000244 if (!MCP.isEmpty()) {
Evan Cheng7755fac2007-01-26 01:04:44 +0000245 DoInitialPlacement(Fn, CPEMIs);
Evan Chengd3d9d662009-07-23 18:27:47 +0000246 if (isThumb1)
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000247 AFI->setAlign(2U);
248 }
Bob Wilson84945262009-05-12 17:09:30 +0000249
Evan Chenga8e29892007-01-19 07:51:42 +0000250 /// The next UID to take is the first unused one.
Evan Chengf1bbb952008-11-08 00:51:41 +0000251 AFI->initConstPoolEntryUId(CPEMIs.size());
Bob Wilson84945262009-05-12 17:09:30 +0000252
Evan Chenga8e29892007-01-19 07:51:42 +0000253 // Do the initial scan of the function, building up information about the
254 // sizes of each block, the location of all the water, and finding all of the
255 // constant pool users.
256 InitialFunctionScan(Fn, CPEMIs);
257 CPEMIs.clear();
Bob Wilson84945262009-05-12 17:09:30 +0000258
Evan Chenged884f32007-04-03 23:39:48 +0000259 /// Remove dead constant pool entries.
260 RemoveUnusedCPEntries();
261
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000262 // Iteratively place constant pool entries and fix up branches until there
263 // is no change.
264 bool MadeChange = false;
265 while (true) {
266 bool Change = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000267 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
Dale Johannesenf1b214d2007-02-28 18:41:23 +0000268 Change |= HandleConstantPoolUser(Fn, i);
Evan Cheng82020102007-07-10 22:00:16 +0000269 DEBUG(dumpBBs());
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000270 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000271 Change |= FixUpImmediateBr(Fn, ImmBranches[i]);
Evan Cheng82020102007-07-10 22:00:16 +0000272 DEBUG(dumpBBs());
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000273 if (!Change)
274 break;
275 MadeChange = true;
276 }
Evan Chenged884f32007-04-03 23:39:48 +0000277
Dale Johannesen8593e412007-04-29 19:19:30 +0000278 // After a while, this might be made debug-only, but it is not expensive.
279 verify(Fn);
280
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000281 // If LR has been forced spilled and no far jumps (i.e. BL) has been issued.
282 // Undo the spill / restore of LR if possible.
Evan Chengf49407b2007-03-01 08:26:31 +0000283 if (!HasFarJump && AFI->isLRSpilledForFarJump() && isThumb)
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000284 MadeChange |= UndoLRSpillRestore();
285
Evan Chenga8e29892007-01-19 07:51:42 +0000286 BBSizes.clear();
Dale Johannesen99c49a42007-02-25 00:47:03 +0000287 BBOffsets.clear();
Evan Chenga8e29892007-01-19 07:51:42 +0000288 WaterList.clear();
289 CPUsers.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000290 CPEntries.clear();
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000291 ImmBranches.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000292 PushPopMIs.clear();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000293
294 return MadeChange;
Evan Chenga8e29892007-01-19 07:51:42 +0000295}
296
297/// DoInitialPlacement - Perform the initial placement of the constant pool
298/// entries. To start with, we put them all at the end of the function.
299void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn,
Bob Wilson84945262009-05-12 17:09:30 +0000300 std::vector<MachineInstr*> &CPEMIs) {
Evan Chenga8e29892007-01-19 07:51:42 +0000301 // Create the basic block to hold the CPE's.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000302 MachineBasicBlock *BB = Fn.CreateMachineBasicBlock();
303 Fn.push_back(BB);
Bob Wilson84945262009-05-12 17:09:30 +0000304
Evan Chenga8e29892007-01-19 07:51:42 +0000305 // Add all of the constants from the constant pool to the end block, use an
306 // identity mapping of CPI's to CPE's.
307 const std::vector<MachineConstantPoolEntry> &CPs =
308 Fn.getConstantPool()->getConstants();
Bob Wilson84945262009-05-12 17:09:30 +0000309
Evan Chenga8e29892007-01-19 07:51:42 +0000310 const TargetData &TD = *Fn.getTarget().getTargetData();
311 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
Duncan Sands777d2302009-05-09 07:06:46 +0000312 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
Evan Chenga8e29892007-01-19 07:51:42 +0000313 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
314 // we would have to pad them out or something so that instructions stay
315 // aligned.
316 assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
317 MachineInstr *CPEMI =
Dale Johannesenb6728402009-02-13 02:25:56 +0000318 BuildMI(BB, DebugLoc::getUnknownLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
Evan Chenga8e29892007-01-19 07:51:42 +0000319 .addImm(i).addConstantPoolIndex(i).addImm(Size);
320 CPEMIs.push_back(CPEMI);
Evan Chengc99ef082007-02-09 20:54:44 +0000321
322 // Add a new CPEntry, but no corresponding CPUser yet.
323 std::vector<CPEntry> CPEs;
324 CPEs.push_back(CPEntry(CPEMI, i));
325 CPEntries.push_back(CPEs);
326 NumCPEs++;
327 DOUT << "Moved CPI#" << i << " to end of function as #" << i << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000328 }
329}
330
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000331/// BBHasFallthrough - Return true if the specified basic block can fallthrough
Evan Chenga8e29892007-01-19 07:51:42 +0000332/// into the block immediately after it.
333static bool BBHasFallthrough(MachineBasicBlock *MBB) {
334 // Get the next machine basic block in the function.
335 MachineFunction::iterator MBBI = MBB;
336 if (next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function.
337 return false;
Bob Wilson84945262009-05-12 17:09:30 +0000338
Evan Chenga8e29892007-01-19 07:51:42 +0000339 MachineBasicBlock *NextBB = next(MBBI);
340 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
341 E = MBB->succ_end(); I != E; ++I)
342 if (*I == NextBB)
343 return true;
Bob Wilson84945262009-05-12 17:09:30 +0000344
Evan Chenga8e29892007-01-19 07:51:42 +0000345 return false;
346}
347
Evan Chengc99ef082007-02-09 20:54:44 +0000348/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
349/// look up the corresponding CPEntry.
350ARMConstantIslands::CPEntry
351*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
352 const MachineInstr *CPEMI) {
353 std::vector<CPEntry> &CPEs = CPEntries[CPI];
354 // Number of entries per constpool index should be small, just do a
355 // linear search.
356 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
357 if (CPEs[i].CPEMI == CPEMI)
358 return &CPEs[i];
359 }
360 return NULL;
361}
362
Evan Chenga8e29892007-01-19 07:51:42 +0000363/// InitialFunctionScan - Do the initial scan of the function, building up
364/// information about the sizes of each block, the location of all the water,
365/// and finding all of the constant pool users.
366void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn,
Evan Chenge03cff62007-02-09 23:59:14 +0000367 const std::vector<MachineInstr*> &CPEMIs) {
Dale Johannesen99c49a42007-02-25 00:47:03 +0000368 unsigned Offset = 0;
Evan Chenga8e29892007-01-19 07:51:42 +0000369 for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
370 MBBI != E; ++MBBI) {
371 MachineBasicBlock &MBB = *MBBI;
Bob Wilson84945262009-05-12 17:09:30 +0000372
Evan Chenga8e29892007-01-19 07:51:42 +0000373 // If this block doesn't fall through into the next MBB, then this is
374 // 'water' that a constant pool island could be placed.
375 if (!BBHasFallthrough(&MBB))
376 WaterList.push_back(&MBB);
Bob Wilson84945262009-05-12 17:09:30 +0000377
Evan Chenga8e29892007-01-19 07:51:42 +0000378 unsigned MBBSize = 0;
379 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
380 I != E; ++I) {
381 // Add instruction size to MBBSize.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000382 MBBSize += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000383
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000384 int Opc = I->getOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000385 if (I->getDesc().isBranch()) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000386 bool isCond = false;
387 unsigned Bits = 0;
388 unsigned Scale = 1;
389 int UOpc = Opc;
390 switch (Opc) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000391 case ARM::tBR_JTr:
David Goodwin5e47a9a2009-06-30 18:04:13 +0000392 case ARM::t2BR_JTr:
David Goodwinc9a59b52009-06-30 19:50:22 +0000393 case ARM::t2BR_JTm:
394 case ARM::t2BR_JTadd:
Dale Johannesen8593e412007-04-29 19:19:30 +0000395 // A Thumb table jump may involve padding; for the offsets to
396 // be right, functions containing these must be 4-byte aligned.
397 AFI->setAlign(2U);
398 if ((Offset+MBBSize)%4 != 0)
399 MBBSize += 2; // padding
400 continue; // Does not get an entry in ImmBranches
Evan Cheng743fa032007-01-25 19:43:52 +0000401 default:
Dale Johannesen8593e412007-04-29 19:19:30 +0000402 continue; // Ignore other JT branches
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000403 case ARM::Bcc:
404 isCond = true;
405 UOpc = ARM::B;
406 // Fallthrough
407 case ARM::B:
408 Bits = 24;
409 Scale = 4;
410 break;
411 case ARM::tBcc:
412 isCond = true;
413 UOpc = ARM::tB;
414 Bits = 8;
415 Scale = 2;
416 break;
417 case ARM::tB:
418 Bits = 11;
419 Scale = 2;
420 break;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000421 case ARM::t2Bcc:
422 isCond = true;
423 UOpc = ARM::t2B;
424 Bits = 20;
425 Scale = 2;
426 break;
427 case ARM::t2B:
428 Bits = 24;
429 Scale = 2;
430 break;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000431 }
Evan Chengb43216e2007-02-01 10:16:15 +0000432
433 // Record this immediate branch.
Evan Chengbd5d3db2007-02-03 02:08:34 +0000434 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Evan Chengb43216e2007-02-01 10:16:15 +0000435 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000436 }
437
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000438 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
439 PushPopMIs.push_back(I);
440
Evan Chengd3d9d662009-07-23 18:27:47 +0000441 if (Opc == ARM::CONSTPOOL_ENTRY)
442 continue;
443
Evan Chenga8e29892007-01-19 07:51:42 +0000444 // Scan the instructions for constant pool operands.
445 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Dan Gohmand735b802008-10-03 15:45:36 +0000446 if (I->getOperand(op).isCPI()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000447 // We found one. The addressing mode tells us the max displacement
448 // from the PC that this instruction permits.
Bob Wilson84945262009-05-12 17:09:30 +0000449
Evan Chenga8e29892007-01-19 07:51:42 +0000450 // Basic size info comes from the TSFlags field.
Evan Chengb43216e2007-02-01 10:16:15 +0000451 unsigned Bits = 0;
452 unsigned Scale = 1;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000453 bool NegOk = false;
Evan Chengd3d9d662009-07-23 18:27:47 +0000454 bool IsSoImm = false;
455
456 switch (Opc) {
Bob Wilson84945262009-05-12 17:09:30 +0000457 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000458 llvm_unreachable("Unknown addressing mode for CP reference!");
Evan Chengd3d9d662009-07-23 18:27:47 +0000459 break;
460
461 // Taking the address of a CP entry.
462 case ARM::LEApcrel:
463 // This takes a SoImm, which is 8 bit immediate rotated. We'll
464 // pretend the maximum offset is 255 * 4. Since each instruction
465 // 4 byte wide, this is always correct. We'llc heck for other
466 // displacements that fits in a SoImm as well.
Evan Chengb43216e2007-02-01 10:16:15 +0000467 Bits = 8;
Evan Chengd3d9d662009-07-23 18:27:47 +0000468 Scale = 4;
469 NegOk = true;
470 IsSoImm = true;
471 break;
472 case ARM::t2LEApcrel:
473 Bits = 12;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000474 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000475 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000476 case ARM::tLEApcrel:
477 Bits = 8;
478 Scale = 4;
479 break;
480
481 case ARM::LDR:
482 case ARM::LDRcp:
483 case ARM::t2LDRpci:
Evan Cheng556f33c2007-02-01 20:44:52 +0000484 Bits = 12; // +-offset_12
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000485 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000486 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000487
488 case ARM::tLDRpci:
489 case ARM::tLDRcp:
Evan Chengb43216e2007-02-01 10:16:15 +0000490 Bits = 8;
491 Scale = 4; // +(offset_8*4)
Evan Cheng012f2d92007-01-24 08:53:17 +0000492 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000493
494 case ARM::FLDD:
495 case ARM::FLDS:
496 Bits = 8;
497 Scale = 4; // +-(offset_8*4)
498 NegOk = true;
Evan Cheng055b0312009-06-29 07:51:04 +0000499 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000500 }
Evan Chengb43216e2007-02-01 10:16:15 +0000501
Evan Chenga8e29892007-01-19 07:51:42 +0000502 // Remember that this is a user of a CP entry.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000503 unsigned CPI = I->getOperand(op).getIndex();
Evan Chengc99ef082007-02-09 20:54:44 +0000504 MachineInstr *CPEMI = CPEMIs[CPI];
Bob Wilson84945262009-05-12 17:09:30 +0000505 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
Evan Chengd3d9d662009-07-23 18:27:47 +0000506 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
Evan Chengc99ef082007-02-09 20:54:44 +0000507
508 // Increment corresponding CPEntry reference count.
509 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
510 assert(CPE && "Cannot find a corresponding CPEntry!");
511 CPE->RefCount++;
Bob Wilson84945262009-05-12 17:09:30 +0000512
Evan Chenga8e29892007-01-19 07:51:42 +0000513 // Instructions can only use one CP entry, don't bother scanning the
514 // rest of the operands.
515 break;
516 }
517 }
Evan Cheng2021abe2007-02-01 01:09:47 +0000518
Dale Johannesen8593e412007-04-29 19:19:30 +0000519 // In thumb mode, if this block is a constpool island, we may need padding
520 // so it's aligned on 4 byte boundary.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000521 if (isThumb &&
Evan Cheng05cc4242007-02-02 19:09:19 +0000522 !MBB.empty() &&
Dale Johannesen8593e412007-04-29 19:19:30 +0000523 MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY &&
524 (Offset%4) != 0)
Evan Cheng2021abe2007-02-01 01:09:47 +0000525 MBBSize += 2;
526
Evan Chenga8e29892007-01-19 07:51:42 +0000527 BBSizes.push_back(MBBSize);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000528 BBOffsets.push_back(Offset);
529 Offset += MBBSize;
Evan Chenga8e29892007-01-19 07:51:42 +0000530 }
531}
532
Evan Chenga8e29892007-01-19 07:51:42 +0000533/// GetOffsetOf - Return the current offset of the specified machine instruction
534/// from the start of the function. This offset changes as stuff is moved
535/// around inside the function.
536unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
537 MachineBasicBlock *MBB = MI->getParent();
Bob Wilson84945262009-05-12 17:09:30 +0000538
Evan Chenga8e29892007-01-19 07:51:42 +0000539 // The offset is composed of two things: the sum of the sizes of all MBB's
540 // before this instruction's block, and the offset from the start of the block
541 // it is in.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000542 unsigned Offset = BBOffsets[MBB->getNumber()];
Evan Chenga8e29892007-01-19 07:51:42 +0000543
Dale Johannesen8593e412007-04-29 19:19:30 +0000544 // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has
545 // alignment padding, and compensate if so.
Bob Wilson84945262009-05-12 17:09:30 +0000546 if (isThumb &&
547 MI->getOpcode() == ARM::CONSTPOOL_ENTRY &&
Dale Johannesen8593e412007-04-29 19:19:30 +0000548 Offset%4 != 0)
549 Offset += 2;
550
Evan Chenga8e29892007-01-19 07:51:42 +0000551 // Sum instructions before MI in MBB.
552 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
553 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
554 if (&*I == MI) return Offset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000555 Offset += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000556 }
557}
558
559/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
560/// ID.
561static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
562 const MachineBasicBlock *RHS) {
563 return LHS->getNumber() < RHS->getNumber();
564}
565
566/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
567/// machine function, it upsets all of the block numbers. Renumber the blocks
568/// and update the arrays that parallel this numbering.
569void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
570 // Renumber the MBB's to keep them consequtive.
571 NewBB->getParent()->RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000572
Evan Chenga8e29892007-01-19 07:51:42 +0000573 // Insert a size into BBSizes to align it properly with the (newly
574 // renumbered) block numbers.
575 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000576
577 // Likewise for BBOffsets.
578 BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0);
Bob Wilson84945262009-05-12 17:09:30 +0000579
580 // Next, update WaterList. Specifically, we need to add NewMBB as having
Evan Chenga8e29892007-01-19 07:51:42 +0000581 // available water after it.
Evan Chenge03cff62007-02-09 23:59:14 +0000582 std::vector<MachineBasicBlock*>::iterator IP =
Evan Chenga8e29892007-01-19 07:51:42 +0000583 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
584 CompareMBBNumbers);
585 WaterList.insert(IP, NewBB);
586}
587
588
589/// Split the basic block containing MI into two blocks, which are joined by
590/// an unconditional branch. Update datastructures and renumber blocks to
Evan Cheng0c615842007-01-31 02:22:22 +0000591/// account for this change and returns the newly created block.
592MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
Evan Chenga8e29892007-01-19 07:51:42 +0000593 MachineBasicBlock *OrigBB = MI->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000594 MachineFunction &MF = *OrigBB->getParent();
Evan Chenga8e29892007-01-19 07:51:42 +0000595
596 // Create a new MBB for the code after the OrigBB.
Bob Wilson84945262009-05-12 17:09:30 +0000597 MachineBasicBlock *NewBB =
598 MF.CreateMachineBasicBlock(OrigBB->getBasicBlock());
Evan Chenga8e29892007-01-19 07:51:42 +0000599 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000600 MF.insert(MBBI, NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000601
Evan Chenga8e29892007-01-19 07:51:42 +0000602 // Splice the instructions starting with MI over to NewBB.
603 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
Bob Wilson84945262009-05-12 17:09:30 +0000604
Evan Chenga8e29892007-01-19 07:51:42 +0000605 // Add an unconditional branch from OrigBB to NewBB.
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000606 // Note the new unconditional branch is not being recorded.
Dale Johannesenb6728402009-02-13 02:25:56 +0000607 // There doesn't seem to be meaningful DebugInfo available; this doesn't
608 // correspond to anything in the source.
Evan Cheng58541fd2009-07-07 01:16:41 +0000609 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
610 BuildMI(OrigBB, DebugLoc::getUnknownLoc(), TII->get(Opc)).addMBB(NewBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000611 NumSplit++;
Bob Wilson84945262009-05-12 17:09:30 +0000612
Evan Chenga8e29892007-01-19 07:51:42 +0000613 // Update the CFG. All succs of OrigBB are now succs of NewBB.
614 while (!OrigBB->succ_empty()) {
615 MachineBasicBlock *Succ = *OrigBB->succ_begin();
616 OrigBB->removeSuccessor(Succ);
617 NewBB->addSuccessor(Succ);
Bob Wilson84945262009-05-12 17:09:30 +0000618
Evan Chenga8e29892007-01-19 07:51:42 +0000619 // This pass should be run after register allocation, so there should be no
620 // PHI nodes to update.
621 assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI)
622 && "PHI nodes should be eliminated by now!");
623 }
Bob Wilson84945262009-05-12 17:09:30 +0000624
Evan Chenga8e29892007-01-19 07:51:42 +0000625 // OrigBB branches to NewBB.
626 OrigBB->addSuccessor(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000627
Evan Chenga8e29892007-01-19 07:51:42 +0000628 // Update internal data structures to account for the newly inserted MBB.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000629 // This is almost the same as UpdateForInsertedWaterBlock, except that
630 // the Water goes after OrigBB, not NewBB.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000631 MF.RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000632
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000633 // Insert a size into BBSizes to align it properly with the (newly
634 // renumbered) block numbers.
635 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
Bob Wilson84945262009-05-12 17:09:30 +0000636
Dale Johannesen99c49a42007-02-25 00:47:03 +0000637 // Likewise for BBOffsets.
638 BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0);
639
Bob Wilson84945262009-05-12 17:09:30 +0000640 // Next, update WaterList. Specifically, we need to add OrigMBB as having
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000641 // available water after it (but not if it's already there, which happens
642 // when splitting before a conditional branch that is followed by an
643 // unconditional branch - in that case we want to insert NewBB).
644 std::vector<MachineBasicBlock*>::iterator IP =
645 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
646 CompareMBBNumbers);
647 MachineBasicBlock* WaterBB = *IP;
648 if (WaterBB == OrigBB)
649 WaterList.insert(next(IP), NewBB);
650 else
651 WaterList.insert(IP, OrigBB);
652
Dale Johannesen8593e412007-04-29 19:19:30 +0000653 // Figure out how large the first NewMBB is. (It cannot
654 // contain a constpool_entry or tablejump.)
Evan Chenga8e29892007-01-19 07:51:42 +0000655 unsigned NewBBSize = 0;
656 for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
657 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000658 NewBBSize += TII->GetInstSizeInBytes(I);
Bob Wilson84945262009-05-12 17:09:30 +0000659
Dale Johannesen99c49a42007-02-25 00:47:03 +0000660 unsigned OrigBBI = OrigBB->getNumber();
661 unsigned NewBBI = NewBB->getNumber();
Evan Chenga8e29892007-01-19 07:51:42 +0000662 // Set the size of NewBB in BBSizes.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000663 BBSizes[NewBBI] = NewBBSize;
Bob Wilson84945262009-05-12 17:09:30 +0000664
Evan Chenga8e29892007-01-19 07:51:42 +0000665 // We removed instructions from UserMBB, subtract that off from its size.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000666 // Add 2 or 4 to the block to count the unconditional branch we added to it.
Evan Chengd3d9d662009-07-23 18:27:47 +0000667 unsigned delta = isThumb1 ? 2 : 4;
Dale Johannesen99c49a42007-02-25 00:47:03 +0000668 BBSizes[OrigBBI] -= NewBBSize - delta;
669
670 // ...and adjust BBOffsets for NewBB accordingly.
671 BBOffsets[NewBBI] = BBOffsets[OrigBBI] + BBSizes[OrigBBI];
672
673 // All BBOffsets following these blocks must be modified.
674 AdjustBBOffsetsAfter(NewBB, delta);
Evan Cheng0c615842007-01-31 02:22:22 +0000675
676 return NewBB;
Evan Chenga8e29892007-01-19 07:51:42 +0000677}
678
Dale Johannesen8593e412007-04-29 19:19:30 +0000679/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
Bob Wilson84945262009-05-12 17:09:30 +0000680/// reference) is within MaxDisp of TrialOffset (a proposed location of a
Dale Johannesen8593e412007-04-29 19:19:30 +0000681/// constant pool entry).
Bob Wilson84945262009-05-12 17:09:30 +0000682bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000683 unsigned TrialOffset, unsigned MaxDisp,
684 bool NegativeOK, bool IsSoImm) {
Bob Wilson84945262009-05-12 17:09:30 +0000685 // On Thumb offsets==2 mod 4 are rounded down by the hardware for
686 // purposes of the displacement computation; compensate for that here.
Dale Johannesen8593e412007-04-29 19:19:30 +0000687 // Effectively, the valid range of displacements is 2 bytes smaller for such
688 // references.
689 if (isThumb && UserOffset%4 !=0)
690 UserOffset -= 2;
691 // CPEs will be rounded up to a multiple of 4.
692 if (isThumb && TrialOffset%4 != 0)
693 TrialOffset += 2;
694
Dale Johannesen99c49a42007-02-25 00:47:03 +0000695 if (UserOffset <= TrialOffset) {
696 // User before the Trial.
Evan Chengd3d9d662009-07-23 18:27:47 +0000697 if (TrialOffset - UserOffset <= MaxDisp)
698 return true;
699 if (IsSoImm && ARM_AM::getSOImmVal(TrialOffset - UserOffset) != -1)
Dale Johannesen99c49a42007-02-25 00:47:03 +0000700 return true;
701 } else if (NegativeOK) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000702 if (UserOffset - TrialOffset <= MaxDisp)
703 return true;
704 if (IsSoImm && ARM_AM::getSOImmVal(~(TrialOffset - UserOffset)) != -1)
Dale Johannesen99c49a42007-02-25 00:47:03 +0000705 return true;
706 }
707 return false;
708}
709
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000710/// WaterIsInRange - Returns true if a CPE placed after the specified
711/// Water (a basic block) will be in range for the specific MI.
712
713bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000714 MachineBasicBlock* Water, CPUser &U) {
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000715 unsigned MaxDisp = U.MaxDisp;
Bob Wilson84945262009-05-12 17:09:30 +0000716 unsigned CPEOffset = BBOffsets[Water->getNumber()] +
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000717 BBSizes[Water->getNumber()];
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000718
Dale Johannesend959aa42007-04-02 20:31:06 +0000719 // If the CPE is to be inserted before the instruction, that will raise
Dale Johannesen8593e412007-04-29 19:19:30 +0000720 // the offset of the instruction. (Currently applies only to ARM, so
721 // no alignment compensation attempted here.)
Dale Johannesend959aa42007-04-02 20:31:06 +0000722 if (CPEOffset < UserOffset)
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000723 UserOffset += U.CPEMI->getOperand(2).getImm();
Dale Johannesend959aa42007-04-02 20:31:06 +0000724
Evan Chengd3d9d662009-07-23 18:27:47 +0000725 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000726}
727
728/// CPEIsInRange - Returns true if the distance between specific MI and
Evan Chengc0dbec72007-01-31 19:57:44 +0000729/// specific ConstPool entry instruction can fit in MI's displacement field.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000730bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000731 MachineInstr *CPEMI, unsigned MaxDisp,
732 bool NegOk, bool DoDump) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000733 unsigned CPEOffset = GetOffsetOf(CPEMI);
734 assert(CPEOffset%4 == 0 && "Misaligned CPE");
Evan Cheng2021abe2007-02-01 01:09:47 +0000735
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000736 if (DoDump) {
737 DOUT << "User of CPE#" << CPEMI->getOperand(0).getImm()
738 << " max delta=" << MaxDisp
739 << " insn address=" << UserOffset
740 << " CPE address=" << CPEOffset
741 << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI;
742 }
Evan Chengc0dbec72007-01-31 19:57:44 +0000743
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000744 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
Evan Chengc0dbec72007-01-31 19:57:44 +0000745}
746
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000747#ifndef NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000748/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
749/// unconditionally branches to its only successor.
750static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
751 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
752 return false;
753
754 MachineBasicBlock *Succ = *MBB->succ_begin();
755 MachineBasicBlock *Pred = *MBB->pred_begin();
756 MachineInstr *PredMI = &Pred->back();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000757 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
758 || PredMI->getOpcode() == ARM::t2B)
Evan Chengc99ef082007-02-09 20:54:44 +0000759 return PredMI->getOperand(0).getMBB() == Succ;
760 return false;
761}
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000762#endif // NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000763
Bob Wilson84945262009-05-12 17:09:30 +0000764void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB,
Dale Johannesen8593e412007-04-29 19:19:30 +0000765 int delta) {
766 MachineFunction::iterator MBBI = BB; MBBI = next(MBBI);
Evan Chengd3d9d662009-07-23 18:27:47 +0000767 for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs();
768 i < e; ++i) {
Dale Johannesen99c49a42007-02-25 00:47:03 +0000769 BBOffsets[i] += delta;
Dale Johannesen8593e412007-04-29 19:19:30 +0000770 // If some existing blocks have padding, adjust the padding as needed, a
771 // bit tricky. delta can be negative so don't use % on that.
Evan Chengd3d9d662009-07-23 18:27:47 +0000772 if (!isThumb)
773 continue;
774 MachineBasicBlock *MBB = MBBI;
775 if (!MBB->empty()) {
776 // Constant pool entries require padding.
777 if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
778 unsigned oldOffset = BBOffsets[i] - delta;
779 if (oldOffset%4==0 && BBOffsets[i]%4!=0) {
780 // add new padding
781 BBSizes[i] += 2;
782 delta += 2;
783 } else if (oldOffset%4!=0 && BBOffsets[i]%4==0) {
784 // remove existing padding
785 BBSizes[i] -=2;
786 delta -= 2;
Dale Johannesen8593e412007-04-29 19:19:30 +0000787 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000788 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000789 // Thumb1 jump tables require padding. They should be at the end;
790 // following unconditional branches are removed by AnalyzeBranch.
791 MachineInstr *ThumbJTMI = NULL;
792 if (prior(MBB->end())->getOpcode() == ARM::tBR_JTr)
793 ThumbJTMI = prior(MBB->end());
794 if (ThumbJTMI) {
795 unsigned newMIOffset = GetOffsetOf(ThumbJTMI);
796 unsigned oldMIOffset = newMIOffset - delta;
797 if (oldMIOffset%4 == 0 && newMIOffset%4 != 0) {
798 // remove existing padding
799 BBSizes[i] -= 2;
800 delta -= 2;
801 } else if (oldMIOffset%4 != 0 && newMIOffset%4 == 0) {
802 // add new padding
803 BBSizes[i] += 2;
804 delta += 2;
805 }
806 }
807 if (delta==0)
808 return;
Dale Johannesen8593e412007-04-29 19:19:30 +0000809 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000810 MBBI = next(MBBI);
Dale Johannesen8593e412007-04-29 19:19:30 +0000811 }
Dale Johannesen99c49a42007-02-25 00:47:03 +0000812}
813
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000814/// DecrementOldEntry - find the constant pool entry with index CPI
815/// and instruction CPEMI, and decrement its refcount. If the refcount
Bob Wilson84945262009-05-12 17:09:30 +0000816/// becomes 0 remove the entry and instruction. Returns true if we removed
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000817/// the entry, false if we didn't.
Evan Chenga8e29892007-01-19 07:51:42 +0000818
Evan Chenged884f32007-04-03 23:39:48 +0000819bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
Evan Chengc99ef082007-02-09 20:54:44 +0000820 // Find the old entry. Eliminate it if it is no longer used.
Evan Chenged884f32007-04-03 23:39:48 +0000821 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
822 assert(CPE && "Unexpected!");
823 if (--CPE->RefCount == 0) {
824 RemoveDeadCPEMI(CPEMI);
825 CPE->CPEMI = NULL;
Evan Chengc99ef082007-02-09 20:54:44 +0000826 NumCPEs--;
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000827 return true;
828 }
829 return false;
830}
831
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000832/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
833/// if not, see if an in-range clone of the CPE is in range, and if so,
834/// change the data structures so the user references the clone. Returns:
835/// 0 = no existing entry found
836/// 1 = entry found, and there were no code insertions or deletions
837/// 2 = entry found, and there were code insertions or deletions
838int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
839{
840 MachineInstr *UserMI = U.MI;
841 MachineInstr *CPEMI = U.CPEMI;
842
843 // Check to see if the CPE is already in-range.
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000844 if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) {
Evan Cheng82020102007-07-10 22:00:16 +0000845 DOUT << "In range\n";
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000846 return 1;
Evan Chengc99ef082007-02-09 20:54:44 +0000847 }
848
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000849 // No. Look for previously created clones of the CPE that are in range.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000850 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000851 std::vector<CPEntry> &CPEs = CPEntries[CPI];
852 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
853 // We already tried this one
854 if (CPEs[i].CPEMI == CPEMI)
855 continue;
856 // Removing CPEs can leave empty entries, skip
857 if (CPEs[i].CPEMI == NULL)
858 continue;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000859 if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) {
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000860 DOUT << "Replacing CPE#" << CPI << " with CPE#" << CPEs[i].CPI << "\n";
861 // Point the CPUser node to the replacement
862 U.CPEMI = CPEs[i].CPEMI;
863 // Change the CPI in the instruction operand to refer to the clone.
864 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
Dan Gohmand735b802008-10-03 15:45:36 +0000865 if (UserMI->getOperand(j).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000866 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000867 break;
868 }
869 // Adjust the refcount of the clone...
870 CPEs[i].RefCount++;
871 // ...and the original. If we didn't remove the old entry, none of the
872 // addresses changed, so we don't need another pass.
Evan Chenged884f32007-04-03 23:39:48 +0000873 return DecrementOldEntry(CPI, CPEMI) ? 2 : 1;
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000874 }
875 }
876 return 0;
877}
878
Dale Johannesenf1b214d2007-02-28 18:41:23 +0000879/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
880/// the specific unconditional branch instruction.
881static inline unsigned getUnconditionalBrDisp(int Opc) {
David Goodwin5e47a9a2009-06-30 18:04:13 +0000882 switch (Opc) {
883 case ARM::tB:
884 return ((1<<10)-1)*2;
885 case ARM::t2B:
886 return ((1<<23)-1)*2;
887 default:
888 break;
889 }
890
891 return ((1<<23)-1)*4;
Dale Johannesenf1b214d2007-02-28 18:41:23 +0000892}
893
Dale Johannesen8593e412007-04-29 19:19:30 +0000894/// AcceptWater - Small amount of common code factored out of the following.
895
Bob Wilson84945262009-05-12 17:09:30 +0000896MachineBasicBlock* ARMConstantIslands::AcceptWater(MachineBasicBlock *WaterBB,
Dale Johannesen8593e412007-04-29 19:19:30 +0000897 std::vector<MachineBasicBlock*>::iterator IP) {
898 DOUT << "found water in range\n";
899 // Remove the original WaterList entry; we want subsequent
900 // insertions in this vicinity to go after the one we're
901 // about to insert. This considerably reduces the number
902 // of times we have to move the same CPE more than once.
903 WaterList.erase(IP);
904 // CPE goes before following block (NewMBB).
905 return next(MachineFunction::iterator(WaterBB));
906}
907
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000908/// LookForWater - look for an existing entry in the WaterList in which
909/// we can place the CPE referenced from U so it's within range of U's MI.
910/// Returns true if found, false if not. If it returns true, *NewMBB
Dale Johannesen8593e412007-04-29 19:19:30 +0000911/// is set to the WaterList entry.
Evan Chengd3d9d662009-07-23 18:27:47 +0000912/// For ARM, we prefer the water that's farthest away. For Thumb, prefer
Dale Johannesen8593e412007-04-29 19:19:30 +0000913/// water that will not introduce padding to water that will; within each
914/// group, prefer the water that's farthest away.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000915bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
Dale Johannesen8593e412007-04-29 19:19:30 +0000916 MachineBasicBlock** NewMBB) {
917 std::vector<MachineBasicBlock*>::iterator IPThatWouldPad;
918 MachineBasicBlock* WaterBBThatWouldPad = NULL;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000919 if (!WaterList.empty()) {
920 for (std::vector<MachineBasicBlock*>::iterator IP = prior(WaterList.end()),
Evan Chengd3d9d662009-07-23 18:27:47 +0000921 B = WaterList.begin();; --IP) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000922 MachineBasicBlock* WaterBB = *IP;
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000923 if (WaterIsInRange(UserOffset, WaterBB, U)) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000924 unsigned WBBId = WaterBB->getNumber();
Dale Johannesen8593e412007-04-29 19:19:30 +0000925 if (isThumb &&
Evan Chengd3d9d662009-07-23 18:27:47 +0000926 (BBOffsets[WBBId] + BBSizes[WBBId])%4 != 0) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000927 // This is valid Water, but would introduce padding. Remember
928 // it in case we don't find any Water that doesn't do this.
929 if (!WaterBBThatWouldPad) {
930 WaterBBThatWouldPad = WaterBB;
931 IPThatWouldPad = IP;
932 }
933 } else {
934 *NewMBB = AcceptWater(WaterBB, IP);
935 return true;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000936 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000937 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000938 if (IP == B)
939 break;
940 }
941 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000942 if (isThumb && WaterBBThatWouldPad) {
943 *NewMBB = AcceptWater(WaterBBThatWouldPad, IPThatWouldPad);
944 return true;
945 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000946 return false;
947}
948
Bob Wilson84945262009-05-12 17:09:30 +0000949/// CreateNewWater - No existing WaterList entry will work for
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000950/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
951/// block is used if in range, and the conditional branch munged so control
952/// flow is correct. Otherwise the block is split to create a hole with an
953/// unconditional branch around it. In either case *NewMBB is set to a
954/// block following which the new island can be inserted (the WaterList
955/// is not adjusted).
956
Bob Wilson84945262009-05-12 17:09:30 +0000957void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000958 unsigned UserOffset, MachineBasicBlock** NewMBB) {
959 CPUser &U = CPUsers[CPUserIndex];
960 MachineInstr *UserMI = U.MI;
961 MachineInstr *CPEMI = U.CPEMI;
962 MachineBasicBlock *UserMBB = UserMI->getParent();
Bob Wilson84945262009-05-12 17:09:30 +0000963 unsigned OffsetOfNextBlock = BBOffsets[UserMBB->getNumber()] +
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000964 BBSizes[UserMBB->getNumber()];
Dale Johannesen8593e412007-04-29 19:19:30 +0000965 assert(OffsetOfNextBlock== BBOffsets[UserMBB->getNumber()+1]);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000966
967 // If the use is at the end of the block, or the end of the block
Dale Johannesen8593e412007-04-29 19:19:30 +0000968 // is within range, make new water there. (The addition below is
Evan Chengd3d9d662009-07-23 18:27:47 +0000969 // for the unconditional branch we will be adding: 4 bytes on ARM + Thumb2,
970 // 2 on Thumb1. Possible Thumb1 alignment padding is allowed for
Dale Johannesen8593e412007-04-29 19:19:30 +0000971 // inside OffsetIsInRange.
Bob Wilson84945262009-05-12 17:09:30 +0000972 // If the block ends in an unconditional branch already, it is water,
Dale Johannesen8593e412007-04-29 19:19:30 +0000973 // and is known to be out of range, so we'll always be adding a branch.)
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000974 if (&UserMBB->back() == UserMI ||
Evan Chengd3d9d662009-07-23 18:27:47 +0000975 OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4),
976 U.MaxDisp, U.NegOk, U.IsSoImm)) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000977 DOUT << "Split at end of block\n";
978 if (&UserMBB->back() == UserMI)
979 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
980 *NewMBB = next(MachineFunction::iterator(UserMBB));
981 // Add an unconditional branch from UserMBB to fallthrough block.
982 // Record it for branch lengthening; this new branch will not get out of
983 // range, but if the preceding conditional branch is out of range, the
984 // targets will be exchanged, and the altered branch may be out of
985 // range, so the machinery has to know about it.
David Goodwin5e47a9a2009-06-30 18:04:13 +0000986 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
Dale Johannesenb6728402009-02-13 02:25:56 +0000987 BuildMI(UserMBB, DebugLoc::getUnknownLoc(),
988 TII->get(UncondBr)).addMBB(*NewMBB);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000989 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
Bob Wilson84945262009-05-12 17:09:30 +0000990 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000991 MaxDisp, false, UncondBr));
Evan Chengd3d9d662009-07-23 18:27:47 +0000992 int delta = isThumb1 ? 2 : 4;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000993 BBSizes[UserMBB->getNumber()] += delta;
994 AdjustBBOffsetsAfter(UserMBB, delta);
995 } else {
996 // What a big block. Find a place within the block to split it.
Evan Chengd3d9d662009-07-23 18:27:47 +0000997 // This is a little tricky on Thumb1 since instructions are 2 bytes
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000998 // and constant pool entries are 4 bytes: if instruction I references
999 // island CPE, and instruction I+1 references CPE', it will
1000 // not work well to put CPE as far forward as possible, since then
1001 // CPE' cannot immediately follow it (that location is 2 bytes
1002 // farther away from I+1 than CPE was from I) and we'd need to create
Dale Johannesen8593e412007-04-29 19:19:30 +00001003 // a new island. So, we make a first guess, then walk through the
1004 // instructions between the one currently being looked at and the
1005 // possible insertion point, and make sure any other instructions
1006 // that reference CPEs will be able to use the same island area;
1007 // if not, we back up the insertion point.
1008
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001009 // The 4 in the following is for the unconditional branch we'll be
Evan Chengd3d9d662009-07-23 18:27:47 +00001010 // inserting (allows for long branch on Thumb1). Alignment of the
Dale Johannesen8593e412007-04-29 19:19:30 +00001011 // island is handled inside OffsetIsInRange.
1012 unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001013 // This could point off the end of the block if we've already got
1014 // constant pool entries following this block; only the last one is
1015 // in the water list. Back past any possible branches (allow for a
1016 // conditional and a maximally long unconditional).
1017 if (BaseInsertOffset >= BBOffsets[UserMBB->getNumber()+1])
Bob Wilson84945262009-05-12 17:09:30 +00001018 BaseInsertOffset = BBOffsets[UserMBB->getNumber()+1] -
Evan Chengd3d9d662009-07-23 18:27:47 +00001019 (isThumb1 ? 6 : 8);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001020 unsigned EndInsertOffset = BaseInsertOffset +
1021 CPEMI->getOperand(2).getImm();
1022 MachineBasicBlock::iterator MI = UserMI;
1023 ++MI;
1024 unsigned CPUIndex = CPUserIndex+1;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001025 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001026 Offset < BaseInsertOffset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001027 Offset += TII->GetInstSizeInBytes(MI),
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001028 MI = next(MI)) {
1029 if (CPUIndex < CPUsers.size() && CPUsers[CPUIndex].MI == MI) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001030 CPUser &U = CPUsers[CPUIndex];
Bob Wilson84945262009-05-12 17:09:30 +00001031 if (!OffsetIsInRange(Offset, EndInsertOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +00001032 U.MaxDisp, U.NegOk, U.IsSoImm)) {
1033 BaseInsertOffset -= (isThumb1 ? 2 : 4);
1034 EndInsertOffset -= (isThumb1 ? 2 : 4);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001035 }
1036 // This is overly conservative, as we don't account for CPEMIs
1037 // being reused within the block, but it doesn't matter much.
1038 EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm();
1039 CPUIndex++;
1040 }
1041 }
1042 DOUT << "Split in middle of big block\n";
1043 *NewMBB = SplitBlockBeforeInstr(prior(MI));
1044 }
1045}
1046
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001047/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
Bob Wilson39bf0512009-05-12 17:35:29 +00001048/// is out-of-range. If so, pick up the constant pool value and move it some
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001049/// place in-range. Return true if we changed any addresses (thus must run
1050/// another pass of branch lengthening), false otherwise.
Bob Wilson84945262009-05-12 17:09:30 +00001051bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn,
1052 unsigned CPUserIndex) {
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001053 CPUser &U = CPUsers[CPUserIndex];
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001054 MachineInstr *UserMI = U.MI;
1055 MachineInstr *CPEMI = U.CPEMI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001056 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001057 unsigned Size = CPEMI->getOperand(2).getImm();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001058 MachineBasicBlock *NewMBB;
Dale Johannesen8593e412007-04-29 19:19:30 +00001059 // Compute this only once, it's expensive. The 4 or 8 is the value the
Bob Wilson39bf0512009-05-12 17:35:29 +00001060 // hardware keeps in the PC (2 insns ahead of the reference).
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001061 unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
Evan Cheng768c9f72007-04-27 08:14:15 +00001062
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001063 // See if the current entry is within range, or there is a clone of it
1064 // in range.
1065 int result = LookForExistingCPEntry(U, UserOffset);
1066 if (result==1) return false;
1067 else if (result==2) return true;
1068
1069 // No existing clone of this CPE is within range.
1070 // We will be generating a new clone. Get a UID for it.
Bob Wilson39bf0512009-05-12 17:35:29 +00001071 unsigned ID = AFI->createConstPoolEntryUId();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001072
1073 // Look for water where we can place this CPE. We look for the farthest one
1074 // away that will work. Forward references only for now (although later
1075 // we might find some that are backwards).
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001076
Dale Johannesen8593e412007-04-29 19:19:30 +00001077 if (!LookForWater(U, UserOffset, &NewMBB)) {
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001078 // No water found.
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001079 DOUT << "No water found\n";
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001080 CreateNewWater(CPUserIndex, UserOffset, &NewMBB);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001081 }
1082
1083 // Okay, we know we can put an island before NewMBB now, do it!
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001084 MachineBasicBlock *NewIsland = Fn.CreateMachineBasicBlock();
1085 Fn.insert(NewMBB, NewIsland);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001086
1087 // Update internal data structures to account for the newly inserted MBB.
1088 UpdateForInsertedWaterBlock(NewIsland);
1089
1090 // Decrement the old entry, and remove it if refcount becomes 0.
Evan Chenged884f32007-04-03 23:39:48 +00001091 DecrementOldEntry(CPI, CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001092
1093 // Now that we have an island to add the CPE to, clone the original CPE and
1094 // add it to the island.
Dale Johannesenb6728402009-02-13 02:25:56 +00001095 U.CPEMI = BuildMI(NewIsland, DebugLoc::getUnknownLoc(),
1096 TII->get(ARM::CONSTPOOL_ENTRY))
Evan Chenga8e29892007-01-19 07:51:42 +00001097 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001098 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
Evan Chengc99ef082007-02-09 20:54:44 +00001099 NumCPEs++;
1100
Dale Johannesen8593e412007-04-29 19:19:30 +00001101 BBOffsets[NewIsland->getNumber()] = BBOffsets[NewMBB->getNumber()];
Evan Chengb43216e2007-02-01 10:16:15 +00001102 // Compensate for .align 2 in thumb mode.
Bob Wilson84945262009-05-12 17:09:30 +00001103 if (isThumb && BBOffsets[NewIsland->getNumber()]%4 != 0)
Dale Johannesen8593e412007-04-29 19:19:30 +00001104 Size += 2;
Evan Chenga8e29892007-01-19 07:51:42 +00001105 // Increase the size of the island block to account for the new entry.
1106 BBSizes[NewIsland->getNumber()] += Size;
Dale Johannesen99c49a42007-02-25 00:47:03 +00001107 AdjustBBOffsetsAfter(NewIsland, Size);
Bob Wilson84945262009-05-12 17:09:30 +00001108
Evan Chenga8e29892007-01-19 07:51:42 +00001109 // Finally, change the CPI in the instruction operand to be ID.
1110 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +00001111 if (UserMI->getOperand(i).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +00001112 UserMI->getOperand(i).setIndex(ID);
Evan Chenga8e29892007-01-19 07:51:42 +00001113 break;
1114 }
Bob Wilson84945262009-05-12 17:09:30 +00001115
Evan Chengc99ef082007-02-09 20:54:44 +00001116 DOUT << " Moved CPE to #" << ID << " CPI=" << CPI << "\t" << *UserMI;
Bob Wilson84945262009-05-12 17:09:30 +00001117
Evan Chenga8e29892007-01-19 07:51:42 +00001118 return true;
1119}
1120
Evan Chenged884f32007-04-03 23:39:48 +00001121/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1122/// sizes and offsets of impacted basic blocks.
1123void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
1124 MachineBasicBlock *CPEBB = CPEMI->getParent();
Dale Johannesen8593e412007-04-29 19:19:30 +00001125 unsigned Size = CPEMI->getOperand(2).getImm();
1126 CPEMI->eraseFromParent();
1127 BBSizes[CPEBB->getNumber()] -= Size;
1128 // All succeeding offsets have the current size value added in, fix this.
Evan Chenged884f32007-04-03 23:39:48 +00001129 if (CPEBB->empty()) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001130 // In thumb1 mode, the size of island may be padded by two to compensate for
Dale Johannesen8593e412007-04-29 19:19:30 +00001131 // the alignment requirement. Then it will now be 2 when the block is
Evan Chenged884f32007-04-03 23:39:48 +00001132 // empty, so fix this.
1133 // All succeeding offsets have the current size value added in, fix this.
1134 if (BBSizes[CPEBB->getNumber()] != 0) {
Dale Johannesen8593e412007-04-29 19:19:30 +00001135 Size += BBSizes[CPEBB->getNumber()];
Evan Chenged884f32007-04-03 23:39:48 +00001136 BBSizes[CPEBB->getNumber()] = 0;
1137 }
Evan Chenged884f32007-04-03 23:39:48 +00001138 }
Dale Johannesen8593e412007-04-29 19:19:30 +00001139 AdjustBBOffsetsAfter(CPEBB, -Size);
1140 // An island has only one predecessor BB and one successor BB. Check if
1141 // this BB's predecessor jumps directly to this BB's successor. This
1142 // shouldn't happen currently.
1143 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1144 // FIXME: remove the empty blocks after all the work is done?
Evan Chenged884f32007-04-03 23:39:48 +00001145}
1146
1147/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1148/// are zero.
1149bool ARMConstantIslands::RemoveUnusedCPEntries() {
1150 unsigned MadeChange = false;
1151 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1152 std::vector<CPEntry> &CPEs = CPEntries[i];
1153 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1154 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1155 RemoveDeadCPEMI(CPEs[j].CPEMI);
1156 CPEs[j].CPEMI = NULL;
1157 MadeChange = true;
1158 }
1159 }
Bob Wilson84945262009-05-12 17:09:30 +00001160 }
Evan Chenged884f32007-04-03 23:39:48 +00001161 return MadeChange;
1162}
1163
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001164/// BBIsInRange - Returns true if the distance between specific MI and
Evan Cheng43aeab62007-01-26 20:38:26 +00001165/// specific BB can fit in MI's displacement field.
Evan Chengc0dbec72007-01-31 19:57:44 +00001166bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1167 unsigned MaxDisp) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001168 unsigned PCAdj = isThumb ? 4 : 8;
Evan Chengc0dbec72007-01-31 19:57:44 +00001169 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
Dale Johannesen99c49a42007-02-25 00:47:03 +00001170 unsigned DestOffset = BBOffsets[DestBB->getNumber()];
Evan Cheng43aeab62007-01-26 20:38:26 +00001171
Evan Chengc99ef082007-02-09 20:54:44 +00001172 DOUT << "Branch of destination BB#" << DestBB->getNumber()
1173 << " from BB#" << MI->getParent()->getNumber()
1174 << " max delta=" << MaxDisp
Dale Johannesen8593e412007-04-29 19:19:30 +00001175 << " from " << GetOffsetOf(MI) << " to " << DestOffset
1176 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI;
Evan Chengc0dbec72007-01-31 19:57:44 +00001177
Dale Johannesen8593e412007-04-29 19:19:30 +00001178 if (BrOffset <= DestOffset) {
1179 // Branch before the Dest.
1180 if (DestOffset-BrOffset <= MaxDisp)
1181 return true;
1182 } else {
1183 if (BrOffset-DestOffset <= MaxDisp)
1184 return true;
1185 }
1186 return false;
Evan Cheng43aeab62007-01-26 20:38:26 +00001187}
1188
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001189/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1190/// away to fit in its displacement field.
1191bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001192 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001193 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001194
Evan Chengc0dbec72007-01-31 19:57:44 +00001195 // Check to see if the DestBB is already in-range.
1196 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
Evan Cheng43aeab62007-01-26 20:38:26 +00001197 return false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001198
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001199 if (!Br.isCond)
1200 return FixUpUnconditionalBr(Fn, Br);
1201 return FixUpConditionalBr(Fn, Br);
1202}
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001203
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001204/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1205/// too far away to fit in its displacement field. If the LR register has been
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001206/// spilled in the epilogue, then we can use BL to implement a far jump.
Bob Wilson39bf0512009-05-12 17:35:29 +00001207/// Otherwise, add an intermediate branch instruction to a branch.
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001208bool
1209ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br) {
1210 MachineInstr *MI = Br.MI;
1211 MachineBasicBlock *MBB = MI->getParent();
Evan Chengd3d9d662009-07-23 18:27:47 +00001212 assert(isThumb && !isThumb2 && "Expected a Thumb1 function!");
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001213
1214 // Use BL to implement far jump.
1215 Br.MaxDisp = (1 << 21) * 2;
Chris Lattner5080f4d2008-01-11 18:10:50 +00001216 MI->setDesc(TII->get(ARM::tBfar));
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001217 BBSizes[MBB->getNumber()] += 2;
Dale Johannesen99c49a42007-02-25 00:47:03 +00001218 AdjustBBOffsetsAfter(MBB, 2);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001219 HasFarJump = true;
1220 NumUBrFixed++;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001221
Evan Chengc99ef082007-02-09 20:54:44 +00001222 DOUT << " Changed B to long jump " << *MI;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001223
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001224 return true;
1225}
1226
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001227/// FixUpConditionalBr - Fix up a conditional branch whose destination is too
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001228/// far away to fit in its displacement field. It is converted to an inverse
1229/// conditional branch + an unconditional branch to the destination.
1230bool
1231ARMConstantIslands::FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br) {
1232 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001233 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001234
Bob Wilson39bf0512009-05-12 17:35:29 +00001235 // Add an unconditional branch to the destination and invert the branch
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001236 // condition to jump over it:
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001237 // blt L1
1238 // =>
1239 // bge L2
1240 // b L1
1241 // L2:
Chris Lattner9a1ceae2007-12-30 20:49:49 +00001242 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001243 CC = ARMCC::getOppositeCondition(CC);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001244 unsigned CCReg = MI->getOperand(2).getReg();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001245
1246 // If the branch is at the end of its MBB and that has a fall-through block,
1247 // direct the updated conditional branch to the fall-through block. Otherwise,
1248 // split the MBB before the next instruction.
1249 MachineBasicBlock *MBB = MI->getParent();
Evan Chengbd5d3db2007-02-03 02:08:34 +00001250 MachineInstr *BMI = &MBB->back();
1251 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Evan Cheng43aeab62007-01-26 20:38:26 +00001252
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001253 NumCBrFixed++;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001254 if (BMI != MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001255 if (next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
Evan Chengbd5d3db2007-02-03 02:08:34 +00001256 BMI->getOpcode() == Br.UncondBr) {
Bob Wilson39bf0512009-05-12 17:35:29 +00001257 // Last MI in the BB is an unconditional branch. Can we simply invert the
Evan Cheng43aeab62007-01-26 20:38:26 +00001258 // condition and swap destinations:
1259 // beq L1
1260 // b L2
1261 // =>
1262 // bne L2
1263 // b L1
Chris Lattner8aa797a2007-12-30 23:10:15 +00001264 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
Evan Chengc0dbec72007-01-31 19:57:44 +00001265 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
Evan Chengc99ef082007-02-09 20:54:44 +00001266 DOUT << " Invert Bcc condition and swap its destination with " << *BMI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001267 BMI->getOperand(0).setMBB(DestBB);
1268 MI->getOperand(0).setMBB(NewDest);
Evan Cheng43aeab62007-01-26 20:38:26 +00001269 MI->getOperand(1).setImm(CC);
1270 return true;
1271 }
1272 }
1273 }
1274
1275 if (NeedSplit) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001276 SplitBlockBeforeInstr(MI);
Bob Wilson39bf0512009-05-12 17:35:29 +00001277 // No need for the branch to the next block. We're adding an unconditional
Evan Chengdd353b82007-01-26 02:02:39 +00001278 // branch to the destination.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001279 int delta = TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001280 BBSizes[MBB->getNumber()] -= delta;
Dale Johannesen8593e412007-04-29 19:19:30 +00001281 MachineBasicBlock* SplitBB = next(MachineFunction::iterator(MBB));
1282 AdjustBBOffsetsAfter(SplitBB, -delta);
Evan Chengdd353b82007-01-26 02:02:39 +00001283 MBB->back().eraseFromParent();
Dale Johannesen8593e412007-04-29 19:19:30 +00001284 // BBOffsets[SplitBB] is wrong temporarily, fixed below
Evan Chengdd353b82007-01-26 02:02:39 +00001285 }
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001286 MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
Bob Wilson84945262009-05-12 17:09:30 +00001287
Evan Chengc99ef082007-02-09 20:54:44 +00001288 DOUT << " Insert B to BB#" << DestBB->getNumber()
1289 << " also invert condition and change dest. to BB#"
1290 << NextBB->getNumber() << "\n";
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001291
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001292 // Insert a new conditional branch and a new unconditional branch.
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001293 // Also update the ImmBranch as well as adding a new entry for the new branch.
Dale Johannesenb6728402009-02-13 02:25:56 +00001294 BuildMI(MBB, DebugLoc::getUnknownLoc(),
1295 TII->get(MI->getOpcode()))
1296 .addMBB(NextBB).addImm(CC).addReg(CCReg);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001297 Br.MI = &MBB->back();
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001298 BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesenb6728402009-02-13 02:25:56 +00001299 BuildMI(MBB, DebugLoc::getUnknownLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001300 BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back());
Evan Chenga9b8b8d2007-01-31 18:29:27 +00001301 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
Evan Chenga0bf7942007-01-25 23:31:04 +00001302 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001303
1304 // Remove the old conditional branch. It may or may not still be in MBB.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001305 BBSizes[MI->getParent()->getNumber()] -= TII->GetInstSizeInBytes(MI);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001306 MI->eraseFromParent();
1307
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001308 // The net size change is an addition of one unconditional branch.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001309 int delta = TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesen99c49a42007-02-25 00:47:03 +00001310 AdjustBBOffsetsAfter(MBB, delta);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001311 return true;
1312}
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001313
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001314/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
1315/// LR / restores LR to pc.
1316bool ARMConstantIslands::UndoLRSpillRestore() {
1317 bool MadeChange = false;
1318 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1319 MachineInstr *MI = PushPopMIs[i];
Evan Cheng44bec522007-05-15 01:29:07 +00001320 if (MI->getOpcode() == ARM::tPOP_RET &&
1321 MI->getOperand(0).getReg() == ARM::PC &&
1322 MI->getNumExplicitOperands() == 1) {
Dale Johannesenb6728402009-02-13 02:25:56 +00001323 BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET));
Evan Cheng44bec522007-05-15 01:29:07 +00001324 MI->eraseFromParent();
1325 MadeChange = true;
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001326 }
1327 }
1328 return MadeChange;
1329}