1 //===- ARMRegisterInfo.cpp - ARM Register Information -----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the "Instituto Nokia de Tecnologia" and
6 // is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file contains the ARM implementation of the MRegisterInfo class.
13 //===----------------------------------------------------------------------===//
16 #include "ARMAddressingModes.h"
17 #include "ARMInstrInfo.h"
18 #include "ARMMachineFunctionInfo.h"
19 #include "ARMRegisterInfo.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.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/MachineLocation.h"
28 #include "llvm/CodeGen/RegisterScavenging.h"
29 #include "llvm/Target/TargetFrameInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/Target/TargetOptions.h"
32 #include "llvm/ADT/BitVector.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/Support/CommandLine.h"
39 static cl::opt<bool> EnableScavenging("enable-arm-reg-scavenging", cl::Hidden,
40 cl::desc("Enable register scavenging on ARM"));
42 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
45 case R0: case S0: case D0: return 0;
46 case R1: case S1: case D1: return 1;
47 case R2: case S2: case D2: return 2;
48 case R3: case S3: case D3: return 3;
49 case R4: case S4: case D4: return 4;
50 case R5: case S5: case D5: return 5;
51 case R6: case S6: case D6: return 6;
52 case R7: case S7: case D7: return 7;
53 case R8: case S8: case D8: return 8;
54 case R9: case S9: case D9: return 9;
55 case R10: case S10: case D10: return 10;
56 case R11: case S11: case D11: return 11;
57 case R12: case S12: case D12: return 12;
58 case SP: case S13: case D13: return 13;
59 case LR: case S14: case D14: return 14;
60 case PC: case S15: case D15: return 15;
78 assert(0 && "Unknown ARM register!");
83 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii,
84 const ARMSubtarget &sti)
85 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
87 FramePtr(STI.useThumbBacktraces() ? ARM::R7 : ARM::R11) {
88 RS = (EnableScavenging) ? new RegScavenger() : NULL;
91 ARMRegisterInfo::~ARMRegisterInfo() {
95 bool ARMRegisterInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
96 MachineBasicBlock::iterator MI,
97 const std::vector<CalleeSavedInfo> &CSI) const {
98 MachineFunction &MF = *MBB.getParent();
99 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
100 if (!AFI->isThumbFunction() || CSI.empty())
103 MachineInstrBuilder MIB = BuildMI(MBB, MI, TII.get(ARM::tPUSH));
104 for (unsigned i = CSI.size(); i != 0; --i) {
105 unsigned Reg = CSI[i-1].getReg();
106 // Add the callee-saved register as live-in. It's killed at the spill.
108 MIB.addReg(Reg, false/*isDef*/,false/*isImp*/,true/*isKill*/);
113 bool ARMRegisterInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
114 MachineBasicBlock::iterator MI,
115 const std::vector<CalleeSavedInfo> &CSI) const {
116 MachineFunction &MF = *MBB.getParent();
117 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
118 if (!AFI->isThumbFunction() || CSI.empty())
121 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
122 MachineInstr *PopMI = new MachineInstr(TII.get(ARM::tPOP));
123 MBB.insert(MI, PopMI);
124 for (unsigned i = CSI.size(); i != 0; --i) {
125 unsigned Reg = CSI[i-1].getReg();
126 if (Reg == ARM::LR) {
127 // Special epilogue for vararg functions. See emitEpilogue
131 PopMI->setInstrDescriptor(TII.get(ARM::tPOP_RET));
134 PopMI->addRegOperand(Reg, true);
139 void ARMRegisterInfo::
140 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
141 unsigned SrcReg, int FI,
142 const TargetRegisterClass *RC) const {
143 if (RC == ARM::GPRRegisterClass) {
144 MachineFunction &MF = *MBB.getParent();
145 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
146 if (AFI->isThumbFunction())
147 BuildMI(MBB, I, TII.get(ARM::tSpill)).addReg(SrcReg, false, false, true)
148 .addFrameIndex(FI).addImm(0);
150 BuildMI(MBB, I, TII.get(ARM::STR)).addReg(SrcReg, false, false, true)
151 .addFrameIndex(FI).addReg(0).addImm(0);
152 } else if (RC == ARM::DPRRegisterClass) {
153 BuildMI(MBB, I, TII.get(ARM::FSTD)).addReg(SrcReg, false, false, true)
154 .addFrameIndex(FI).addImm(0);
156 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
157 BuildMI(MBB, I, TII.get(ARM::FSTS)).addReg(SrcReg, false, false, true)
158 .addFrameIndex(FI).addImm(0);
162 void ARMRegisterInfo::
163 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
164 unsigned DestReg, int FI,
165 const TargetRegisterClass *RC) const {
166 if (RC == ARM::GPRRegisterClass) {
167 MachineFunction &MF = *MBB.getParent();
168 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
169 if (AFI->isThumbFunction())
170 BuildMI(MBB, I, TII.get(ARM::tRestore), DestReg)
171 .addFrameIndex(FI).addImm(0);
173 BuildMI(MBB, I, TII.get(ARM::LDR), DestReg)
174 .addFrameIndex(FI).addReg(0).addImm(0);
175 } else if (RC == ARM::DPRRegisterClass) {
176 BuildMI(MBB, I, TII.get(ARM::FLDD), DestReg)
177 .addFrameIndex(FI).addImm(0);
179 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
180 BuildMI(MBB, I, TII.get(ARM::FLDS), DestReg)
181 .addFrameIndex(FI).addImm(0);
185 void ARMRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
186 MachineBasicBlock::iterator I,
187 unsigned DestReg, unsigned SrcReg,
188 const TargetRegisterClass *RC) const {
189 if (RC == ARM::GPRRegisterClass) {
190 MachineFunction &MF = *MBB.getParent();
191 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
192 BuildMI(MBB, I, TII.get(AFI->isThumbFunction() ? ARM::tMOVrr : ARM::MOVrr),
193 DestReg).addReg(SrcReg);
194 } else if (RC == ARM::SPRRegisterClass)
195 BuildMI(MBB, I, TII.get(ARM::FCPYS), DestReg).addReg(SrcReg);
196 else if (RC == ARM::DPRRegisterClass)
197 BuildMI(MBB, I, TII.get(ARM::FCPYD), DestReg).addReg(SrcReg);
202 /// isLowRegister - Returns true if the register is low register r0-r7.
204 static bool isLowRegister(unsigned Reg) {
207 case R0: case R1: case R2: case R3:
208 case R4: case R5: case R6: case R7:
215 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr *MI,
216 unsigned OpNum, int FI) const {
217 unsigned Opc = MI->getOpcode();
218 MachineInstr *NewMI = NULL;
222 if (OpNum == 0) { // move -> store
223 unsigned SrcReg = MI->getOperand(1).getReg();
224 NewMI = BuildMI(TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
225 .addReg(0).addImm(0);
226 } else { // move -> load
227 unsigned DstReg = MI->getOperand(0).getReg();
228 NewMI = BuildMI(TII.get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
234 if (OpNum == 0) { // move -> store
235 unsigned SrcReg = MI->getOperand(1).getReg();
236 if (isPhysicalRegister(SrcReg) && !isLowRegister(SrcReg))
237 // tSpill cannot take a high register operand.
239 NewMI = BuildMI(TII.get(ARM::tSpill)).addReg(SrcReg).addFrameIndex(FI)
241 } else { // move -> load
242 unsigned DstReg = MI->getOperand(0).getReg();
243 if (isPhysicalRegister(DstReg) && !isLowRegister(DstReg))
244 // tRestore cannot target a high register operand.
246 NewMI = BuildMI(TII.get(ARM::tRestore), DstReg).addFrameIndex(FI)
252 if (OpNum == 0) { // move -> store
253 unsigned SrcReg = MI->getOperand(1).getReg();
254 NewMI = BuildMI(TII.get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
256 } else { // move -> load
257 unsigned DstReg = MI->getOperand(0).getReg();
258 NewMI = BuildMI(TII.get(ARM::FLDS), DstReg).addFrameIndex(FI).addImm(0);
263 if (OpNum == 0) { // move -> store
264 unsigned SrcReg = MI->getOperand(1).getReg();
265 NewMI = BuildMI(TII.get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
267 } else { // move -> load
268 unsigned DstReg = MI->getOperand(0).getReg();
269 NewMI = BuildMI(TII.get(ARM::FLDD), DstReg).addFrameIndex(FI).addImm(0);
276 NewMI->copyKillDeadInfo(MI);
280 const unsigned* ARMRegisterInfo::getCalleeSavedRegs() const {
281 static const unsigned CalleeSavedRegs[] = {
282 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
283 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
285 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
286 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
290 static const unsigned DarwinCalleeSavedRegs[] = {
291 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
292 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
294 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
295 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
298 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
301 const TargetRegisterClass* const *
302 ARMRegisterInfo::getCalleeSavedRegClasses() const {
303 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
304 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
305 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
306 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
308 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
309 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
312 return CalleeSavedRegClasses;
315 BitVector ARMRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
316 BitVector Reserved(getNumRegs());
317 Reserved.set(ARM::SP);
318 Reserved.set(ARM::PC);
319 if (STI.isTargetDarwin() || hasFP(MF))
320 Reserved.set(FramePtr);
321 // Some targets reserve R9.
322 if (STI.isR9Reserved())
323 Reserved.set(ARM::R9);
324 // At PEI time, if LR is used, it will be spilled upon entry.
325 if (MF.getUsedPhysregs() && !MF.isPhysRegUsed((unsigned)ARM::LR))
326 Reserved.set(ARM::LR);
331 ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
332 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
333 return EnableScavenging && !AFI->isThumbFunction();
336 /// hasFP - Return true if the specified function should have a dedicated frame
337 /// pointer register. This is true if the function has variable sized allocas
338 /// or if frame pointer elimination is disabled.
340 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
341 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
344 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
345 /// a destreg = basereg + immediate in ARM code.
347 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
348 MachineBasicBlock::iterator &MBBI,
349 unsigned DestReg, unsigned BaseReg,
350 int NumBytes, const TargetInstrInfo &TII) {
351 bool isSub = NumBytes < 0;
352 if (isSub) NumBytes = -NumBytes;
355 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
356 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
357 assert(ThisVal && "Didn't extract field correctly");
359 // We will handle these bits from offset, clear them.
360 NumBytes &= ~ThisVal;
362 // Get the properly encoded SOImmVal field.
363 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
364 assert(SOImmVal != -1 && "Bit extraction didn't work?");
366 // Build the new ADD / SUB.
367 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
368 .addReg(BaseReg, false, false, true).addImm(SOImmVal);
373 /// calcNumMI - Returns the number of instructions required to materialize
374 /// the specific add / sub r, c instruction.
375 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
376 unsigned NumBits, unsigned Scale) {
378 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
380 if (Opc == ARM::tADDrSPi) {
381 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
386 Chunk = ((1 << NumBits) - 1) * Scale;
389 NumMIs += Bytes / Chunk;
390 if ((Bytes % Chunk) != 0)
397 /// emitLoadConstPool - Emits a load from constpool to materialize NumBytes
399 static void emitLoadConstPool(MachineBasicBlock &MBB,
400 MachineBasicBlock::iterator &MBBI,
401 unsigned DestReg, int NumBytes,
402 const TargetInstrInfo &TII) {
403 MachineFunction &MF = *MBB.getParent();
404 MachineConstantPool *ConstantPool = MF.getConstantPool();
405 Constant *C = ConstantInt::get(Type::Int32Ty, NumBytes);
406 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 2);
407 BuildMI(MBB, MBBI, TII.get(ARM::tLDRpci), DestReg).addConstantPoolIndex(Idx);
410 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
411 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
412 /// in a register using mov / mvn sequences or load the immediate from a
415 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
416 MachineBasicBlock::iterator &MBBI,
417 unsigned DestReg, unsigned BaseReg,
418 int NumBytes, bool CanChangeCC,
419 const TargetInstrInfo &TII) {
420 bool isHigh = !isLowRegister(DestReg) ||
421 (BaseReg != 0 && !isLowRegister(BaseReg));
423 // Subtract doesn't have high register version. Load the negative value
424 // if either base or dest register is a high register. Also, if do not
425 // issue sub as part of the sequence if condition register is to be
427 if (NumBytes < 0 && !isHigh && CanChangeCC) {
429 NumBytes = -NumBytes;
431 unsigned LdReg = DestReg;
432 if (DestReg == ARM::SP) {
433 assert(BaseReg == ARM::SP && "Unexpected!");
435 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::R12)
436 .addReg(ARM::R3, false, false, true);
439 if (NumBytes <= 255 && NumBytes >= 0)
440 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), LdReg).addImm(NumBytes);
441 else if (NumBytes < 0 && NumBytes >= -255) {
442 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), LdReg).addImm(NumBytes);
443 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), LdReg)
444 .addReg(LdReg, false, false, true);
446 emitLoadConstPool(MBB, MBBI, LdReg, NumBytes, TII);
449 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
450 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, TII.get(Opc), DestReg);
451 if (DestReg == ARM::SP || isSub)
452 MIB.addReg(BaseReg).addReg(LdReg, false, false, true);
454 MIB.addReg(LdReg).addReg(BaseReg, false, false, true);
455 if (DestReg == ARM::SP)
456 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::R3)
457 .addReg(ARM::R12, false, false, true);
460 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
461 /// a destreg = basereg + immediate in Thumb code.
463 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
464 MachineBasicBlock::iterator &MBBI,
465 unsigned DestReg, unsigned BaseReg,
466 int NumBytes, const TargetInstrInfo &TII) {
467 bool isSub = NumBytes < 0;
468 unsigned Bytes = (unsigned)NumBytes;
469 if (isSub) Bytes = -NumBytes;
470 bool isMul4 = (Bytes & 3) == 0;
471 bool isTwoAddr = false;
472 bool DstNotEqBase = false;
473 unsigned NumBits = 1;
478 if (DestReg == BaseReg && BaseReg == ARM::SP) {
479 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
482 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
484 } else if (!isSub && BaseReg == ARM::SP) {
487 // r1 = add sp, 100 * 4
491 ExtraOpc = ARM::tADDi3;
500 if (DestReg != BaseReg)
503 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
507 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
508 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
509 if (NumMIs > Threshold) {
510 // This will expand into too many instructions. Load the immediate from a
512 emitThumbRegPlusImmInReg(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII);
517 if (isLowRegister(DestReg) && isLowRegister(BaseReg)) {
518 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
519 unsigned Chunk = (1 << 3) - 1;
520 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
522 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
523 .addReg(BaseReg, false, false, true).addImm(ThisVal);
525 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), DestReg)
526 .addReg(BaseReg, false, false, true);
531 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
533 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
536 // Build the new tADD / tSUB.
538 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(DestReg).addImm(ThisVal);
540 bool isKill = BaseReg != ARM::SP;
541 BuildMI(MBB, MBBI, TII.get(Opc), DestReg)
542 .addReg(BaseReg, false, false, isKill).addImm(ThisVal);
545 if (Opc == ARM::tADDrSPi) {
551 Chunk = ((1 << NumBits) - 1) * Scale;
552 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
559 BuildMI(MBB, MBBI, TII.get(ExtraOpc), DestReg)
560 .addReg(DestReg, false, false, true)
561 .addImm(((unsigned)NumBytes) & 3);
565 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
566 int NumBytes, bool isThumb, const TargetInstrInfo &TII) {
568 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
570 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
573 void ARMRegisterInfo::
574 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
575 MachineBasicBlock::iterator I) const {
577 // If we have alloca, convert as follows:
578 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
579 // ADJCALLSTACKUP -> add, sp, sp, amount
580 MachineInstr *Old = I;
581 unsigned Amount = Old->getOperand(0).getImmedValue();
583 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
584 // We need to keep the stack aligned properly. To do this, we round the
585 // amount of space needed for the outgoing arguments up to the next
586 // alignment boundary.
587 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
588 Amount = (Amount+Align-1)/Align*Align;
590 // Replace the pseudo instruction with a new instruction...
591 if (Old->getOpcode() == ARM::ADJCALLSTACKDOWN) {
592 emitSPUpdate(MBB, I, -Amount, AFI->isThumbFunction(), TII);
594 assert(Old->getOpcode() == ARM::ADJCALLSTACKUP);
595 emitSPUpdate(MBB, I, Amount, AFI->isThumbFunction(), TII);
602 /// emitThumbConstant - Emit a series of instructions to materialize a
604 static void emitThumbConstant(MachineBasicBlock &MBB,
605 MachineBasicBlock::iterator &MBBI,
606 unsigned DestReg, int Imm,
607 const TargetInstrInfo &TII) {
608 bool isSub = Imm < 0;
609 if (isSub) Imm = -Imm;
611 int Chunk = (1 << 8) - 1;
612 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
614 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), DestReg).addImm(ThisVal);
616 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII);
618 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), DestReg)
619 .addReg(DestReg, false, false, true);
622 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
623 RegScavenger *RS) const{
625 MachineInstr &MI = *II;
626 MachineBasicBlock &MBB = *MI.getParent();
627 MachineFunction &MF = *MBB.getParent();
628 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
629 bool isThumb = AFI->isThumbFunction();
631 while (!MI.getOperand(i).isFrameIndex()) {
633 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
636 unsigned FrameReg = ARM::SP;
637 int FrameIndex = MI.getOperand(i).getFrameIndex();
638 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
639 MF.getFrameInfo()->getStackSize();
641 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
642 Offset -= AFI->getGPRCalleeSavedArea1Offset();
643 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
644 Offset -= AFI->getGPRCalleeSavedArea2Offset();
645 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
646 Offset -= AFI->getDPRCalleeSavedAreaOffset();
647 else if (hasFP(MF)) {
648 // There is alloca()'s in this function, must reference off the frame
650 FrameReg = getFrameRegister(MF);
651 Offset -= AFI->getFramePtrSpillOffset();
654 unsigned Opcode = MI.getOpcode();
655 const TargetInstrDescriptor &Desc = TII.get(Opcode);
656 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
659 if (Opcode == ARM::ADDri) {
660 Offset += MI.getOperand(i+1).getImm();
662 // Turn it into a move.
663 MI.setInstrDescriptor(TII.get(ARM::MOVrr));
664 MI.getOperand(i).ChangeToRegister(FrameReg, false);
665 MI.RemoveOperand(i+1);
667 } else if (Offset < 0) {
670 MI.setInstrDescriptor(TII.get(ARM::SUBri));
673 // Common case: small offset, fits into instruction.
674 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
675 if (ImmedOffset != -1) {
676 // Replace the FrameIndex with sp / fp
677 MI.getOperand(i).ChangeToRegister(FrameReg, false);
678 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
682 // Otherwise, we fallback to common code below to form the imm offset with
683 // a sequence of ADDri instructions. First though, pull as much of the imm
684 // into this ADDri as possible.
685 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
686 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, (32-RotAmt) & 31);
688 // We will handle these bits from offset, clear them.
689 Offset &= ~ThisImmVal;
691 // Get the properly encoded SOImmVal field.
692 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
693 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
694 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
695 } else if (Opcode == ARM::tADDrSPi) {
696 Offset += MI.getOperand(i+1).getImm();
697 assert((Offset & 3) == 0 &&
698 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
700 // Turn it into a move.
701 MI.setInstrDescriptor(TII.get(ARM::tMOVrr));
702 MI.getOperand(i).ChangeToRegister(FrameReg, false);
703 MI.RemoveOperand(i+1);
707 // Common case: small offset, fits into instruction.
708 if (((Offset >> 2) & ~255U) == 0) {
709 // Replace the FrameIndex with sp / fp
710 MI.getOperand(i).ChangeToRegister(FrameReg, false);
711 MI.getOperand(i+1).ChangeToImmediate(Offset >> 2);
715 unsigned DestReg = MI.getOperand(0).getReg();
716 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
717 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, 8, 1);
718 // MI would expand into a large number of instructions. Don't try to
719 // simplify the immediate.
721 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII);
727 // Translate r0 = add sp, imm to
728 // r0 = add sp, 255*4
729 // r0 = add r0, (imm - 255*4)
730 MI.getOperand(i).ChangeToRegister(FrameReg, false);
731 MI.getOperand(i+1).ChangeToImmediate(255);
732 Offset = (Offset - 255 * 4);
733 MachineBasicBlock::iterator NII = next(II);
734 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII);
736 // Translate r0 = add sp, -imm to
737 // r0 = -imm (this is then translated into a series of instructons)
739 emitThumbConstant(MBB, II, DestReg, Offset, TII);
740 MI.setInstrDescriptor(TII.get(ARM::tADDhirr));
741 MI.getOperand(i).ChangeToRegister(DestReg, false, false, true);
742 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
748 unsigned NumBits = 0;
751 case ARMII::AddrMode2: {
753 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
754 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
759 case ARMII::AddrMode3: {
761 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
762 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
767 case ARMII::AddrMode5: {
769 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
770 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
776 case ARMII::AddrModeTs: {
778 InstrOffs = MI.getOperand(ImmIdx).getImm();
779 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
784 assert(0 && "Unsupported addressing mode!");
789 Offset += InstrOffs * Scale;
790 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
791 if (Offset < 0 && !isThumb) {
796 // Common case: small offset, fits into instruction.
797 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
798 int ImmedOffset = Offset / Scale;
799 unsigned Mask = (1 << NumBits) - 1;
800 if ((unsigned)Offset <= Mask * Scale) {
801 // Replace the FrameIndex with sp
802 MI.getOperand(i).ChangeToRegister(FrameReg, false);
804 ImmedOffset |= 1 << NumBits;
805 ImmOp.ChangeToImmediate(ImmedOffset);
809 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
810 if (AddrMode == ARMII::AddrModeTs) {
811 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
812 // a different base register.
814 Mask = (1 << NumBits) - 1;
816 // If this is a thumb spill / restore, we will be using a constpool load to
817 // materialize the offset.
818 if (AddrMode == ARMII::AddrModeTs && isThumSpillRestore)
819 ImmOp.ChangeToImmediate(0);
821 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
822 ImmedOffset = ImmedOffset & Mask;
824 ImmedOffset |= 1 << NumBits;
825 ImmOp.ChangeToImmediate(ImmedOffset);
826 Offset &= ~(Mask*Scale);
830 // If we get here, the immediate doesn't fit into the instruction. We folded
831 // as much as possible above, handle the rest, providing a register that is
833 assert(Offset && "This code isn't needed if offset already handled!");
836 if (TII.isLoad(Opcode)) {
837 // Use the destination register to materialize sp + offset.
838 unsigned TmpReg = MI.getOperand(0).getReg();
840 if (Opcode == ARM::tRestore) {
841 if (FrameReg == ARM::SP)
842 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
844 emitLoadConstPool(MBB, II, TmpReg, Offset, TII);
848 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
849 MI.setInstrDescriptor(TII.get(ARM::tLDR));
850 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
852 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
854 MI.addRegOperand(0, false); // tLDR has an extra register operand.
855 } else if (TII.isStore(Opcode)) {
856 // FIXME! This is horrific!!! We need register scavenging.
857 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
858 // also a ABI register so it's possible that is is the register that is
859 // being storing here. If that's the case, we do the following:
861 // Use r2 to materialize sp + offset
864 unsigned ValReg = MI.getOperand(0).getReg();
865 unsigned TmpReg = ARM::R3;
867 if (ValReg == ARM::R3) {
868 BuildMI(MBB, II, TII.get(ARM::tMOVrr), ARM::R12)
869 .addReg(ARM::R2, false, false, true);
872 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
873 BuildMI(MBB, II, TII.get(ARM::tMOVrr), ARM::R12)
874 .addReg(ARM::R3, false, false, true);
875 if (Opcode == ARM::tSpill) {
876 if (FrameReg == ARM::SP)
877 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
879 emitLoadConstPool(MBB, II, TmpReg, Offset, TII);
883 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
884 MI.setInstrDescriptor(TII.get(ARM::tSTR));
885 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
887 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
889 MI.addRegOperand(0, false); // tSTR has an extra register operand.
891 MachineBasicBlock::iterator NII = next(II);
892 if (ValReg == ARM::R3)
893 BuildMI(MBB, NII, TII.get(ARM::tMOVrr), ARM::R2)
894 .addReg(ARM::R12, false, false, true);
895 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
896 BuildMI(MBB, NII, TII.get(ARM::tMOVrr), ARM::R3)
897 .addReg(ARM::R12, false, false, true);
899 assert(false && "Unexpected opcode!");
901 // Insert a set of r12 with the full address: r12 = sp + offset
902 // If the offset we have is too large to fit into the instruction, we need
903 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
905 unsigned ScratchReg = RS
906 ? RS->FindUnusedReg(&ARM::GPRRegClass, true) : (unsigned)ARM::R12;
907 assert(ScratchReg != 0 && "Unable to find a free call-clobbered register!");
908 emitARMRegPlusImmediate(MBB, II, ScratchReg, FrameReg,
909 isSub ? -Offset : Offset, TII);
910 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
914 void ARMRegisterInfo::
915 processFunctionBeforeCalleeSavedScan(MachineFunction &MF) const {
916 // This tells PEI to spill the FP as if it is any other callee-save register
917 // to take advantage the eliminateFrameIndex machinery. This also ensures it
918 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
919 // to combine multiple loads / stores.
920 bool CanEliminateFrame = true;
921 bool CS1Spilled = false;
922 bool LRSpilled = false;
923 unsigned NumGPRSpills = 0;
924 SmallVector<unsigned, 4> UnspilledCS1GPRs;
925 SmallVector<unsigned, 4> UnspilledCS2GPRs;
926 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
928 // Don't spill FP if the frame can be eliminated. This is determined
929 // by scanning the callee-save registers to see if any is used.
930 const unsigned *CSRegs = getCalleeSavedRegs();
931 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
932 for (unsigned i = 0; CSRegs[i]; ++i) {
933 unsigned Reg = CSRegs[i];
934 bool Spilled = false;
935 if (MF.isPhysRegUsed(Reg)) {
936 AFI->setCSRegisterIsSpilled(Reg);
938 CanEliminateFrame = false;
940 // Check alias registers too.
941 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
942 if (MF.isPhysRegUsed(*Aliases)) {
944 CanEliminateFrame = false;
949 if (CSRegClasses[i] == &ARM::GPRRegClass) {
953 if (!STI.isTargetDarwin()) {
961 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
976 if (!STI.isTargetDarwin()) {
977 UnspilledCS1GPRs.push_back(Reg);
987 UnspilledCS1GPRs.push_back(Reg);
990 UnspilledCS2GPRs.push_back(Reg);
997 bool ForceLRSpill = false;
998 if (!LRSpilled && AFI->isThumbFunction()) {
999 unsigned FnSize = ARM::GetFunctionSize(MF);
1000 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1001 // use of BL to implement far jump. If it turns out that it's not needed
1002 // then the branch fix up path will undo it.
1003 if (FnSize >= (1 << 11)) {
1004 CanEliminateFrame = false;
1005 ForceLRSpill = true;
1009 if (!CanEliminateFrame || hasFP(MF)) {
1010 AFI->setHasStackFrame(true);
1012 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1013 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1014 if (!LRSpilled && CS1Spilled) {
1015 MF.changePhyRegUsed(ARM::LR, true);
1016 AFI->setCSRegisterIsSpilled(ARM::LR);
1018 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1019 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1020 ForceLRSpill = false;
1023 // Darwin ABI requires FP to point to the stack slot that contains the
1025 if (STI.isTargetDarwin() || hasFP(MF)) {
1026 MF.changePhyRegUsed(FramePtr, true);
1030 // If stack and double are 8-byte aligned and we are spilling an odd number
1031 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
1032 // the integer and double callee save areas.
1033 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1034 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1035 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1036 unsigned Reg = UnspilledCS1GPRs.front();
1037 MF.changePhyRegUsed(Reg, true);
1038 AFI->setCSRegisterIsSpilled(Reg);
1039 } else if (!UnspilledCS2GPRs.empty()) {
1040 unsigned Reg = UnspilledCS2GPRs.front();
1041 MF.changePhyRegUsed(Reg, true);
1042 AFI->setCSRegisterIsSpilled(Reg);
1048 MF.changePhyRegUsed(ARM::LR, true);
1049 AFI->setCSRegisterIsSpilled(ARM::LR);
1050 AFI->setLRIsSpilledForFarJump(true);
1054 /// Move iterator pass the next bunch of callee save load / store ops for
1055 /// the particular spill area (1: integer area 1, 2: integer area 2,
1056 /// 3: fp area, 0: don't care).
1057 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1058 MachineBasicBlock::iterator &MBBI,
1059 int Opc, unsigned Area,
1060 const ARMSubtarget &STI) {
1061 while (MBBI != MBB.end() &&
1062 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFrameIndex()) {
1065 unsigned Category = 0;
1066 switch (MBBI->getOperand(0).getReg()) {
1067 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1071 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1072 Category = STI.isTargetDarwin() ? 2 : 1;
1074 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1075 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1082 if (Done || Category != Area)
1090 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
1091 MachineBasicBlock &MBB = MF.front();
1092 MachineBasicBlock::iterator MBBI = MBB.begin();
1093 MachineFrameInfo *MFI = MF.getFrameInfo();
1094 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1095 bool isThumb = AFI->isThumbFunction();
1096 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1097 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1098 unsigned NumBytes = MFI->getStackSize();
1099 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1102 // Check if R3 is live in. It might have to be used as a scratch register.
1103 for (MachineFunction::livein_iterator I=MF.livein_begin(),E=MF.livein_end();
1105 if ((*I).first == ARM::R3) {
1106 AFI->setR3IsLiveIn(true);
1111 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1112 NumBytes = (NumBytes + 3) & ~3;
1113 MFI->setStackSize(NumBytes);
1116 // Determine the sizes of each callee-save spill areas and record which frame
1117 // belongs to which callee-save spill areas.
1118 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1119 int FramePtrSpillFI = 0;
1122 emitSPUpdate(MBB, MBBI, -VARegSaveSize, isThumb, TII);
1124 if (!AFI->hasStackFrame()) {
1126 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1130 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1131 unsigned Reg = CSI[i].getReg();
1132 int FI = CSI[i].getFrameIdx();
1139 if (Reg == FramePtr)
1140 FramePtrSpillFI = FI;
1141 AFI->addGPRCalleeSavedArea1Frame(FI);
1148 if (Reg == FramePtr)
1149 FramePtrSpillFI = FI;
1150 if (STI.isTargetDarwin()) {
1151 AFI->addGPRCalleeSavedArea2Frame(FI);
1154 AFI->addGPRCalleeSavedArea1Frame(FI);
1159 AFI->addDPRCalleeSavedAreaFrame(FI);
1164 if (Align == 8 && (GPRCS1Size & 7) != 0)
1165 // Pad CS1 to ensure proper alignment.
1169 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1170 emitSPUpdate(MBB, MBBI, -GPRCS1Size, isThumb, TII);
1171 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1172 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH)
1175 // Darwin ABI requires FP to point to the stack slot that contains the
1177 if (STI.isTargetDarwin() || hasFP(MF))
1178 BuildMI(MBB, MBBI, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri), FramePtr)
1179 .addFrameIndex(FramePtrSpillFI).addImm(0);
1182 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1183 emitSPUpdate(MBB, MBBI, -GPRCS2Size, false, TII);
1185 // Build the new SUBri to adjust SP for FP callee-save spill area.
1186 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1187 emitSPUpdate(MBB, MBBI, -DPRCSSize, false, TII);
1190 // Determine starting offsets of spill areas.
1191 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1192 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1193 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1194 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1195 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1196 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1197 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1199 NumBytes = DPRCSOffset;
1201 // Insert it after all the callee-save spills.
1203 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1204 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1207 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1208 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1209 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1212 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1213 for (unsigned i = 0; CSRegs[i]; ++i)
1214 if (Reg == CSRegs[i])
1219 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1220 return ((MI->getOpcode() == ARM::FLDD ||
1221 MI->getOpcode() == ARM::LDR ||
1222 MI->getOpcode() == ARM::tRestore) &&
1223 MI->getOperand(1).isFrameIndex() &&
1224 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1227 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1228 MachineBasicBlock &MBB) const {
1229 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1230 assert((MBBI->getOpcode() == ARM::BX_RET ||
1231 MBBI->getOpcode() == ARM::tBX_RET ||
1232 MBBI->getOpcode() == ARM::tPOP_RET) &&
1233 "Can only insert epilog into returning blocks");
1235 MachineFrameInfo *MFI = MF.getFrameInfo();
1236 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1237 bool isThumb = AFI->isThumbFunction();
1238 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1239 int NumBytes = (int)MFI->getStackSize();
1240 if (!AFI->hasStackFrame()) {
1242 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1244 // Unwind MBBI to point to first LDR / FLDD.
1245 const unsigned *CSRegs = getCalleeSavedRegs();
1246 if (MBBI != MBB.begin()) {
1249 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1250 if (!isCSRestore(MBBI, CSRegs))
1254 // Move SP to start of FP callee save spill area.
1255 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1256 AFI->getGPRCalleeSavedArea2Size() +
1257 AFI->getDPRCalleeSavedAreaSize());
1260 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1261 // Reset SP based on frame pointer only if the stack frame extends beyond
1262 // frame pointer stack slot or target is ELF and the function has FP.
1264 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes, TII);
1266 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::SP).addReg(FramePtr);
1268 if (MBBI->getOpcode() == ARM::tBX_RET &&
1269 &MBB.front() != MBBI &&
1270 prior(MBBI)->getOpcode() == ARM::tPOP) {
1271 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1272 emitSPUpdate(MBB, PMBBI, NumBytes, isThumb, TII);
1274 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1277 // Darwin ABI requires FP to point to the stack slot that contains the
1279 if (STI.isTargetDarwin() || hasFP(MF)) {
1280 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1281 // Reset SP based on frame pointer only if the stack frame extends beyond
1282 // frame pointer stack slot or target is ELF and the function has FP.
1283 if (AFI->getGPRCalleeSavedArea2Size() ||
1284 AFI->getDPRCalleeSavedAreaSize() ||
1285 AFI->getDPRCalleeSavedAreaOffset()||
1288 BuildMI(MBB, MBBI, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1291 BuildMI(MBB, MBBI, TII.get(ARM::MOVrr), ARM::SP).addReg(FramePtr);
1292 } else if (NumBytes) {
1293 emitSPUpdate(MBB, MBBI, NumBytes, false, TII);
1296 // Move SP to start of integer callee save spill area 2.
1297 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1298 emitSPUpdate(MBB, MBBI, AFI->getDPRCalleeSavedAreaSize(), false, TII);
1300 // Move SP to start of integer callee save spill area 1.
1301 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1302 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea2Size(), false, TII);
1304 // Move SP to SP upon entry to the function.
1305 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1306 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea1Size(), false, TII);
1310 if (VARegSaveSize) {
1312 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1313 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1314 BuildMI(MBB, MBBI, TII.get(ARM::tPOP)).addReg(ARM::R3);
1316 emitSPUpdate(MBB, MBBI, VARegSaveSize, isThumb, TII);
1319 BuildMI(MBB, MBBI, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1325 unsigned ARMRegisterInfo::getRARegister() const {
1329 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1330 return STI.useThumbBacktraces() ? ARM::R7 : ARM::R11;
1333 unsigned ARMRegisterInfo::getEHExceptionRegister() const {
1334 assert(0 && "What is the exception register");
1338 unsigned ARMRegisterInfo::getEHHandlerRegister() const {
1339 assert(0 && "What is the exception handler register");
1343 #include "ARMGenRegisterInfo.inc"