blob: 782765a4335185cbda55a845eedd6386caff51e2 [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 Cheng5657c012009-07-29 02:18:14 +000024#include "llvm/CodeGen/MachineJumpTableInfo.h"
Evan Chenga8e29892007-01-19 07:51:42 +000025#include "llvm/Target/TargetData.h"
26#include "llvm/Target/TargetMachine.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Evan Chengc99ef082007-02-09 20:54:44 +000030#include "llvm/ADT/SmallVector.h"
Evan Chenga8e29892007-01-19 07:51:42 +000031#include "llvm/ADT/STLExtras.h"
32#include "llvm/ADT/Statistic.h"
Evan Chenga8e29892007-01-19 07:51:42 +000033using namespace llvm;
34
Evan Chengc99ef082007-02-09 20:54:44 +000035STATISTIC(NumCPEs, "Number of constpool entries");
Evan Chengd1b2c1e2007-01-30 01:18:38 +000036STATISTIC(NumSplit, "Number of uncond branches inserted");
37STATISTIC(NumCBrFixed, "Number of cond branches fixed");
38STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
Evan Cheng5657c012009-07-29 02:18:14 +000039STATISTIC(NumTBs, "Number of table branches generated");
Evan Chenga8e29892007-01-19 07:51:42 +000040
41namespace {
Dale Johannesen88e37ae2007-02-23 05:02:36 +000042 /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
Evan Chenga8e29892007-01-19 07:51:42 +000043 /// requires constant pool entries to be scattered among the instructions
44 /// inside a function. To do this, it completely ignores the normal LLVM
Dale Johannesen88e37ae2007-02-23 05:02:36 +000045 /// constant pool; instead, it places constants wherever it feels like with
Evan Chenga8e29892007-01-19 07:51:42 +000046 /// special instructions.
47 ///
48 /// The terminology used in this pass includes:
49 /// Islands - Clumps of constants placed in the function.
50 /// Water - Potential places where an island could be formed.
51 /// CPE - A constant pool entry that has been placed somewhere, which
52 /// tracks a list of users.
53 class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass {
Evan Chenga8e29892007-01-19 07:51:42 +000054 /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
Dale Johannesen8593e412007-04-29 19:19:30 +000055 /// by MBB Number. The two-byte pads required for Thumb alignment are
56 /// counted as part of the following block (i.e., the offset and size for
57 /// a padded block will both be ==2 mod 4).
Evan Chenge03cff62007-02-09 23:59:14 +000058 std::vector<unsigned> BBSizes;
Bob Wilson84945262009-05-12 17:09:30 +000059
Dale Johannesen99c49a42007-02-25 00:47:03 +000060 /// BBOffsets - the offset of each MBB in bytes, starting from 0.
Dale Johannesen8593e412007-04-29 19:19:30 +000061 /// The two-byte pads required for Thumb alignment are counted as part of
62 /// the following block.
Dale Johannesen99c49a42007-02-25 00:47:03 +000063 std::vector<unsigned> BBOffsets;
64
Evan Chenga8e29892007-01-19 07:51:42 +000065 /// WaterList - A sorted list of basic blocks where islands could be placed
66 /// (i.e. blocks that don't fall through to the following block, due
67 /// to a return, unreachable, or unconditional branch).
Evan Chenge03cff62007-02-09 23:59:14 +000068 std::vector<MachineBasicBlock*> WaterList;
Evan Chengc99ef082007-02-09 20:54:44 +000069
Evan Chenga8e29892007-01-19 07:51:42 +000070 /// CPUser - One user of a constant pool, keeping the machine instruction
71 /// pointer, the constant pool being referenced, and the max displacement
72 /// allowed from the instruction to the CP.
73 struct CPUser {
74 MachineInstr *MI;
75 MachineInstr *CPEMI;
76 unsigned MaxDisp;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +000077 bool NegOk;
Evan Chengd3d9d662009-07-23 18:27:47 +000078 bool IsSoImm;
79 CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
80 bool neg, bool soimm)
81 : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) {}
Evan Chenga8e29892007-01-19 07:51:42 +000082 };
Bob Wilson84945262009-05-12 17:09:30 +000083
Evan Chenga8e29892007-01-19 07:51:42 +000084 /// CPUsers - Keep track of all of the machine instructions that use various
85 /// constant pools and their max displacement.
Evan Chenge03cff62007-02-09 23:59:14 +000086 std::vector<CPUser> CPUsers;
Bob Wilson84945262009-05-12 17:09:30 +000087
Evan Chengc99ef082007-02-09 20:54:44 +000088 /// CPEntry - One per constant pool entry, keeping the machine instruction
89 /// pointer, the constpool index, and the number of CPUser's which
90 /// reference this entry.
91 struct CPEntry {
92 MachineInstr *CPEMI;
93 unsigned CPI;
94 unsigned RefCount;
95 CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
96 : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
97 };
98
99 /// CPEntries - Keep track of all of the constant pool entry machine
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000100 /// instructions. For each original constpool index (i.e. those that
101 /// existed upon entry to this pass), it keeps a vector of entries.
102 /// Original elements are cloned as we go along; the clones are
103 /// put in the vector of the original element, but have distinct CPIs.
Evan Chengc99ef082007-02-09 20:54:44 +0000104 std::vector<std::vector<CPEntry> > CPEntries;
Bob Wilson84945262009-05-12 17:09:30 +0000105
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000106 /// ImmBranch - One per immediate branch, keeping the machine instruction
107 /// pointer, conditional or unconditional, the max displacement,
108 /// and (if isCond is true) the corresponding unconditional branch
109 /// opcode.
110 struct ImmBranch {
111 MachineInstr *MI;
Evan Chengc2854142007-01-25 23:18:59 +0000112 unsigned MaxDisp : 31;
113 bool isCond : 1;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000114 int UncondBr;
Evan Chengc2854142007-01-25 23:18:59 +0000115 ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
116 : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000117 };
118
Evan Cheng2706f972007-05-16 05:14:06 +0000119 /// ImmBranches - Keep track of all the immediate branch instructions.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000120 ///
Evan Chenge03cff62007-02-09 23:59:14 +0000121 std::vector<ImmBranch> ImmBranches;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000122
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000123 /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
124 ///
Evan Chengc99ef082007-02-09 20:54:44 +0000125 SmallVector<MachineInstr*, 4> PushPopMIs;
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000126
Evan Cheng5657c012009-07-29 02:18:14 +0000127 /// T2JumpTables - Keep track of all the Thumb2 jumptable instructions.
128 SmallVector<MachineInstr*, 4> T2JumpTables;
129
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000130 /// HasFarJump - True if any far jump instruction has been emitted during
131 /// the branch fix up pass.
132 bool HasFarJump;
133
Evan Chenga8e29892007-01-19 07:51:42 +0000134 const TargetInstrInfo *TII;
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000135 const ARMSubtarget *STI;
Dale Johannesen8593e412007-04-29 19:19:30 +0000136 ARMFunctionInfo *AFI;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000137 bool isThumb;
Evan Chengd3d9d662009-07-23 18:27:47 +0000138 bool isThumb1;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000139 bool isThumb2;
Evan Chenga8e29892007-01-19 07:51:42 +0000140 public:
Devang Patel19974732007-05-03 01:11:54 +0000141 static char ID;
Dan Gohmanae73dc12008-09-04 17:05:41 +0000142 ARMConstantIslands() : MachineFunctionPass(&ID) {}
Devang Patel794fd752007-05-01 21:15:47 +0000143
Evan Cheng5657c012009-07-29 02:18:14 +0000144 virtual bool runOnMachineFunction(MachineFunction &MF);
Evan Chenga8e29892007-01-19 07:51:42 +0000145
146 virtual const char *getPassName() const {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000147 return "ARM constant island placement and branch shortening pass";
Evan Chenga8e29892007-01-19 07:51:42 +0000148 }
Bob Wilson84945262009-05-12 17:09:30 +0000149
Evan Chenga8e29892007-01-19 07:51:42 +0000150 private:
Evan Cheng5657c012009-07-29 02:18:14 +0000151 void DoInitialPlacement(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000152 std::vector<MachineInstr*> &CPEMIs);
Evan Chengc99ef082007-02-09 20:54:44 +0000153 CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
Evan Cheng5657c012009-07-29 02:18:14 +0000154 void InitialFunctionScan(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000155 const std::vector<MachineInstr*> &CPEMIs);
Evan Cheng0c615842007-01-31 02:22:22 +0000156 MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
Evan Chenga8e29892007-01-19 07:51:42 +0000157 void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000158 void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta);
Evan Chenged884f32007-04-03 23:39:48 +0000159 bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000160 int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
Bob Wilson84945262009-05-12 17:09:30 +0000161 bool LookForWater(CPUser&U, unsigned UserOffset,
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000162 MachineBasicBlock** NewMBB);
Bob Wilson84945262009-05-12 17:09:30 +0000163 MachineBasicBlock* AcceptWater(MachineBasicBlock *WaterBB,
Dale Johannesen8593e412007-04-29 19:19:30 +0000164 std::vector<MachineBasicBlock*>::iterator IP);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000165 void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset,
166 MachineBasicBlock** NewMBB);
Evan Cheng5657c012009-07-29 02:18:14 +0000167 bool HandleConstantPoolUser(MachineFunction &MF, unsigned CPUserIndex);
Evan Chenged884f32007-04-03 23:39:48 +0000168 void RemoveDeadCPEMI(MachineInstr *CPEMI);
169 bool RemoveUnusedCPEntries();
Bob Wilson84945262009-05-12 17:09:30 +0000170 bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000171 MachineInstr *CPEMI, unsigned Disp, bool NegOk,
172 bool DoDump = false);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000173 bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water,
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000174 CPUser &U);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000175 bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000176 unsigned Disp, bool NegativeOK, bool IsSoImm = false);
Evan Chengc0dbec72007-01-31 19:57:44 +0000177 bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
Evan Cheng5657c012009-07-29 02:18:14 +0000178 bool FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br);
179 bool FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br);
180 bool FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br);
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000181 bool UndoLRSpillRestore();
Evan Cheng5657c012009-07-29 02:18:14 +0000182 bool OptimizeThumb2JumpTables(MachineFunction &MF);
Evan Chenga8e29892007-01-19 07:51:42 +0000183
Evan Chenga8e29892007-01-19 07:51:42 +0000184 unsigned GetOffsetOf(MachineInstr *MI) const;
Dale Johannesen8593e412007-04-29 19:19:30 +0000185 void dumpBBs();
Evan Cheng5657c012009-07-29 02:18:14 +0000186 void verify(MachineFunction &MF);
Evan Chenga8e29892007-01-19 07:51:42 +0000187 };
Devang Patel19974732007-05-03 01:11:54 +0000188 char ARMConstantIslands::ID = 0;
Evan Chenga8e29892007-01-19 07:51:42 +0000189}
190
Dale Johannesen8593e412007-04-29 19:19:30 +0000191/// verify - check BBOffsets, BBSizes, alignment of islands
Evan Cheng5657c012009-07-29 02:18:14 +0000192void ARMConstantIslands::verify(MachineFunction &MF) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000193 assert(BBOffsets.size() == BBSizes.size());
194 for (unsigned i = 1, e = BBOffsets.size(); i != e; ++i)
195 assert(BBOffsets[i-1]+BBSizes[i-1] == BBOffsets[i]);
Evan Chengd3d9d662009-07-23 18:27:47 +0000196 if (!isThumb)
197 return;
198#ifndef NDEBUG
Evan Cheng5657c012009-07-29 02:18:14 +0000199 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chengd3d9d662009-07-23 18:27:47 +0000200 MBBI != E; ++MBBI) {
201 MachineBasicBlock *MBB = MBBI;
202 if (!MBB->empty() &&
203 MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
204 unsigned MBBId = MBB->getNumber();
205 assert((BBOffsets[MBBId]%4 == 0 && BBSizes[MBBId]%4 == 0) ||
206 (BBOffsets[MBBId]%4 != 0 && BBSizes[MBBId]%4 != 0));
Dale Johannesen8593e412007-04-29 19:19:30 +0000207 }
208 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000209#endif
Dale Johannesen8593e412007-04-29 19:19:30 +0000210}
211
212/// print block size and offset information - debugging
213void ARMConstantIslands::dumpBBs() {
214 for (unsigned J = 0, E = BBOffsets.size(); J !=E; ++J) {
Bob Wilson84945262009-05-12 17:09:30 +0000215 DOUT << "block " << J << " offset " << BBOffsets[J] <<
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000216 " size " << BBSizes[J] << "\n";
Dale Johannesen8593e412007-04-29 19:19:30 +0000217 }
218}
219
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000220/// createARMConstantIslandPass - returns an instance of the constpool
221/// island pass.
Evan Chenga8e29892007-01-19 07:51:42 +0000222FunctionPass *llvm::createARMConstantIslandPass() {
223 return new ARMConstantIslands();
224}
225
Evan Cheng5657c012009-07-29 02:18:14 +0000226bool ARMConstantIslands::runOnMachineFunction(MachineFunction &MF) {
227 MachineConstantPool &MCP = *MF.getConstantPool();
Bob Wilson84945262009-05-12 17:09:30 +0000228
Evan Cheng5657c012009-07-29 02:18:14 +0000229 TII = MF.getTarget().getInstrInfo();
230 AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000231 STI = &MF.getTarget().getSubtarget<ARMSubtarget>();
232
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000233 isThumb = AFI->isThumbFunction();
Evan Chengd3d9d662009-07-23 18:27:47 +0000234 isThumb1 = AFI->isThumb1OnlyFunction();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000235 isThumb2 = AFI->isThumb2Function();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000236
237 HasFarJump = false;
238
Evan Chenga8e29892007-01-19 07:51:42 +0000239 // Renumber all of the machine basic blocks in the function, guaranteeing that
240 // the numbers agree with the position of the block in the function.
Evan Cheng5657c012009-07-29 02:18:14 +0000241 MF.RenumberBlocks();
Evan Chenga8e29892007-01-19 07:51:42 +0000242
Evan Chengd26b14c2009-07-31 18:28:05 +0000243 // Thumb1 functions containing constant pools get 4-byte alignment.
Evan Chengd3d9d662009-07-23 18:27:47 +0000244 // This is so we can keep exact track of where the alignment padding goes.
245
Evan Chengd26b14c2009-07-31 18:28:05 +0000246 // Set default. Thumb1 function is 2-byte aligned, ARM and Thumb2 are 4-byte
Evan Chengd3d9d662009-07-23 18:27:47 +0000247 // aligned.
248 AFI->setAlign(isThumb1 ? 1U : 2U);
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000249
Evan Chenga8e29892007-01-19 07:51:42 +0000250 // Perform the initial placement of the constant pool entries. To start with,
251 // we put them all at the end of the function.
Evan Chenge03cff62007-02-09 23:59:14 +0000252 std::vector<MachineInstr*> CPEMIs;
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000253 if (!MCP.isEmpty()) {
Evan Cheng5657c012009-07-29 02:18:14 +0000254 DoInitialPlacement(MF, CPEMIs);
Evan Chengd3d9d662009-07-23 18:27:47 +0000255 if (isThumb1)
Dale Johannesen56c42ef2007-04-23 20:09:04 +0000256 AFI->setAlign(2U);
257 }
Bob Wilson84945262009-05-12 17:09:30 +0000258
Evan Chenga8e29892007-01-19 07:51:42 +0000259 /// The next UID to take is the first unused one.
Evan Chengf1bbb952008-11-08 00:51:41 +0000260 AFI->initConstPoolEntryUId(CPEMIs.size());
Bob Wilson84945262009-05-12 17:09:30 +0000261
Evan Chenga8e29892007-01-19 07:51:42 +0000262 // Do the initial scan of the function, building up information about the
263 // sizes of each block, the location of all the water, and finding all of the
264 // constant pool users.
Evan Cheng5657c012009-07-29 02:18:14 +0000265 InitialFunctionScan(MF, CPEMIs);
Evan Chenga8e29892007-01-19 07:51:42 +0000266 CPEMIs.clear();
Bob Wilson84945262009-05-12 17:09:30 +0000267
Evan Chenged884f32007-04-03 23:39:48 +0000268 /// Remove dead constant pool entries.
269 RemoveUnusedCPEntries();
270
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000271 // Iteratively place constant pool entries and fix up branches until there
272 // is no change.
273 bool MadeChange = false;
274 while (true) {
275 bool Change = false;
Evan Chenga8e29892007-01-19 07:51:42 +0000276 for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
Evan Cheng5657c012009-07-29 02:18:14 +0000277 Change |= HandleConstantPoolUser(MF, i);
Evan Cheng82020102007-07-10 22:00:16 +0000278 DEBUG(dumpBBs());
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000279 for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
Evan Cheng5657c012009-07-29 02:18:14 +0000280 Change |= FixUpImmediateBr(MF, ImmBranches[i]);
Evan Cheng82020102007-07-10 22:00:16 +0000281 DEBUG(dumpBBs());
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000282 if (!Change)
283 break;
284 MadeChange = true;
285 }
Evan Chenged884f32007-04-03 23:39:48 +0000286
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000287 // Let's see if we can use tbb / tbh to do jump tables.
288 MadeChange |= OptimizeThumb2JumpTables(MF);
289
Dale Johannesen8593e412007-04-29 19:19:30 +0000290 // After a while, this might be made debug-only, but it is not expensive.
Evan Cheng5657c012009-07-29 02:18:14 +0000291 verify(MF);
Dale Johannesen8593e412007-04-29 19:19:30 +0000292
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000293 // If LR has been forced spilled and no far jumps (i.e. BL) has been issued.
294 // Undo the spill / restore of LR if possible.
Evan Cheng5657c012009-07-29 02:18:14 +0000295 if (isThumb && !HasFarJump && AFI->isLRSpilledForFarJump())
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000296 MadeChange |= UndoLRSpillRestore();
297
Evan Chenga8e29892007-01-19 07:51:42 +0000298 BBSizes.clear();
Dale Johannesen99c49a42007-02-25 00:47:03 +0000299 BBOffsets.clear();
Evan Chenga8e29892007-01-19 07:51:42 +0000300 WaterList.clear();
301 CPUsers.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000302 CPEntries.clear();
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000303 ImmBranches.clear();
Evan Chengc99ef082007-02-09 20:54:44 +0000304 PushPopMIs.clear();
Evan Cheng5657c012009-07-29 02:18:14 +0000305 T2JumpTables.clear();
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000306
307 return MadeChange;
Evan Chenga8e29892007-01-19 07:51:42 +0000308}
309
310/// DoInitialPlacement - Perform the initial placement of the constant pool
311/// entries. To start with, we put them all at the end of the function.
Evan Cheng5657c012009-07-29 02:18:14 +0000312void ARMConstantIslands::DoInitialPlacement(MachineFunction &MF,
Bob Wilson84945262009-05-12 17:09:30 +0000313 std::vector<MachineInstr*> &CPEMIs) {
Evan Chenga8e29892007-01-19 07:51:42 +0000314 // Create the basic block to hold the CPE's.
Evan Cheng5657c012009-07-29 02:18:14 +0000315 MachineBasicBlock *BB = MF.CreateMachineBasicBlock();
316 MF.push_back(BB);
Bob Wilson84945262009-05-12 17:09:30 +0000317
Evan Chenga8e29892007-01-19 07:51:42 +0000318 // Add all of the constants from the constant pool to the end block, use an
319 // identity mapping of CPI's to CPE's.
320 const std::vector<MachineConstantPoolEntry> &CPs =
Evan Cheng5657c012009-07-29 02:18:14 +0000321 MF.getConstantPool()->getConstants();
Bob Wilson84945262009-05-12 17:09:30 +0000322
Evan Cheng5657c012009-07-29 02:18:14 +0000323 const TargetData &TD = *MF.getTarget().getTargetData();
Evan Chenga8e29892007-01-19 07:51:42 +0000324 for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
Duncan Sands777d2302009-05-09 07:06:46 +0000325 unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
Evan Chenga8e29892007-01-19 07:51:42 +0000326 // Verify that all constant pool entries are a multiple of 4 bytes. If not,
327 // we would have to pad them out or something so that instructions stay
328 // aligned.
329 assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
330 MachineInstr *CPEMI =
Dale Johannesenb6728402009-02-13 02:25:56 +0000331 BuildMI(BB, DebugLoc::getUnknownLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
Evan Chenga8e29892007-01-19 07:51:42 +0000332 .addImm(i).addConstantPoolIndex(i).addImm(Size);
333 CPEMIs.push_back(CPEMI);
Evan Chengc99ef082007-02-09 20:54:44 +0000334
335 // Add a new CPEntry, but no corresponding CPUser yet.
336 std::vector<CPEntry> CPEs;
337 CPEs.push_back(CPEntry(CPEMI, i));
338 CPEntries.push_back(CPEs);
339 NumCPEs++;
340 DOUT << "Moved CPI#" << i << " to end of function as #" << i << "\n";
Evan Chenga8e29892007-01-19 07:51:42 +0000341 }
342}
343
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000344/// BBHasFallthrough - Return true if the specified basic block can fallthrough
Evan Chenga8e29892007-01-19 07:51:42 +0000345/// into the block immediately after it.
346static bool BBHasFallthrough(MachineBasicBlock *MBB) {
347 // Get the next machine basic block in the function.
348 MachineFunction::iterator MBBI = MBB;
349 if (next(MBBI) == MBB->getParent()->end()) // Can't fall off end of function.
350 return false;
Bob Wilson84945262009-05-12 17:09:30 +0000351
Evan Chenga8e29892007-01-19 07:51:42 +0000352 MachineBasicBlock *NextBB = next(MBBI);
353 for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
354 E = MBB->succ_end(); I != E; ++I)
355 if (*I == NextBB)
356 return true;
Bob Wilson84945262009-05-12 17:09:30 +0000357
Evan Chenga8e29892007-01-19 07:51:42 +0000358 return false;
359}
360
Evan Chengc99ef082007-02-09 20:54:44 +0000361/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
362/// look up the corresponding CPEntry.
363ARMConstantIslands::CPEntry
364*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
365 const MachineInstr *CPEMI) {
366 std::vector<CPEntry> &CPEs = CPEntries[CPI];
367 // Number of entries per constpool index should be small, just do a
368 // linear search.
369 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
370 if (CPEs[i].CPEMI == CPEMI)
371 return &CPEs[i];
372 }
373 return NULL;
374}
375
Evan Chenga8e29892007-01-19 07:51:42 +0000376/// InitialFunctionScan - Do the initial scan of the function, building up
377/// information about the sizes of each block, the location of all the water,
378/// and finding all of the constant pool users.
Evan Cheng5657c012009-07-29 02:18:14 +0000379void ARMConstantIslands::InitialFunctionScan(MachineFunction &MF,
Evan Chenge03cff62007-02-09 23:59:14 +0000380 const std::vector<MachineInstr*> &CPEMIs) {
Dale Johannesen99c49a42007-02-25 00:47:03 +0000381 unsigned Offset = 0;
Evan Cheng5657c012009-07-29 02:18:14 +0000382 for (MachineFunction::iterator MBBI = MF.begin(), E = MF.end();
Evan Chenga8e29892007-01-19 07:51:42 +0000383 MBBI != E; ++MBBI) {
384 MachineBasicBlock &MBB = *MBBI;
Bob Wilson84945262009-05-12 17:09:30 +0000385
Evan Chenga8e29892007-01-19 07:51:42 +0000386 // If this block doesn't fall through into the next MBB, then this is
387 // 'water' that a constant pool island could be placed.
388 if (!BBHasFallthrough(&MBB))
389 WaterList.push_back(&MBB);
Bob Wilson84945262009-05-12 17:09:30 +0000390
Evan Chenga8e29892007-01-19 07:51:42 +0000391 unsigned MBBSize = 0;
392 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
393 I != E; ++I) {
394 // Add instruction size to MBBSize.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000395 MBBSize += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000396
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000397 int Opc = I->getOpcode();
Chris Lattner749c6f62008-01-07 07:27:27 +0000398 if (I->getDesc().isBranch()) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000399 bool isCond = false;
400 unsigned Bits = 0;
401 unsigned Scale = 1;
402 int UOpc = Opc;
403 switch (Opc) {
Evan Cheng5657c012009-07-29 02:18:14 +0000404 default:
405 continue; // Ignore other JT branches
Dale Johannesen8593e412007-04-29 19:19:30 +0000406 case ARM::tBR_JTr:
Evan Cheng66ac5312009-07-25 00:33:29 +0000407 // A Thumb1 table jump may involve padding; for the offsets to
Dale Johannesen8593e412007-04-29 19:19:30 +0000408 // be right, functions containing these must be 4-byte aligned.
409 AFI->setAlign(2U);
410 if ((Offset+MBBSize)%4 != 0)
Evan Cheng5657c012009-07-29 02:18:14 +0000411 // FIXME: Add a pseudo ALIGN instruction instead.
Dale Johannesen8593e412007-04-29 19:19:30 +0000412 MBBSize += 2; // padding
413 continue; // Does not get an entry in ImmBranches
Evan Cheng5657c012009-07-29 02:18:14 +0000414 case ARM::t2BR_JT:
415 T2JumpTables.push_back(I);
416 continue; // Does not get an entry in ImmBranches
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000417 case ARM::Bcc:
418 isCond = true;
419 UOpc = ARM::B;
420 // Fallthrough
421 case ARM::B:
422 Bits = 24;
423 Scale = 4;
424 break;
425 case ARM::tBcc:
426 isCond = true;
427 UOpc = ARM::tB;
428 Bits = 8;
429 Scale = 2;
430 break;
431 case ARM::tB:
432 Bits = 11;
433 Scale = 2;
434 break;
David Goodwin5e47a9a2009-06-30 18:04:13 +0000435 case ARM::t2Bcc:
436 isCond = true;
437 UOpc = ARM::t2B;
438 Bits = 20;
439 Scale = 2;
440 break;
441 case ARM::t2B:
442 Bits = 24;
443 Scale = 2;
444 break;
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000445 }
Evan Chengb43216e2007-02-01 10:16:15 +0000446
447 // Record this immediate branch.
Evan Chengbd5d3db2007-02-03 02:08:34 +0000448 unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
Evan Chengb43216e2007-02-01 10:16:15 +0000449 ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000450 }
451
Evan Chengd1b2c1e2007-01-30 01:18:38 +0000452 if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
453 PushPopMIs.push_back(I);
454
Evan Chengd3d9d662009-07-23 18:27:47 +0000455 if (Opc == ARM::CONSTPOOL_ENTRY)
456 continue;
457
Evan Chenga8e29892007-01-19 07:51:42 +0000458 // Scan the instructions for constant pool operands.
459 for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
Dan Gohmand735b802008-10-03 15:45:36 +0000460 if (I->getOperand(op).isCPI()) {
Evan Chenga8e29892007-01-19 07:51:42 +0000461 // We found one. The addressing mode tells us the max displacement
462 // from the PC that this instruction permits.
Bob Wilson84945262009-05-12 17:09:30 +0000463
Evan Chenga8e29892007-01-19 07:51:42 +0000464 // Basic size info comes from the TSFlags field.
Evan Chengb43216e2007-02-01 10:16:15 +0000465 unsigned Bits = 0;
466 unsigned Scale = 1;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000467 bool NegOk = false;
Evan Chengd3d9d662009-07-23 18:27:47 +0000468 bool IsSoImm = false;
469
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000470 // FIXME: Temporary workaround until I can figure out what's going on.
471 unsigned Slack = T2JumpTables.empty() ? 0 : 4;
Evan Chengd3d9d662009-07-23 18:27:47 +0000472 switch (Opc) {
Bob Wilson84945262009-05-12 17:09:30 +0000473 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000474 llvm_unreachable("Unknown addressing mode for CP reference!");
Evan Chengd3d9d662009-07-23 18:27:47 +0000475 break;
476
477 // Taking the address of a CP entry.
478 case ARM::LEApcrel:
479 // This takes a SoImm, which is 8 bit immediate rotated. We'll
480 // pretend the maximum offset is 255 * 4. Since each instruction
481 // 4 byte wide, this is always correct. We'llc heck for other
482 // displacements that fits in a SoImm as well.
Evan Chengb43216e2007-02-01 10:16:15 +0000483 Bits = 8;
Evan Chengd3d9d662009-07-23 18:27:47 +0000484 Scale = 4;
485 NegOk = true;
486 IsSoImm = true;
487 break;
488 case ARM::t2LEApcrel:
489 Bits = 12;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000490 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000491 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000492 case ARM::tLEApcrel:
493 Bits = 8;
494 Scale = 4;
495 break;
496
497 case ARM::LDR:
498 case ARM::LDRcp:
499 case ARM::t2LDRpci:
Evan Cheng556f33c2007-02-01 20:44:52 +0000500 Bits = 12; // +-offset_12
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000501 NegOk = true;
Evan Chenga8e29892007-01-19 07:51:42 +0000502 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000503
504 case ARM::tLDRpci:
505 case ARM::tLDRcp:
Evan Chengb43216e2007-02-01 10:16:15 +0000506 Bits = 8;
507 Scale = 4; // +(offset_8*4)
Evan Cheng012f2d92007-01-24 08:53:17 +0000508 break;
Evan Chengd3d9d662009-07-23 18:27:47 +0000509
510 case ARM::FLDD:
511 case ARM::FLDS:
512 Bits = 8;
513 Scale = 4; // +-(offset_8*4)
514 NegOk = true;
Evan Cheng055b0312009-06-29 07:51:04 +0000515 break;
Evan Chenga8e29892007-01-19 07:51:42 +0000516 }
Evan Chengb43216e2007-02-01 10:16:15 +0000517
Evan Chenga8e29892007-01-19 07:51:42 +0000518 // Remember that this is a user of a CP entry.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000519 unsigned CPI = I->getOperand(op).getIndex();
Evan Chengc99ef082007-02-09 20:54:44 +0000520 MachineInstr *CPEMI = CPEMIs[CPI];
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000521 unsigned MaxOffs = ((1 << Bits)-1) * Scale - Slack;
Evan Chengd3d9d662009-07-23 18:27:47 +0000522 CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
Evan Chengc99ef082007-02-09 20:54:44 +0000523
524 // Increment corresponding CPEntry reference count.
525 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
526 assert(CPE && "Cannot find a corresponding CPEntry!");
527 CPE->RefCount++;
Bob Wilson84945262009-05-12 17:09:30 +0000528
Evan Chenga8e29892007-01-19 07:51:42 +0000529 // Instructions can only use one CP entry, don't bother scanning the
530 // rest of the operands.
531 break;
532 }
533 }
Evan Cheng2021abe2007-02-01 01:09:47 +0000534
Dale Johannesen8593e412007-04-29 19:19:30 +0000535 // In thumb mode, if this block is a constpool island, we may need padding
536 // so it's aligned on 4 byte boundary.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000537 if (isThumb &&
Evan Cheng05cc4242007-02-02 19:09:19 +0000538 !MBB.empty() &&
Dale Johannesen8593e412007-04-29 19:19:30 +0000539 MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY &&
540 (Offset%4) != 0)
Evan Cheng2021abe2007-02-01 01:09:47 +0000541 MBBSize += 2;
542
Evan Chenga8e29892007-01-19 07:51:42 +0000543 BBSizes.push_back(MBBSize);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000544 BBOffsets.push_back(Offset);
545 Offset += MBBSize;
Evan Chenga8e29892007-01-19 07:51:42 +0000546 }
547}
548
Evan Chenga8e29892007-01-19 07:51:42 +0000549/// GetOffsetOf - Return the current offset of the specified machine instruction
550/// from the start of the function. This offset changes as stuff is moved
551/// around inside the function.
552unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
553 MachineBasicBlock *MBB = MI->getParent();
Bob Wilson84945262009-05-12 17:09:30 +0000554
Evan Chenga8e29892007-01-19 07:51:42 +0000555 // The offset is composed of two things: the sum of the sizes of all MBB's
556 // before this instruction's block, and the offset from the start of the block
557 // it is in.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000558 unsigned Offset = BBOffsets[MBB->getNumber()];
Evan Chenga8e29892007-01-19 07:51:42 +0000559
Dale Johannesen8593e412007-04-29 19:19:30 +0000560 // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has
561 // alignment padding, and compensate if so.
Bob Wilson84945262009-05-12 17:09:30 +0000562 if (isThumb &&
563 MI->getOpcode() == ARM::CONSTPOOL_ENTRY &&
Dale Johannesen8593e412007-04-29 19:19:30 +0000564 Offset%4 != 0)
565 Offset += 2;
566
Evan Chenga8e29892007-01-19 07:51:42 +0000567 // Sum instructions before MI in MBB.
568 for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
569 assert(I != MBB->end() && "Didn't find MI in its own basic block?");
570 if (&*I == MI) return Offset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000571 Offset += TII->GetInstSizeInBytes(I);
Evan Chenga8e29892007-01-19 07:51:42 +0000572 }
573}
574
575/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
576/// ID.
577static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
578 const MachineBasicBlock *RHS) {
579 return LHS->getNumber() < RHS->getNumber();
580}
581
582/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
583/// machine function, it upsets all of the block numbers. Renumber the blocks
584/// and update the arrays that parallel this numbering.
585void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
586 // Renumber the MBB's to keep them consequtive.
587 NewBB->getParent()->RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000588
Evan Chenga8e29892007-01-19 07:51:42 +0000589 // Insert a size into BBSizes to align it properly with the (newly
590 // renumbered) block numbers.
591 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
Dale Johannesen99c49a42007-02-25 00:47:03 +0000592
593 // Likewise for BBOffsets.
594 BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0);
Bob Wilson84945262009-05-12 17:09:30 +0000595
596 // Next, update WaterList. Specifically, we need to add NewMBB as having
Evan Chenga8e29892007-01-19 07:51:42 +0000597 // available water after it.
Evan Chenge03cff62007-02-09 23:59:14 +0000598 std::vector<MachineBasicBlock*>::iterator IP =
Evan Chenga8e29892007-01-19 07:51:42 +0000599 std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
600 CompareMBBNumbers);
601 WaterList.insert(IP, NewBB);
602}
603
604
605/// Split the basic block containing MI into two blocks, which are joined by
606/// an unconditional branch. Update datastructures and renumber blocks to
Evan Cheng0c615842007-01-31 02:22:22 +0000607/// account for this change and returns the newly created block.
608MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
Evan Chenga8e29892007-01-19 07:51:42 +0000609 MachineBasicBlock *OrigBB = MI->getParent();
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000610 MachineFunction &MF = *OrigBB->getParent();
Evan Chenga8e29892007-01-19 07:51:42 +0000611
612 // Create a new MBB for the code after the OrigBB.
Bob Wilson84945262009-05-12 17:09:30 +0000613 MachineBasicBlock *NewBB =
614 MF.CreateMachineBasicBlock(OrigBB->getBasicBlock());
Evan Chenga8e29892007-01-19 07:51:42 +0000615 MachineFunction::iterator MBBI = OrigBB; ++MBBI;
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000616 MF.insert(MBBI, NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000617
Evan Chenga8e29892007-01-19 07:51:42 +0000618 // Splice the instructions starting with MI over to NewBB.
619 NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
Bob Wilson84945262009-05-12 17:09:30 +0000620
Evan Chenga8e29892007-01-19 07:51:42 +0000621 // Add an unconditional branch from OrigBB to NewBB.
Evan Chenga9b8b8d2007-01-31 18:29:27 +0000622 // Note the new unconditional branch is not being recorded.
Dale Johannesenb6728402009-02-13 02:25:56 +0000623 // There doesn't seem to be meaningful DebugInfo available; this doesn't
624 // correspond to anything in the source.
Evan Cheng58541fd2009-07-07 01:16:41 +0000625 unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
626 BuildMI(OrigBB, DebugLoc::getUnknownLoc(), TII->get(Opc)).addMBB(NewBB);
Evan Chenga8e29892007-01-19 07:51:42 +0000627 NumSplit++;
Bob Wilson84945262009-05-12 17:09:30 +0000628
Evan Chenga8e29892007-01-19 07:51:42 +0000629 // Update the CFG. All succs of OrigBB are now succs of NewBB.
630 while (!OrigBB->succ_empty()) {
631 MachineBasicBlock *Succ = *OrigBB->succ_begin();
632 OrigBB->removeSuccessor(Succ);
633 NewBB->addSuccessor(Succ);
Bob Wilson84945262009-05-12 17:09:30 +0000634
Evan Chenga8e29892007-01-19 07:51:42 +0000635 // This pass should be run after register allocation, so there should be no
636 // PHI nodes to update.
637 assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI)
638 && "PHI nodes should be eliminated by now!");
639 }
Bob Wilson84945262009-05-12 17:09:30 +0000640
Evan Chenga8e29892007-01-19 07:51:42 +0000641 // OrigBB branches to NewBB.
642 OrigBB->addSuccessor(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000643
Evan Chenga8e29892007-01-19 07:51:42 +0000644 // Update internal data structures to account for the newly inserted MBB.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000645 // This is almost the same as UpdateForInsertedWaterBlock, except that
646 // the Water goes after OrigBB, not NewBB.
Dan Gohman8e5f2c62008-07-07 23:14:23 +0000647 MF.RenumberBlocks(NewBB);
Bob Wilson84945262009-05-12 17:09:30 +0000648
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000649 // Insert a size into BBSizes to align it properly with the (newly
650 // renumbered) block numbers.
651 BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
Bob Wilson84945262009-05-12 17:09:30 +0000652
Dale Johannesen99c49a42007-02-25 00:47:03 +0000653 // Likewise for BBOffsets.
654 BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0);
655
Bob Wilson84945262009-05-12 17:09:30 +0000656 // Next, update WaterList. Specifically, we need to add OrigMBB as having
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000657 // available water after it (but not if it's already there, which happens
658 // when splitting before a conditional branch that is followed by an
659 // unconditional branch - in that case we want to insert NewBB).
660 std::vector<MachineBasicBlock*>::iterator IP =
661 std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
662 CompareMBBNumbers);
663 MachineBasicBlock* WaterBB = *IP;
664 if (WaterBB == OrigBB)
665 WaterList.insert(next(IP), NewBB);
666 else
667 WaterList.insert(IP, OrigBB);
668
Dale Johannesen8593e412007-04-29 19:19:30 +0000669 // Figure out how large the first NewMBB is. (It cannot
670 // contain a constpool_entry or tablejump.)
Evan Chenga8e29892007-01-19 07:51:42 +0000671 unsigned NewBBSize = 0;
672 for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
673 I != E; ++I)
Nicolas Geoffray52e724a2008-04-16 20:10:13 +0000674 NewBBSize += TII->GetInstSizeInBytes(I);
Bob Wilson84945262009-05-12 17:09:30 +0000675
Dale Johannesen99c49a42007-02-25 00:47:03 +0000676 unsigned OrigBBI = OrigBB->getNumber();
677 unsigned NewBBI = NewBB->getNumber();
Evan Chenga8e29892007-01-19 07:51:42 +0000678 // Set the size of NewBB in BBSizes.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000679 BBSizes[NewBBI] = NewBBSize;
Bob Wilson84945262009-05-12 17:09:30 +0000680
Evan Chenga8e29892007-01-19 07:51:42 +0000681 // We removed instructions from UserMBB, subtract that off from its size.
Evan Chengaf5cbcb2007-01-25 03:12:46 +0000682 // Add 2 or 4 to the block to count the unconditional branch we added to it.
Evan Cheng25f7cfc2009-08-01 06:13:52 +0000683 int delta = isThumb1 ? 2 : 4;
Dale Johannesen99c49a42007-02-25 00:47:03 +0000684 BBSizes[OrigBBI] -= NewBBSize - delta;
685
686 // ...and adjust BBOffsets for NewBB accordingly.
687 BBOffsets[NewBBI] = BBOffsets[OrigBBI] + BBSizes[OrigBBI];
688
689 // All BBOffsets following these blocks must be modified.
690 AdjustBBOffsetsAfter(NewBB, delta);
Evan Cheng0c615842007-01-31 02:22:22 +0000691
692 return NewBB;
Evan Chenga8e29892007-01-19 07:51:42 +0000693}
694
Dale Johannesen8593e412007-04-29 19:19:30 +0000695/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
Bob Wilson84945262009-05-12 17:09:30 +0000696/// reference) is within MaxDisp of TrialOffset (a proposed location of a
Dale Johannesen8593e412007-04-29 19:19:30 +0000697/// constant pool entry).
Bob Wilson84945262009-05-12 17:09:30 +0000698bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +0000699 unsigned TrialOffset, unsigned MaxDisp,
700 bool NegativeOK, bool IsSoImm) {
Bob Wilson84945262009-05-12 17:09:30 +0000701 // On Thumb offsets==2 mod 4 are rounded down by the hardware for
702 // purposes of the displacement computation; compensate for that here.
Dale Johannesen8593e412007-04-29 19:19:30 +0000703 // Effectively, the valid range of displacements is 2 bytes smaller for such
704 // references.
705 if (isThumb && UserOffset%4 !=0)
706 UserOffset -= 2;
707 // CPEs will be rounded up to a multiple of 4.
708 if (isThumb && TrialOffset%4 != 0)
709 TrialOffset += 2;
710
Dale Johannesen99c49a42007-02-25 00:47:03 +0000711 if (UserOffset <= TrialOffset) {
712 // User before the Trial.
Evan Chengd3d9d662009-07-23 18:27:47 +0000713 if (TrialOffset - UserOffset <= MaxDisp)
714 return true;
Evan Cheng40efc252009-07-24 19:31:03 +0000715 // FIXME: Make use full range of soimm values.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000716 } else if (NegativeOK) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000717 if (UserOffset - TrialOffset <= MaxDisp)
718 return true;
Evan Cheng40efc252009-07-24 19:31:03 +0000719 // FIXME: Make use full range of soimm values.
Dale Johannesen99c49a42007-02-25 00:47:03 +0000720 }
721 return false;
722}
723
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000724/// WaterIsInRange - Returns true if a CPE placed after the specified
725/// Water (a basic block) will be in range for the specific MI.
726
727bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000728 MachineBasicBlock* Water, CPUser &U) {
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000729 unsigned MaxDisp = U.MaxDisp;
Bob Wilson84945262009-05-12 17:09:30 +0000730 unsigned CPEOffset = BBOffsets[Water->getNumber()] +
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000731 BBSizes[Water->getNumber()];
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000732
Dale Johannesend959aa42007-04-02 20:31:06 +0000733 // If the CPE is to be inserted before the instruction, that will raise
Dale Johannesen8593e412007-04-29 19:19:30 +0000734 // the offset of the instruction. (Currently applies only to ARM, so
735 // no alignment compensation attempted here.)
Dale Johannesend959aa42007-04-02 20:31:06 +0000736 if (CPEOffset < UserOffset)
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000737 UserOffset += U.CPEMI->getOperand(2).getImm();
Dale Johannesend959aa42007-04-02 20:31:06 +0000738
Evan Chengd3d9d662009-07-23 18:27:47 +0000739 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000740}
741
742/// CPEIsInRange - Returns true if the distance between specific MI and
Evan Chengc0dbec72007-01-31 19:57:44 +0000743/// specific ConstPool entry instruction can fit in MI's displacement field.
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000744bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000745 MachineInstr *CPEMI, unsigned MaxDisp,
746 bool NegOk, bool DoDump) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000747 unsigned CPEOffset = GetOffsetOf(CPEMI);
748 assert(CPEOffset%4 == 0 && "Misaligned CPE");
Evan Cheng2021abe2007-02-01 01:09:47 +0000749
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000750 if (DoDump) {
751 DOUT << "User of CPE#" << CPEMI->getOperand(0).getImm()
752 << " max delta=" << MaxDisp
753 << " insn address=" << UserOffset
754 << " CPE address=" << CPEOffset
755 << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI;
756 }
Evan Chengc0dbec72007-01-31 19:57:44 +0000757
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000758 return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
Evan Chengc0dbec72007-01-31 19:57:44 +0000759}
760
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000761#ifndef NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000762/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
763/// unconditionally branches to its only successor.
764static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
765 if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
766 return false;
767
768 MachineBasicBlock *Succ = *MBB->succ_begin();
769 MachineBasicBlock *Pred = *MBB->pred_begin();
770 MachineInstr *PredMI = &Pred->back();
David Goodwin5e47a9a2009-06-30 18:04:13 +0000771 if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
772 || PredMI->getOpcode() == ARM::t2B)
Evan Chengc99ef082007-02-09 20:54:44 +0000773 return PredMI->getOperand(0).getMBB() == Succ;
774 return false;
775}
Evan Chengd1e7d9a2009-01-28 00:53:34 +0000776#endif // NDEBUG
Evan Chengc99ef082007-02-09 20:54:44 +0000777
Bob Wilson84945262009-05-12 17:09:30 +0000778void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB,
Dale Johannesen8593e412007-04-29 19:19:30 +0000779 int delta) {
780 MachineFunction::iterator MBBI = BB; MBBI = next(MBBI);
Evan Chengd3d9d662009-07-23 18:27:47 +0000781 for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs();
782 i < e; ++i) {
Dale Johannesen99c49a42007-02-25 00:47:03 +0000783 BBOffsets[i] += delta;
Dale Johannesen8593e412007-04-29 19:19:30 +0000784 // If some existing blocks have padding, adjust the padding as needed, a
785 // bit tricky. delta can be negative so don't use % on that.
Evan Chengd3d9d662009-07-23 18:27:47 +0000786 if (!isThumb)
787 continue;
788 MachineBasicBlock *MBB = MBBI;
789 if (!MBB->empty()) {
790 // Constant pool entries require padding.
791 if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
792 unsigned oldOffset = BBOffsets[i] - delta;
793 if (oldOffset%4==0 && BBOffsets[i]%4!=0) {
794 // add new padding
795 BBSizes[i] += 2;
796 delta += 2;
797 } else if (oldOffset%4!=0 && BBOffsets[i]%4==0) {
798 // remove existing padding
799 BBSizes[i] -=2;
800 delta -= 2;
Dale Johannesen8593e412007-04-29 19:19:30 +0000801 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000802 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000803 // Thumb1 jump tables require padding. They should be at the end;
804 // following unconditional branches are removed by AnalyzeBranch.
Evan Cheng78947622009-07-24 18:20:44 +0000805 MachineInstr *ThumbJTMI = prior(MBB->end());
Evan Cheng66ac5312009-07-25 00:33:29 +0000806 if (ThumbJTMI->getOpcode() == ARM::tBR_JTr) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000807 unsigned newMIOffset = GetOffsetOf(ThumbJTMI);
808 unsigned oldMIOffset = newMIOffset - delta;
809 if (oldMIOffset%4 == 0 && newMIOffset%4 != 0) {
810 // remove existing padding
811 BBSizes[i] -= 2;
812 delta -= 2;
813 } else if (oldMIOffset%4 != 0 && newMIOffset%4 == 0) {
814 // add new padding
815 BBSizes[i] += 2;
816 delta += 2;
817 }
818 }
819 if (delta==0)
820 return;
Dale Johannesen8593e412007-04-29 19:19:30 +0000821 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000822 MBBI = next(MBBI);
Dale Johannesen8593e412007-04-29 19:19:30 +0000823 }
Dale Johannesen99c49a42007-02-25 00:47:03 +0000824}
825
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000826/// DecrementOldEntry - find the constant pool entry with index CPI
827/// and instruction CPEMI, and decrement its refcount. If the refcount
Bob Wilson84945262009-05-12 17:09:30 +0000828/// becomes 0 remove the entry and instruction. Returns true if we removed
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000829/// the entry, false if we didn't.
Evan Chenga8e29892007-01-19 07:51:42 +0000830
Evan Chenged884f32007-04-03 23:39:48 +0000831bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
Evan Chengc99ef082007-02-09 20:54:44 +0000832 // Find the old entry. Eliminate it if it is no longer used.
Evan Chenged884f32007-04-03 23:39:48 +0000833 CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
834 assert(CPE && "Unexpected!");
835 if (--CPE->RefCount == 0) {
836 RemoveDeadCPEMI(CPEMI);
837 CPE->CPEMI = NULL;
Evan Chengc99ef082007-02-09 20:54:44 +0000838 NumCPEs--;
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000839 return true;
840 }
841 return false;
842}
843
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000844/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
845/// if not, see if an in-range clone of the CPE is in range, and if so,
846/// change the data structures so the user references the clone. Returns:
847/// 0 = no existing entry found
848/// 1 = entry found, and there were no code insertions or deletions
849/// 2 = entry found, and there were code insertions or deletions
850int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
851{
852 MachineInstr *UserMI = U.MI;
853 MachineInstr *CPEMI = U.CPEMI;
854
855 // Check to see if the CPE is already in-range.
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000856 if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) {
Evan Cheng82020102007-07-10 22:00:16 +0000857 DOUT << "In range\n";
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000858 return 1;
Evan Chengc99ef082007-02-09 20:54:44 +0000859 }
860
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000861 // No. Look for previously created clones of the CPE that are in range.
Chris Lattner8aa797a2007-12-30 23:10:15 +0000862 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000863 std::vector<CPEntry> &CPEs = CPEntries[CPI];
864 for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
865 // We already tried this one
866 if (CPEs[i].CPEMI == CPEMI)
867 continue;
868 // Removing CPEs can leave empty entries, skip
869 if (CPEs[i].CPEMI == NULL)
870 continue;
Evan Cheng5d8f1ca2009-07-21 23:56:01 +0000871 if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) {
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000872 DOUT << "Replacing CPE#" << CPI << " with CPE#" << CPEs[i].CPI << "\n";
873 // Point the CPUser node to the replacement
874 U.CPEMI = CPEs[i].CPEMI;
875 // Change the CPI in the instruction operand to refer to the clone.
876 for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
Dan Gohmand735b802008-10-03 15:45:36 +0000877 if (UserMI->getOperand(j).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +0000878 UserMI->getOperand(j).setIndex(CPEs[i].CPI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000879 break;
880 }
881 // Adjust the refcount of the clone...
882 CPEs[i].RefCount++;
883 // ...and the original. If we didn't remove the old entry, none of the
884 // addresses changed, so we don't need another pass.
Evan Chenged884f32007-04-03 23:39:48 +0000885 return DecrementOldEntry(CPI, CPEMI) ? 2 : 1;
Dale Johannesen88e37ae2007-02-23 05:02:36 +0000886 }
887 }
888 return 0;
889}
890
Dale Johannesenf1b214d2007-02-28 18:41:23 +0000891/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
892/// the specific unconditional branch instruction.
893static inline unsigned getUnconditionalBrDisp(int Opc) {
David Goodwin5e47a9a2009-06-30 18:04:13 +0000894 switch (Opc) {
895 case ARM::tB:
896 return ((1<<10)-1)*2;
897 case ARM::t2B:
898 return ((1<<23)-1)*2;
899 default:
900 break;
901 }
902
903 return ((1<<23)-1)*4;
Dale Johannesenf1b214d2007-02-28 18:41:23 +0000904}
905
Dale Johannesen8593e412007-04-29 19:19:30 +0000906/// AcceptWater - Small amount of common code factored out of the following.
907
Bob Wilson84945262009-05-12 17:09:30 +0000908MachineBasicBlock* ARMConstantIslands::AcceptWater(MachineBasicBlock *WaterBB,
Dale Johannesen8593e412007-04-29 19:19:30 +0000909 std::vector<MachineBasicBlock*>::iterator IP) {
910 DOUT << "found water in range\n";
911 // Remove the original WaterList entry; we want subsequent
912 // insertions in this vicinity to go after the one we're
913 // about to insert. This considerably reduces the number
914 // of times we have to move the same CPE more than once.
915 WaterList.erase(IP);
916 // CPE goes before following block (NewMBB).
917 return next(MachineFunction::iterator(WaterBB));
918}
919
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000920/// LookForWater - look for an existing entry in the WaterList in which
921/// we can place the CPE referenced from U so it's within range of U's MI.
922/// Returns true if found, false if not. If it returns true, *NewMBB
Dale Johannesen8593e412007-04-29 19:19:30 +0000923/// is set to the WaterList entry.
Evan Chengd3d9d662009-07-23 18:27:47 +0000924/// For ARM, we prefer the water that's farthest away. For Thumb, prefer
Dale Johannesen8593e412007-04-29 19:19:30 +0000925/// water that will not introduce padding to water that will; within each
926/// group, prefer the water that's farthest away.
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000927bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
Dale Johannesen8593e412007-04-29 19:19:30 +0000928 MachineBasicBlock** NewMBB) {
929 std::vector<MachineBasicBlock*>::iterator IPThatWouldPad;
930 MachineBasicBlock* WaterBBThatWouldPad = NULL;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000931 if (!WaterList.empty()) {
932 for (std::vector<MachineBasicBlock*>::iterator IP = prior(WaterList.end()),
Evan Chengd3d9d662009-07-23 18:27:47 +0000933 B = WaterList.begin();; --IP) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000934 MachineBasicBlock* WaterBB = *IP;
Dale Johannesen5d9c4b62007-07-11 18:32:38 +0000935 if (WaterIsInRange(UserOffset, WaterBB, U)) {
Evan Chengd3d9d662009-07-23 18:27:47 +0000936 unsigned WBBId = WaterBB->getNumber();
Dale Johannesen8593e412007-04-29 19:19:30 +0000937 if (isThumb &&
Evan Chengd3d9d662009-07-23 18:27:47 +0000938 (BBOffsets[WBBId] + BBSizes[WBBId])%4 != 0) {
Dale Johannesen8593e412007-04-29 19:19:30 +0000939 // This is valid Water, but would introduce padding. Remember
940 // it in case we don't find any Water that doesn't do this.
941 if (!WaterBBThatWouldPad) {
942 WaterBBThatWouldPad = WaterBB;
943 IPThatWouldPad = IP;
944 }
945 } else {
946 *NewMBB = AcceptWater(WaterBB, IP);
947 return true;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000948 }
Evan Chengd3d9d662009-07-23 18:27:47 +0000949 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000950 if (IP == B)
951 break;
952 }
953 }
Dale Johannesen8593e412007-04-29 19:19:30 +0000954 if (isThumb && WaterBBThatWouldPad) {
955 *NewMBB = AcceptWater(WaterBBThatWouldPad, IPThatWouldPad);
956 return true;
957 }
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000958 return false;
959}
960
Bob Wilson84945262009-05-12 17:09:30 +0000961/// CreateNewWater - No existing WaterList entry will work for
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000962/// CPUsers[CPUserIndex], so create a place to put the CPE. The end of the
963/// block is used if in range, and the conditional branch munged so control
964/// flow is correct. Otherwise the block is split to create a hole with an
965/// unconditional branch around it. In either case *NewMBB is set to a
966/// block following which the new island can be inserted (the WaterList
967/// is not adjusted).
968
Bob Wilson84945262009-05-12 17:09:30 +0000969void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000970 unsigned UserOffset, MachineBasicBlock** NewMBB) {
971 CPUser &U = CPUsers[CPUserIndex];
972 MachineInstr *UserMI = U.MI;
973 MachineInstr *CPEMI = U.CPEMI;
974 MachineBasicBlock *UserMBB = UserMI->getParent();
Bob Wilson84945262009-05-12 17:09:30 +0000975 unsigned OffsetOfNextBlock = BBOffsets[UserMBB->getNumber()] +
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000976 BBSizes[UserMBB->getNumber()];
Dale Johannesen8593e412007-04-29 19:19:30 +0000977 assert(OffsetOfNextBlock== BBOffsets[UserMBB->getNumber()+1]);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000978
979 // If the use is at the end of the block, or the end of the block
Dale Johannesen8593e412007-04-29 19:19:30 +0000980 // is within range, make new water there. (The addition below is
Evan Chengd3d9d662009-07-23 18:27:47 +0000981 // for the unconditional branch we will be adding: 4 bytes on ARM + Thumb2,
982 // 2 on Thumb1. Possible Thumb1 alignment padding is allowed for
Dale Johannesen8593e412007-04-29 19:19:30 +0000983 // inside OffsetIsInRange.
Bob Wilson84945262009-05-12 17:09:30 +0000984 // If the block ends in an unconditional branch already, it is water,
Dale Johannesen8593e412007-04-29 19:19:30 +0000985 // and is known to be out of range, so we'll always be adding a branch.)
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000986 if (&UserMBB->back() == UserMI ||
Evan Chengd3d9d662009-07-23 18:27:47 +0000987 OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4),
988 U.MaxDisp, U.NegOk, U.IsSoImm)) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +0000989 DOUT << "Split at end of block\n";
990 if (&UserMBB->back() == UserMI)
991 assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
992 *NewMBB = next(MachineFunction::iterator(UserMBB));
993 // Add an unconditional branch from UserMBB to fallthrough block.
994 // Record it for branch lengthening; this new branch will not get out of
995 // range, but if the preceding conditional branch is out of range, the
996 // targets will be exchanged, and the altered branch may be out of
997 // range, so the machinery has to know about it.
David Goodwin5e47a9a2009-06-30 18:04:13 +0000998 int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
Dale Johannesenb6728402009-02-13 02:25:56 +0000999 BuildMI(UserMBB, DebugLoc::getUnknownLoc(),
1000 TII->get(UncondBr)).addMBB(*NewMBB);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001001 unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
Bob Wilson84945262009-05-12 17:09:30 +00001002 ImmBranches.push_back(ImmBranch(&UserMBB->back(),
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001003 MaxDisp, false, UncondBr));
Evan Chengd3d9d662009-07-23 18:27:47 +00001004 int delta = isThumb1 ? 2 : 4;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001005 BBSizes[UserMBB->getNumber()] += delta;
1006 AdjustBBOffsetsAfter(UserMBB, delta);
1007 } else {
1008 // What a big block. Find a place within the block to split it.
Evan Chengd3d9d662009-07-23 18:27:47 +00001009 // This is a little tricky on Thumb1 since instructions are 2 bytes
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001010 // and constant pool entries are 4 bytes: if instruction I references
1011 // island CPE, and instruction I+1 references CPE', it will
1012 // not work well to put CPE as far forward as possible, since then
1013 // CPE' cannot immediately follow it (that location is 2 bytes
1014 // farther away from I+1 than CPE was from I) and we'd need to create
Dale Johannesen8593e412007-04-29 19:19:30 +00001015 // a new island. So, we make a first guess, then walk through the
1016 // instructions between the one currently being looked at and the
1017 // possible insertion point, and make sure any other instructions
1018 // that reference CPEs will be able to use the same island area;
1019 // if not, we back up the insertion point.
1020
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001021 // The 4 in the following is for the unconditional branch we'll be
Evan Chengd3d9d662009-07-23 18:27:47 +00001022 // inserting (allows for long branch on Thumb1). Alignment of the
Dale Johannesen8593e412007-04-29 19:19:30 +00001023 // island is handled inside OffsetIsInRange.
1024 unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4;
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001025 // This could point off the end of the block if we've already got
1026 // constant pool entries following this block; only the last one is
1027 // in the water list. Back past any possible branches (allow for a
1028 // conditional and a maximally long unconditional).
1029 if (BaseInsertOffset >= BBOffsets[UserMBB->getNumber()+1])
Bob Wilson84945262009-05-12 17:09:30 +00001030 BaseInsertOffset = BBOffsets[UserMBB->getNumber()+1] -
Evan Chengd3d9d662009-07-23 18:27:47 +00001031 (isThumb1 ? 6 : 8);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001032 unsigned EndInsertOffset = BaseInsertOffset +
1033 CPEMI->getOperand(2).getImm();
1034 MachineBasicBlock::iterator MI = UserMI;
1035 ++MI;
1036 unsigned CPUIndex = CPUserIndex+1;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001037 for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001038 Offset < BaseInsertOffset;
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001039 Offset += TII->GetInstSizeInBytes(MI),
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001040 MI = next(MI)) {
1041 if (CPUIndex < CPUsers.size() && CPUsers[CPUIndex].MI == MI) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001042 CPUser &U = CPUsers[CPUIndex];
Bob Wilson84945262009-05-12 17:09:30 +00001043 if (!OffsetIsInRange(Offset, EndInsertOffset,
Evan Chengd3d9d662009-07-23 18:27:47 +00001044 U.MaxDisp, U.NegOk, U.IsSoImm)) {
1045 BaseInsertOffset -= (isThumb1 ? 2 : 4);
1046 EndInsertOffset -= (isThumb1 ? 2 : 4);
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001047 }
1048 // This is overly conservative, as we don't account for CPEMIs
1049 // being reused within the block, but it doesn't matter much.
1050 EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm();
1051 CPUIndex++;
1052 }
1053 }
1054 DOUT << "Split in middle of big block\n";
1055 *NewMBB = SplitBlockBeforeInstr(prior(MI));
1056 }
1057}
1058
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001059/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
Bob Wilson39bf0512009-05-12 17:35:29 +00001060/// is out-of-range. If so, pick up the constant pool value and move it some
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001061/// place in-range. Return true if we changed any addresses (thus must run
1062/// another pass of branch lengthening), false otherwise.
Evan Cheng5657c012009-07-29 02:18:14 +00001063bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &MF,
Bob Wilson84945262009-05-12 17:09:30 +00001064 unsigned CPUserIndex) {
Dale Johannesenf1b214d2007-02-28 18:41:23 +00001065 CPUser &U = CPUsers[CPUserIndex];
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001066 MachineInstr *UserMI = U.MI;
1067 MachineInstr *CPEMI = U.CPEMI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001068 unsigned CPI = CPEMI->getOperand(1).getIndex();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001069 unsigned Size = CPEMI->getOperand(2).getImm();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001070 MachineBasicBlock *NewMBB;
Dale Johannesen8593e412007-04-29 19:19:30 +00001071 // Compute this only once, it's expensive. The 4 or 8 is the value the
Bob Wilson39bf0512009-05-12 17:35:29 +00001072 // hardware keeps in the PC (2 insns ahead of the reference).
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001073 unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
Evan Cheng768c9f72007-04-27 08:14:15 +00001074
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001075 // See if the current entry is within range, or there is a clone of it
1076 // in range.
1077 int result = LookForExistingCPEntry(U, UserOffset);
1078 if (result==1) return false;
1079 else if (result==2) return true;
1080
1081 // No existing clone of this CPE is within range.
1082 // We will be generating a new clone. Get a UID for it.
Bob Wilson39bf0512009-05-12 17:35:29 +00001083 unsigned ID = AFI->createConstPoolEntryUId();
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001084
1085 // Look for water where we can place this CPE. We look for the farthest one
1086 // away that will work. Forward references only for now (although later
1087 // we might find some that are backwards).
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001088
Dale Johannesen8593e412007-04-29 19:19:30 +00001089 if (!LookForWater(U, UserOffset, &NewMBB)) {
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001090 // No water found.
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001091 DOUT << "No water found\n";
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001092 CreateNewWater(CPUserIndex, UserOffset, &NewMBB);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001093 }
1094
1095 // Okay, we know we can put an island before NewMBB now, do it!
Evan Cheng5657c012009-07-29 02:18:14 +00001096 MachineBasicBlock *NewIsland = MF.CreateMachineBasicBlock();
1097 MF.insert(NewMBB, NewIsland);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001098
1099 // Update internal data structures to account for the newly inserted MBB.
1100 UpdateForInsertedWaterBlock(NewIsland);
1101
1102 // Decrement the old entry, and remove it if refcount becomes 0.
Evan Chenged884f32007-04-03 23:39:48 +00001103 DecrementOldEntry(CPI, CPEMI);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001104
1105 // Now that we have an island to add the CPE to, clone the original CPE and
1106 // add it to the island.
Dale Johannesenb6728402009-02-13 02:25:56 +00001107 U.CPEMI = BuildMI(NewIsland, DebugLoc::getUnknownLoc(),
1108 TII->get(ARM::CONSTPOOL_ENTRY))
Evan Chenga8e29892007-01-19 07:51:42 +00001109 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001110 CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
Evan Chengc99ef082007-02-09 20:54:44 +00001111 NumCPEs++;
1112
Dale Johannesen8593e412007-04-29 19:19:30 +00001113 BBOffsets[NewIsland->getNumber()] = BBOffsets[NewMBB->getNumber()];
Evan Chengb43216e2007-02-01 10:16:15 +00001114 // Compensate for .align 2 in thumb mode.
Bob Wilson84945262009-05-12 17:09:30 +00001115 if (isThumb && BBOffsets[NewIsland->getNumber()]%4 != 0)
Dale Johannesen8593e412007-04-29 19:19:30 +00001116 Size += 2;
Evan Chenga8e29892007-01-19 07:51:42 +00001117 // Increase the size of the island block to account for the new entry.
1118 BBSizes[NewIsland->getNumber()] += Size;
Dale Johannesen99c49a42007-02-25 00:47:03 +00001119 AdjustBBOffsetsAfter(NewIsland, Size);
Bob Wilson84945262009-05-12 17:09:30 +00001120
Evan Chenga8e29892007-01-19 07:51:42 +00001121 // Finally, change the CPI in the instruction operand to be ID.
1122 for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
Dan Gohmand735b802008-10-03 15:45:36 +00001123 if (UserMI->getOperand(i).isCPI()) {
Chris Lattner8aa797a2007-12-30 23:10:15 +00001124 UserMI->getOperand(i).setIndex(ID);
Evan Chenga8e29892007-01-19 07:51:42 +00001125 break;
1126 }
Bob Wilson84945262009-05-12 17:09:30 +00001127
Evan Chengc99ef082007-02-09 20:54:44 +00001128 DOUT << " Moved CPE to #" << ID << " CPI=" << CPI << "\t" << *UserMI;
Bob Wilson84945262009-05-12 17:09:30 +00001129
Evan Chenga8e29892007-01-19 07:51:42 +00001130 return true;
1131}
1132
Evan Chenged884f32007-04-03 23:39:48 +00001133/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1134/// sizes and offsets of impacted basic blocks.
1135void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
1136 MachineBasicBlock *CPEBB = CPEMI->getParent();
Dale Johannesen8593e412007-04-29 19:19:30 +00001137 unsigned Size = CPEMI->getOperand(2).getImm();
1138 CPEMI->eraseFromParent();
1139 BBSizes[CPEBB->getNumber()] -= Size;
1140 // All succeeding offsets have the current size value added in, fix this.
Evan Chenged884f32007-04-03 23:39:48 +00001141 if (CPEBB->empty()) {
Evan Chengd3d9d662009-07-23 18:27:47 +00001142 // In thumb1 mode, the size of island may be padded by two to compensate for
Dale Johannesen8593e412007-04-29 19:19:30 +00001143 // the alignment requirement. Then it will now be 2 when the block is
Evan Chenged884f32007-04-03 23:39:48 +00001144 // empty, so fix this.
1145 // All succeeding offsets have the current size value added in, fix this.
1146 if (BBSizes[CPEBB->getNumber()] != 0) {
Dale Johannesen8593e412007-04-29 19:19:30 +00001147 Size += BBSizes[CPEBB->getNumber()];
Evan Chenged884f32007-04-03 23:39:48 +00001148 BBSizes[CPEBB->getNumber()] = 0;
1149 }
Evan Chenged884f32007-04-03 23:39:48 +00001150 }
Dale Johannesen8593e412007-04-29 19:19:30 +00001151 AdjustBBOffsetsAfter(CPEBB, -Size);
1152 // An island has only one predecessor BB and one successor BB. Check if
1153 // this BB's predecessor jumps directly to this BB's successor. This
1154 // shouldn't happen currently.
1155 assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1156 // FIXME: remove the empty blocks after all the work is done?
Evan Chenged884f32007-04-03 23:39:48 +00001157}
1158
1159/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1160/// are zero.
1161bool ARMConstantIslands::RemoveUnusedCPEntries() {
1162 unsigned MadeChange = false;
1163 for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1164 std::vector<CPEntry> &CPEs = CPEntries[i];
1165 for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1166 if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1167 RemoveDeadCPEMI(CPEs[j].CPEMI);
1168 CPEs[j].CPEMI = NULL;
1169 MadeChange = true;
1170 }
1171 }
Bob Wilson84945262009-05-12 17:09:30 +00001172 }
Evan Chenged884f32007-04-03 23:39:48 +00001173 return MadeChange;
1174}
1175
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001176/// BBIsInRange - Returns true if the distance between specific MI and
Evan Cheng43aeab62007-01-26 20:38:26 +00001177/// specific BB can fit in MI's displacement field.
Evan Chengc0dbec72007-01-31 19:57:44 +00001178bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1179 unsigned MaxDisp) {
Dale Johannesenb71aa2b2007-02-28 23:20:38 +00001180 unsigned PCAdj = isThumb ? 4 : 8;
Evan Chengc0dbec72007-01-31 19:57:44 +00001181 unsigned BrOffset = GetOffsetOf(MI) + PCAdj;
Dale Johannesen99c49a42007-02-25 00:47:03 +00001182 unsigned DestOffset = BBOffsets[DestBB->getNumber()];
Evan Cheng43aeab62007-01-26 20:38:26 +00001183
Evan Chengc99ef082007-02-09 20:54:44 +00001184 DOUT << "Branch of destination BB#" << DestBB->getNumber()
1185 << " from BB#" << MI->getParent()->getNumber()
1186 << " max delta=" << MaxDisp
Dale Johannesen8593e412007-04-29 19:19:30 +00001187 << " from " << GetOffsetOf(MI) << " to " << DestOffset
1188 << " offset " << int(DestOffset-BrOffset) << "\t" << *MI;
Evan Chengc0dbec72007-01-31 19:57:44 +00001189
Dale Johannesen8593e412007-04-29 19:19:30 +00001190 if (BrOffset <= DestOffset) {
1191 // Branch before the Dest.
1192 if (DestOffset-BrOffset <= MaxDisp)
1193 return true;
1194 } else {
1195 if (BrOffset-DestOffset <= MaxDisp)
1196 return true;
1197 }
1198 return false;
Evan Cheng43aeab62007-01-26 20:38:26 +00001199}
1200
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001201/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1202/// away to fit in its displacement field.
Evan Cheng5657c012009-07-29 02:18:14 +00001203bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001204 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001205 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001206
Evan Chengc0dbec72007-01-31 19:57:44 +00001207 // Check to see if the DestBB is already in-range.
1208 if (BBIsInRange(MI, DestBB, Br.MaxDisp))
Evan Cheng43aeab62007-01-26 20:38:26 +00001209 return false;
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001210
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001211 if (!Br.isCond)
Evan Cheng5657c012009-07-29 02:18:14 +00001212 return FixUpUnconditionalBr(MF, Br);
1213 return FixUpConditionalBr(MF, Br);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001214}
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001215
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001216/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1217/// too far away to fit in its displacement field. If the LR register has been
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001218/// spilled in the epilogue, then we can use BL to implement a far jump.
Bob Wilson39bf0512009-05-12 17:35:29 +00001219/// Otherwise, add an intermediate branch instruction to a branch.
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001220bool
Evan Cheng5657c012009-07-29 02:18:14 +00001221ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001222 MachineInstr *MI = Br.MI;
1223 MachineBasicBlock *MBB = MI->getParent();
Evan Chengd3d9d662009-07-23 18:27:47 +00001224 assert(isThumb && !isThumb2 && "Expected a Thumb1 function!");
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001225
1226 // Use BL to implement far jump.
1227 Br.MaxDisp = (1 << 21) * 2;
Chris Lattner5080f4d2008-01-11 18:10:50 +00001228 MI->setDesc(TII->get(ARM::tBfar));
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001229 BBSizes[MBB->getNumber()] += 2;
Dale Johannesen99c49a42007-02-25 00:47:03 +00001230 AdjustBBOffsetsAfter(MBB, 2);
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001231 HasFarJump = true;
1232 NumUBrFixed++;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001233
Evan Chengc99ef082007-02-09 20:54:44 +00001234 DOUT << " Changed B to long jump " << *MI;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001235
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001236 return true;
1237}
1238
Dale Johannesen88e37ae2007-02-23 05:02:36 +00001239/// FixUpConditionalBr - Fix up a conditional branch whose destination is too
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001240/// far away to fit in its displacement field. It is converted to an inverse
1241/// conditional branch + an unconditional branch to the destination.
1242bool
Evan Cheng5657c012009-07-29 02:18:14 +00001243ARMConstantIslands::FixUpConditionalBr(MachineFunction &MF, ImmBranch &Br) {
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001244 MachineInstr *MI = Br.MI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001245 MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001246
Bob Wilson39bf0512009-05-12 17:35:29 +00001247 // Add an unconditional branch to the destination and invert the branch
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001248 // condition to jump over it:
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001249 // blt L1
1250 // =>
1251 // bge L2
1252 // b L1
1253 // L2:
Chris Lattner9a1ceae2007-12-30 20:49:49 +00001254 ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001255 CC = ARMCC::getOppositeCondition(CC);
Evan Cheng0e1d3792007-07-05 07:18:20 +00001256 unsigned CCReg = MI->getOperand(2).getReg();
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001257
1258 // If the branch is at the end of its MBB and that has a fall-through block,
1259 // direct the updated conditional branch to the fall-through block. Otherwise,
1260 // split the MBB before the next instruction.
1261 MachineBasicBlock *MBB = MI->getParent();
Evan Chengbd5d3db2007-02-03 02:08:34 +00001262 MachineInstr *BMI = &MBB->back();
1263 bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
Evan Cheng43aeab62007-01-26 20:38:26 +00001264
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001265 NumCBrFixed++;
Evan Chengbd5d3db2007-02-03 02:08:34 +00001266 if (BMI != MI) {
Dan Gohman8e5f2c62008-07-07 23:14:23 +00001267 if (next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
Evan Chengbd5d3db2007-02-03 02:08:34 +00001268 BMI->getOpcode() == Br.UncondBr) {
Bob Wilson39bf0512009-05-12 17:35:29 +00001269 // Last MI in the BB is an unconditional branch. Can we simply invert the
Evan Cheng43aeab62007-01-26 20:38:26 +00001270 // condition and swap destinations:
1271 // beq L1
1272 // b L2
1273 // =>
1274 // bne L2
1275 // b L1
Chris Lattner8aa797a2007-12-30 23:10:15 +00001276 MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
Evan Chengc0dbec72007-01-31 19:57:44 +00001277 if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
Evan Chengc99ef082007-02-09 20:54:44 +00001278 DOUT << " Invert Bcc condition and swap its destination with " << *BMI;
Chris Lattner8aa797a2007-12-30 23:10:15 +00001279 BMI->getOperand(0).setMBB(DestBB);
1280 MI->getOperand(0).setMBB(NewDest);
Evan Cheng43aeab62007-01-26 20:38:26 +00001281 MI->getOperand(1).setImm(CC);
1282 return true;
1283 }
1284 }
1285 }
1286
1287 if (NeedSplit) {
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001288 SplitBlockBeforeInstr(MI);
Bob Wilson39bf0512009-05-12 17:35:29 +00001289 // No need for the branch to the next block. We're adding an unconditional
Evan Chengdd353b82007-01-26 02:02:39 +00001290 // branch to the destination.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001291 int delta = TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001292 BBSizes[MBB->getNumber()] -= delta;
Dale Johannesen8593e412007-04-29 19:19:30 +00001293 MachineBasicBlock* SplitBB = next(MachineFunction::iterator(MBB));
1294 AdjustBBOffsetsAfter(SplitBB, -delta);
Evan Chengdd353b82007-01-26 02:02:39 +00001295 MBB->back().eraseFromParent();
Dale Johannesen8593e412007-04-29 19:19:30 +00001296 // BBOffsets[SplitBB] is wrong temporarily, fixed below
Evan Chengdd353b82007-01-26 02:02:39 +00001297 }
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001298 MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
Bob Wilson84945262009-05-12 17:09:30 +00001299
Evan Chengc99ef082007-02-09 20:54:44 +00001300 DOUT << " Insert B to BB#" << DestBB->getNumber()
1301 << " also invert condition and change dest. to BB#"
1302 << NextBB->getNumber() << "\n";
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001303
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001304 // Insert a new conditional branch and a new unconditional branch.
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001305 // Also update the ImmBranch as well as adding a new entry for the new branch.
Dale Johannesenb6728402009-02-13 02:25:56 +00001306 BuildMI(MBB, DebugLoc::getUnknownLoc(),
1307 TII->get(MI->getOpcode()))
1308 .addMBB(NextBB).addImm(CC).addReg(CCReg);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001309 Br.MI = &MBB->back();
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001310 BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesenb6728402009-02-13 02:25:56 +00001311 BuildMI(MBB, DebugLoc::getUnknownLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001312 BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back());
Evan Chenga9b8b8d2007-01-31 18:29:27 +00001313 unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
Evan Chenga0bf7942007-01-25 23:31:04 +00001314 ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001315
1316 // Remove the old conditional branch. It may or may not still be in MBB.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001317 BBSizes[MI->getParent()->getNumber()] -= TII->GetInstSizeInBytes(MI);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001318 MI->eraseFromParent();
1319
Dale Johannesen56c42ef2007-04-23 20:09:04 +00001320 // The net size change is an addition of one unconditional branch.
Nicolas Geoffray52e724a2008-04-16 20:10:13 +00001321 int delta = TII->GetInstSizeInBytes(&MBB->back());
Dale Johannesen99c49a42007-02-25 00:47:03 +00001322 AdjustBBOffsetsAfter(MBB, delta);
Evan Chengaf5cbcb2007-01-25 03:12:46 +00001323 return true;
1324}
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001325
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001326/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
1327/// LR / restores LR to pc.
1328bool ARMConstantIslands::UndoLRSpillRestore() {
1329 bool MadeChange = false;
1330 for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1331 MachineInstr *MI = PushPopMIs[i];
Evan Cheng44bec522007-05-15 01:29:07 +00001332 if (MI->getOpcode() == ARM::tPOP_RET &&
1333 MI->getOperand(0).getReg() == ARM::PC &&
1334 MI->getNumExplicitOperands() == 1) {
Dale Johannesenb6728402009-02-13 02:25:56 +00001335 BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET));
Evan Cheng44bec522007-05-15 01:29:07 +00001336 MI->eraseFromParent();
1337 MadeChange = true;
Evan Chengd1b2c1e2007-01-30 01:18:38 +00001338 }
1339 }
1340 return MadeChange;
1341}
Evan Cheng5657c012009-07-29 02:18:14 +00001342
1343bool ARMConstantIslands::OptimizeThumb2JumpTables(MachineFunction &MF) {
1344 bool MadeChange = false;
1345
1346 // FIXME: After the tables are shrunk, can we get rid some of the
1347 // constantpool tables?
1348 const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
1349 const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
1350 for (unsigned i = 0, e = T2JumpTables.size(); i != e; ++i) {
1351 MachineInstr *MI = T2JumpTables[i];
1352 const TargetInstrDesc &TID = MI->getDesc();
1353 unsigned NumOps = TID.getNumOperands();
1354 unsigned JTOpIdx = NumOps - (TID.isPredicable() ? 3 : 2);
1355 MachineOperand JTOP = MI->getOperand(JTOpIdx);
1356 unsigned JTI = JTOP.getIndex();
1357 assert(JTI < JT.size());
1358
1359 bool ByteOk = true;
1360 bool HalfWordOk = true;
1361 unsigned JTOffset = GetOffsetOf(MI) + 4;
1362 const std::vector<MachineBasicBlock*> &JTBBs = JT[JTI].MBBs;
1363 for (unsigned j = 0, ee = JTBBs.size(); j != ee; ++j) {
1364 MachineBasicBlock *MBB = JTBBs[j];
1365 unsigned DstOffset = BBOffsets[MBB->getNumber()];
Evan Cheng8770f742009-07-29 23:20:20 +00001366 // Negative offset is not ok. FIXME: We should change BB layout to make
1367 // sure all the branches are forward.
Evan Chengd26b14c2009-07-31 18:28:05 +00001368 if (ByteOk && (DstOffset - JTOffset) > ((1<<8)-1)*2)
Evan Cheng5657c012009-07-29 02:18:14 +00001369 ByteOk = false;
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001370 unsigned TBHLimit = ((1<<16)-1)*2;
1371 if (STI->isTargetDarwin())
1372 TBHLimit >>= 1; // FIXME: Work around an assembler bug.
1373 if (HalfWordOk && (DstOffset - JTOffset) > TBHLimit)
Evan Cheng5657c012009-07-29 02:18:14 +00001374 HalfWordOk = false;
1375 if (!ByteOk && !HalfWordOk)
1376 break;
1377 }
1378
1379 if (ByteOk || HalfWordOk) {
1380 MachineBasicBlock *MBB = MI->getParent();
1381 unsigned BaseReg = MI->getOperand(0).getReg();
1382 bool BaseRegKill = MI->getOperand(0).isKill();
1383 if (!BaseRegKill)
1384 continue;
1385 unsigned IdxReg = MI->getOperand(1).getReg();
1386 bool IdxRegKill = MI->getOperand(1).isKill();
1387 MachineBasicBlock::iterator PrevI = MI;
1388 if (PrevI == MBB->begin())
1389 continue;
1390
1391 MachineInstr *AddrMI = --PrevI;
1392 bool OptOk = true;
1393 // Examine the instruction that calculate the jumptable entry address.
1394 // If it's not the one just before the t2BR_JT, we won't delete it, then
1395 // it's not worth doing the optimization.
1396 for (unsigned k = 0, eee = AddrMI->getNumOperands(); k != eee; ++k) {
1397 const MachineOperand &MO = AddrMI->getOperand(k);
1398 if (!MO.isReg() || !MO.getReg())
1399 continue;
1400 if (MO.isDef() && MO.getReg() != BaseReg) {
1401 OptOk = false;
1402 break;
1403 }
1404 if (MO.isUse() && !MO.isKill() && MO.getReg() != IdxReg) {
1405 OptOk = false;
1406 break;
1407 }
1408 }
1409 if (!OptOk)
1410 continue;
1411
1412 // The previous instruction should be a t2LEApcrelJT, we want to delete
1413 // it as well.
1414 MachineInstr *LeaMI = --PrevI;
1415 if (LeaMI->getOpcode() != ARM::t2LEApcrelJT ||
1416 LeaMI->getOperand(0).getReg() != BaseReg)
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001417 OptOk = false;
Evan Cheng5657c012009-07-29 02:18:14 +00001418
Evan Cheng25f7cfc2009-08-01 06:13:52 +00001419 if (!OptOk)
1420 continue;
1421
1422 unsigned Opc = ByteOk ? ARM::t2TBB : ARM::t2TBH;
1423 MachineInstr *NewJTMI = BuildMI(MBB, MI->getDebugLoc(), TII->get(Opc))
1424 .addReg(IdxReg, getKillRegState(IdxRegKill))
1425 .addJumpTableIndex(JTI, JTOP.getTargetFlags())
1426 .addImm(MI->getOperand(JTOpIdx+1).getImm());
1427 // FIXME: Insert an "ALIGN" instruction to ensure the next instruction
1428 // is 2-byte aligned. For now, asm printer will fix it up.
1429 unsigned NewSize = TII->GetInstSizeInBytes(NewJTMI);
1430 unsigned OrigSize = TII->GetInstSizeInBytes(AddrMI);
1431 OrigSize += TII->GetInstSizeInBytes(LeaMI);
1432 OrigSize += TII->GetInstSizeInBytes(MI);
1433
1434 AddrMI->eraseFromParent();
1435 LeaMI->eraseFromParent();
1436 MI->eraseFromParent();
1437
1438 int delta = OrigSize - NewSize;
1439 BBSizes[MBB->getNumber()] -= delta;
1440 AdjustBBOffsetsAfter(MBB, -delta);
1441
1442 ++NumTBs;
1443 MadeChange = true;
Evan Cheng5657c012009-07-29 02:18:14 +00001444 }
1445 }
1446
1447 return MadeChange;
1448}