blob: 5a83bf7ee845e036b3c741c0d75e118ae41a5cfd [file] [log] [blame]
Jia Liu31d157a2012-02-18 12:03:15 +00001//===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
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
Craig Topperc1f6f422012-03-17 07:33:42 +000014#include "ARMBaseRegisterInfo.h"
David Goodwinc140c482009-07-08 17:28:55 +000015#include "ARM.h"
David Goodwindb5a71a2009-07-08 18:31:39 +000016#include "ARMBaseInstrInfo.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000017#include "ARMFrameLowering.h"
David Goodwinc140c482009-07-08 17:28:55 +000018#include "ARMMachineFunctionInfo.h"
19#include "ARMSubtarget.h"
Evan Chengee04a6d2011-07-20 23:34:39 +000020#include "MCTargetDesc/ARMAddressingModes.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000021#include "llvm/ADT/BitVector.h"
22#include "llvm/ADT/SmallVector.h"
David Goodwinc140c482009-07-08 17:28:55 +000023#include "llvm/CodeGen/MachineConstantPool.h"
24#include "llvm/CodeGen/MachineFrameInfo.h"
25#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
David Goodwinc140c482009-07-08 17:28:55 +000027#include "llvm/CodeGen/MachineRegisterInfo.h"
28#include "llvm/CodeGen/RegisterScavenging.h"
Jakob Stoklund Olesen303da1b2012-12-03 22:35:35 +000029#include "llvm/CodeGen/VirtRegMap.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/Constants.h"
31#include "llvm/DerivedTypes.h"
32#include "llvm/Function.h"
33#include "llvm/LLVMContext.h"
Jim Grosbach3dab2772009-10-27 22:45:39 +000034#include "llvm/Support/Debug.h"
Torok Edwinab7c09b2009-07-08 18:01:40 +000035#include "llvm/Support/ErrorHandling.h"
Torok Edwindac237e2009-07-08 20:53:28 +000036#include "llvm/Support/raw_ostream.h"
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000037#include "llvm/Target/TargetFrameLowering.h"
David Goodwinc140c482009-07-08 17:28:55 +000038#include "llvm/Target/TargetMachine.h"
39#include "llvm/Target/TargetOptions.h"
Evan Cheng73f50d92011-06-27 18:32:37 +000040
Evan Cheng73f50d92011-06-27 18:32:37 +000041#define GET_REGINFO_TARGET_DESC
Evan Chenga347f852011-06-24 01:44:41 +000042#include "ARMGenRegisterInfo.inc"
David Goodwinc140c482009-07-08 17:28:55 +000043
Evan Cheng1b4886d2010-11-18 01:28:51 +000044using namespace llvm;
45
David Goodwindb5a71a2009-07-08 18:31:39 +000046ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
David Goodwinc140c482009-07-08 17:28:55 +000047 const ARMSubtarget &sti)
Evan Cheng0e6a0522011-07-18 20:57:22 +000048 : ARMGenRegisterInfo(ARM::LR), TII(tii), STI(sti),
Jim Grosbach65482b12010-09-03 18:37:12 +000049 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
50 BasePtr(ARM::R6) {
David Goodwinc140c482009-07-08 17:28:55 +000051}
52
Craig Topper015f2282012-03-04 03:33:22 +000053const uint16_t*
David Goodwinc140c482009-07-08 17:28:55 +000054ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
Eric Christophere94ac882012-08-03 00:05:53 +000055 bool ghcCall = false;
56
57 if (MF) {
58 const Function *F = MF->getFunction();
59 ghcCall = (F ? F->getCallingConv() == CallingConv::GHC : false);
60 }
61
62 if (ghcCall) {
63 return CSR_GHC_SaveList;
64 }
65 else {
Evan Chengafb3b5e2012-04-27 02:11:10 +000066 return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
67 ? CSR_iOS_SaveList : CSR_AAPCS_SaveList;
Eric Christophere94ac882012-08-03 00:05:53 +000068 }
Jakob Stoklund Olesen3ee7d152012-01-17 23:09:00 +000069}
David Goodwinc140c482009-07-08 17:28:55 +000070
Jakob Stoklund Olesen3ee7d152012-01-17 23:09:00 +000071const uint32_t*
72ARMBaseRegisterInfo::getCallPreservedMask(CallingConv::ID) const {
Evan Chengafb3b5e2012-04-27 02:11:10 +000073 return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
74 ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
David Goodwinc140c482009-07-08 17:28:55 +000075}
76
Chad Rosiere7bd5192012-11-06 23:05:24 +000077const uint32_t*
78ARMBaseRegisterInfo::getNoPreservedMask() const {
79 return CSR_NoRegs_RegMask;
80}
81
Jim Grosbach96318642010-01-06 23:54:42 +000082BitVector ARMBaseRegisterInfo::
83getReservedRegs(const MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +000084 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000085
Chris Lattner7a2bdde2011-04-15 05:18:47 +000086 // FIXME: avoid re-calculating this every time.
David Goodwinc140c482009-07-08 17:28:55 +000087 BitVector Reserved(getNumRegs());
88 Reserved.set(ARM::SP);
89 Reserved.set(ARM::PC);
Lang Hames4f92b5e2012-03-06 00:19:55 +000090 Reserved.set(ARM::FPSCR);
Anton Korobeynikovd0c38172010-11-18 21:19:35 +000091 if (TFI->hasFP(MF))
David Goodwinc140c482009-07-08 17:28:55 +000092 Reserved.set(FramePtr);
Jim Grosbach65482b12010-09-03 18:37:12 +000093 if (hasBasePointer(MF))
94 Reserved.set(BasePtr);
David Goodwinc140c482009-07-08 17:28:55 +000095 // Some targets reserve R9.
96 if (STI.isR9Reserved())
97 Reserved.set(ARM::R9);
Jakob Stoklund Olesen3b6434e2011-06-18 00:53:27 +000098 // Reserve D16-D31 if the subtarget doesn't support them.
99 if (!STI.hasVFP3() || STI.hasD16()) {
100 assert(ARM::D31 == ARM::D16 + 15);
101 for (unsigned i = 0; i != 16; ++i)
102 Reserved.set(ARM::D16 + i);
103 }
Jakob Stoklund Olesencd275f52012-10-26 21:29:15 +0000104 const TargetRegisterClass *RC = &ARM::GPRPairRegClass;
105 for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I)
106 for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI)
107 if (Reserved.test(*SI)) Reserved.set(*I);
108
David Goodwinc140c482009-07-08 17:28:55 +0000109 return Reserved;
110}
111
Jakob Stoklund Olesenc9e50152011-04-26 18:52:33 +0000112const TargetRegisterClass*
113ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
114 const {
115 const TargetRegisterClass *Super = RC;
Jakob Stoklund Olesenc8e2bb62011-09-30 22:19:07 +0000116 TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
Jakob Stoklund Olesenc9e50152011-04-26 18:52:33 +0000117 do {
118 switch (Super->getID()) {
119 case ARM::GPRRegClassID:
120 case ARM::SPRRegClassID:
121 case ARM::DPRRegClassID:
122 case ARM::QPRRegClassID:
123 case ARM::QQPRRegClassID:
124 case ARM::QQQQPRRegClassID:
Jakob Stoklund Olesencd275f52012-10-26 21:29:15 +0000125 case ARM::GPRPairRegClassID:
Jakob Stoklund Olesenc9e50152011-04-26 18:52:33 +0000126 return Super;
127 }
128 Super = *I++;
129 } while (Super);
130 return RC;
131}
Evan Chengb990a2f2010-05-14 23:21:14 +0000132
Evan Cheng4f54c122009-10-25 07:53:28 +0000133const TargetRegisterClass *
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000134ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
135 const {
Craig Topper420761a2012-04-20 07:30:17 +0000136 return &ARM::GPRRegClass;
David Goodwinc140c482009-07-08 17:28:55 +0000137}
138
Evan Cheng342e3162011-08-30 01:34:54 +0000139const TargetRegisterClass *
140ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
141 if (RC == &ARM::CCRRegClass)
142 return 0; // Can't copy CCR registers.
143 return RC;
144}
145
Cameron Zwarichbe2119e2011-03-07 21:56:36 +0000146unsigned
147ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
148 MachineFunction &MF) const {
149 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
150
151 switch (RC->getID()) {
152 default:
153 return 0;
154 case ARM::tGPRRegClassID:
155 return TFI->hasFP(MF) ? 4 : 5;
156 case ARM::GPRRegClassID: {
157 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
158 return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
159 }
160 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
161 case ARM::DPRRegClassID:
162 return 32 - 10;
163 }
164}
165
Jakob Stoklund Olesen303da1b2012-12-03 22:35:35 +0000166// Get the other register in a GPRPair.
167static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) {
168 for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
169 if (ARM::GPRPairRegClass.contains(*Supers))
170 return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
171 return 0;
172}
173
174// Resolve the RegPairEven / RegPairOdd register allocator hints.
175void
176ARMBaseRegisterInfo::getRegAllocationHints(unsigned VirtReg,
177 ArrayRef<MCPhysReg> Order,
178 SmallVectorImpl<MCPhysReg> &Hints,
179 const MachineFunction &MF,
180 const VirtRegMap *VRM) const {
181 const MachineRegisterInfo &MRI = MF.getRegInfo();
182 std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
183
184 unsigned Odd;
185 switch (Hint.first) {
186 case ARMRI::RegPairEven:
187 Odd = 0;
188 break;
189 case ARMRI::RegPairOdd:
190 Odd = 1;
191 break;
192 default:
193 TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
194 return;
195 }
196
197 // This register should preferably be even (Odd == 0) or odd (Odd == 1).
198 // Check if the other part of the pair has already been assigned, and provide
199 // the paired register as the first hint.
200 unsigned PairedPhys = 0;
201 if (VRM && VRM->hasPhys(Hint.second)) {
202 PairedPhys = getPairedGPR(VRM->getPhys(Hint.second), Odd, this);
203 if (PairedPhys && MRI.isReserved(PairedPhys))
204 PairedPhys = 0;
205 }
206
207 // First prefer the paired physreg.
208 if (PairedPhys)
209 Hints.push_back(PairedPhys);
210
211 // Then prefer even or odd registers.
212 for (unsigned I = 0, E = Order.size(); I != E; ++I) {
213 unsigned Reg = Order[I];
214 if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
215 continue;
216 // Don't provide hints that are paired to a reserved register.
217 unsigned Paired = getPairedGPR(Reg, !Odd, this);
218 if (!Paired || MRI.isReserved(Paired))
219 continue;
220 Hints.push_back(Reg);
221 }
222}
223
David Goodwinc140c482009-07-08 17:28:55 +0000224void
225ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
226 MachineFunction &MF) const {
227 MachineRegisterInfo *MRI = &MF.getRegInfo();
228 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
229 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
230 Hint.first == (unsigned)ARMRI::RegPairEven) &&
Jakob Stoklund Olesenc9df0252011-01-10 02:58:51 +0000231 TargetRegisterInfo::isVirtualRegister(Hint.second)) {
David Goodwinc140c482009-07-08 17:28:55 +0000232 // If 'Reg' is one of the even / odd register pair and it's now changed
233 // (e.g. coalesced) into a different register. The other register of the
234 // pair allocation hint must be updated to reflect the relationship
235 // change.
236 unsigned OtherReg = Hint.second;
237 Hint = MRI->getRegAllocationHint(OtherReg);
238 if (Hint.second == Reg)
239 // Make sure the pair has not already divorced.
240 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
241 }
242}
243
Bob Wilsonf6a4d3c2011-04-19 18:11:45 +0000244bool
245ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
246 // CortexA9 has a Write-after-write hazard for NEON registers.
Silviu Baranga616471d2012-09-13 15:05:10 +0000247 if (!STI.isLikeA9())
Bob Wilsonf6a4d3c2011-04-19 18:11:45 +0000248 return false;
249
250 switch (RC->getID()) {
251 case ARM::DPRRegClassID:
252 case ARM::DPR_8RegClassID:
253 case ARM::DPR_VFP2RegClassID:
254 case ARM::QPRRegClassID:
255 case ARM::QPR_8RegClassID:
256 case ARM::QPR_VFP2RegClassID:
257 case ARM::SPRRegClassID:
258 case ARM::SPR_8RegClassID:
259 // Avoid reusing S, D, and Q registers.
260 // Don't increase register pressure for QQ and QQQQ.
261 return true;
262 default:
263 return false;
264 }
265}
266
Jim Grosbach65482b12010-09-03 18:37:12 +0000267bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
Jim Grosbache45ab8a2010-01-19 18:31:11 +0000268 const MachineFrameInfo *MFI = MF.getFrameInfo();
269 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000270 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Jim Grosbach65482b12010-09-03 18:37:12 +0000271
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000272 // When outgoing call frames are so large that we adjust the stack pointer
273 // around the call, we can no longer use the stack pointer to reach the
274 // emergency spill slot.
Bob Wilson055a8122012-03-20 19:28:22 +0000275 if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
Jim Grosbach65482b12010-09-03 18:37:12 +0000276 return true;
277
278 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
279 // negative range for ldr/str (255), and thumb1 is positive offsets only.
280 // It's going to be better to use the SP or Base Pointer instead. When there
281 // are variable sized objects, we can't reference off of the SP, so we
282 // reserve a Base Pointer.
283 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
284 // Conservatively estimate whether the negative offset from the frame
285 // pointer will be sufficient to reach. If a function has a smallish
286 // frame, it's less likely to have lots of spills and callee saved
287 // space, so it's all more likely to be within range of the frame pointer.
288 // If it's wrong, the scavenger will still enable access to work, it just
289 // won't be optimal.
290 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
291 return false;
292 return true;
293 }
294
295 return false;
296}
297
298bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
Jakob Stoklund Olesen54f3b7a2012-01-05 00:26:52 +0000299 const MachineRegisterInfo *MRI = &MF.getRegInfo();
Chad Rosier6690bca2011-10-20 00:07:12 +0000300 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbach30c93e12010-09-08 17:22:12 +0000301 // We can't realign the stack if:
302 // 1. Dynamic stack realignment is explicitly disabled,
Chad Rosier6690bca2011-10-20 00:07:12 +0000303 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
304 // 3. There are VLAs in the function and the base pointer is disabled.
Jakob Stoklund Olesen54f3b7a2012-01-05 00:26:52 +0000305 if (!MF.getTarget().Options.RealignStack)
306 return false;
307 if (AFI->isThumb1OnlyFunction())
308 return false;
309 // Stack realignment requires a frame pointer. If we already started
310 // register allocation with frame pointer elimination, it is too late now.
311 if (!MRI->canReserveReg(FramePtr))
312 return false;
Bob Wilsonaaa1e2f2012-03-20 19:28:25 +0000313 // We may also need a base pointer if there are dynamic allocas or stack
314 // pointer adjustments around calls.
315 if (MF.getTarget().getFrameLowering()->hasReservedCallFrame(MF))
Jakob Stoklund Olesen54f3b7a2012-01-05 00:26:52 +0000316 return true;
Jakob Stoklund Olesen54f3b7a2012-01-05 00:26:52 +0000317 // A base pointer is required and allowed. Check that it isn't too late to
318 // reserve it.
319 return MRI->canReserveReg(BasePtr);
Jim Grosbache45ab8a2010-01-19 18:31:11 +0000320}
321
Jim Grosbach3dab2772009-10-27 22:45:39 +0000322bool ARMBaseRegisterInfo::
323needsStackRealignment(const MachineFunction &MF) const {
Jim Grosbach3dab2772009-10-27 22:45:39 +0000324 const MachineFrameInfo *MFI = MF.getFrameInfo();
Eric Christopherd4c36ce2010-07-17 00:27:24 +0000325 const Function *F = MF.getFunction();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000326 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
Bill Wendling67658342012-10-09 07:45:08 +0000327 bool requiresRealignment =
328 ((MFI->getMaxAlignment() > StackAlign) ||
329 F->getFnAttributes().hasAttribute(Attributes::StackAlignment));
Jim Grosbach5c33f5b2010-09-02 19:52:39 +0000330
Eric Christopherd4c36ce2010-07-17 00:27:24 +0000331 return requiresRealignment && canRealignStack(MF);
Jim Grosbach3dab2772009-10-27 22:45:39 +0000332}
333
Jim Grosbach96318642010-01-06 23:54:42 +0000334bool ARMBaseRegisterInfo::
335cannotEliminateFrame(const MachineFunction &MF) const {
Evan Cheng98a01042009-08-14 20:48:13 +0000336 const MachineFrameInfo *MFI = MF.getFrameInfo();
Nick Lewycky8a8d4792011-12-02 22:16:29 +0000337 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack())
Evan Cheng98a01042009-08-14 20:48:13 +0000338 return true;
Jim Grosbach31bc8492009-11-08 00:27:19 +0000339 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
340 || needsStackRealignment(MF);
Evan Cheng98a01042009-08-14 20:48:13 +0000341}
342
Jim Grosbach5c33f5b2010-09-02 19:52:39 +0000343unsigned
David Greene3f2bf852009-11-12 20:49:22 +0000344ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000345 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000346
347 if (TFI->hasFP(MF))
David Goodwinc140c482009-07-08 17:28:55 +0000348 return FramePtr;
349 return ARM::SP;
350}
351
352unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000353 llvm_unreachable("What is the exception register");
David Goodwinc140c482009-07-08 17:28:55 +0000354}
355
356unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
Torok Edwinc23197a2009-07-14 16:55:14 +0000357 llvm_unreachable("What is the exception handler register");
David Goodwinc140c482009-07-08 17:28:55 +0000358}
359
David Goodwindb5a71a2009-07-08 18:31:39 +0000360/// emitLoadConstPool - Emits a load from constpool to materialize the
361/// specified immediate.
362void ARMBaseRegisterInfo::
363emitLoadConstPool(MachineBasicBlock &MBB,
364 MachineBasicBlock::iterator &MBBI,
David Goodwin77521f52009-07-08 20:28:28 +0000365 DebugLoc dl,
Evan Cheng37844532009-07-16 09:20:10 +0000366 unsigned DestReg, unsigned SubIdx, int Val,
David Goodwindb5a71a2009-07-08 18:31:39 +0000367 ARMCC::CondCodes Pred,
Anton Korobeynikov3daccd82011-03-05 18:43:50 +0000368 unsigned PredReg, unsigned MIFlags) const {
David Goodwindb5a71a2009-07-08 18:31:39 +0000369 MachineFunction &MF = *MBB.getParent();
370 MachineConstantPool *ConstantPool = MF.getConstantPool();
Dan Gohman46510a72010-04-15 01:51:59 +0000371 const Constant *C =
Owen Anderson1d0be152009-08-13 21:58:54 +0000372 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
David Goodwindb5a71a2009-07-08 18:31:39 +0000373 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
374
Evan Cheng37844532009-07-16 09:20:10 +0000375 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
376 .addReg(DestReg, getDefRegState(true), SubIdx)
David Goodwindb5a71a2009-07-08 18:31:39 +0000377 .addConstantPoolIndex(Idx)
Anton Korobeynikov3daccd82011-03-05 18:43:50 +0000378 .addImm(0).addImm(Pred).addReg(PredReg)
379 .setMIFlags(MIFlags);
David Goodwindb5a71a2009-07-08 18:31:39 +0000380}
381
382bool ARMBaseRegisterInfo::
383requiresRegisterScavenging(const MachineFunction &MF) const {
384 return true;
385}
Jim Grosbach41fff8c2009-10-21 23:40:56 +0000386
Jim Grosbach7e831db2009-10-20 01:26:58 +0000387bool ARMBaseRegisterInfo::
Preston Gurd6a8c7bf2012-04-23 21:39:35 +0000388trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
389 return true;
390}
391
392bool ARMBaseRegisterInfo::
Jim Grosbach7e831db2009-10-20 01:26:58 +0000393requiresFrameIndexScavenging(const MachineFunction &MF) const {
Jim Grosbachca5dfb72009-10-28 17:33:28 +0000394 return true;
Jim Grosbach7e831db2009-10-20 01:26:58 +0000395}
David Goodwindb5a71a2009-07-08 18:31:39 +0000396
Jim Grosbacha2734422010-08-24 19:05:43 +0000397bool ARMBaseRegisterInfo::
398requiresVirtualBaseRegisters(const MachineFunction &MF) const {
Jim Grosbachc8cd8aa2012-12-11 23:31:12 +0000399 return true;
Jim Grosbacha2734422010-08-24 19:05:43 +0000400}
401
David Goodwindb5a71a2009-07-08 18:31:39 +0000402static void
Evan Cheng6495f632009-07-28 05:48:47 +0000403emitSPUpdate(bool isARM,
404 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
405 DebugLoc dl, const ARMBaseInstrInfo &TII,
David Goodwindb5a71a2009-07-08 18:31:39 +0000406 int NumBytes,
407 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
Evan Cheng6495f632009-07-28 05:48:47 +0000408 if (isARM)
409 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
410 Pred, PredReg, TII);
411 else
412 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
413 Pred, PredReg, TII);
David Goodwindb5a71a2009-07-08 18:31:39 +0000414}
415
Evan Cheng6495f632009-07-28 05:48:47 +0000416
David Goodwindb5a71a2009-07-08 18:31:39 +0000417void ARMBaseRegisterInfo::
418eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
419 MachineBasicBlock::iterator I) const {
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000420 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000421 if (!TFI->hasReservedCallFrame(MF)) {
David Goodwindb5a71a2009-07-08 18:31:39 +0000422 // If we have alloca, convert as follows:
423 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
424 // ADJCALLSTACKUP -> add, sp, sp, amount
425 MachineInstr *Old = I;
426 DebugLoc dl = Old->getDebugLoc();
427 unsigned Amount = Old->getOperand(0).getImm();
428 if (Amount != 0) {
429 // We need to keep the stack aligned properly. To do this, we round the
430 // amount of space needed for the outgoing arguments up to the next
431 // alignment boundary.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000432 unsigned Align = TFI->getStackAlignment();
David Goodwindb5a71a2009-07-08 18:31:39 +0000433 Amount = (Amount+Align-1)/Align*Align;
434
Evan Cheng6495f632009-07-28 05:48:47 +0000435 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
436 assert(!AFI->isThumb1OnlyFunction() &&
Jim Grosbachcf453ee2010-02-23 17:16:27 +0000437 "This eliminateCallFramePseudoInstr does not support Thumb1!");
Evan Cheng6495f632009-07-28 05:48:47 +0000438 bool isARM = !AFI->isThumbFunction();
439
David Goodwindb5a71a2009-07-08 18:31:39 +0000440 // Replace the pseudo instruction with a new instruction...
441 unsigned Opc = Old->getOpcode();
Jim Grosbach4c7628e2010-02-22 22:47:46 +0000442 int PIdx = Old->findFirstPredOperandIdx();
443 ARMCC::CondCodes Pred = (PIdx == -1)
444 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
David Goodwindb5a71a2009-07-08 18:31:39 +0000445 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
446 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
447 unsigned PredReg = Old->getOperand(2).getReg();
Evan Cheng6495f632009-07-28 05:48:47 +0000448 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
David Goodwindb5a71a2009-07-08 18:31:39 +0000449 } else {
450 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
451 unsigned PredReg = Old->getOperand(3).getReg();
452 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
Evan Cheng6495f632009-07-28 05:48:47 +0000453 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
David Goodwindb5a71a2009-07-08 18:31:39 +0000454 }
455 }
456 }
457 MBB.erase(I);
458}
459
Jim Grosbache2f55692010-08-19 23:52:25 +0000460int64_t ARMBaseRegisterInfo::
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000461getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000462 const MCInstrDesc &Desc = MI->getDesc();
Jim Grosbache2f55692010-08-19 23:52:25 +0000463 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
Chad Rosier90f20042012-02-22 17:25:00 +0000464 int64_t InstrOffs = 0;
Jim Grosbache2f55692010-08-19 23:52:25 +0000465 int Scale = 1;
466 unsigned ImmIdx = 0;
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000467 switch (AddrMode) {
Jim Grosbache2f55692010-08-19 23:52:25 +0000468 case ARMII::AddrModeT2_i8:
469 case ARMII::AddrModeT2_i12:
Jim Grosbach3e556122010-10-26 22:37:02 +0000470 case ARMII::AddrMode_i12:
Jim Grosbache2f55692010-08-19 23:52:25 +0000471 InstrOffs = MI->getOperand(Idx+1).getImm();
472 Scale = 1;
473 break;
474 case ARMII::AddrMode5: {
475 // VFP address mode.
476 const MachineOperand &OffOp = MI->getOperand(Idx+1);
Jim Grosbachf78ee632010-08-25 19:11:34 +0000477 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
Jim Grosbache2f55692010-08-19 23:52:25 +0000478 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
479 InstrOffs = -InstrOffs;
480 Scale = 4;
481 break;
482 }
483 case ARMII::AddrMode2: {
484 ImmIdx = Idx+2;
485 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
486 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
487 InstrOffs = -InstrOffs;
488 break;
489 }
490 case ARMII::AddrMode3: {
491 ImmIdx = Idx+2;
492 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
493 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
494 InstrOffs = -InstrOffs;
495 break;
496 }
497 case ARMII::AddrModeT1_s: {
498 ImmIdx = Idx+1;
499 InstrOffs = MI->getOperand(ImmIdx).getImm();
500 Scale = 4;
501 break;
502 }
503 default:
504 llvm_unreachable("Unsupported addressing mode!");
Jim Grosbache2f55692010-08-19 23:52:25 +0000505 }
506
507 return InstrOffs * Scale;
508}
509
Jim Grosbach8708ead2010-08-17 18:13:53 +0000510/// needsFrameBaseReg - Returns true if the instruction's frame index
511/// reference would be better served by a base register other than FP
512/// or SP. Used by LocalStackFrameAllocation to determine which frame index
513/// references it should create new base registers for.
514bool ARMBaseRegisterInfo::
Jim Grosbach31973802010-08-24 21:19:33 +0000515needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
516 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
517 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
518 }
Jim Grosbach8708ead2010-08-17 18:13:53 +0000519
520 // It's the load/store FI references that cause issues, as it can be difficult
521 // to materialize the offset if it won't fit in the literal field. Estimate
522 // based on the size of the local frame and some conservative assumptions
523 // about the rest of the stack frame (note, this is pre-regalloc, so
524 // we don't know everything for certain yet) whether this offset is likely
525 // to be out of range of the immediate. Return true if so.
526
Jim Grosbachcd59dc52010-08-24 18:04:52 +0000527 // We only generate virtual base registers for loads and stores, so
528 // return false for everything else.
Jim Grosbach8708ead2010-08-17 18:13:53 +0000529 unsigned Opc = MI->getOpcode();
Jim Grosbach8708ead2010-08-17 18:13:53 +0000530 switch (Opc) {
Jakob Stoklund Olesencff9baa2012-08-28 03:11:27 +0000531 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
Jim Grosbach7e3383c2010-10-27 23:12:14 +0000532 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
Jakob Stoklund Olesencff9baa2012-08-28 03:11:27 +0000533 case ARM::t2LDRi12: case ARM::t2LDRi8:
534 case ARM::t2STRi12: case ARM::t2STRi8:
Jim Grosbach8708ead2010-08-17 18:13:53 +0000535 case ARM::VLDRS: case ARM::VLDRD:
536 case ARM::VSTRS: case ARM::VSTRD:
Jim Grosbach74d7b0a2010-08-19 17:52:13 +0000537 case ARM::tSTRspi: case ARM::tLDRspi:
Jim Grosbachcd59dc52010-08-24 18:04:52 +0000538 break;
Jim Grosbach8708ead2010-08-17 18:13:53 +0000539 default:
540 return false;
541 }
Jim Grosbachcd59dc52010-08-24 18:04:52 +0000542
Jim Grosbachcd59dc52010-08-24 18:04:52 +0000543 // Without a virtual base register, if the function has variable sized
544 // objects, all fixed-size local references will be via the frame pointer,
Jim Grosbach31973802010-08-24 21:19:33 +0000545 // Approximate the offset and see if it's legal for the instruction.
546 // Note that the incoming offset is based on the SP value at function entry,
547 // so it'll be negative.
548 MachineFunction &MF = *MI->getParent()->getParent();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000549 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
Jim Grosbach31973802010-08-24 21:19:33 +0000550 MachineFrameInfo *MFI = MF.getFrameInfo();
551 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Jim Grosbachcd59dc52010-08-24 18:04:52 +0000552
Jim Grosbach31973802010-08-24 21:19:33 +0000553 // Estimate an offset from the frame pointer.
554 // Conservatively assume all callee-saved registers get pushed. R4-R6
555 // will be earlier than the FP, so we ignore those.
556 // R7, LR
557 int64_t FPOffset = Offset - 8;
558 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
559 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
560 FPOffset -= 80;
561 // Estimate an offset from the stack pointer.
Jim Grosbachc1dc78d2010-08-31 18:52:31 +0000562 // The incoming offset is relating to the SP at the start of the function,
563 // but when we access the local it'll be relative to the SP after local
564 // allocation, so adjust our SP-relative offset by that allocation size.
Jim Grosbach31973802010-08-24 21:19:33 +0000565 Offset = -Offset;
Jim Grosbachc1dc78d2010-08-31 18:52:31 +0000566 Offset += MFI->getLocalFrameSize();
Jim Grosbach31973802010-08-24 21:19:33 +0000567 // Assume that we'll have at least some spill slots allocated.
568 // FIXME: This is a total SWAG number. We should run some statistics
569 // and pick a real one.
570 Offset += 128; // 128 bytes of spill slots
571
572 // If there is a frame pointer, try using it.
573 // The FP is only available if there is no dynamic realignment. We
574 // don't know for sure yet whether we'll need that, so we guess based
575 // on whether there are any local variables that would trigger it.
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000576 unsigned StackAlign = TFI->getStackAlignment();
Anton Korobeynikovd0c38172010-11-18 21:19:35 +0000577 if (TFI->hasFP(MF) &&
Jim Grosbach31973802010-08-24 21:19:33 +0000578 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
579 if (isFrameOffsetLegal(MI, FPOffset))
580 return false;
581 }
582 // If we can reference via the stack pointer, try that.
583 // FIXME: This (and the code that resolves the references) can be improved
584 // to only disallow SP relative references in the live range of
585 // the VLA(s). In practice, it's unclear how much difference that
586 // would make, but it may be worth doing.
587 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
588 return false;
589
590 // The offset likely isn't legal, we want to allocate a virtual base register.
Jim Grosbachcd59dc52010-08-24 18:04:52 +0000591 return true;
Jim Grosbach8708ead2010-08-17 18:13:53 +0000592}
593
Bill Wendling976ef862010-12-17 23:09:14 +0000594/// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
595/// be a pointer to FrameIdx at the beginning of the basic block.
Jim Grosbachdc140c62010-08-17 22:41:55 +0000596void ARMBaseRegisterInfo::
Bill Wendling976ef862010-12-17 23:09:14 +0000597materializeFrameBaseRegister(MachineBasicBlock *MBB,
598 unsigned BaseReg, int FrameIdx,
599 int64_t Offset) const {
600 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
Jim Grosbach74d7b0a2010-08-19 17:52:13 +0000601 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
602 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
Jim Grosbachdc140c62010-08-17 22:41:55 +0000603
Bill Wendling976ef862010-12-17 23:09:14 +0000604 MachineBasicBlock::iterator Ins = MBB->begin();
605 DebugLoc DL; // Defaults to "unknown"
606 if (Ins != MBB->end())
607 DL = Ins->getDebugLoc();
608
Evan Chenge837dea2011-06-28 19:10:37 +0000609 const MCInstrDesc &MCID = TII.get(ADDriOpc);
Cameron Zwarich21803722011-05-19 02:18:27 +0000610 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
Jakob Stoklund Olesen397fc482012-05-07 22:10:26 +0000611 const MachineFunction &MF = *MBB->getParent();
612 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
Cameron Zwarich21803722011-05-19 02:18:27 +0000613
Jim Grosbach5b815842011-08-24 17:46:13 +0000614 MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg)
615 .addFrameIndex(FrameIdx).addImm(Offset));
Bill Wendling976ef862010-12-17 23:09:14 +0000616
Jim Grosbach74d7b0a2010-08-19 17:52:13 +0000617 if (!AFI->isThumb1OnlyFunction())
Jim Grosbach5b815842011-08-24 17:46:13 +0000618 AddDefaultCC(MIB);
Jim Grosbachdc140c62010-08-17 22:41:55 +0000619}
620
621void
622ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
623 unsigned BaseReg, int64_t Offset) const {
624 MachineInstr &MI = *I;
625 MachineBasicBlock &MBB = *MI.getParent();
626 MachineFunction &MF = *MBB.getParent();
627 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
628 int Off = Offset; // ARM doesn't need the general 64-bit offsets
629 unsigned i = 0;
630
631 assert(!AFI->isThumb1OnlyFunction() &&
632 "This resolveFrameIndex does not support Thumb1!");
633
634 while (!MI.getOperand(i).isFI()) {
635 ++i;
636 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
637 }
638 bool Done = false;
639 if (!AFI->isThumbFunction())
640 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
641 else {
642 assert(AFI->isThumb2Function());
643 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
644 }
645 assert (Done && "Unable to resolve frame index!");
Duncan Sands1f6a3292011-08-12 14:54:45 +0000646 (void)Done;
Jim Grosbachdc140c62010-08-17 22:41:55 +0000647}
Jim Grosbach8708ead2010-08-17 18:13:53 +0000648
Jim Grosbache2f55692010-08-19 23:52:25 +0000649bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
650 int64_t Offset) const {
Evan Chenge837dea2011-06-28 19:10:37 +0000651 const MCInstrDesc &Desc = MI->getDesc();
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000652 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
653 unsigned i = 0;
654
655 while (!MI->getOperand(i).isFI()) {
656 ++i;
657 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
658 }
659
660 // AddrMode4 and AddrMode6 cannot handle any offset.
661 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
662 return Offset == 0;
663
664 unsigned NumBits = 0;
665 unsigned Scale = 1;
Jim Grosbache2f55692010-08-19 23:52:25 +0000666 bool isSigned = true;
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000667 switch (AddrMode) {
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000668 case ARMII::AddrModeT2_i8:
669 case ARMII::AddrModeT2_i12:
670 // i8 supports only negative, and i12 supports only positive, so
671 // based on Offset sign, consider the appropriate instruction
Jim Grosbach74d7b0a2010-08-19 17:52:13 +0000672 Scale = 1;
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000673 if (Offset < 0) {
674 NumBits = 8;
675 Offset = -Offset;
676 } else {
677 NumBits = 12;
678 }
679 break;
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000680 case ARMII::AddrMode5:
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000681 // VFP address mode.
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000682 NumBits = 8;
683 Scale = 4;
684 break;
Jim Grosbach3e556122010-10-26 22:37:02 +0000685 case ARMII::AddrMode_i12:
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000686 case ARMII::AddrMode2:
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000687 NumBits = 12;
688 break;
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000689 case ARMII::AddrMode3:
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000690 NumBits = 8;
691 break;
Bill Wendlinge5754992011-10-11 21:40:47 +0000692 case ARMII::AddrModeT1_s:
693 NumBits = 5;
Jim Grosbach74d7b0a2010-08-19 17:52:13 +0000694 Scale = 4;
Jim Grosbache2f55692010-08-19 23:52:25 +0000695 isSigned = false;
Jim Grosbach74d7b0a2010-08-19 17:52:13 +0000696 break;
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000697 default:
698 llvm_unreachable("Unsupported addressing mode!");
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000699 }
700
Jim Grosbach1ab3f162010-08-26 21:56:30 +0000701 Offset += getFrameIndexInstrOffset(MI, i);
Jim Grosbachd4511e92010-08-31 18:49:31 +0000702 // Make sure the offset is encodable for instructions that scale the
703 // immediate.
704 if ((Offset & (Scale-1)) != 0)
705 return false;
706
Jim Grosbache2f55692010-08-19 23:52:25 +0000707 if (isSigned && Offset < 0)
Jim Grosbach2b1e2022010-08-18 22:44:49 +0000708 Offset = -Offset;
709
710 unsigned Mask = (1 << NumBits) - 1;
711 if ((unsigned)Offset <= Mask * Scale)
712 return true;
Jim Grosbach74d803a2010-08-18 17:57:37 +0000713
714 return false;
715}
716
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +0000717void
Evan Cheng6495f632009-07-28 05:48:47 +0000718ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +0000719 int SPAdj, RegScavenger *RS) const {
David Goodwindb5a71a2009-07-08 18:31:39 +0000720 unsigned i = 0;
721 MachineInstr &MI = *II;
722 MachineBasicBlock &MBB = *MI.getParent();
723 MachineFunction &MF = *MBB.getParent();
Anton Korobeynikov16c29b52011-01-10 12:39:04 +0000724 const ARMFrameLowering *TFI =
725 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
David Goodwindb5a71a2009-07-08 18:31:39 +0000726 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
Evan Cheng6495f632009-07-28 05:48:47 +0000727 assert(!AFI->isThumb1OnlyFunction() &&
Bob Wilsona15de002009-09-18 21:42:44 +0000728 "This eliminateFrameIndex does not support Thumb1!");
David Goodwindb5a71a2009-07-08 18:31:39 +0000729
730 while (!MI.getOperand(i).isFI()) {
731 ++i;
732 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
733 }
734
David Goodwindb5a71a2009-07-08 18:31:39 +0000735 int FrameIndex = MI.getOperand(i).getIndex();
Jim Grosbacha37aa542009-11-22 20:05:32 +0000736 unsigned FrameReg;
David Goodwindb5a71a2009-07-08 18:31:39 +0000737
Anton Korobeynikov82f58742010-11-20 15:59:32 +0000738 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
David Goodwindb5a71a2009-07-08 18:31:39 +0000739
Jakob Stoklund Olesen0f9d07f2012-02-28 01:15:01 +0000740 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
741 // call frame setup/destroy instructions have already been eliminated. That
742 // means the stack pointer cannot be used to access the emergency spill slot
743 // when !hasReservedCallFrame().
744#ifndef NDEBUG
745 if (RS && FrameReg == ARM::SP && FrameIndex == RS->getScavengingFrameIndex()){
746 assert(TFI->hasReservedCallFrame(MF) &&
747 "Cannot use SP to access the emergency spill slot in "
748 "functions without a reserved call frame");
749 assert(!MF.getFrameInfo()->hasVarSizedObjects() &&
750 "Cannot use SP to access the emergency spill slot in "
751 "functions with variable sized frame objects");
752 }
753#endif // NDEBUG
754
Evan Cheng62b50652010-04-26 07:39:25 +0000755 // Special handling of dbg_value instructions.
756 if (MI.isDebugValue()) {
757 MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/);
758 MI.getOperand(i+1).ChangeToImmediate(Offset);
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +0000759 return;
Evan Cheng62b50652010-04-26 07:39:25 +0000760 }
761
Evan Cheng48d8afa2009-11-01 21:12:51 +0000762 // Modify MI as necessary to handle as much of 'Offset' as possible
Evan Chengcdbb3f52009-08-27 01:23:50 +0000763 bool Done = false;
Evan Cheng6495f632009-07-28 05:48:47 +0000764 if (!AFI->isThumbFunction())
Evan Chengcdbb3f52009-08-27 01:23:50 +0000765 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
Evan Cheng6495f632009-07-28 05:48:47 +0000766 else {
767 assert(AFI->isThumb2Function());
Evan Chengcdbb3f52009-08-27 01:23:50 +0000768 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
Evan Cheng6495f632009-07-28 05:48:47 +0000769 }
Evan Chengcdbb3f52009-08-27 01:23:50 +0000770 if (Done)
Jim Grosbachfcb4a8e2010-08-26 23:32:16 +0000771 return;
David Goodwindb5a71a2009-07-08 18:31:39 +0000772
773 // If we get here, the immediate doesn't fit into the instruction. We folded
774 // as much as possible above, handle the rest, providing a register that is
775 // SP+LargeImm.
Daniel Dunbar19bb87d2009-08-28 08:08:22 +0000776 assert((Offset ||
Jim Grosbacha4432172009-11-15 21:45:34 +0000777 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
778 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
Evan Chengcdbb3f52009-08-27 01:23:50 +0000779 "This code isn't needed if offset already handled!");
David Goodwindb5a71a2009-07-08 18:31:39 +0000780
Jim Grosbach7e831db2009-10-20 01:26:58 +0000781 unsigned ScratchReg = 0;
David Goodwindb5a71a2009-07-08 18:31:39 +0000782 int PIdx = MI.findFirstPredOperandIdx();
783 ARMCC::CondCodes Pred = (PIdx == -1)
784 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
785 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
Evan Chengcdbb3f52009-08-27 01:23:50 +0000786 if (Offset == 0)
Jim Grosbacha4432172009-11-15 21:45:34 +0000787 // Must be addrmode4/6.
Evan Chengcdbb3f52009-08-27 01:23:50 +0000788 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
Evan Cheng6495f632009-07-28 05:48:47 +0000789 else {
Craig Topper420761a2012-04-20 07:30:17 +0000790 ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass);
Evan Chengcdbb3f52009-08-27 01:23:50 +0000791 if (!AFI->isThumbFunction())
792 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
793 Offset, Pred, PredReg, TII);
794 else {
795 assert(AFI->isThumb2Function());
796 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
797 Offset, Pred, PredReg, TII);
798 }
Jim Grosbachcde31292010-12-09 01:22:13 +0000799 // Update the original instruction to use the scratch register.
Evan Chengcdbb3f52009-08-27 01:23:50 +0000800 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
Evan Cheng6495f632009-07-28 05:48:47 +0000801 }
David Goodwindb5a71a2009-07-08 18:31:39 +0000802}