1 //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the base ARM implementation of TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "ARMBaseRegisterInfo.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMFrameLowering.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMSubtarget.h"
20 #include "MCTargetDesc/ARMAddressingModes.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/SmallVector.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineFunction.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/CodeGen/VirtRegMap.h"
30 #include "llvm/IR/Constants.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/LLVMContext.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include "llvm/Target/TargetFrameLowering.h"
38 #include "llvm/Target/TargetMachine.h"
39 #include "llvm/Target/TargetOptions.h"
41 #define GET_REGINFO_TARGET_DESC
42 #include "ARMGenRegisterInfo.inc"
46 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
47 const ARMSubtarget &sti)
48 : ARMGenRegisterInfo(ARM::LR, 0, 0, ARM::PC), TII(tii), STI(sti),
49 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
54 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
58 const Function *F = MF->getFunction();
59 ghcCall = (F ? F->getCallingConv() == CallingConv::GHC : false);
63 return CSR_GHC_SaveList;
66 return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
67 ? CSR_iOS_SaveList : CSR_AAPCS_SaveList;
72 ARMBaseRegisterInfo::getCallPreservedMask(CallingConv::ID) const {
73 return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
74 ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
78 ARMBaseRegisterInfo::getThisReturnPreservedMask(CallingConv::ID) const {
79 return (STI.isTargetIOS() && !STI.isAAPCS_ABI())
80 ? CSR_iOS_ThisReturn_RegMask : CSR_AAPCS_ThisReturn_RegMask;
84 ARMBaseRegisterInfo::getNoPreservedMask() const {
85 return CSR_NoRegs_RegMask;
88 BitVector ARMBaseRegisterInfo::
89 getReservedRegs(const MachineFunction &MF) const {
90 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
92 // FIXME: avoid re-calculating this every time.
93 BitVector Reserved(getNumRegs());
94 Reserved.set(ARM::SP);
95 Reserved.set(ARM::PC);
96 Reserved.set(ARM::FPSCR);
98 Reserved.set(FramePtr);
99 if (hasBasePointer(MF))
100 Reserved.set(BasePtr);
101 // Some targets reserve R9.
102 if (STI.isR9Reserved())
103 Reserved.set(ARM::R9);
104 // Reserve D16-D31 if the subtarget doesn't support them.
105 if (!STI.hasVFP3() || STI.hasD16()) {
106 assert(ARM::D31 == ARM::D16 + 15);
107 for (unsigned i = 0; i != 16; ++i)
108 Reserved.set(ARM::D16 + i);
110 const TargetRegisterClass *RC = &ARM::GPRPairRegClass;
111 for(TargetRegisterClass::iterator I = RC->begin(), E = RC->end(); I!=E; ++I)
112 for (MCSubRegIterator SI(*I, this); SI.isValid(); ++SI)
113 if (Reserved.test(*SI)) Reserved.set(*I);
118 const TargetRegisterClass*
119 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
121 const TargetRegisterClass *Super = RC;
122 TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
124 switch (Super->getID()) {
125 case ARM::GPRRegClassID:
126 case ARM::SPRRegClassID:
127 case ARM::DPRRegClassID:
128 case ARM::QPRRegClassID:
129 case ARM::QQPRRegClassID:
130 case ARM::QQQQPRRegClassID:
131 case ARM::GPRPairRegClassID:
139 const TargetRegisterClass *
140 ARMBaseRegisterInfo::getPointerRegClass(const MachineFunction &MF, unsigned Kind)
142 return &ARM::GPRRegClass;
145 const TargetRegisterClass *
146 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
147 if (RC == &ARM::CCRRegClass)
148 return 0; // Can't copy CCR registers.
153 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
154 MachineFunction &MF) const {
155 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
157 switch (RC->getID()) {
160 case ARM::tGPRRegClassID:
161 return TFI->hasFP(MF) ? 4 : 5;
162 case ARM::GPRRegClassID: {
163 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
164 return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
166 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
167 case ARM::DPRRegClassID:
172 // Get the other register in a GPRPair.
173 static unsigned getPairedGPR(unsigned Reg, bool Odd, const MCRegisterInfo *RI) {
174 for (MCSuperRegIterator Supers(Reg, RI); Supers.isValid(); ++Supers)
175 if (ARM::GPRPairRegClass.contains(*Supers))
176 return RI->getSubReg(*Supers, Odd ? ARM::gsub_1 : ARM::gsub_0);
180 // Resolve the RegPairEven / RegPairOdd register allocator hints.
182 ARMBaseRegisterInfo::getRegAllocationHints(unsigned VirtReg,
183 ArrayRef<MCPhysReg> Order,
184 SmallVectorImpl<MCPhysReg> &Hints,
185 const MachineFunction &MF,
186 const VirtRegMap *VRM) const {
187 const MachineRegisterInfo &MRI = MF.getRegInfo();
188 std::pair<unsigned, unsigned> Hint = MRI.getRegAllocationHint(VirtReg);
191 switch (Hint.first) {
192 case ARMRI::RegPairEven:
195 case ARMRI::RegPairOdd:
199 TargetRegisterInfo::getRegAllocationHints(VirtReg, Order, Hints, MF, VRM);
203 // This register should preferably be even (Odd == 0) or odd (Odd == 1).
204 // Check if the other part of the pair has already been assigned, and provide
205 // the paired register as the first hint.
206 unsigned PairedPhys = 0;
207 if (VRM && VRM->hasPhys(Hint.second)) {
208 PairedPhys = getPairedGPR(VRM->getPhys(Hint.second), Odd, this);
209 if (PairedPhys && MRI.isReserved(PairedPhys))
213 // First prefer the paired physreg.
215 std::find(Order.begin(), Order.end(), PairedPhys) != Order.end())
216 Hints.push_back(PairedPhys);
218 // Then prefer even or odd registers.
219 for (unsigned I = 0, E = Order.size(); I != E; ++I) {
220 unsigned Reg = Order[I];
221 if (Reg == PairedPhys || (getEncodingValue(Reg) & 1) != Odd)
223 // Don't provide hints that are paired to a reserved register.
224 unsigned Paired = getPairedGPR(Reg, !Odd, this);
225 if (!Paired || MRI.isReserved(Paired))
227 Hints.push_back(Reg);
232 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
233 MachineFunction &MF) const {
234 MachineRegisterInfo *MRI = &MF.getRegInfo();
235 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
236 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
237 Hint.first == (unsigned)ARMRI::RegPairEven) &&
238 TargetRegisterInfo::isVirtualRegister(Hint.second)) {
239 // If 'Reg' is one of the even / odd register pair and it's now changed
240 // (e.g. coalesced) into a different register. The other register of the
241 // pair allocation hint must be updated to reflect the relationship
243 unsigned OtherReg = Hint.second;
244 Hint = MRI->getRegAllocationHint(OtherReg);
245 if (Hint.second == Reg)
246 // Make sure the pair has not already divorced.
247 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
252 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
253 // CortexA9 has a Write-after-write hazard for NEON registers.
257 switch (RC->getID()) {
258 case ARM::DPRRegClassID:
259 case ARM::DPR_8RegClassID:
260 case ARM::DPR_VFP2RegClassID:
261 case ARM::QPRRegClassID:
262 case ARM::QPR_8RegClassID:
263 case ARM::QPR_VFP2RegClassID:
264 case ARM::SPRRegClassID:
265 case ARM::SPR_8RegClassID:
266 // Avoid reusing S, D, and Q registers.
267 // Don't increase register pressure for QQ and QQQQ.
274 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
275 const MachineFrameInfo *MFI = MF.getFrameInfo();
276 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
277 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
279 // When outgoing call frames are so large that we adjust the stack pointer
280 // around the call, we can no longer use the stack pointer to reach the
281 // emergency spill slot.
282 if (needsStackRealignment(MF) && !TFI->hasReservedCallFrame(MF))
285 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
286 // negative range for ldr/str (255), and thumb1 is positive offsets only.
287 // It's going to be better to use the SP or Base Pointer instead. When there
288 // are variable sized objects, we can't reference off of the SP, so we
289 // reserve a Base Pointer.
290 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
291 // Conservatively estimate whether the negative offset from the frame
292 // pointer will be sufficient to reach. If a function has a smallish
293 // frame, it's less likely to have lots of spills and callee saved
294 // space, so it's all more likely to be within range of the frame pointer.
295 // If it's wrong, the scavenger will still enable access to work, it just
297 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
305 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
306 const MachineRegisterInfo *MRI = &MF.getRegInfo();
307 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
308 // We can't realign the stack if:
309 // 1. Dynamic stack realignment is explicitly disabled,
310 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
311 // 3. There are VLAs in the function and the base pointer is disabled.
312 if (!MF.getTarget().Options.RealignStack)
314 if (AFI->isThumb1OnlyFunction())
316 // Stack realignment requires a frame pointer. If we already started
317 // register allocation with frame pointer elimination, it is too late now.
318 if (!MRI->canReserveReg(FramePtr))
320 // We may also need a base pointer if there are dynamic allocas or stack
321 // pointer adjustments around calls.
322 if (MF.getTarget().getFrameLowering()->hasReservedCallFrame(MF))
324 // A base pointer is required and allowed. Check that it isn't too late to
326 return MRI->canReserveReg(BasePtr);
329 bool ARMBaseRegisterInfo::
330 needsStackRealignment(const MachineFunction &MF) const {
331 const MachineFrameInfo *MFI = MF.getFrameInfo();
332 const Function *F = MF.getFunction();
333 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
334 bool requiresRealignment =
335 ((MFI->getMaxAlignment() > StackAlign) ||
336 F->getAttributes().hasAttribute(AttributeSet::FunctionIndex,
337 Attribute::StackAlignment));
339 return requiresRealignment && canRealignStack(MF);
342 bool ARMBaseRegisterInfo::
343 cannotEliminateFrame(const MachineFunction &MF) const {
344 const MachineFrameInfo *MFI = MF.getFrameInfo();
345 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack())
347 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
348 || needsStackRealignment(MF);
352 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
353 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
360 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
361 llvm_unreachable("What is the exception register");
364 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
365 llvm_unreachable("What is the exception handler register");
368 /// emitLoadConstPool - Emits a load from constpool to materialize the
369 /// specified immediate.
370 void ARMBaseRegisterInfo::
371 emitLoadConstPool(MachineBasicBlock &MBB,
372 MachineBasicBlock::iterator &MBBI,
374 unsigned DestReg, unsigned SubIdx, int Val,
375 ARMCC::CondCodes Pred,
376 unsigned PredReg, unsigned MIFlags) const {
377 MachineFunction &MF = *MBB.getParent();
378 MachineConstantPool *ConstantPool = MF.getConstantPool();
380 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
381 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
383 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
384 .addReg(DestReg, getDefRegState(true), SubIdx)
385 .addConstantPoolIndex(Idx)
386 .addImm(0).addImm(Pred).addReg(PredReg)
387 .setMIFlags(MIFlags);
390 bool ARMBaseRegisterInfo::
391 requiresRegisterScavenging(const MachineFunction &MF) const {
395 bool ARMBaseRegisterInfo::
396 trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
400 bool ARMBaseRegisterInfo::
401 requiresFrameIndexScavenging(const MachineFunction &MF) const {
405 bool ARMBaseRegisterInfo::
406 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
410 int64_t ARMBaseRegisterInfo::
411 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
412 const MCInstrDesc &Desc = MI->getDesc();
413 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
414 int64_t InstrOffs = 0;
418 case ARMII::AddrModeT2_i8:
419 case ARMII::AddrModeT2_i12:
420 case ARMII::AddrMode_i12:
421 InstrOffs = MI->getOperand(Idx+1).getImm();
424 case ARMII::AddrMode5: {
426 const MachineOperand &OffOp = MI->getOperand(Idx+1);
427 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
428 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
429 InstrOffs = -InstrOffs;
433 case ARMII::AddrMode2: {
435 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
436 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
437 InstrOffs = -InstrOffs;
440 case ARMII::AddrMode3: {
442 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
443 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
444 InstrOffs = -InstrOffs;
447 case ARMII::AddrModeT1_s: {
449 InstrOffs = MI->getOperand(ImmIdx).getImm();
454 llvm_unreachable("Unsupported addressing mode!");
457 return InstrOffs * Scale;
460 /// needsFrameBaseReg - Returns true if the instruction's frame index
461 /// reference would be better served by a base register other than FP
462 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
463 /// references it should create new base registers for.
464 bool ARMBaseRegisterInfo::
465 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
466 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
467 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
470 // It's the load/store FI references that cause issues, as it can be difficult
471 // to materialize the offset if it won't fit in the literal field. Estimate
472 // based on the size of the local frame and some conservative assumptions
473 // about the rest of the stack frame (note, this is pre-regalloc, so
474 // we don't know everything for certain yet) whether this offset is likely
475 // to be out of range of the immediate. Return true if so.
477 // We only generate virtual base registers for loads and stores, so
478 // return false for everything else.
479 unsigned Opc = MI->getOpcode();
481 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
482 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
483 case ARM::t2LDRi12: case ARM::t2LDRi8:
484 case ARM::t2STRi12: case ARM::t2STRi8:
485 case ARM::VLDRS: case ARM::VLDRD:
486 case ARM::VSTRS: case ARM::VSTRD:
487 case ARM::tSTRspi: case ARM::tLDRspi:
493 // Without a virtual base register, if the function has variable sized
494 // objects, all fixed-size local references will be via the frame pointer,
495 // Approximate the offset and see if it's legal for the instruction.
496 // Note that the incoming offset is based on the SP value at function entry,
497 // so it'll be negative.
498 MachineFunction &MF = *MI->getParent()->getParent();
499 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
500 MachineFrameInfo *MFI = MF.getFrameInfo();
501 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
503 // Estimate an offset from the frame pointer.
504 // Conservatively assume all callee-saved registers get pushed. R4-R6
505 // will be earlier than the FP, so we ignore those.
507 int64_t FPOffset = Offset - 8;
508 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
509 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
511 // Estimate an offset from the stack pointer.
512 // The incoming offset is relating to the SP at the start of the function,
513 // but when we access the local it'll be relative to the SP after local
514 // allocation, so adjust our SP-relative offset by that allocation size.
516 Offset += MFI->getLocalFrameSize();
517 // Assume that we'll have at least some spill slots allocated.
518 // FIXME: This is a total SWAG number. We should run some statistics
519 // and pick a real one.
520 Offset += 128; // 128 bytes of spill slots
522 // If there is a frame pointer, try using it.
523 // The FP is only available if there is no dynamic realignment. We
524 // don't know for sure yet whether we'll need that, so we guess based
525 // on whether there are any local variables that would trigger it.
526 unsigned StackAlign = TFI->getStackAlignment();
527 if (TFI->hasFP(MF) &&
528 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
529 if (isFrameOffsetLegal(MI, FPOffset))
532 // If we can reference via the stack pointer, try that.
533 // FIXME: This (and the code that resolves the references) can be improved
534 // to only disallow SP relative references in the live range of
535 // the VLA(s). In practice, it's unclear how much difference that
536 // would make, but it may be worth doing.
537 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
540 // The offset likely isn't legal, we want to allocate a virtual base register.
544 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
545 /// be a pointer to FrameIdx at the beginning of the basic block.
546 void ARMBaseRegisterInfo::
547 materializeFrameBaseRegister(MachineBasicBlock *MBB,
548 unsigned BaseReg, int FrameIdx,
549 int64_t Offset) const {
550 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
551 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
552 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
554 MachineBasicBlock::iterator Ins = MBB->begin();
555 DebugLoc DL; // Defaults to "unknown"
556 if (Ins != MBB->end())
557 DL = Ins->getDebugLoc();
559 const MCInstrDesc &MCID = TII.get(ADDriOpc);
560 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
561 const MachineFunction &MF = *MBB->getParent();
562 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this, MF));
564 MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg)
565 .addFrameIndex(FrameIdx).addImm(Offset));
567 if (!AFI->isThumb1OnlyFunction())
572 ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
573 unsigned BaseReg, int64_t Offset) const {
574 MachineInstr &MI = *I;
575 MachineBasicBlock &MBB = *MI.getParent();
576 MachineFunction &MF = *MBB.getParent();
577 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
578 int Off = Offset; // ARM doesn't need the general 64-bit offsets
581 assert(!AFI->isThumb1OnlyFunction() &&
582 "This resolveFrameIndex does not support Thumb1!");
584 while (!MI.getOperand(i).isFI()) {
586 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
589 if (!AFI->isThumbFunction())
590 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
592 assert(AFI->isThumb2Function());
593 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
595 assert (Done && "Unable to resolve frame index!");
599 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
600 int64_t Offset) const {
601 const MCInstrDesc &Desc = MI->getDesc();
602 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
605 while (!MI->getOperand(i).isFI()) {
607 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
610 // AddrMode4 and AddrMode6 cannot handle any offset.
611 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
614 unsigned NumBits = 0;
616 bool isSigned = true;
618 case ARMII::AddrModeT2_i8:
619 case ARMII::AddrModeT2_i12:
620 // i8 supports only negative, and i12 supports only positive, so
621 // based on Offset sign, consider the appropriate instruction
630 case ARMII::AddrMode5:
635 case ARMII::AddrMode_i12:
636 case ARMII::AddrMode2:
639 case ARMII::AddrMode3:
642 case ARMII::AddrModeT1_s:
648 llvm_unreachable("Unsupported addressing mode!");
651 Offset += getFrameIndexInstrOffset(MI, i);
652 // Make sure the offset is encodable for instructions that scale the
654 if ((Offset & (Scale-1)) != 0)
657 if (isSigned && Offset < 0)
660 unsigned Mask = (1 << NumBits) - 1;
661 if ((unsigned)Offset <= Mask * Scale)
668 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
669 int SPAdj, unsigned FIOperandNum,
670 RegScavenger *RS) const {
671 MachineInstr &MI = *II;
672 MachineBasicBlock &MBB = *MI.getParent();
673 MachineFunction &MF = *MBB.getParent();
674 const ARMFrameLowering *TFI =
675 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
676 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
677 assert(!AFI->isThumb1OnlyFunction() &&
678 "This eliminateFrameIndex does not support Thumb1!");
679 int FrameIndex = MI.getOperand(FIOperandNum).getIndex();
682 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
684 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
685 // call frame setup/destroy instructions have already been eliminated. That
686 // means the stack pointer cannot be used to access the emergency spill slot
687 // when !hasReservedCallFrame().
689 if (RS && FrameReg == ARM::SP && RS->isScavengingFrameIndex(FrameIndex)){
690 assert(TFI->hasReservedCallFrame(MF) &&
691 "Cannot use SP to access the emergency spill slot in "
692 "functions without a reserved call frame");
693 assert(!MF.getFrameInfo()->hasVarSizedObjects() &&
694 "Cannot use SP to access the emergency spill slot in "
695 "functions with variable sized frame objects");
699 // Special handling of dbg_value instructions.
700 if (MI.isDebugValue()) {
701 MI.getOperand(FIOperandNum). ChangeToRegister(FrameReg, false /*isDef*/);
702 MI.getOperand(FIOperandNum + 1).ChangeToImmediate(Offset);
706 // Modify MI as necessary to handle as much of 'Offset' as possible
708 if (!AFI->isThumbFunction())
709 Done = rewriteARMFrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
711 assert(AFI->isThumb2Function());
712 Done = rewriteT2FrameIndex(MI, FIOperandNum, FrameReg, Offset, TII);
717 // If we get here, the immediate doesn't fit into the instruction. We folded
718 // as much as possible above, handle the rest, providing a register that is
721 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
722 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
723 "This code isn't needed if offset already handled!");
725 unsigned ScratchReg = 0;
726 int PIdx = MI.findFirstPredOperandIdx();
727 ARMCC::CondCodes Pred = (PIdx == -1)
728 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
729 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
731 // Must be addrmode4/6.
732 MI.getOperand(FIOperandNum).ChangeToRegister(FrameReg, false, false, false);
734 ScratchReg = MF.getRegInfo().createVirtualRegister(&ARM::GPRRegClass);
735 if (!AFI->isThumbFunction())
736 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
737 Offset, Pred, PredReg, TII);
739 assert(AFI->isThumb2Function());
740 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
741 Offset, Pred, PredReg, TII);
743 // Update the original instruction to use the scratch register.
744 MI.getOperand(FIOperandNum).ChangeToRegister(ScratchReg, false, false,true);