blob: f53bd7d3a6583f8f0de39a56d8f1a8ea24116746 [file] [log] [blame]
Evan Chenga8e29892007-01-19 07:51:42 +00001//===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
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 Chengaf5cbcb2007-01-25 03:12:46 +000018#include "ARMMachineFunctionInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000019#include "ARMInstrInfo.h"
20#include "llvm/CodeGen/MachineConstantPool.h"
21#include "llvm/CodeGen/MachineFunctionPass.h"
22#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga8e29892007-01-19 07:51:42 +000023#include "llvm/Target/TargetData.h"
24#include "llvm/Target/TargetMachine.h"
25#include "llvm/Support/Compiler.h"
26#include "llvm/Support/Debug.h"
Evan Chengc99ef082007-02-09 20:54:44 +000027#include "llvm/ADT/SmallVector.h"
Evan Chenga8e29892007-01-19 07:51:42 +000028#include "llvm/ADT/STLExtras.h"
29#include "llvm/ADT/Statistic.h"
Evan Chenga8e29892007-01-19 07:51:42 +000030using namespace llvm;
31
Evan Chengc99ef082007-02-09 20:54:44 +000032STATISTIC(NumCPEs, "Number of constpool entries");
Evan Chengd1b2c1e2007-01-30 01:18:38 +000033STATISTIC(NumSplit, "Number of uncond branches inserted");
34STATISTIC(NumCBrFixed, "Number of cond branches fixed");
35STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
Evan Chenga8e29892007-01-19 07:51:42 +000036
37namespace {
38 /// ARMConstantIslands - Due to limited pc-relative displacements, ARM
39 /// requires constant pool entries to be scattered among the instructions
40 /// inside a function. To do this, it completely ignores the normal LLVM
41 /// constant pool, instead, it places constants where-ever it feels like with
42 /// special instructions.
43 ///
44 /// The terminology used in this pass includes:
45 /// Islands - Clumps of constants placed in the function.
46 /// Water - Potential places where an island could be formed.
47 /// CPE - A constant pool entry that has been placed somewhere, which
48 /// tracks a list of users.
49 class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass {
50 /// NextUID - Assign unique ID's to CPE's.
51 unsigned NextUID;
52
53 /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
54 /// by MBB Number.
Evan Chengc99ef082007-02-09 20:54:44 +000055 SmallVector<unsigned, 8> BBSizes;
Evan Chenga8e29892007-01-19 07:51:42 +000056
57 /// WaterList - A sorted list of basic blocks where islands could be placed
58 /// (i.e. blocks that don't fall through to the following block, due
59 /// to a return, unreachable, or unconditional branch).
Evan Chengc99ef082007-02-09 20:54:44 +000060 SmallVector<MachineBasicBlock*, 8> WaterList;
61
Evan Chenga8e29892007-01-19 07:51:42 +000062 /// CPUser - One user of a constant pool, keeping the machine instruction
63 /// pointer, the constant pool being referenced, and the max displacement
64 /// allowed from the instruction to the CP.
65 struct CPUser {
66 MachineInstr *MI;
67 MachineInstr *CPEMI;
68 unsigned MaxDisp;
69 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp)
70 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp) {}
71 };
72
73 /// CPUsers - Keep track of all of the machine instructions that use various
74 /// constant pools and their max displacement.
Evan Chengc99ef082007-02-09 20:54:44 +000075 SmallVector<CPUser, 16> CPUsers;
76
77 /// CPEntry - One per constant pool entry, keeping the machine instruction
78 /// pointer, the constpool index, and the number of CPUser's which
79 /// reference this entry.
80 struct CPEntry {
81 MachineInstr *CPEMI;
82 unsigned CPI;
83 unsigned RefCount;
84 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
85 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
86 };
87
88 /// CPEntries - Keep track of all of the constant pool entry machine
89 /// instructions. For each constpool index, it keeps a vector of entries.
90 std::vector<std::vector<CPEntry> > CPEntries;
Evan Chenga8e29892007-01-19 07:51:42 +000091
Evan Chengaf5cbcb2007-01-25 03:12:46 +000092 /// ImmBranch - One per immediate branch, keeping the machine instruction
93 /// pointer, conditional or unconditional, the max displacement,
94 /// and (if isCond is true) the corresponding unconditional branch
95 /// opcode.
96 struct ImmBranch {
97 MachineInstr *MI;
Evan Chengc2854142007-01-25 23:18:59 +000098 unsigned MaxDisp : 31;
99 bool isCond : 1;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000100 int UncondBr;
Evan Chengc2854142007-01-25 23:18:59 +0000101 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
102 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000103 };
104
Evan Chengc2854142007-01-25 23:18:59 +0000105 /// Branches - Keep track of all the immediate branch instructions.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000106 ///
Evan Chengc99ef082007-02-09 20:54:44 +0000107 SmallVector<ImmBranch, 32> ImmBranches;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000108
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000109 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
110 ///
Evan Chengc99ef082007-02-09 20:54:44 +0000111 SmallVector<MachineInstr*, 4> PushPopMIs;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000112
113 /// HasFarJump - True if any far jump instruction has been emitted during
114 /// the branch fix up pass.
115 bool HasFarJump;
116
Evan Chenga8e29892007-01-19 07:51:42 +0000117 const TargetInstrInfo *TII;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000118 const ARMFunctionInfo *AFI;
Evan Chenga8e29892007-01-19 07:51:42 +0000119 public:
120 virtual bool runOnMachineFunction(MachineFunction &Fn);
121
122 virtual const char *getPassName() const {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000123 return "ARM constant island placement and branch shortening pass";
Evan Chenga8e29892007-01-19 07:51:42 +0000124 }
125
126 private:
127 void DoInitialPlacement(MachineFunction &Fn,
Evan Chengc99ef082007-02-09 20:54:44 +0000128 SmallVector<MachineInstr*, 16> &CPEMIs);
129 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Evan Chenga8e29892007-01-19 07:51:42 +0000130 void InitialFunctionScan(MachineFunction &Fn,
Evan Chengc99ef082007-02-09 20:54:44 +0000131 const SmallVector<MachineInstr*, 16> &CPEMIs);
Evan Cheng0c615842007-01-31 02:22:22 +0000132 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
Evan Chenga8e29892007-01-19 07:51:42 +0000133 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000134 bool HandleConstantPoolUser(MachineFunction &Fn, CPUser &U);
Evan Chengc0dbec72007-01-31 19:57:44 +0000135 bool CPEIsInRange(MachineInstr *MI, MachineInstr *CPEMI, unsigned Disp);
136 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000137 bool FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br);
138 bool FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br);
139 bool FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br);
140 bool UndoLRSpillRestore();
Evan Chenga8e29892007-01-19 07:51:42 +0000141
Evan Chenga8e29892007-01-19 07:51:42 +0000142 unsigned GetOffsetOf(MachineInstr *MI) const;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000143 unsigned GetOffsetOf(MachineBasicBlock *MBB) const;
Evan Chenga8e29892007-01-19 07:51:42 +0000144 };
145}
146
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000147/// createARMConstantIslandPass - returns an instance of the constpool
148/// island pass.
Evan Chenga8e29892007-01-19 07:51:42 +0000149FunctionPass *llvm::createARMConstantIslandPass() {
150 return new ARMConstantIslands();
151}
152
153bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) {
Evan Chenga8e29892007-01-19 07:51:42 +0000154 MachineConstantPool &MCP = *Fn.getConstantPool();
Evan Chenga8e29892007-01-19 07:51:42 +0000155
156 TII = Fn.getTarget().getInstrInfo();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000157 AFI = Fn.getInfo<ARMFunctionInfo>();
158
159 HasFarJump = false;
160
Evan Chenga8e29892007-01-19 07:51:42 +0000161 // Renumber all of the machine basic blocks in the function, guaranteeing that
162 // the numbers agree with the position of the block in the function.
163 Fn.RenumberBlocks();
164
165 // Perform the initial placement of the constant pool entries. To start with,
166 // we put them all at the end of the function.
Evan Chengc99ef082007-02-09 20:54:44 +0000167 SmallVector<MachineInstr*, 16> CPEMIs;
Evan Cheng7755fac2007-01-26 01:04:44 +0000168 if (!MCP.isEmpty())
169 DoInitialPlacement(Fn, CPEMIs);
Evan Chenga8e29892007-01-19 07:51:42 +0000170
171 /// The next UID to take is the first unused one.
172 NextUID = CPEMIs.size();
173
174 // Do the initial scan of the function, building up information about the
175 // sizes of each block, the location of all the water, and finding all of the
176 // constant pool users.
177 InitialFunctionScan(Fn, CPEMIs);
178 CPEMIs.clear();
179
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000180 // Iteratively place constant pool entries and fix up branches until there
181 // is no change.
182 bool MadeChange = false;
183 while (true) {
184 bool Change = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000185 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000186 Change |= HandleConstantPoolUser(Fn, CPUsers[i]);
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000187 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000188 Change |= FixUpImmediateBr(Fn, ImmBranches[i]);
189 if (!Change)
190 break;
191 MadeChange = true;
192 }
Evan Chenga8e29892007-01-19 07:51:42 +0000193
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000194 // If LR has been forced spilled and no far jumps (i.e. BL) has been issued.
195 // Undo the spill / restore of LR if possible.
196 if (!HasFarJump && AFI->isLRForceSpilled() && AFI->isThumbFunction())
197 MadeChange |= UndoLRSpillRestore();
198
Evan Chenga8e29892007-01-19 07:51:42 +0000199 BBSizes.clear();
200 WaterList.clear();
201 CPUsers.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000202 CPEntries.clear();
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000203 ImmBranches.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000204 PushPopMIs.clear();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000205
206 return MadeChange;
Evan Chenga8e29892007-01-19 07:51:42 +0000207}
208
209/// DoInitialPlacement - Perform the initial placement of the constant pool
210/// entries. To start with, we put them all at the end of the function.
211void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn,
Evan Chengc99ef082007-02-09 20:54:44 +0000212 SmallVector<MachineInstr*, 16> &CPEMIs){
Evan Chenga8e29892007-01-19 07:51:42 +0000213 // Create the basic block to hold the CPE's.
214 MachineBasicBlock *BB = new MachineBasicBlock();
215 Fn.getBasicBlockList().push_back(BB);
216
217 // Add all of the constants from the constant pool to the end block, use an
218 // identity mapping of CPI's to CPE's.
219 const std::vector<MachineConstantPoolEntry> &CPs =
220 Fn.getConstantPool()->getConstants();
221
222 const TargetData &TD = *Fn.getTarget().getTargetData();
223 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
224 unsigned Size = TD.getTypeSize(CPs[i].getType());
225 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
226 // we would have to pad them out or something so that instructions stay
227 // aligned.
228 assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
229 MachineInstr *CPEMI =
230 BuildMI(BB, TII->get(ARM::CONSTPOOL_ENTRY))
231 .addImm(i).addConstantPoolIndex(i).addImm(Size);
232 CPEMIs.push_back(CPEMI);
Evan Chengc99ef082007-02-09 20:54:44 +0000233
234 // Add a new CPEntry, but no corresponding CPUser yet.
235 std::vector<CPEntry> CPEs;
236 CPEs.push_back(CPEntry(CPEMI, i));
237 CPEntries.push_back(CPEs);
238 NumCPEs++;
239 DOUT << "Moved CPI#" << i << " to end of function as #" << i << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000240 }
241}
242
243/// BBHasFallthrough - Return true of the specified basic block can fallthrough
244/// into the block immediately after it.
245static bool BBHasFallthrough(MachineBasicBlock *MBB) {
246 // Get the next machine basic block in the function.
247 MachineFunction::iterator MBBI = MBB;
248 if (next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function.
249 return false;
250
251 MachineBasicBlock *NextBB = next(MBBI);
252 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
253 E = MBB->succ_end(); I != E; ++I)
254 if (*I == NextBB)
255 return true;
256
257 return false;
258}
259
Evan Chengc99ef082007-02-09 20:54:44 +0000260/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
261/// look up the corresponding CPEntry.
262ARMConstantIslands::CPEntry
263*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
264 const MachineInstr *CPEMI) {
265 std::vector<CPEntry> &CPEs = CPEntries[CPI];
266 // Number of entries per constpool index should be small, just do a
267 // linear search.
268 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
269 if (CPEs[i].CPEMI == CPEMI)
270 return &CPEs[i];
271 }
272 return NULL;
273}
274
Evan Chenga8e29892007-01-19 07:51:42 +0000275/// InitialFunctionScan - Do the initial scan of the function, building up
276/// information about the sizes of each block, the location of all the water,
277/// and finding all of the constant pool users.
278void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn,
Evan Chengc99ef082007-02-09 20:54:44 +0000279 const SmallVector<MachineInstr*, 16> &CPEMIs) {
Evan Chenga8e29892007-01-19 07:51:42 +0000280 for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
281 MBBI != E; ++MBBI) {
282 MachineBasicBlock &MBB = *MBBI;
283
284 // If this block doesn't fall through into the next MBB, then this is
285 // 'water' that a constant pool island could be placed.
286 if (!BBHasFallthrough(&MBB))
287 WaterList.push_back(&MBB);
288
289 unsigned MBBSize = 0;
290 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
291 I != E; ++I) {
292 // Add instruction size to MBBSize.
Evan Cheng29836c32007-01-29 23:45:17 +0000293 MBBSize += ARM::GetInstSize(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000294
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000295 int Opc = I->getOpcode();
296 if (TII->isBranch(Opc)) {
297 bool isCond = false;
298 unsigned Bits = 0;
299 unsigned Scale = 1;
300 int UOpc = Opc;
301 switch (Opc) {
Evan Cheng743fa032007-01-25 19:43:52 +0000302 default:
303 continue; // Ignore JT branches
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000304 case ARM::Bcc:
305 isCond = true;
306 UOpc = ARM::B;
307 // Fallthrough
308 case ARM::B:
309 Bits = 24;
310 Scale = 4;
311 break;
312 case ARM::tBcc:
313 isCond = true;
314 UOpc = ARM::tB;
315 Bits = 8;
316 Scale = 2;
317 break;
318 case ARM::tB:
319 Bits = 11;
320 Scale = 2;
321 break;
322 }
Evan Chengb43216e2007-02-01 10:16:15 +0000323
324 // Record this immediate branch.
Evan Chengbd5d3db2007-02-03 02:08:34 +0000325 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Evan Chengb43216e2007-02-01 10:16:15 +0000326 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000327 }
328
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000329 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
330 PushPopMIs.push_back(I);
331
Evan Chenga8e29892007-01-19 07:51:42 +0000332 // Scan the instructions for constant pool operands.
333 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
334 if (I->getOperand(op).isConstantPoolIndex()) {
335 // We found one. The addressing mode tells us the max displacement
336 // from the PC that this instruction permits.
Evan Chenga8e29892007-01-19 07:51:42 +0000337
338 // Basic size info comes from the TSFlags field.
Evan Chengb43216e2007-02-01 10:16:15 +0000339 unsigned Bits = 0;
340 unsigned Scale = 1;
Evan Chenga8e29892007-01-19 07:51:42 +0000341 unsigned TSFlags = I->getInstrDescriptor()->TSFlags;
342 switch (TSFlags & ARMII::AddrModeMask) {
343 default:
344 // Constant pool entries can reach anything.
345 if (I->getOpcode() == ARM::CONSTPOOL_ENTRY)
346 continue;
347 assert(0 && "Unknown addressing mode for CP reference!");
348 case ARMII::AddrMode1: // AM1: 8 bits << 2
Evan Chengb43216e2007-02-01 10:16:15 +0000349 Bits = 8;
350 Scale = 4; // Taking the address of a CP entry.
Evan Chenga8e29892007-01-19 07:51:42 +0000351 break;
352 case ARMII::AddrMode2:
Evan Cheng556f33c2007-02-01 20:44:52 +0000353 Bits = 12; // +-offset_12
Evan Chenga8e29892007-01-19 07:51:42 +0000354 break;
355 case ARMII::AddrMode3:
Evan Cheng556f33c2007-02-01 20:44:52 +0000356 Bits = 8; // +-offset_8
Evan Chenga8e29892007-01-19 07:51:42 +0000357 break;
358 // addrmode4 has no immediate offset.
359 case ARMII::AddrMode5:
Evan Chengb43216e2007-02-01 10:16:15 +0000360 Bits = 8;
361 Scale = 4; // +-(offset_8*4)
Evan Chenga8e29892007-01-19 07:51:42 +0000362 break;
363 case ARMII::AddrModeT1:
Evan Chengb43216e2007-02-01 10:16:15 +0000364 Bits = 5; // +offset_5
Evan Chenga8e29892007-01-19 07:51:42 +0000365 break;
366 case ARMII::AddrModeT2:
Evan Chengb43216e2007-02-01 10:16:15 +0000367 Bits = 5;
368 Scale = 2; // +(offset_5*2)
Evan Chenga8e29892007-01-19 07:51:42 +0000369 break;
370 case ARMII::AddrModeT4:
Evan Chengb43216e2007-02-01 10:16:15 +0000371 Bits = 5;
372 Scale = 4; // +(offset_5*4)
Evan Chenga8e29892007-01-19 07:51:42 +0000373 break;
Evan Cheng012f2d92007-01-24 08:53:17 +0000374 case ARMII::AddrModeTs:
Evan Chengb43216e2007-02-01 10:16:15 +0000375 Bits = 8;
376 Scale = 4; // +(offset_8*4)
Evan Cheng012f2d92007-01-24 08:53:17 +0000377 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000378 }
Evan Chengb43216e2007-02-01 10:16:15 +0000379
Evan Chenga8e29892007-01-19 07:51:42 +0000380 // Remember that this is a user of a CP entry.
Evan Chengc99ef082007-02-09 20:54:44 +0000381 unsigned CPI = I->getOperand(op).getConstantPoolIndex();
382 MachineInstr *CPEMI = CPEMIs[CPI];
Evan Cheng556f33c2007-02-01 20:44:52 +0000383 unsigned MaxOffs = ((1 << Bits)-1) * Scale;
Evan Chenga8e29892007-01-19 07:51:42 +0000384 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs));
Evan Chengc99ef082007-02-09 20:54:44 +0000385
386 // Increment corresponding CPEntry reference count.
387 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
388 assert(CPE && "Cannot find a corresponding CPEntry!");
389 CPE->RefCount++;
Evan Chenga8e29892007-01-19 07:51:42 +0000390
391 // Instructions can only use one CP entry, don't bother scanning the
392 // rest of the operands.
393 break;
394 }
395 }
Evan Cheng2021abe2007-02-01 01:09:47 +0000396
397 // In thumb mode, if this block is a constpool island, pessmisticly assume
398 // it needs to be padded by two byte so it's aligned on 4 byte boundary.
399 if (AFI->isThumbFunction() &&
Evan Cheng05cc4242007-02-02 19:09:19 +0000400 !MBB.empty() &&
Evan Cheng2021abe2007-02-01 01:09:47 +0000401 MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY)
402 MBBSize += 2;
403
Evan Chenga8e29892007-01-19 07:51:42 +0000404 BBSizes.push_back(MBBSize);
405 }
406}
407
Evan Chenga8e29892007-01-19 07:51:42 +0000408/// GetOffsetOf - Return the current offset of the specified machine instruction
409/// from the start of the function. This offset changes as stuff is moved
410/// around inside the function.
411unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
412 MachineBasicBlock *MBB = MI->getParent();
413
414 // The offset is composed of two things: the sum of the sizes of all MBB's
415 // before this instruction's block, and the offset from the start of the block
416 // it is in.
417 unsigned Offset = 0;
418
419 // Sum block sizes before MBB.
420 for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB)
421 Offset += BBSizes[BB];
422
423 // Sum instructions before MI in MBB.
424 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
425 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
426 if (&*I == MI) return Offset;
Evan Cheng29836c32007-01-29 23:45:17 +0000427 Offset += ARM::GetInstSize(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000428 }
429}
430
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000431/// GetOffsetOf - Return the current offset of the specified machine BB
432/// from the start of the function. This offset changes as stuff is moved
433/// around inside the function.
434unsigned ARMConstantIslands::GetOffsetOf(MachineBasicBlock *MBB) const {
435 // Sum block sizes before MBB.
436 unsigned Offset = 0;
437 for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB)
438 Offset += BBSizes[BB];
439
440 return Offset;
441}
442
Evan Chenga8e29892007-01-19 07:51:42 +0000443/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
444/// ID.
445static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
446 const MachineBasicBlock *RHS) {
447 return LHS->getNumber() < RHS->getNumber();
448}
449
450/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
451/// machine function, it upsets all of the block numbers. Renumber the blocks
452/// and update the arrays that parallel this numbering.
453void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
454 // Renumber the MBB's to keep them consequtive.
455 NewBB->getParent()->RenumberBlocks(NewBB);
456
457 // Insert a size into BBSizes to align it properly with the (newly
458 // renumbered) block numbers.
459 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
460
461 // Next, update WaterList. Specifically, we need to add NewMBB as having
462 // available water after it.
Evan Chengc99ef082007-02-09 20:54:44 +0000463 SmallVector<MachineBasicBlock*, 8>::iterator IP =
Evan Chenga8e29892007-01-19 07:51:42 +0000464 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
465 CompareMBBNumbers);
466 WaterList.insert(IP, NewBB);
467}
468
469
470/// Split the basic block containing MI into two blocks, which are joined by
471/// an unconditional branch. Update datastructures and renumber blocks to
Evan Cheng0c615842007-01-31 02:22:22 +0000472/// account for this change and returns the newly created block.
473MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
Evan Chenga8e29892007-01-19 07:51:42 +0000474 MachineBasicBlock *OrigBB = MI->getParent();
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000475 bool isThumb = AFI->isThumbFunction();
Evan Chenga8e29892007-01-19 07:51:42 +0000476
477 // Create a new MBB for the code after the OrigBB.
478 MachineBasicBlock *NewBB = new MachineBasicBlock(OrigBB->getBasicBlock());
479 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
480 OrigBB->getParent()->getBasicBlockList().insert(MBBI, NewBB);
481
482 // Splice the instructions starting with MI over to NewBB.
483 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
484
485 // Add an unconditional branch from OrigBB to NewBB.
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000486 // Note the new unconditional branch is not being recorded.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000487 BuildMI(OrigBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000488 NumSplit++;
489
490 // Update the CFG. All succs of OrigBB are now succs of NewBB.
491 while (!OrigBB->succ_empty()) {
492 MachineBasicBlock *Succ = *OrigBB->succ_begin();
493 OrigBB->removeSuccessor(Succ);
494 NewBB->addSuccessor(Succ);
495
496 // This pass should be run after register allocation, so there should be no
497 // PHI nodes to update.
498 assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI)
499 && "PHI nodes should be eliminated by now!");
500 }
501
502 // OrigBB branches to NewBB.
503 OrigBB->addSuccessor(NewBB);
504
505 // Update internal data structures to account for the newly inserted MBB.
506 UpdateForInsertedWaterBlock(NewBB);
507
508 // Figure out how large the first NewMBB is.
509 unsigned NewBBSize = 0;
510 for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
511 I != E; ++I)
Evan Cheng29836c32007-01-29 23:45:17 +0000512 NewBBSize += ARM::GetInstSize(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000513
514 // Set the size of NewBB in BBSizes.
515 BBSizes[NewBB->getNumber()] = NewBBSize;
516
517 // We removed instructions from UserMBB, subtract that off from its size.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000518 // Add 2 or 4 to the block to count the unconditional branch we added to it.
519 BBSizes[OrigBB->getNumber()] -= NewBBSize - (isThumb ? 2 : 4);
Evan Cheng0c615842007-01-31 02:22:22 +0000520
521 return NewBB;
Evan Chenga8e29892007-01-19 07:51:42 +0000522}
523
Evan Chengc0dbec72007-01-31 19:57:44 +0000524/// CPEIsInRange - Returns true is the distance between specific MI and
525/// specific ConstPool entry instruction can fit in MI's displacement field.
526bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, MachineInstr *CPEMI,
527 unsigned MaxDisp) {
528 unsigned PCAdj = AFI->isThumbFunction() ? 4 : 8;
529 unsigned UserOffset = GetOffsetOf(MI) + PCAdj;
Evan Cheng2021abe2007-02-01 01:09:47 +0000530 // In thumb mode, pessmisticly assumes the .align 2 before the first CPE
531 // in the island adds two byte padding.
532 unsigned AlignAdj = AFI->isThumbFunction() ? 2 : 0;
533 unsigned CPEOffset = GetOffsetOf(CPEMI) + AlignAdj;
534
Evan Chengc99ef082007-02-09 20:54:44 +0000535 DOUT << "User of CPE#" << CPEMI->getOperand(0).getImm()
536 << " max delta=" << MaxDisp
537 << " at offset " << int(CPEOffset-UserOffset) << "\t" << *MI;
Evan Chengc0dbec72007-01-31 19:57:44 +0000538
Evan Chenga2e35582007-01-31 23:35:18 +0000539 if (UserOffset <= CPEOffset) {
Evan Chengc0dbec72007-01-31 19:57:44 +0000540 // User before the CPE.
541 if (CPEOffset-UserOffset <= MaxDisp)
542 return true;
543 } else if (!AFI->isThumbFunction()) {
544 // Thumb LDR cannot encode negative offset.
545 if (UserOffset-CPEOffset <= MaxDisp)
546 return true;
547 }
548 return false;
549}
550
Evan Chengc99ef082007-02-09 20:54:44 +0000551/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
552/// unconditionally branches to its only successor.
553static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
554 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
555 return false;
556
557 MachineBasicBlock *Succ = *MBB->succ_begin();
558 MachineBasicBlock *Pred = *MBB->pred_begin();
559 MachineInstr *PredMI = &Pred->back();
560 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB)
561 return PredMI->getOperand(0).getMBB() == Succ;
562 return false;
563}
564
Evan Chenga8e29892007-01-19 07:51:42 +0000565/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
566/// is out-of-range. If so, pick it up the constant pool value and move it some
567/// place in-range.
568bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn, CPUser &U){
569 MachineInstr *UserMI = U.MI;
570 MachineInstr *CPEMI = U.CPEMI;
571
Evan Chenga8e29892007-01-19 07:51:42 +0000572 // Check to see if the CPE is already in-range.
Evan Chengc0dbec72007-01-31 19:57:44 +0000573 if (CPEIsInRange(UserMI, CPEMI, U.MaxDisp))
574 return false;
Evan Cheng0c615842007-01-31 02:22:22 +0000575
576 // Solution guaranteed to work: split the user's MBB right after the user and
Evan Chenga8e29892007-01-19 07:51:42 +0000577 // insert a clone the CPE into the newly created water.
Evan Cheng0c615842007-01-31 02:22:22 +0000578
Evan Cheng934536d2007-01-31 18:19:07 +0000579 MachineBasicBlock *UserMBB = UserMI->getParent();
580 MachineBasicBlock *NewMBB;
581
Evan Cheng0c615842007-01-31 02:22:22 +0000582 // TODO: Search for the best place to split the code. In practice, using
583 // loop nesting information to insert these guys outside of loops would be
584 // sufficient.
Evan Chengb43216e2007-02-01 10:16:15 +0000585 bool isThumb = AFI->isThumbFunction();
Evan Cheng934536d2007-01-31 18:19:07 +0000586 if (&UserMBB->back() == UserMI) {
587 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
588 NewMBB = next(MachineFunction::iterator(UserMBB));
589 // Add an unconditional branch from UserMBB to fallthrough block.
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000590 // Note the new unconditional branch is not being recorded.
Evan Cheng934536d2007-01-31 18:19:07 +0000591 BuildMI(UserMBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewMBB);
592 BBSizes[UserMBB->getNumber()] += isThumb ? 2 : 4;
593 } else {
594 MachineInstr *NextMI = next(MachineBasicBlock::iterator(UserMI));
595 NewMBB = SplitBlockBeforeInstr(NextMI);
596 }
Evan Cheng0c615842007-01-31 02:22:22 +0000597
Evan Chenga8e29892007-01-19 07:51:42 +0000598 // Okay, we know we can put an island before UserMBB now, do it!
599 MachineBasicBlock *NewIsland = new MachineBasicBlock();
Evan Cheng934536d2007-01-31 18:19:07 +0000600 Fn.getBasicBlockList().insert(NewMBB, NewIsland);
Evan Chenga8e29892007-01-19 07:51:42 +0000601
602 // Update internal data structures to account for the newly inserted MBB.
603 UpdateForInsertedWaterBlock(NewIsland);
604
605 // Now that we have an island to add the CPE to, clone the original CPE and
606 // add it to the island.
607 unsigned ID = NextUID++;
608 unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex();
609 unsigned Size = CPEMI->getOperand(2).getImm();
Evan Chengb43216e2007-02-01 10:16:15 +0000610
Evan Chengc99ef082007-02-09 20:54:44 +0000611 // Find the old entry. Eliminate it if it is no longer used.
612 CPEntry *OldCPE = findConstPoolEntry(CPI, CPEMI);
613 assert(OldCPE && "Unexpected!");
614 if (--OldCPE->RefCount == 0) {
615 MachineBasicBlock *OldCPEBB = OldCPE->CPEMI->getParent();
616 if (OldCPEBB->empty()) {
617 // In thumb mode, the size of island is padded by two to compensate for
618 // the alignment requirement.
619 BBSizes[OldCPEBB->getNumber()] = 0;
620 // An island has only one predecessor BB and one successor BB. Check if
621 // this BB's predecessor jumps directly to this BB's successor. This
622 // shouldn't happen currently.
623 assert(!BBIsJumpedOver(OldCPEBB) && "How did this happen?");
624 // FIXME: remove the empty blocks after all the work is done?
625 } else
626 BBSizes[OldCPEBB->getNumber()] -= Size;
627 OldCPE->CPEMI->eraseFromParent();
628 OldCPE->CPEMI = NULL;
629 NumCPEs--;
630 }
631
Evan Chenga8e29892007-01-19 07:51:42 +0000632 // Build a new CPE for this user.
633 U.CPEMI = BuildMI(NewIsland, TII->get(ARM::CONSTPOOL_ENTRY))
634 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
Evan Chengc99ef082007-02-09 20:54:44 +0000635 CPEntries[CPI].push_back(CPEntry(U.CPEMI, CPI, 1));
636 NumCPEs++;
637
Evan Chengb43216e2007-02-01 10:16:15 +0000638 // Compensate for .align 2 in thumb mode.
639 if (isThumb) Size += 2;
Evan Chenga8e29892007-01-19 07:51:42 +0000640 // Increase the size of the island block to account for the new entry.
641 BBSizes[NewIsland->getNumber()] += Size;
642
643 // Finally, change the CPI in the instruction operand to be ID.
644 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
645 if (UserMI->getOperand(i).isConstantPoolIndex()) {
646 UserMI->getOperand(i).setConstantPoolIndex(ID);
647 break;
648 }
649
Evan Chengc99ef082007-02-09 20:54:44 +0000650 DOUT << " Moved CPE to #" << ID << " CPI=" << CPI << "\t" << *UserMI;
Evan Chenga8e29892007-01-19 07:51:42 +0000651
652 return true;
653}
654
Evan Chengc0dbec72007-01-31 19:57:44 +0000655/// BBIsInRange - Returns true is the distance between specific MI and
Evan Cheng43aeab62007-01-26 20:38:26 +0000656/// specific BB can fit in MI's displacement field.
Evan Chengc0dbec72007-01-31 19:57:44 +0000657bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
658 unsigned MaxDisp) {
659 unsigned PCAdj = AFI->isThumbFunction() ? 4 : 8;
660 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
Evan Cheng43aeab62007-01-26 20:38:26 +0000661 unsigned DestOffset = GetOffsetOf(DestBB);
662
Evan Chengc99ef082007-02-09 20:54:44 +0000663 DOUT << "Branch of destination BB#" << DestBB->getNumber()
664 << " from BB#" << MI->getParent()->getNumber()
665 << " max delta=" << MaxDisp
666 << " at offset " << int(DestOffset-BrOffset) << "\t" << *MI;
Evan Chengc0dbec72007-01-31 19:57:44 +0000667
Evan Chenga2e35582007-01-31 23:35:18 +0000668 if (BrOffset <= DestOffset) {
Evan Chengb43216e2007-02-01 10:16:15 +0000669 if (DestOffset - BrOffset <= MaxDisp)
Evan Cheng43aeab62007-01-26 20:38:26 +0000670 return true;
671 } else {
672 if (BrOffset - DestOffset <= MaxDisp)
673 return true;
674 }
675 return false;
676}
677
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000678/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
679/// away to fit in its displacement field.
680bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000681 MachineInstr *MI = Br.MI;
682 MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock();
683
Evan Chengc0dbec72007-01-31 19:57:44 +0000684 // Check to see if the DestBB is already in-range.
685 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
Evan Cheng43aeab62007-01-26 20:38:26 +0000686 return false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000687
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000688 if (!Br.isCond)
689 return FixUpUnconditionalBr(Fn, Br);
690 return FixUpConditionalBr(Fn, Br);
691}
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000692
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000693/// FixUpUnconditionalBr - Fix up an unconditional branches whose destination is
Evan Chengc99ef082007-02-09 20:54:44 +0000694/// too far away to fit in its displacement field. If LR register ha been
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000695/// spilled in the epilogue, then we can use BL to implement a far jump.
696/// Otherwise, add a intermediate branch instruction to to a branch.
697bool
698ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br) {
699 MachineInstr *MI = Br.MI;
700 MachineBasicBlock *MBB = MI->getParent();
701 assert(AFI->isThumbFunction() && "Expected a Thumb function!");
702
703 // Use BL to implement far jump.
704 Br.MaxDisp = (1 << 21) * 2;
705 MI->setInstrDescriptor(TII->get(ARM::tBfar));
706 BBSizes[MBB->getNumber()] += 2;
707 HasFarJump = true;
708 NumUBrFixed++;
Evan Chengbd5d3db2007-02-03 02:08:34 +0000709
Evan Chengc99ef082007-02-09 20:54:44 +0000710 DOUT << " Changed B to long jump " << *MI;
Evan Chengbd5d3db2007-02-03 02:08:34 +0000711
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000712 return true;
713}
714
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000715/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in the
716/// specific unconditional branch instruction.
717static inline unsigned getUnconditionalBrDisp(int Opc) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000718 return (Opc == ARM::tB) ? (1<<10)*2 : (1<<23)*4;
719}
720
721/// FixUpConditionalBr - Fix up a conditional branches whose destination is too
722/// far away to fit in its displacement field. It is converted to an inverse
723/// conditional branch + an unconditional branch to the destination.
724bool
725ARMConstantIslands::FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br) {
726 MachineInstr *MI = Br.MI;
727 MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock();
728
729 // Add a unconditional branch to the destination and invert the branch
730 // condition to jump over it:
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000731 // blt L1
732 // =>
733 // bge L2
734 // b L1
735 // L2:
736 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImmedValue();
737 CC = ARMCC::getOppositeCondition(CC);
738
739 // If the branch is at the end of its MBB and that has a fall-through block,
740 // direct the updated conditional branch to the fall-through block. Otherwise,
741 // split the MBB before the next instruction.
742 MachineBasicBlock *MBB = MI->getParent();
Evan Chengbd5d3db2007-02-03 02:08:34 +0000743 MachineInstr *BMI = &MBB->back();
744 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Evan Cheng43aeab62007-01-26 20:38:26 +0000745
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000746 NumCBrFixed++;
Evan Chengbd5d3db2007-02-03 02:08:34 +0000747 if (BMI != MI) {
Evan Cheng43aeab62007-01-26 20:38:26 +0000748 if (next(MachineBasicBlock::iterator(MI)) == MBB->back() &&
Evan Chengbd5d3db2007-02-03 02:08:34 +0000749 BMI->getOpcode() == Br.UncondBr) {
Evan Cheng43aeab62007-01-26 20:38:26 +0000750 // Last MI in the BB is a unconditional branch. Can we simply invert the
751 // condition and swap destinations:
752 // beq L1
753 // b L2
754 // =>
755 // bne L2
756 // b L1
Evan Chengbd5d3db2007-02-03 02:08:34 +0000757 MachineBasicBlock *NewDest = BMI->getOperand(0).getMachineBasicBlock();
Evan Chengc0dbec72007-01-31 19:57:44 +0000758 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
Evan Chengc99ef082007-02-09 20:54:44 +0000759 DOUT << " Invert Bcc condition and swap its destination with " << *BMI;
Evan Chengbd5d3db2007-02-03 02:08:34 +0000760 BMI->getOperand(0).setMachineBasicBlock(DestBB);
Evan Cheng43aeab62007-01-26 20:38:26 +0000761 MI->getOperand(0).setMachineBasicBlock(NewDest);
762 MI->getOperand(1).setImm(CC);
763 return true;
764 }
765 }
766 }
767
768 if (NeedSplit) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000769 SplitBlockBeforeInstr(MI);
Evan Chengdd353b82007-01-26 02:02:39 +0000770 // No need for the branch to the next block. We're adding a unconditional
771 // branch to the destination.
772 MBB->back().eraseFromParent();
773 }
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000774 MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
Evan Chengbd5d3db2007-02-03 02:08:34 +0000775
Evan Chengc99ef082007-02-09 20:54:44 +0000776 DOUT << " Insert B to BB#" << DestBB->getNumber()
777 << " also invert condition and change dest. to BB#"
778 << NextBB->getNumber() << "\n";
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000779
780 // Insert a unconditional branch and replace the conditional branch.
781 // Also update the ImmBranch as well as adding a new entry for the new branch.
Evan Chengdd353b82007-01-26 02:02:39 +0000782 BuildMI(MBB, TII->get(MI->getOpcode())).addMBB(NextBB).addImm(CC);
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000783 Br.MI = &MBB->back();
784 BuildMI(MBB, TII->get(Br.UncondBr)).addMBB(DestBB);
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000785 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
Evan Chenga0bf7942007-01-25 23:31:04 +0000786 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000787 MI->eraseFromParent();
788
789 // Increase the size of MBB to account for the new unconditional branch.
Evan Cheng29836c32007-01-29 23:45:17 +0000790 BBSizes[MBB->getNumber()] += ARM::GetInstSize(&MBB->back());
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000791 return true;
792}
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000793
794
795/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
796/// LR / restores LR to pc.
797bool ARMConstantIslands::UndoLRSpillRestore() {
798 bool MadeChange = false;
799 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
800 MachineInstr *MI = PushPopMIs[i];
801 if (MI->getNumOperands() == 1) {
802 if (MI->getOpcode() == ARM::tPOP_RET &&
803 MI->getOperand(0).getReg() == ARM::PC)
804 BuildMI(MI->getParent(), TII->get(ARM::tBX_RET));
805 MI->eraseFromParent();
806 MadeChange = true;
807 }
808 }
809 return MadeChange;
810}