blob: 37dc2c27286b866ccee1502a97ca9721aebeb590 [file] [log] [blame]
Chris Lattner2cfd52c2009-07-29 20:31:52 +00001//===- ARMBaseRegisterInfo.cpp - ARM Register Information -------*- C++ -*-===//
David Goodwinc140c482009-07-08 17:28:55 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the base ARM implementation of TargetRegisterInfo class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ARM.h"
15#include "ARMAddressingModes.h"
David Goodwindb5a71a2009-07-08 18:31:39 +000016#include "ARMBaseInstrInfo.h"
David Goodwinc140c482009-07-08 17:28:55 +000017#include "ARMBaseRegisterInfo.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000018#include "ARMFrameLowering.h"
David Goodwinc140c482009-07-08 17:28:55 +000019#include "ARMInstrInfo.h"
20#include "ARMMachineFunctionInfo.h"
21#include "ARMSubtarget.h"
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
Owen Anderson9adc0ab2009-07-14 23:09:55 +000024#include "llvm/Function.h"
25#include "llvm/LLVMContext.h"
David Goodwinc140c482009-07-08 17:28:55 +000026#include "llvm/CodeGen/MachineConstantPool.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineLocation.h"
31#include "llvm/CodeGen/MachineRegisterInfo.h"
32#include "llvm/CodeGen/RegisterScavenging.h"
Jim Grosbach3dab2772009-10-27 22:45:39 +000033#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000034#include "llvm/Support/ErrorHandling.h"
Torok Edwindac237e2009-07-08 20:53:28 +000035#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000036#include "llvm/Target/TargetFrameLowering.h"
David Goodwinc140c482009-07-08 17:28:55 +000037#include "llvm/Target/TargetMachine.h"
38#include "llvm/Target/TargetOptions.h"
39#include "llvm/ADT/BitVector.h"
40#include "llvm/ADT/SmallVector.h"
Jim Grosbach18ed9c92009-10-20 20:19:50 +000041#include "llvm/Support/CommandLine.h"
David Goodwinc140c482009-07-08 17:28:55 +000042
Evan Cheng1b4886d2010-11-18 01:28:51 +000043using namespace llvm;
44
Jim Grosbacha2734422010-08-24 19:05:43 +000045static cl::opt<bool>
Jim Grosbach31973802010-08-24 21:19:33 +000046ForceAllBaseRegAlloc("arm-force-base-reg-alloc", cl::Hidden, cl::init(false),
Jim Grosbachcd59dc52010-08-24 18:04:52 +000047 cl::desc("Force use of virtual base registers for stack load/store"));
Jim Grosbacha2734422010-08-24 19:05:43 +000048static cl::opt<bool>
Jim Grosbachae47c6d2010-08-26 00:58:06 +000049EnableLocalStackAlloc("enable-local-stack-alloc", cl::init(true), cl::Hidden,
Jim Grosbacha2734422010-08-24 19:05:43 +000050 cl::desc("Enable pre-regalloc stack frame index allocation"));
Jim Grosbach65482b12010-09-03 18:37:12 +000051static cl::opt<bool>
Jim Grosbachd0bd76b2010-09-08 20:12:02 +000052EnableBasePointer("arm-use-base-pointer", cl::Hidden, cl::init(true),
Jim Grosbach65482b12010-09-03 18:37:12 +000053 cl::desc("Enable use of a base pointer for complex stack frames"));
54
David Goodwindb5a71a2009-07-08 18:31:39 +000055ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
David Goodwinc140c482009-07-08 17:28:55 +000056 const ARMSubtarget &sti)
57 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
58 TII(tii), STI(sti),
Jim Grosbach65482b12010-09-03 18:37:12 +000059 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
60 BasePtr(ARM::R6) {
David Goodwinc140c482009-07-08 17:28:55 +000061}
62
63const unsigned*
64ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
65 static const unsigned CalleeSavedRegs[] = {
66 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
67 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
68
69 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
70 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
71 0
72 };
73
74 static const unsigned DarwinCalleeSavedRegs[] = {
75 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
76 // register.
Jim Grosbachab3d00e2010-11-02 17:35:25 +000077 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
78 ARM::R11, ARM::R10, ARM::R8,
David Goodwinc140c482009-07-08 17:28:55 +000079
80 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
81 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
82 0
83 };
84 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
85}
86
Jim Grosbach96318642010-01-06 23:54:42 +000087BitVector ARMBaseRegisterInfo::
88getReservedRegs(const MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000089 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000090
Chris Lattner7a2bdde2011-04-15 05:18:47 +000091 // FIXME: avoid re-calculating this every time.
David Goodwinc140c482009-07-08 17:28:55 +000092 BitVector Reserved(getNumRegs());
93 Reserved.set(ARM::SP);
94 Reserved.set(ARM::PC);
Nate Begemand1fb5832010-08-03 21:31:55 +000095 Reserved.set(ARM::FPSCR);
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000096 if (TFI->hasFP(MF))
David Goodwinc140c482009-07-08 17:28:55 +000097 Reserved.set(FramePtr);
Jim Grosbach65482b12010-09-03 18:37:12 +000098 if (hasBasePointer(MF))
99 Reserved.set(BasePtr);
David Goodwinc140c482009-07-08 17:28:55 +0000100 // Some targets reserve R9.
101 if (STI.isR9Reserved())
102 Reserved.set(ARM::R9);
103 return Reserved;
104}
105
Chris Lattner2cfd52c2009-07-29 20:31:52 +0000106bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
107 unsigned Reg) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000108 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000109
David Goodwinc140c482009-07-08 17:28:55 +0000110 switch (Reg) {
111 default: break;
112 case ARM::SP:
113 case ARM::PC:
114 return true;
Jim Grosbach65482b12010-09-03 18:37:12 +0000115 case ARM::R6:
116 if (hasBasePointer(MF))
117 return true;
118 break;
David Goodwinc140c482009-07-08 17:28:55 +0000119 case ARM::R7:
120 case ARM::R11:
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000121 if (FramePtr == Reg && TFI->hasFP(MF))
David Goodwinc140c482009-07-08 17:28:55 +0000122 return true;
123 break;
124 case ARM::R9:
125 return STI.isR9Reserved();
126 }
127
128 return false;
129}
130
Chris Lattner2cfd52c2009-07-29 20:31:52 +0000131const TargetRegisterClass *
Evan Cheng4f54c122009-10-25 07:53:28 +0000132ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
133 const TargetRegisterClass *B,
134 unsigned SubIdx) const {
135 switch (SubIdx) {
136 default: return 0;
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000137 case ARM::ssub_0:
138 case ARM::ssub_1:
139 case ARM::ssub_2:
140 case ARM::ssub_3: {
Evan Cheng4f54c122009-10-25 07:53:28 +0000141 // S sub-registers.
142 if (A->getSize() == 8) {
Evan Chengba908642009-11-03 05:52:54 +0000143 if (B == &ARM::SPR_8RegClass)
144 return &ARM::DPR_8RegClass;
145 assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
Evan Cheng4f54c122009-10-25 07:53:28 +0000146 if (A == &ARM::DPR_8RegClass)
147 return A;
148 return &ARM::DPR_VFP2RegClass;
149 }
150
Evan Chengb63387a2010-05-06 06:36:08 +0000151 if (A->getSize() == 16) {
152 if (B == &ARM::SPR_8RegClass)
153 return &ARM::QPR_8RegClass;
154 return &ARM::QPR_VFP2RegClass;
155 }
156
Evan Cheng22c687b2010-05-14 02:13:41 +0000157 if (A->getSize() == 32) {
158 if (B == &ARM::SPR_8RegClass)
159 return 0; // Do not allow coalescing!
160 return &ARM::QQPR_VFP2RegClass;
161 }
162
163 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
164 return 0; // Do not allow coalescing!
Evan Chengb63387a2010-05-06 06:36:08 +0000165 }
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000166 case ARM::dsub_0:
167 case ARM::dsub_1:
168 case ARM::dsub_2:
169 case ARM::dsub_3: {
Evan Cheng4f54c122009-10-25 07:53:28 +0000170 // D sub-registers.
Evan Chengb63387a2010-05-06 06:36:08 +0000171 if (A->getSize() == 16) {
172 if (B == &ARM::DPR_VFP2RegClass)
173 return &ARM::QPR_VFP2RegClass;
174 if (B == &ARM::DPR_8RegClass)
Evan Cheng22c687b2010-05-14 02:13:41 +0000175 return 0; // Do not allow coalescing!
Evan Chengb63387a2010-05-06 06:36:08 +0000176 return A;
177 }
178
Evan Cheng22c687b2010-05-14 02:13:41 +0000179 if (A->getSize() == 32) {
180 if (B == &ARM::DPR_VFP2RegClass)
181 return &ARM::QQPR_VFP2RegClass;
182 if (B == &ARM::DPR_8RegClass)
183 return 0; // Do not allow coalescing!
184 return A;
185 }
186
187 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
188 if (B != &ARM::DPRRegClass)
189 return 0; // Do not allow coalescing!
Evan Cheng4f54c122009-10-25 07:53:28 +0000190 return A;
191 }
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000192 case ARM::dsub_4:
193 case ARM::dsub_5:
194 case ARM::dsub_6:
195 case ARM::dsub_7: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000196 // D sub-registers of QQQQ registers.
197 if (A->getSize() == 64 && B == &ARM::DPRRegClass)
198 return A;
199 return 0; // Do not allow coalescing!
200 }
201
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000202 case ARM::qsub_0:
203 case ARM::qsub_1: {
Evan Chengb63387a2010-05-06 06:36:08 +0000204 // Q sub-registers.
Evan Cheng22c687b2010-05-14 02:13:41 +0000205 if (A->getSize() == 32) {
206 if (B == &ARM::QPR_VFP2RegClass)
207 return &ARM::QQPR_VFP2RegClass;
208 if (B == &ARM::QPR_8RegClass)
209 return 0; // Do not allow coalescing!
210 return A;
211 }
212
213 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
214 if (B == &ARM::QPRRegClass)
215 return A;
216 return 0; // Do not allow coalescing!
217 }
Jakob Stoklund Olesene00fa642010-05-25 00:15:15 +0000218 case ARM::qsub_2:
219 case ARM::qsub_3: {
Evan Cheng22c687b2010-05-14 02:13:41 +0000220 // Q sub-registers of QQQQ registers.
221 if (A->getSize() == 64 && B == &ARM::QPRRegClass)
222 return A;
223 return 0; // Do not allow coalescing!
Evan Chengb63387a2010-05-06 06:36:08 +0000224 }
225 }
Evan Cheng4f54c122009-10-25 07:53:28 +0000226 return 0;
227}
228
Evan Chengb990a2f2010-05-14 23:21:14 +0000229bool
Bob Wilson91a74da2010-06-02 18:54:47 +0000230ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
Evan Chengb990a2f2010-05-14 23:21:14 +0000231 SmallVectorImpl<unsigned> &SubIndices,
232 unsigned &NewSubIdx) const {
233
234 unsigned Size = RC->getSize() * 8;
235 if (Size < 6)
236 return 0;
237
238 NewSubIdx = 0; // Whole register.
239 unsigned NumRegs = SubIndices.size();
240 if (NumRegs == 8) {
241 // 8 D registers -> 1 QQQQ register.
242 return (Size == 512 &&
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000243 SubIndices[0] == ARM::dsub_0 &&
244 SubIndices[1] == ARM::dsub_1 &&
245 SubIndices[2] == ARM::dsub_2 &&
246 SubIndices[3] == ARM::dsub_3 &&
247 SubIndices[4] == ARM::dsub_4 &&
248 SubIndices[5] == ARM::dsub_5 &&
249 SubIndices[6] == ARM::dsub_6 &&
250 SubIndices[7] == ARM::dsub_7);
Evan Chengb990a2f2010-05-14 23:21:14 +0000251 } else if (NumRegs == 4) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000252 if (SubIndices[0] == ARM::qsub_0) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000253 // 4 Q registers -> 1 QQQQ register.
254 return (Size == 512 &&
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000255 SubIndices[1] == ARM::qsub_1 &&
256 SubIndices[2] == ARM::qsub_2 &&
257 SubIndices[3] == ARM::qsub_3);
258 } else if (SubIndices[0] == ARM::dsub_0) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000259 // 4 D registers -> 1 QQ register.
260 if (Size >= 256 &&
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000261 SubIndices[1] == ARM::dsub_1 &&
262 SubIndices[2] == ARM::dsub_2 &&
263 SubIndices[3] == ARM::dsub_3) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000264 if (Size == 512)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000265 NewSubIdx = ARM::qqsub_0;
Evan Chengb990a2f2010-05-14 23:21:14 +0000266 return true;
267 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000268 } else if (SubIndices[0] == ARM::dsub_4) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000269 // 4 D registers -> 1 QQ register (2nd).
270 if (Size == 512 &&
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000271 SubIndices[1] == ARM::dsub_5 &&
272 SubIndices[2] == ARM::dsub_6 &&
273 SubIndices[3] == ARM::dsub_7) {
274 NewSubIdx = ARM::qqsub_1;
Evan Chengb990a2f2010-05-14 23:21:14 +0000275 return true;
276 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000277 } else if (SubIndices[0] == ARM::ssub_0) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000278 // 4 S registers -> 1 Q register.
279 if (Size >= 128 &&
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000280 SubIndices[1] == ARM::ssub_1 &&
281 SubIndices[2] == ARM::ssub_2 &&
282 SubIndices[3] == ARM::ssub_3) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000283 if (Size >= 256)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000284 NewSubIdx = ARM::qsub_0;
Evan Chengb990a2f2010-05-14 23:21:14 +0000285 return true;
286 }
287 }
288 } else if (NumRegs == 2) {
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000289 if (SubIndices[0] == ARM::qsub_0) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000290 // 2 Q registers -> 1 QQ register.
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000291 if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000292 if (Size == 512)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000293 NewSubIdx = ARM::qqsub_0;
Evan Chengb990a2f2010-05-14 23:21:14 +0000294 return true;
295 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000296 } else if (SubIndices[0] == ARM::qsub_2) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000297 // 2 Q registers -> 1 QQ register (2nd).
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000298 if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
299 NewSubIdx = ARM::qqsub_1;
Evan Chengb990a2f2010-05-14 23:21:14 +0000300 return true;
301 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000302 } else if (SubIndices[0] == ARM::dsub_0) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000303 // 2 D registers -> 1 Q register.
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000304 if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000305 if (Size >= 256)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000306 NewSubIdx = ARM::qsub_0;
Evan Chengb990a2f2010-05-14 23:21:14 +0000307 return true;
308 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000309 } else if (SubIndices[0] == ARM::dsub_2) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000310 // 2 D registers -> 1 Q register (2nd).
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000311 if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
312 NewSubIdx = ARM::qsub_1;
Evan Chengb990a2f2010-05-14 23:21:14 +0000313 return true;
314 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000315 } else if (SubIndices[0] == ARM::dsub_4) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000316 // 2 D registers -> 1 Q register (3rd).
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000317 if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
318 NewSubIdx = ARM::qsub_2;
Evan Chengb990a2f2010-05-14 23:21:14 +0000319 return true;
320 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000321 } else if (SubIndices[0] == ARM::dsub_6) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000322 // 2 D registers -> 1 Q register (3rd).
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000323 if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
324 NewSubIdx = ARM::qsub_3;
Evan Chengb990a2f2010-05-14 23:21:14 +0000325 return true;
326 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000327 } else if (SubIndices[0] == ARM::ssub_0) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000328 // 2 S registers -> 1 D register.
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000329 if (SubIndices[1] == ARM::ssub_1) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000330 if (Size >= 128)
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000331 NewSubIdx = ARM::dsub_0;
Evan Chengb990a2f2010-05-14 23:21:14 +0000332 return true;
333 }
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000334 } else if (SubIndices[0] == ARM::ssub_2) {
Evan Chengb990a2f2010-05-14 23:21:14 +0000335 // 2 S registers -> 1 D register (2nd).
Jakob Stoklund Olesen558661d2010-05-24 16:54:32 +0000336 if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
337 NewSubIdx = ARM::dsub_1;
Evan Chengb990a2f2010-05-14 23:21:14 +0000338 return true;
339 }
340 }
341 }
342 return false;
343}
344
Jakob Stoklund Olesenc9e50152011-04-26 18:52:33 +0000345const TargetRegisterClass*
346ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
347 const {
348 const TargetRegisterClass *Super = RC;
349 TargetRegisterClass::sc_iterator I = RC->superclasses_begin();
350 do {
351 switch (Super->getID()) {
352 case ARM::GPRRegClassID:
353 case ARM::SPRRegClassID:
354 case ARM::DPRRegClassID:
355 case ARM::QPRRegClassID:
356 case ARM::QQPRRegClassID:
357 case ARM::QQQQPRRegClassID:
358 return Super;
359 }
360 Super = *I++;
361 } while (Super);
362 return RC;
363}
Evan Chengb990a2f2010-05-14 23:21:14 +0000364
Evan Cheng4f54c122009-10-25 07:53:28 +0000365const TargetRegisterClass *
Chris Lattner2cfd52c2009-07-29 20:31:52 +0000366ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
Jim Grosbache11a8f52009-09-11 19:49:06 +0000367 return ARM::GPRRegisterClass;
David Goodwinc140c482009-07-08 17:28:55 +0000368}
369
Cameron Zwarichbe2119e2011-03-07 21:56:36 +0000370unsigned
371ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
372 MachineFunction &MF) const {
373 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
374
375 switch (RC->getID()) {
376 default:
377 return 0;
378 case ARM::tGPRRegClassID:
379 return TFI->hasFP(MF) ? 4 : 5;
380 case ARM::GPRRegClassID: {
381 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
382 return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
383 }
384 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
385 case ARM::DPRRegClassID:
386 return 32 - 10;
387 }
388}
389
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000390/// getRawAllocationOrder - Returns the register allocation order for a
391/// specified register class with a target-dependent hint.
392ArrayRef<unsigned>
393ARMBaseRegisterInfo::getRawAllocationOrder(const TargetRegisterClass *RC,
394 unsigned HintType, unsigned HintReg,
395 const MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000396 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
David Goodwinc140c482009-07-08 17:28:55 +0000397 // Alternative register allocation orders when favoring even / odd registers
398 // of register pairs.
399
400 // No FP, R9 is available.
401 static const unsigned GPREven1[] = {
402 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
403 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
404 ARM::R9, ARM::R11
405 };
406 static const unsigned GPROdd1[] = {
407 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
408 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
409 ARM::R8, ARM::R10
410 };
411
412 // FP is R7, R9 is available.
413 static const unsigned GPREven2[] = {
414 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
415 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
416 ARM::R9, ARM::R11
417 };
418 static const unsigned GPROdd2[] = {
419 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
420 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
421 ARM::R8, ARM::R10
422 };
423
424 // FP is R11, R9 is available.
425 static const unsigned GPREven3[] = {
426 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
427 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
428 ARM::R9
429 };
430 static const unsigned GPROdd3[] = {
431 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
432 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
433 ARM::R8
434 };
435
436 // No FP, R9 is not available.
437 static const unsigned GPREven4[] = {
438 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
439 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
440 ARM::R11
441 };
442 static const unsigned GPROdd4[] = {
443 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
444 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
445 ARM::R10
446 };
447
448 // FP is R7, R9 is not available.
449 static const unsigned GPREven5[] = {
450 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
451 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
452 ARM::R11
453 };
454 static const unsigned GPROdd5[] = {
455 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
456 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
457 ARM::R10
458 };
459
460 // FP is R11, R9 is not available.
461 static const unsigned GPREven6[] = {
462 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
463 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
464 };
465 static const unsigned GPROdd6[] = {
466 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
467 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
468 };
469
Jakob Stoklund Oleseneb5067e2011-03-25 01:48:18 +0000470 // We only support even/odd hints for GPR and rGPR.
471 if (RC != ARM::GPRRegisterClass && RC != ARM::rGPRRegisterClass)
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000472 return RC->getRawAllocationOrder(MF);
David Goodwinc140c482009-07-08 17:28:55 +0000473
474 if (HintType == ARMRI::RegPairEven) {
475 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
476 // It's no longer possible to fulfill this hint. Return the default
477 // allocation order.
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000478 return RC->getRawAllocationOrder(MF);
David Goodwinc140c482009-07-08 17:28:55 +0000479
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000480 if (!TFI->hasFP(MF)) {
David Goodwinc140c482009-07-08 17:28:55 +0000481 if (!STI.isR9Reserved())
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000482 return GPREven1;
David Goodwinc140c482009-07-08 17:28:55 +0000483 else
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000484 return GPREven4;
David Goodwinc140c482009-07-08 17:28:55 +0000485 } else if (FramePtr == ARM::R7) {
486 if (!STI.isR9Reserved())
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000487 return GPREven2;
David Goodwinc140c482009-07-08 17:28:55 +0000488 else
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000489 return GPREven5;
David Goodwinc140c482009-07-08 17:28:55 +0000490 } else { // FramePtr == ARM::R11
491 if (!STI.isR9Reserved())
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000492 return GPREven3;
David Goodwinc140c482009-07-08 17:28:55 +0000493 else
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000494 return GPREven6;
David Goodwinc140c482009-07-08 17:28:55 +0000495 }
496 } else if (HintType == ARMRI::RegPairOdd) {
497 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
498 // It's no longer possible to fulfill this hint. Return the default
499 // allocation order.
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000500 return RC->getRawAllocationOrder(MF);
David Goodwinc140c482009-07-08 17:28:55 +0000501
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000502 if (!TFI->hasFP(MF)) {
David Goodwinc140c482009-07-08 17:28:55 +0000503 if (!STI.isR9Reserved())
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000504 return GPROdd1;
David Goodwinc140c482009-07-08 17:28:55 +0000505 else
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000506 return GPROdd4;
David Goodwinc140c482009-07-08 17:28:55 +0000507 } else if (FramePtr == ARM::R7) {
508 if (!STI.isR9Reserved())
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000509 return GPROdd2;
David Goodwinc140c482009-07-08 17:28:55 +0000510 else
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000511 return GPROdd5;
David Goodwinc140c482009-07-08 17:28:55 +0000512 } else { // FramePtr == ARM::R11
513 if (!STI.isR9Reserved())
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000514 return GPROdd3;
David Goodwinc140c482009-07-08 17:28:55 +0000515 else
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000516 return GPROdd6;
David Goodwinc140c482009-07-08 17:28:55 +0000517 }
518 }
Jakob Stoklund Olesendd5a8472011-06-16 23:31:16 +0000519 return RC->getRawAllocationOrder(MF);
David Goodwinc140c482009-07-08 17:28:55 +0000520}
521
522/// ResolveRegAllocHint - Resolves the specified register allocation hint
523/// to a physical register. Returns the physical register if it is successful.
524unsigned
525ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
526 const MachineFunction &MF) const {
527 if (Reg == 0 || !isPhysicalRegister(Reg))
528 return 0;
529 if (Type == 0)
530 return Reg;
531 else if (Type == (unsigned)ARMRI::RegPairOdd)
532 // Odd register.
533 return getRegisterPairOdd(Reg, MF);
534 else if (Type == (unsigned)ARMRI::RegPairEven)
535 // Even register.
536 return getRegisterPairEven(Reg, MF);
537 return 0;
538}
539
540void
541ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
542 MachineFunction &MF) const {
543 MachineRegisterInfo *MRI = &MF.getRegInfo();
544 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
545 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
546 Hint.first == (unsigned)ARMRI::RegPairEven) &&
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000547 TargetRegisterInfo::isVirtualRegister(Hint.second)) {
David Goodwinc140c482009-07-08 17:28:55 +0000548 // If 'Reg' is one of the even / odd register pair and it's now changed
549 // (e.g. coalesced) into a different register. The other register of the
550 // pair allocation hint must be updated to reflect the relationship
551 // change.
552 unsigned OtherReg = Hint.second;
553 Hint = MRI->getRegAllocationHint(OtherReg);
554 if (Hint.second == Reg)
555 // Make sure the pair has not already divorced.
556 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
557 }
558}
559
Bob Wilsonf6a4d3c2011-04-19 18:11:45 +0000560bool
561ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
562 // CortexA9 has a Write-after-write hazard for NEON registers.
563 if (!STI.isCortexA9())
564 return false;
565
566 switch (RC->getID()) {
567 case ARM::DPRRegClassID:
568 case ARM::DPR_8RegClassID:
569 case ARM::DPR_VFP2RegClassID:
570 case ARM::QPRRegClassID:
571 case ARM::QPR_8RegClassID:
572 case ARM::QPR_VFP2RegClassID:
573 case ARM::SPRRegClassID:
574 case ARM::SPR_8RegClassID:
575 // Avoid reusing S, D, and Q registers.
576 // Don't increase register pressure for QQ and QQQQ.
577 return true;
578 default:
579 return false;
580 }
581}
582
Jim Grosbach65482b12010-09-03 18:37:12 +0000583bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
Jim Grosbache45ab8a2010-01-19 18:31:11 +0000584 const MachineFrameInfo *MFI = MF.getFrameInfo();
585 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach65482b12010-09-03 18:37:12 +0000586
587 if (!EnableBasePointer)
588 return false;
589
590 if (needsStackRealignment(MF) && MFI->hasVarSizedObjects())
591 return true;
592
593 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
594 // negative range for ldr/str (255), and thumb1 is positive offsets only.
595 // It's going to be better to use the SP or Base Pointer instead. When there
596 // are variable sized objects, we can't reference off of the SP, so we
597 // reserve a Base Pointer.
598 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
599 // Conservatively estimate whether the negative offset from the frame
600 // pointer will be sufficient to reach. If a function has a smallish
601 // frame, it's less likely to have lots of spills and callee saved
602 // space, so it's all more likely to be within range of the frame pointer.
603 // If it's wrong, the scavenger will still enable access to work, it just
604 // won't be optimal.
605 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
606 return false;
607 return true;
608 }
609
610 return false;
611}
612
613bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
Jim Grosbach30c93e12010-09-08 17:22:12 +0000614 const MachineFrameInfo *MFI = MF.getFrameInfo();
Jim Grosbach65482b12010-09-03 18:37:12 +0000615 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach30c93e12010-09-08 17:22:12 +0000616 // We can't realign the stack if:
617 // 1. Dynamic stack realignment is explicitly disabled,
618 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
619 // 3. There are VLAs in the function and the base pointer is disabled.
620 return (RealignStack && !AFI->isThumb1OnlyFunction() &&
621 (!MFI->hasVarSizedObjects() || EnableBasePointer));
Jim Grosbache45ab8a2010-01-19 18:31:11 +0000622}
623
Jim Grosbach3dab2772009-10-27 22:45:39 +0000624bool ARMBaseRegisterInfo::
625needsStackRealignment(const MachineFunction &MF) const {
Jim Grosbach3dab2772009-10-27 22:45:39 +0000626 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherd4c36ce2010-07-17 00:27:24 +0000627 const Function *F = MF.getFunction();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000628 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
Jim Grosbachfc633002010-09-03 18:28:19 +0000629 bool requiresRealignment = ((MFI->getLocalFrameMaxAlign() > StackAlign) ||
Eric Christopher697cba82010-07-17 00:33:04 +0000630 F->hasFnAttr(Attribute::StackAlignment));
Jim Grosbach5c33f5b2010-09-02 19:52:39 +0000631
Eric Christopherd4c36ce2010-07-17 00:27:24 +0000632 return requiresRealignment && canRealignStack(MF);
Jim Grosbach3dab2772009-10-27 22:45:39 +0000633}
634
Jim Grosbach96318642010-01-06 23:54:42 +0000635bool ARMBaseRegisterInfo::
636cannotEliminateFrame(const MachineFunction &MF) const {
Evan Cheng98a01042009-08-14 20:48:13 +0000637 const MachineFrameInfo *MFI = MF.getFrameInfo();
Bill Wendlingb92187a2010-05-14 21:14:32 +0000638 if (DisableFramePointerElim(MF) && MFI->adjustsStack())
Evan Cheng98a01042009-08-14 20:48:13 +0000639 return true;
Jim Grosbach31bc8492009-11-08 00:27:19 +0000640 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
641 || needsStackRealignment(MF);
Evan Cheng98a01042009-08-14 20:48:13 +0000642}
643
David Goodwinc140c482009-07-08 17:28:55 +0000644unsigned ARMBaseRegisterInfo::getRARegister() const {
645 return ARM::LR;
646}
647
Jim Grosbach5c33f5b2010-09-02 19:52:39 +0000648unsigned
David Greene3f2bf852009-11-12 20:49:22 +0000649ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000650 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000651
652 if (TFI->hasFP(MF))
David Goodwinc140c482009-07-08 17:28:55 +0000653 return FramePtr;
654 return ARM::SP;
655}
656
657unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000658 llvm_unreachable("What is the exception register");
David Goodwinc140c482009-07-08 17:28:55 +0000659 return 0;
660}
661
662unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000663 llvm_unreachable("What is the exception handler register");
David Goodwinc140c482009-07-08 17:28:55 +0000664 return 0;
665}
666
667int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
668 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
669}
670
Rafael Espindola6e032942011-05-30 20:20:15 +0000671int ARMBaseRegisterInfo::getLLVMRegNum(unsigned DwarfRegNo, bool isEH) const {
672 return ARMGenRegisterInfo::getLLVMRegNumFull(DwarfRegNo,0);
673}
674
David Goodwinc140c482009-07-08 17:28:55 +0000675unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
Jim Grosbach96318642010-01-06 23:54:42 +0000676 const MachineFunction &MF) const {
David Goodwinc140c482009-07-08 17:28:55 +0000677 switch (Reg) {
678 default: break;
679 // Return 0 if either register of the pair is a special register.
680 // So no R12, etc.
681 case ARM::R1:
682 return ARM::R0;
683 case ARM::R3:
Jim Grosbach60097512009-10-19 22:57:03 +0000684 return ARM::R2;
David Goodwinc140c482009-07-08 17:28:55 +0000685 case ARM::R5:
686 return ARM::R4;
687 case ARM::R7:
Jim Grosbach65482b12010-09-03 18:37:12 +0000688 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
689 ? 0 : ARM::R6;
David Goodwinc140c482009-07-08 17:28:55 +0000690 case ARM::R9:
691 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
692 case ARM::R11:
693 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
694
695 case ARM::S1:
696 return ARM::S0;
697 case ARM::S3:
698 return ARM::S2;
699 case ARM::S5:
700 return ARM::S4;
701 case ARM::S7:
702 return ARM::S6;
703 case ARM::S9:
704 return ARM::S8;
705 case ARM::S11:
706 return ARM::S10;
707 case ARM::S13:
708 return ARM::S12;
709 case ARM::S15:
710 return ARM::S14;
711 case ARM::S17:
712 return ARM::S16;
713 case ARM::S19:
714 return ARM::S18;
715 case ARM::S21:
716 return ARM::S20;
717 case ARM::S23:
718 return ARM::S22;
719 case ARM::S25:
720 return ARM::S24;
721 case ARM::S27:
722 return ARM::S26;
723 case ARM::S29:
724 return ARM::S28;
725 case ARM::S31:
726 return ARM::S30;
727
728 case ARM::D1:
729 return ARM::D0;
730 case ARM::D3:
731 return ARM::D2;
732 case ARM::D5:
733 return ARM::D4;
734 case ARM::D7:
735 return ARM::D6;
736 case ARM::D9:
737 return ARM::D8;
738 case ARM::D11:
739 return ARM::D10;
740 case ARM::D13:
741 return ARM::D12;
742 case ARM::D15:
743 return ARM::D14;
Evan Cheng8295d992009-07-22 05:55:18 +0000744 case ARM::D17:
745 return ARM::D16;
746 case ARM::D19:
747 return ARM::D18;
748 case ARM::D21:
749 return ARM::D20;
750 case ARM::D23:
751 return ARM::D22;
752 case ARM::D25:
753 return ARM::D24;
754 case ARM::D27:
755 return ARM::D26;
756 case ARM::D29:
757 return ARM::D28;
758 case ARM::D31:
759 return ARM::D30;
David Goodwinc140c482009-07-08 17:28:55 +0000760 }
761
762 return 0;
763}
764
765unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
766 const MachineFunction &MF) const {
767 switch (Reg) {
768 default: break;
769 // Return 0 if either register of the pair is a special register.
770 // So no R12, etc.
771 case ARM::R0:
772 return ARM::R1;
773 case ARM::R2:
Jim Grosbach60097512009-10-19 22:57:03 +0000774 return ARM::R3;
David Goodwinc140c482009-07-08 17:28:55 +0000775 case ARM::R4:
776 return ARM::R5;
777 case ARM::R6:
Jim Grosbach65482b12010-09-03 18:37:12 +0000778 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
779 ? 0 : ARM::R7;
David Goodwinc140c482009-07-08 17:28:55 +0000780 case ARM::R8:
781 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
782 case ARM::R10:
783 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
784
785 case ARM::S0:
786 return ARM::S1;
787 case ARM::S2:
788 return ARM::S3;
789 case ARM::S4:
790 return ARM::S5;
791 case ARM::S6:
792 return ARM::S7;
793 case ARM::S8:
794 return ARM::S9;
795 case ARM::S10:
796 return ARM::S11;
797 case ARM::S12:
798 return ARM::S13;
799 case ARM::S14:
800 return ARM::S15;
801 case ARM::S16:
802 return ARM::S17;
803 case ARM::S18:
804 return ARM::S19;
805 case ARM::S20:
806 return ARM::S21;
807 case ARM::S22:
808 return ARM::S23;
809 case ARM::S24:
810 return ARM::S25;
811 case ARM::S26:
812 return ARM::S27;
813 case ARM::S28:
814 return ARM::S29;
815 case ARM::S30:
816 return ARM::S31;
817
818 case ARM::D0:
819 return ARM::D1;
820 case ARM::D2:
821 return ARM::D3;
822 case ARM::D4:
823 return ARM::D5;
824 case ARM::D6:
825 return ARM::D7;
826 case ARM::D8:
827 return ARM::D9;
828 case ARM::D10:
829 return ARM::D11;
830 case ARM::D12:
831 return ARM::D13;
832 case ARM::D14:
833 return ARM::D15;
Evan Cheng8295d992009-07-22 05:55:18 +0000834 case ARM::D16:
835 return ARM::D17;
836 case ARM::D18:
837 return ARM::D19;
838 case ARM::D20:
839 return ARM::D21;
840 case ARM::D22:
841 return ARM::D23;
842 case ARM::D24:
843 return ARM::D25;
844 case ARM::D26:
845 return ARM::D27;
846 case ARM::D28:
847 return ARM::D29;
848 case ARM::D30:
849 return ARM::D31;
David Goodwinc140c482009-07-08 17:28:55 +0000850 }
851
852 return 0;
853}
854
David Goodwindb5a71a2009-07-08 18:31:39 +0000855/// emitLoadConstPool - Emits a load from constpool to materialize the
856/// specified immediate.
857void ARMBaseRegisterInfo::
858emitLoadConstPool(MachineBasicBlock &MBB,
859 MachineBasicBlock::iterator &MBBI,
David Goodwin77521f52009-07-08 20:28:28 +0000860 DebugLoc dl,
Evan Cheng37844532009-07-16 09:20:10 +0000861 unsigned DestReg, unsigned SubIdx, int Val,
David Goodwindb5a71a2009-07-08 18:31:39 +0000862 ARMCC::CondCodes Pred,
Anton Korobeynikov3daccd82011-03-05 18:43:50 +0000863 unsigned PredReg, unsigned MIFlags) const {
David Goodwindb5a71a2009-07-08 18:31:39 +0000864 MachineFunction &MF = *MBB.getParent();
865 MachineConstantPool *ConstantPool = MF.getConstantPool();
Dan Gohman46510a72010-04-15 01:51:59 +0000866 const Constant *C =
Owen Anderson1d0be152009-08-13 21:58:54 +0000867 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
David Goodwindb5a71a2009-07-08 18:31:39 +0000868 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
869
Evan Cheng37844532009-07-16 09:20:10 +0000870 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
871 .addReg(DestReg, getDefRegState(true), SubIdx)
David Goodwindb5a71a2009-07-08 18:31:39 +0000872 .addConstantPoolIndex(Idx)
Anton Korobeynikov3daccd82011-03-05 18:43:50 +0000873 .addImm(0).addImm(Pred).addReg(PredReg)
874 .setMIFlags(MIFlags);
David Goodwindb5a71a2009-07-08 18:31:39 +0000875}
876
877bool ARMBaseRegisterInfo::
878requiresRegisterScavenging(const MachineFunction &MF) const {
879 return true;
880}
Jim Grosbach41fff8c2009-10-21 23:40:56 +0000881
Jim Grosbach7e831db2009-10-20 01:26:58 +0000882bool ARMBaseRegisterInfo::
883requiresFrameIndexScavenging(const MachineFunction &MF) const {
Jim Grosbachca5dfb72009-10-28 17:33:28 +0000884 return true;
Jim Grosbach7e831db2009-10-20 01:26:58 +0000885}
David Goodwindb5a71a2009-07-08 18:31:39 +0000886
Jim Grosbacha2734422010-08-24 19:05:43 +0000887bool ARMBaseRegisterInfo::
888requiresVirtualBaseRegisters(const MachineFunction &MF) const {
889 return EnableLocalStackAlloc;
890}
891
David Goodwindb5a71a2009-07-08 18:31:39 +0000892static void
Evan Cheng6495f632009-07-28 05:48:47 +0000893emitSPUpdate(bool isARM,
894 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
895 DebugLoc dl, const ARMBaseInstrInfo &TII,
David Goodwindb5a71a2009-07-08 18:31:39 +0000896 int NumBytes,
897 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Evan Cheng6495f632009-07-28 05:48:47 +0000898 if (isARM)
899 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
900 Pred, PredReg, TII);
901 else
902 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
903 Pred, PredReg, TII);
David Goodwindb5a71a2009-07-08 18:31:39 +0000904}
905
Evan Cheng6495f632009-07-28 05:48:47 +0000906
David Goodwindb5a71a2009-07-08 18:31:39 +0000907void ARMBaseRegisterInfo::
908eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
909 MachineBasicBlock::iterator I) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000910 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000911 if (!TFI->hasReservedCallFrame(MF)) {
David Goodwindb5a71a2009-07-08 18:31:39 +0000912 // If we have alloca, convert as follows:
913 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
914 // ADJCALLSTACKUP -> add, sp, sp, amount
915 MachineInstr *Old = I;
916 DebugLoc dl = Old->getDebugLoc();
917 unsigned Amount = Old->getOperand(0).getImm();
918 if (Amount != 0) {
919 // We need to keep the stack aligned properly. To do this, we round the
920 // amount of space needed for the outgoing arguments up to the next
921 // alignment boundary.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000922 unsigned Align = TFI->getStackAlignment();
David Goodwindb5a71a2009-07-08 18:31:39 +0000923 Amount = (Amount+Align-1)/Align*Align;
924
Evan Cheng6495f632009-07-28 05:48:47 +0000925 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
926 assert(!AFI->isThumb1OnlyFunction() &&
Jim Grosbachcf453ee2010-02-23 17:16:27 +0000927 "This eliminateCallFramePseudoInstr does not support Thumb1!");
Evan Cheng6495f632009-07-28 05:48:47 +0000928 bool isARM = !AFI->isThumbFunction();
929
David Goodwindb5a71a2009-07-08 18:31:39 +0000930 // Replace the pseudo instruction with a new instruction...
931 unsigned Opc = Old->getOpcode();
Jim Grosbach4c7628e2010-02-22 22:47:46 +0000932 int PIdx = Old->findFirstPredOperandIdx();
933 ARMCC::CondCodes Pred = (PIdx == -1)
934 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
David Goodwindb5a71a2009-07-08 18:31:39 +0000935 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
936 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
937 unsigned PredReg = Old->getOperand(2).getReg();
Evan Cheng6495f632009-07-28 05:48:47 +0000938 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
David Goodwindb5a71a2009-07-08 18:31:39 +0000939 } else {
940 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
941 unsigned PredReg = Old->getOperand(3).getReg();
942 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
Evan Cheng6495f632009-07-28 05:48:47 +0000943 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
David Goodwindb5a71a2009-07-08 18:31:39 +0000944 }
945 }
946 }
947 MBB.erase(I);
948}
949
Jim Grosbache2f55692010-08-19 23:52:25 +0000950int64_t ARMBaseRegisterInfo::
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000951getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
Jim Grosbache2f55692010-08-19 23:52:25 +0000952 const TargetInstrDesc &Desc = MI->getDesc();
953 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
954 int64_t InstrOffs = 0;;
955 int Scale = 1;
956 unsigned ImmIdx = 0;
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000957 switch (AddrMode) {
Jim Grosbache2f55692010-08-19 23:52:25 +0000958 case ARMII::AddrModeT2_i8:
959 case ARMII::AddrModeT2_i12:
Jim Grosbach3e556122010-10-26 22:37:02 +0000960 case ARMII::AddrMode_i12:
Jim Grosbache2f55692010-08-19 23:52:25 +0000961 InstrOffs = MI->getOperand(Idx+1).getImm();
962 Scale = 1;
963 break;
964 case ARMII::AddrMode5: {
965 // VFP address mode.
966 const MachineOperand &OffOp = MI->getOperand(Idx+1);
Jim Grosbachf78ee632010-08-25 19:11:34 +0000967 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
Jim Grosbache2f55692010-08-19 23:52:25 +0000968 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
969 InstrOffs = -InstrOffs;
970 Scale = 4;
971 break;
972 }
973 case ARMII::AddrMode2: {
974 ImmIdx = Idx+2;
975 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
976 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
977 InstrOffs = -InstrOffs;
978 break;
979 }
980 case ARMII::AddrMode3: {
981 ImmIdx = Idx+2;
982 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
983 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
984 InstrOffs = -InstrOffs;
985 break;
986 }
987 case ARMII::AddrModeT1_s: {
988 ImmIdx = Idx+1;
989 InstrOffs = MI->getOperand(ImmIdx).getImm();
990 Scale = 4;
991 break;
992 }
993 default:
994 llvm_unreachable("Unsupported addressing mode!");
995 break;
996 }
997
998 return InstrOffs * Scale;
999}
1000
Jim Grosbach8708ead2010-08-17 18:13:53 +00001001/// needsFrameBaseReg - Returns true if the instruction's frame index
1002/// reference would be better served by a base register other than FP
1003/// or SP. Used by LocalStackFrameAllocation to determine which frame index
1004/// references it should create new base registers for.
1005bool ARMBaseRegisterInfo::
Jim Grosbach31973802010-08-24 21:19:33 +00001006needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1007 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
1008 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1009 }
Jim Grosbach8708ead2010-08-17 18:13:53 +00001010
1011 // It's the load/store FI references that cause issues, as it can be difficult
1012 // to materialize the offset if it won't fit in the literal field. Estimate
1013 // based on the size of the local frame and some conservative assumptions
1014 // about the rest of the stack frame (note, this is pre-regalloc, so
1015 // we don't know everything for certain yet) whether this offset is likely
1016 // to be out of range of the immediate. Return true if so.
1017
Jim Grosbachcd59dc52010-08-24 18:04:52 +00001018 // We only generate virtual base registers for loads and stores, so
1019 // return false for everything else.
Jim Grosbach8708ead2010-08-17 18:13:53 +00001020 unsigned Opc = MI->getOpcode();
Jim Grosbach8708ead2010-08-17 18:13:53 +00001021 switch (Opc) {
Jim Grosbachc1d30212010-10-27 00:19:44 +00001022 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
Jim Grosbach7e3383c2010-10-27 23:12:14 +00001023 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
Jim Grosbach8708ead2010-08-17 18:13:53 +00001024 case ARM::t2LDRi12: case ARM::t2LDRi8:
1025 case ARM::t2STRi12: case ARM::t2STRi8:
1026 case ARM::VLDRS: case ARM::VLDRD:
1027 case ARM::VSTRS: case ARM::VSTRD:
Jim Grosbach74d7b0a2010-08-19 17:52:13 +00001028 case ARM::tSTRspi: case ARM::tLDRspi:
Jim Grosbachcd59dc52010-08-24 18:04:52 +00001029 if (ForceAllBaseRegAlloc)
1030 return true;
1031 break;
Jim Grosbach8708ead2010-08-17 18:13:53 +00001032 default:
1033 return false;
1034 }
Jim Grosbachcd59dc52010-08-24 18:04:52 +00001035
Jim Grosbachcd59dc52010-08-24 18:04:52 +00001036 // Without a virtual base register, if the function has variable sized
1037 // objects, all fixed-size local references will be via the frame pointer,
Jim Grosbach31973802010-08-24 21:19:33 +00001038 // Approximate the offset and see if it's legal for the instruction.
1039 // Note that the incoming offset is based on the SP value at function entry,
1040 // so it'll be negative.
1041 MachineFunction &MF = *MI->getParent()->getParent();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001042 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Jim Grosbach31973802010-08-24 21:19:33 +00001043 MachineFrameInfo *MFI = MF.getFrameInfo();
1044 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbachcd59dc52010-08-24 18:04:52 +00001045
Jim Grosbach31973802010-08-24 21:19:33 +00001046 // Estimate an offset from the frame pointer.
1047 // Conservatively assume all callee-saved registers get pushed. R4-R6
1048 // will be earlier than the FP, so we ignore those.
1049 // R7, LR
1050 int64_t FPOffset = Offset - 8;
1051 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
1052 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
1053 FPOffset -= 80;
1054 // Estimate an offset from the stack pointer.
Jim Grosbachc1dc78d2010-08-31 18:52:31 +00001055 // The incoming offset is relating to the SP at the start of the function,
1056 // but when we access the local it'll be relative to the SP after local
1057 // allocation, so adjust our SP-relative offset by that allocation size.
Jim Grosbach31973802010-08-24 21:19:33 +00001058 Offset = -Offset;
Jim Grosbachc1dc78d2010-08-31 18:52:31 +00001059 Offset += MFI->getLocalFrameSize();
Jim Grosbach31973802010-08-24 21:19:33 +00001060 // Assume that we'll have at least some spill slots allocated.
1061 // FIXME: This is a total SWAG number. We should run some statistics
1062 // and pick a real one.
1063 Offset += 128; // 128 bytes of spill slots
1064
1065 // If there is a frame pointer, try using it.
1066 // The FP is only available if there is no dynamic realignment. We
1067 // don't know for sure yet whether we'll need that, so we guess based
1068 // on whether there are any local variables that would trigger it.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001069 unsigned StackAlign = TFI->getStackAlignment();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +00001070 if (TFI->hasFP(MF) &&
Jim Grosbach31973802010-08-24 21:19:33 +00001071 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
1072 if (isFrameOffsetLegal(MI, FPOffset))
1073 return false;
1074 }
1075 // If we can reference via the stack pointer, try that.
1076 // FIXME: This (and the code that resolves the references) can be improved
1077 // to only disallow SP relative references in the live range of
1078 // the VLA(s). In practice, it's unclear how much difference that
1079 // would make, but it may be worth doing.
1080 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
1081 return false;
1082
1083 // The offset likely isn't legal, we want to allocate a virtual base register.
Jim Grosbachcd59dc52010-08-24 18:04:52 +00001084 return true;
Jim Grosbach8708ead2010-08-17 18:13:53 +00001085}
1086
Bill Wendling976ef862010-12-17 23:09:14 +00001087/// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
1088/// be a pointer to FrameIdx at the beginning of the basic block.
Jim Grosbachdc140c62010-08-17 22:41:55 +00001089void ARMBaseRegisterInfo::
Bill Wendling976ef862010-12-17 23:09:14 +00001090materializeFrameBaseRegister(MachineBasicBlock *MBB,
1091 unsigned BaseReg, int FrameIdx,
1092 int64_t Offset) const {
1093 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
Jim Grosbach74d7b0a2010-08-19 17:52:13 +00001094 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
1095 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
Jim Grosbachdc140c62010-08-17 22:41:55 +00001096
Bill Wendling976ef862010-12-17 23:09:14 +00001097 MachineBasicBlock::iterator Ins = MBB->begin();
1098 DebugLoc DL; // Defaults to "unknown"
1099 if (Ins != MBB->end())
1100 DL = Ins->getDebugLoc();
1101
Cameron Zwarich21803722011-05-19 02:18:27 +00001102 const TargetInstrDesc &TID = TII.get(ADDriOpc);
1103 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1104 MRI.constrainRegClass(BaseReg, TID.OpInfo[0].getRegClass(this));
1105
Cameron Zwarich462b6dc2011-05-19 02:56:23 +00001106 MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, TID, BaseReg)
Jim Grosbache2f55692010-08-19 23:52:25 +00001107 .addFrameIndex(FrameIdx).addImm(Offset);
Bill Wendling976ef862010-12-17 23:09:14 +00001108
Jim Grosbach74d7b0a2010-08-19 17:52:13 +00001109 if (!AFI->isThumb1OnlyFunction())
1110 AddDefaultCC(AddDefaultPred(MIB));
Jim Grosbachdc140c62010-08-17 22:41:55 +00001111}
1112
1113void
1114ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
1115 unsigned BaseReg, int64_t Offset) const {
1116 MachineInstr &MI = *I;
1117 MachineBasicBlock &MBB = *MI.getParent();
1118 MachineFunction &MF = *MBB.getParent();
1119 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1120 int Off = Offset; // ARM doesn't need the general 64-bit offsets
1121 unsigned i = 0;
1122
1123 assert(!AFI->isThumb1OnlyFunction() &&
1124 "This resolveFrameIndex does not support Thumb1!");
1125
1126 while (!MI.getOperand(i).isFI()) {
1127 ++i;
1128 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1129 }
1130 bool Done = false;
1131 if (!AFI->isThumbFunction())
1132 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
1133 else {
1134 assert(AFI->isThumb2Function());
1135 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
1136 }
1137 assert (Done && "Unable to resolve frame index!");
1138}
Jim Grosbach8708ead2010-08-17 18:13:53 +00001139
Jim Grosbache2f55692010-08-19 23:52:25 +00001140bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1141 int64_t Offset) const {
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001142 const TargetInstrDesc &Desc = MI->getDesc();
1143 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1144 unsigned i = 0;
1145
1146 while (!MI->getOperand(i).isFI()) {
1147 ++i;
1148 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1149 }
1150
1151 // AddrMode4 and AddrMode6 cannot handle any offset.
1152 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
1153 return Offset == 0;
1154
1155 unsigned NumBits = 0;
1156 unsigned Scale = 1;
Jim Grosbache2f55692010-08-19 23:52:25 +00001157 bool isSigned = true;
Jim Grosbach1ab3f162010-08-26 21:56:30 +00001158 switch (AddrMode) {
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001159 case ARMII::AddrModeT2_i8:
1160 case ARMII::AddrModeT2_i12:
1161 // i8 supports only negative, and i12 supports only positive, so
1162 // based on Offset sign, consider the appropriate instruction
Jim Grosbach74d7b0a2010-08-19 17:52:13 +00001163 Scale = 1;
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001164 if (Offset < 0) {
1165 NumBits = 8;
1166 Offset = -Offset;
1167 } else {
1168 NumBits = 12;
1169 }
1170 break;
Jim Grosbach1ab3f162010-08-26 21:56:30 +00001171 case ARMII::AddrMode5:
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001172 // VFP address mode.
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001173 NumBits = 8;
1174 Scale = 4;
1175 break;
Jim Grosbach3e556122010-10-26 22:37:02 +00001176 case ARMII::AddrMode_i12:
Jim Grosbach1ab3f162010-08-26 21:56:30 +00001177 case ARMII::AddrMode2:
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001178 NumBits = 12;
1179 break;
Jim Grosbach1ab3f162010-08-26 21:56:30 +00001180 case ARMII::AddrMode3:
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001181 NumBits = 8;
1182 break;
Jim Grosbach1ab3f162010-08-26 21:56:30 +00001183 case ARMII::AddrModeT1_s:
Jim Grosbach74d7b0a2010-08-19 17:52:13 +00001184 NumBits = 5;
1185 Scale = 4;
Jim Grosbache2f55692010-08-19 23:52:25 +00001186 isSigned = false;
Jim Grosbach74d7b0a2010-08-19 17:52:13 +00001187 break;
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001188 default:
1189 llvm_unreachable("Unsupported addressing mode!");
1190 break;
1191 }
1192
Jim Grosbach1ab3f162010-08-26 21:56:30 +00001193 Offset += getFrameIndexInstrOffset(MI, i);
Jim Grosbachd4511e92010-08-31 18:49:31 +00001194 // Make sure the offset is encodable for instructions that scale the
1195 // immediate.
1196 if ((Offset & (Scale-1)) != 0)
1197 return false;
1198
Jim Grosbache2f55692010-08-19 23:52:25 +00001199 if (isSigned && Offset < 0)
Jim Grosbach2b1e2022010-08-18 22:44:49 +00001200 Offset = -Offset;
1201
1202 unsigned Mask = (1 << NumBits) - 1;
1203 if ((unsigned)Offset <= Mask * Scale)
1204 return true;
Jim Grosbach74d803a2010-08-18 17:57:37 +00001205
1206 return false;
1207}
1208
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +00001209void
Evan Cheng6495f632009-07-28 05:48:47 +00001210ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +00001211 int SPAdj, RegScavenger *RS) const {
David Goodwindb5a71a2009-07-08 18:31:39 +00001212 unsigned i = 0;
1213 MachineInstr &MI = *II;
1214 MachineBasicBlock &MBB = *MI.getParent();
1215 MachineFunction &MF = *MBB.getParent();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +00001216 const ARMFrameLowering *TFI =
1217 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
David Goodwindb5a71a2009-07-08 18:31:39 +00001218 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6495f632009-07-28 05:48:47 +00001219 assert(!AFI->isThumb1OnlyFunction() &&
Bob Wilsona15de002009-09-18 21:42:44 +00001220 "This eliminateFrameIndex does not support Thumb1!");
David Goodwindb5a71a2009-07-08 18:31:39 +00001221
1222 while (!MI.getOperand(i).isFI()) {
1223 ++i;
1224 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1225 }
1226
David Goodwindb5a71a2009-07-08 18:31:39 +00001227 int FrameIndex = MI.getOperand(i).getIndex();
Jim Grosbacha37aa542009-11-22 20:05:32 +00001228 unsigned FrameReg;
David Goodwindb5a71a2009-07-08 18:31:39 +00001229
Anton Korobeynikov82f58742010-11-20 15:59:32 +00001230 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
David Goodwindb5a71a2009-07-08 18:31:39 +00001231
Evan Cheng62b50652010-04-26 07:39:25 +00001232 // Special handling of dbg_value instructions.
1233 if (MI.isDebugValue()) {
1234 MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/);
1235 MI.getOperand(i+1).ChangeToImmediate(Offset);
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +00001236 return;
Evan Cheng62b50652010-04-26 07:39:25 +00001237 }
1238
Evan Cheng48d8afa2009-11-01 21:12:51 +00001239 // Modify MI as necessary to handle as much of 'Offset' as possible
Evan Chengcdbb3f52009-08-27 01:23:50 +00001240 bool Done = false;
Evan Cheng6495f632009-07-28 05:48:47 +00001241 if (!AFI->isThumbFunction())
Evan Chengcdbb3f52009-08-27 01:23:50 +00001242 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
Evan Cheng6495f632009-07-28 05:48:47 +00001243 else {
1244 assert(AFI->isThumb2Function());
Evan Chengcdbb3f52009-08-27 01:23:50 +00001245 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
Evan Cheng6495f632009-07-28 05:48:47 +00001246 }
Evan Chengcdbb3f52009-08-27 01:23:50 +00001247 if (Done)
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +00001248 return;
David Goodwindb5a71a2009-07-08 18:31:39 +00001249
1250 // If we get here, the immediate doesn't fit into the instruction. We folded
1251 // as much as possible above, handle the rest, providing a register that is
1252 // SP+LargeImm.
Daniel Dunbar19bb87d2009-08-28 08:08:22 +00001253 assert((Offset ||
Jim Grosbacha4432172009-11-15 21:45:34 +00001254 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1255 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
Evan Chengcdbb3f52009-08-27 01:23:50 +00001256 "This code isn't needed if offset already handled!");
David Goodwindb5a71a2009-07-08 18:31:39 +00001257
Jim Grosbach7e831db2009-10-20 01:26:58 +00001258 unsigned ScratchReg = 0;
David Goodwindb5a71a2009-07-08 18:31:39 +00001259 int PIdx = MI.findFirstPredOperandIdx();
1260 ARMCC::CondCodes Pred = (PIdx == -1)
1261 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1262 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
Evan Chengcdbb3f52009-08-27 01:23:50 +00001263 if (Offset == 0)
Jim Grosbacha4432172009-11-15 21:45:34 +00001264 // Must be addrmode4/6.
Evan Chengcdbb3f52009-08-27 01:23:50 +00001265 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
Evan Cheng6495f632009-07-28 05:48:47 +00001266 else {
Jim Grosbachca5dfb72009-10-28 17:33:28 +00001267 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
Evan Chengcdbb3f52009-08-27 01:23:50 +00001268 if (!AFI->isThumbFunction())
1269 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1270 Offset, Pred, PredReg, TII);
1271 else {
1272 assert(AFI->isThumb2Function());
1273 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1274 Offset, Pred, PredReg, TII);
1275 }
Jim Grosbachcde31292010-12-09 01:22:13 +00001276 // Update the original instruction to use the scratch register.
Evan Chengcdbb3f52009-08-27 01:23:50 +00001277 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
Jim Grosbachcde31292010-12-09 01:22:13 +00001278 if (MI.getOpcode() == ARM::t2ADDrSPi)
1279 MI.setDesc(TII.get(ARM::t2ADDri));
1280 else if (MI.getOpcode() == ARM::t2SUBrSPi)
1281 MI.setDesc(TII.get(ARM::t2SUBri));
Evan Cheng6495f632009-07-28 05:48:47 +00001282 }
David Goodwindb5a71a2009-07-08 18:31:39 +00001283}
1284
David Goodwinc140c482009-07-08 17:28:55 +00001285#include "ARMGenRegisterInfo.inc"