1 //===- ARMRegisterInfo.cpp - ARM Register Information -----------*- C++ -*-===//
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 ARM implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMInstrInfo.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMRegisterInfo.h"
19 #include "ARMSubtarget.h"
20 #include "llvm/Constants.h"
21 #include "llvm/DerivedTypes.h"
22 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineFrameInfo.h"
24 #include "llvm/CodeGen/MachineFunction.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineLocation.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.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> ThumbRegScavenging("enable-thumb-reg-scavenging",
41 cl::desc("Enable register scavenging on Thumb"));
43 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
46 case R0: case S0: case D0: return 0;
47 case R1: case S1: case D1: return 1;
48 case R2: case S2: case D2: return 2;
49 case R3: case S3: case D3: return 3;
50 case R4: case S4: case D4: return 4;
51 case R5: case S5: case D5: return 5;
52 case R6: case S6: case D6: return 6;
53 case R7: case S7: case D7: return 7;
54 case R8: case S8: case D8: return 8;
55 case R9: case S9: case D9: return 9;
56 case R10: case S10: case D10: return 10;
57 case R11: case S11: case D11: return 11;
58 case R12: case S12: case D12: return 12;
59 case SP: case S13: case D13: return 13;
60 case LR: case S14: case D14: return 14;
61 case PC: case S15: case D15: return 15;
79 assert(0 && "Unknown ARM register!");
84 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum,
91 assert(0 && "Unknown ARM register!");
93 case R0: case D0: return 0;
94 case R1: case D1: return 1;
95 case R2: case D2: return 2;
96 case R3: case D3: return 3;
97 case R4: case D4: return 4;
98 case R5: case D5: return 5;
99 case R6: case D6: return 6;
100 case R7: case D7: return 7;
101 case R8: case D8: return 8;
102 case R9: case D9: return 9;
103 case R10: case D10: return 10;
104 case R11: case D11: return 11;
105 case R12: case D12: return 12;
106 case SP: case D13: return 13;
107 case LR: case D14: return 14;
108 case PC: case D15: return 15;
110 case S0: case S1: case S2: case S3:
111 case S4: case S5: case S6: case S7:
112 case S8: case S9: case S10: case S11:
113 case S12: case S13: case S14: case S15:
114 case S16: case S17: case S18: case S19:
115 case S20: case S21: case S22: case S23:
116 case S24: case S25: case S26: case S27:
117 case S28: case S29: case S30: case S31: {
120 default: return 0; // Avoid compile time warning.
158 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii,
159 const ARMSubtarget &sti)
160 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
162 FramePtr((STI.useThumbBacktraces() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
166 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
167 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
171 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
172 return MIB.addReg(0);
175 /// emitLoadConstPool - Emits a load from constpool to materialize the
176 /// specified immediate.
177 void ARMRegisterInfo::emitLoadConstPool(MachineBasicBlock &MBB,
178 MachineBasicBlock::iterator &MBBI,
179 unsigned DestReg, int Val,
180 unsigned Pred, unsigned PredReg,
181 const TargetInstrInfo *TII,
184 MachineFunction &MF = *MBB.getParent();
185 MachineConstantPool *ConstantPool = MF.getConstantPool();
186 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
187 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
189 BuildMI(MBB, MBBI, dl,
190 TII->get(ARM::tLDRcp),DestReg).addConstantPoolIndex(Idx);
192 BuildMI(MBB, MBBI, dl, TII->get(ARM::LDRcp), DestReg)
193 .addConstantPoolIndex(Idx)
194 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
197 const TargetRegisterClass *ARMRegisterInfo::getPointerRegClass() const {
198 return &ARM::GPRRegClass;
201 /// isLowRegister - Returns true if the register is low register r0-r7.
203 bool ARMRegisterInfo::isLowRegister(unsigned Reg) const {
206 case R0: case R1: case R2: case R3:
207 case R4: case R5: case R6: case R7:
214 const TargetRegisterClass*
215 ARMRegisterInfo::getPhysicalRegisterRegClass(unsigned Reg, MVT VT) const {
217 if (isLowRegister(Reg))
218 return ARM::tGPRRegisterClass;
222 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
223 case ARM::R12: case ARM::SP: case ARM::LR: case ARM::PC:
224 return ARM::GPRRegisterClass;
227 return TargetRegisterInfo::getPhysicalRegisterRegClass(Reg, VT);
231 ARMRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
232 static const unsigned CalleeSavedRegs[] = {
233 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
234 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
236 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
237 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
241 static const unsigned DarwinCalleeSavedRegs[] = {
242 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
243 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
245 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
246 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
249 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
252 const TargetRegisterClass* const *
253 ARMRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
254 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
255 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
256 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
257 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
259 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
260 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
263 static const TargetRegisterClass * const ThumbCalleeSavedRegClasses[] = {
264 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
265 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::tGPRRegClass,
266 &ARM::tGPRRegClass,&ARM::tGPRRegClass,&ARM::tGPRRegClass,
268 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
269 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
272 return STI.isThumb() ? ThumbCalleeSavedRegClasses : CalleeSavedRegClasses;
275 BitVector ARMRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
276 // FIXME: avoid re-calculating this everytime.
277 BitVector Reserved(getNumRegs());
278 Reserved.set(ARM::SP);
279 Reserved.set(ARM::PC);
280 if (STI.isTargetDarwin() || hasFP(MF))
281 Reserved.set(FramePtr);
282 // Some targets reserve R9.
283 if (STI.isR9Reserved())
284 Reserved.set(ARM::R9);
289 ARMRegisterInfo::isReservedReg(const MachineFunction &MF, unsigned Reg) const {
297 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
301 return STI.isR9Reserved();
308 ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
309 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
310 return ThumbRegScavenging || !AFI->isThumbFunction();
313 /// hasFP - Return true if the specified function should have a dedicated frame
314 /// pointer register. This is true if the function has variable sized allocas
315 /// or if frame pointer elimination is disabled.
317 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
318 const MachineFrameInfo *MFI = MF.getFrameInfo();
319 return NoFramePointerElim || MFI->hasVarSizedObjects();
322 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
323 // not required, we reserve argument space for call sites in the function
324 // immediately on entry to the current function. This eliminates the need for
325 // add/sub sp brackets around call sites. Returns true if the call frame is
326 // included as part of the stack frame.
327 bool ARMRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
328 const MachineFrameInfo *FFI = MF.getFrameInfo();
329 unsigned CFSize = FFI->getMaxCallFrameSize();
330 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
331 // It's not always a good idea to include the call frame as part of the
332 // stack frame. ARM (especially Thumb) has small immediate offset to
333 // address the stack frame. So a large call frame can cause poor codegen
334 // and may even makes it impossible to scavenge a register.
335 if (AFI->isThumbFunction()) {
336 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
339 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
342 return !MF.getFrameInfo()->hasVarSizedObjects();
345 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
346 /// a destreg = basereg + immediate in ARM code.
348 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
349 MachineBasicBlock::iterator &MBBI,
350 unsigned DestReg, unsigned BaseReg, int NumBytes,
351 ARMCC::CondCodes Pred, unsigned PredReg,
352 const TargetInstrInfo &TII,
354 bool isSub = NumBytes < 0;
355 if (isSub) NumBytes = -NumBytes;
358 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
359 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
360 assert(ThisVal && "Didn't extract field correctly");
362 // We will handle these bits from offset, clear them.
363 NumBytes &= ~ThisVal;
365 // Get the properly encoded SOImmVal field.
366 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
367 assert(SOImmVal != -1 && "Bit extraction didn't work?");
369 // Build the new ADD / SUB.
370 BuildMI(MBB, MBBI, dl, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
371 .addReg(BaseReg, RegState::Kill).addImm(SOImmVal)
372 .addImm((unsigned)Pred).addReg(PredReg).addReg(0);
377 /// calcNumMI - Returns the number of instructions required to materialize
378 /// the specific add / sub r, c instruction.
379 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
380 unsigned NumBits, unsigned Scale) {
382 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
384 if (Opc == ARM::tADDrSPi) {
385 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
389 Scale = 1; // Followed by a number of tADDi8.
390 Chunk = ((1 << NumBits) - 1) * Scale;
393 NumMIs += Bytes / Chunk;
394 if ((Bytes % Chunk) != 0)
401 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
402 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
403 /// in a register using mov / mvn sequences or load the immediate from a
406 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
407 MachineBasicBlock::iterator &MBBI,
408 unsigned DestReg, unsigned BaseReg,
409 int NumBytes, bool CanChangeCC,
410 const TargetInstrInfo &TII,
411 const ARMRegisterInfo& MRI,
413 bool isHigh = !MRI.isLowRegister(DestReg) ||
414 (BaseReg != 0 && !MRI.isLowRegister(BaseReg));
416 // Subtract doesn't have high register version. Load the negative value
417 // if either base or dest register is a high register. Also, if do not
418 // issue sub as part of the sequence if condition register is to be
420 if (NumBytes < 0 && !isHigh && CanChangeCC) {
422 NumBytes = -NumBytes;
424 unsigned LdReg = DestReg;
425 if (DestReg == ARM::SP) {
426 assert(BaseReg == ARM::SP && "Unexpected!");
428 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVlor2hir), ARM::R12)
429 .addReg(ARM::R3, RegState::Kill);
432 if (NumBytes <= 255 && NumBytes >= 0)
433 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
434 else if (NumBytes < 0 && NumBytes >= -255) {
435 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
436 BuildMI(MBB, MBBI, dl, TII.get(ARM::tNEG), LdReg)
437 .addReg(LdReg, RegState::Kill);
439 MRI.emitLoadConstPool(MBB, MBBI, LdReg, NumBytes, ARMCC::AL, 0, &TII,
443 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
444 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl,
445 TII.get(Opc), DestReg);
446 if (DestReg == ARM::SP || isSub)
447 MIB.addReg(BaseReg).addReg(LdReg, RegState::Kill);
449 MIB.addReg(LdReg).addReg(BaseReg, RegState::Kill);
450 if (DestReg == ARM::SP)
451 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVhir2lor), ARM::R3)
452 .addReg(ARM::R12, RegState::Kill);
455 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
456 /// a destreg = basereg + immediate in Thumb code.
458 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
459 MachineBasicBlock::iterator &MBBI,
460 unsigned DestReg, unsigned BaseReg,
461 int NumBytes, const TargetInstrInfo &TII,
462 const ARMRegisterInfo& MRI,
464 bool isSub = NumBytes < 0;
465 unsigned Bytes = (unsigned)NumBytes;
466 if (isSub) Bytes = -NumBytes;
467 bool isMul4 = (Bytes & 3) == 0;
468 bool isTwoAddr = false;
469 bool DstNotEqBase = false;
470 unsigned NumBits = 1;
475 if (DestReg == BaseReg && BaseReg == ARM::SP) {
476 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
479 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
481 } else if (!isSub && BaseReg == ARM::SP) {
484 // r1 = add sp, 100 * 4
488 ExtraOpc = ARM::tADDi3;
497 if (DestReg != BaseReg)
500 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
504 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
505 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
506 if (NumMIs > Threshold) {
507 // This will expand into too many instructions. Load the immediate from a
509 emitThumbRegPlusImmInReg(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII,
515 if (MRI.isLowRegister(DestReg) && MRI.isLowRegister(BaseReg)) {
516 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
517 unsigned Chunk = (1 << 3) - 1;
518 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
520 BuildMI(MBB, MBBI, dl,TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
521 .addReg(BaseReg, RegState::Kill).addImm(ThisVal);
523 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
524 .addReg(BaseReg, RegState::Kill);
529 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
531 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
534 // Build the new tADD / tSUB.
536 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
537 .addReg(DestReg).addImm(ThisVal);
539 bool isKill = BaseReg != ARM::SP;
540 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg)
541 .addReg(BaseReg, getKillRegState(isKill)).addImm(ThisVal);
544 if (Opc == ARM::tADDrSPi) {
550 Chunk = ((1 << NumBits) - 1) * Scale;
551 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
558 BuildMI(MBB, MBBI, dl, TII.get(ExtraOpc), DestReg)
559 .addReg(DestReg, RegState::Kill)
560 .addImm(((unsigned)NumBytes) & 3);
564 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
565 int NumBytes, ARMCC::CondCodes Pred, unsigned PredReg,
566 bool isThumb, const TargetInstrInfo &TII,
567 const ARMRegisterInfo& MRI,
570 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII,
573 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes,
574 Pred, PredReg, TII, dl);
577 void ARMRegisterInfo::
578 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
579 MachineBasicBlock::iterator I) const {
580 if (!hasReservedCallFrame(MF)) {
581 // If we have alloca, convert as follows:
582 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
583 // ADJCALLSTACKUP -> add, sp, sp, amount
584 MachineInstr *Old = I;
585 DebugLoc dl = Old->getDebugLoc();
586 unsigned Amount = Old->getOperand(0).getImm();
588 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
589 // We need to keep the stack aligned properly. To do this, we round the
590 // amount of space needed for the outgoing arguments up to the next
591 // alignment boundary.
592 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
593 Amount = (Amount+Align-1)/Align*Align;
595 // Replace the pseudo instruction with a new instruction...
596 unsigned Opc = Old->getOpcode();
597 bool isThumb = AFI->isThumbFunction();
598 ARMCC::CondCodes Pred = isThumb
599 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(1).getImm();
600 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
601 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
602 unsigned PredReg = isThumb ? 0 : Old->getOperand(2).getReg();
603 emitSPUpdate(MBB, I, -Amount, Pred, PredReg, isThumb, TII, *this, dl);
605 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
606 unsigned PredReg = isThumb ? 0 : Old->getOperand(3).getReg();
607 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
608 emitSPUpdate(MBB, I, Amount, Pred, PredReg, isThumb, TII, *this, dl);
615 /// emitThumbConstant - Emit a series of instructions to materialize a
617 static void emitThumbConstant(MachineBasicBlock &MBB,
618 MachineBasicBlock::iterator &MBBI,
619 unsigned DestReg, int Imm,
620 const TargetInstrInfo &TII,
621 const ARMRegisterInfo& MRI,
623 bool isSub = Imm < 0;
624 if (isSub) Imm = -Imm;
626 int Chunk = (1 << 8) - 1;
627 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
629 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), DestReg).addImm(ThisVal);
631 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII, MRI, dl);
633 BuildMI(MBB, MBBI, dl, TII.get(ARM::tNEG), DestReg)
634 .addReg(DestReg, RegState::Kill);
637 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
638 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
639 /// register first and then a spilled callee-saved register if that fails.
641 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
642 ARMFunctionInfo *AFI) {
643 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
644 assert (!AFI->isThumbFunction());
646 // Try a already spilled CS register.
647 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
652 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
653 int SPAdj, RegScavenger *RS) const{
655 MachineInstr &MI = *II;
656 MachineBasicBlock &MBB = *MI.getParent();
657 MachineFunction &MF = *MBB.getParent();
658 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
659 bool isThumb = AFI->isThumbFunction();
660 DebugLoc dl = MI.getDebugLoc();
662 while (!MI.getOperand(i).isFI()) {
664 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
667 unsigned FrameReg = ARM::SP;
668 int FrameIndex = MI.getOperand(i).getIndex();
669 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
670 MF.getFrameInfo()->getStackSize() + SPAdj;
672 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
673 Offset -= AFI->getGPRCalleeSavedArea1Offset();
674 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
675 Offset -= AFI->getGPRCalleeSavedArea2Offset();
676 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
677 Offset -= AFI->getDPRCalleeSavedAreaOffset();
678 else if (hasFP(MF)) {
679 assert(SPAdj == 0 && "Unexpected");
680 // There is alloca()'s in this function, must reference off the frame
682 FrameReg = getFrameRegister(MF);
683 Offset -= AFI->getFramePtrSpillOffset();
686 unsigned Opcode = MI.getOpcode();
687 const TargetInstrDesc &Desc = MI.getDesc();
688 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
691 // Memory operands in inline assembly always use AddrMode2.
692 if (Opcode == ARM::INLINEASM)
693 AddrMode = ARMII::AddrMode2;
695 if (Opcode == ARM::ADDri) {
696 Offset += MI.getOperand(i+1).getImm();
698 // Turn it into a move.
699 MI.setDesc(TII.get(ARM::MOVr));
700 MI.getOperand(i).ChangeToRegister(FrameReg, false);
701 MI.RemoveOperand(i+1);
703 } else if (Offset < 0) {
706 MI.setDesc(TII.get(ARM::SUBri));
709 // Common case: small offset, fits into instruction.
710 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
711 if (ImmedOffset != -1) {
712 // Replace the FrameIndex with sp / fp
713 MI.getOperand(i).ChangeToRegister(FrameReg, false);
714 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
718 // Otherwise, we fallback to common code below to form the imm offset with
719 // a sequence of ADDri instructions. First though, pull as much of the imm
720 // into this ADDri as possible.
721 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
722 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
724 // We will handle these bits from offset, clear them.
725 Offset &= ~ThisImmVal;
727 // Get the properly encoded SOImmVal field.
728 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
729 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
730 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
731 } else if (Opcode == ARM::tADDrSPi) {
732 Offset += MI.getOperand(i+1).getImm();
734 // Can't use tADDrSPi if it's based off the frame pointer.
735 unsigned NumBits = 0;
737 if (FrameReg != ARM::SP) {
738 Opcode = ARM::tADDi3;
739 MI.setDesc(TII.get(ARM::tADDi3));
744 assert((Offset & 3) == 0 &&
745 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
749 // Turn it into a move.
750 MI.setDesc(TII.get(ARM::tMOVhir2lor));
751 MI.getOperand(i).ChangeToRegister(FrameReg, false);
752 MI.RemoveOperand(i+1);
756 // Common case: small offset, fits into instruction.
757 unsigned Mask = (1 << NumBits) - 1;
758 if (((Offset / Scale) & ~Mask) == 0) {
759 // Replace the FrameIndex with sp / fp
760 MI.getOperand(i).ChangeToRegister(FrameReg, false);
761 MI.getOperand(i+1).ChangeToImmediate(Offset / Scale);
765 unsigned DestReg = MI.getOperand(0).getReg();
766 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
767 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, NumBits, Scale);
768 // MI would expand into a large number of instructions. Don't try to
769 // simplify the immediate.
771 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII,
778 // Translate r0 = add sp, imm to
779 // r0 = add sp, 255*4
780 // r0 = add r0, (imm - 255*4)
781 MI.getOperand(i).ChangeToRegister(FrameReg, false);
782 MI.getOperand(i+1).ChangeToImmediate(Mask);
783 Offset = (Offset - Mask * Scale);
784 MachineBasicBlock::iterator NII = next(II);
785 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII,
788 // Translate r0 = add sp, -imm to
789 // r0 = -imm (this is then translated into a series of instructons)
791 emitThumbConstant(MBB, II, DestReg, Offset, TII, *this, dl);
792 MI.setDesc(TII.get(ARM::tADDhirr));
793 MI.getOperand(i).ChangeToRegister(DestReg, false, false, true);
794 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
800 unsigned NumBits = 0;
803 case ARMII::AddrMode2: {
805 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
806 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
811 case ARMII::AddrMode3: {
813 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
814 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
819 case ARMII::AddrMode5: {
821 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
822 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
828 case ARMII::AddrModeTs: {
830 InstrOffs = MI.getOperand(ImmIdx).getImm();
831 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
836 assert(0 && "Unsupported addressing mode!");
841 Offset += InstrOffs * Scale;
842 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
843 if (Offset < 0 && !isThumb) {
848 // Common case: small offset, fits into instruction.
849 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
850 int ImmedOffset = Offset / Scale;
851 unsigned Mask = (1 << NumBits) - 1;
852 if ((unsigned)Offset <= Mask * Scale) {
853 // Replace the FrameIndex with sp
854 MI.getOperand(i).ChangeToRegister(FrameReg, false);
856 ImmedOffset |= 1 << NumBits;
857 ImmOp.ChangeToImmediate(ImmedOffset);
861 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
862 if (AddrMode == ARMII::AddrModeTs) {
863 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
864 // a different base register.
866 Mask = (1 << NumBits) - 1;
868 // If this is a thumb spill / restore, we will be using a constpool load to
869 // materialize the offset.
870 if (AddrMode == ARMII::AddrModeTs && isThumSpillRestore)
871 ImmOp.ChangeToImmediate(0);
873 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
874 ImmedOffset = ImmedOffset & Mask;
876 ImmedOffset |= 1 << NumBits;
877 ImmOp.ChangeToImmediate(ImmedOffset);
878 Offset &= ~(Mask*Scale);
882 // If we get here, the immediate doesn't fit into the instruction. We folded
883 // as much as possible above, handle the rest, providing a register that is
885 assert(Offset && "This code isn't needed if offset already handled!");
888 if (Desc.mayLoad()) {
889 // Use the destination register to materialize sp + offset.
890 unsigned TmpReg = MI.getOperand(0).getReg();
892 if (Opcode == ARM::tRestore) {
893 if (FrameReg == ARM::SP)
894 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,
895 Offset, false, TII, *this, dl);
897 emitLoadConstPool(MBB, II, TmpReg, Offset, ARMCC::AL, 0, &TII,
902 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII,
904 MI.setDesc(TII.get(ARM::tLDR));
905 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
907 // Use [reg, reg] addrmode.
908 MI.addOperand(MachineOperand::CreateReg(FrameReg, false));
909 else // tLDR has an extra register operand.
910 MI.addOperand(MachineOperand::CreateReg(0, false));
911 } else if (Desc.mayStore()) {
912 // FIXME! This is horrific!!! We need register scavenging.
913 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
914 // also a ABI register so it's possible that is is the register that is
915 // being storing here. If that's the case, we do the following:
917 // Use r2 to materialize sp + offset
920 unsigned ValReg = MI.getOperand(0).getReg();
921 unsigned TmpReg = ARM::R3;
923 if (ValReg == ARM::R3) {
924 BuildMI(MBB, II, dl, TII.get(ARM::tMOVlor2hir), ARM::R12)
925 .addReg(ARM::R2, RegState::Kill);
928 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
929 BuildMI(MBB, II, dl, TII.get(ARM::tMOVlor2hir), ARM::R12)
930 .addReg(ARM::R3, RegState::Kill);
931 if (Opcode == ARM::tSpill) {
932 if (FrameReg == ARM::SP)
933 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,
934 Offset, false, TII, *this, dl);
936 emitLoadConstPool(MBB, II, TmpReg, Offset, ARMCC::AL, 0, &TII,
941 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII,
943 MI.setDesc(TII.get(ARM::tSTR));
944 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
945 if (UseRR) // Use [reg, reg] addrmode.
946 MI.addOperand(MachineOperand::CreateReg(FrameReg, false));
947 else // tSTR has an extra register operand.
948 MI.addOperand(MachineOperand::CreateReg(0, false));
950 MachineBasicBlock::iterator NII = next(II);
951 if (ValReg == ARM::R3)
952 BuildMI(MBB, NII, dl, TII.get(ARM::tMOVhir2lor), ARM::R2)
953 .addReg(ARM::R12, RegState::Kill);
954 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
955 BuildMI(MBB, NII, dl, TII.get(ARM::tMOVhir2lor), ARM::R3)
956 .addReg(ARM::R12, RegState::Kill);
958 assert(false && "Unexpected opcode!");
960 // Insert a set of r12 with the full address: r12 = sp + offset
961 // If the offset we have is too large to fit into the instruction, we need
962 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
964 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
966 // No register is "free". Scavenge a register.
967 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II, SPAdj);
968 int PIdx = MI.findFirstPredOperandIdx();
969 ARMCC::CondCodes Pred = (PIdx == -1)
970 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
971 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
972 emitARMRegPlusImmediate(MBB, II, ScratchReg, FrameReg,
973 isSub ? -Offset : Offset, Pred, PredReg, TII, dl);
974 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
978 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
979 const MachineFrameInfo *FFI = MF.getFrameInfo();
981 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
982 int FixedOff = -FFI->getObjectOffset(i);
983 if (FixedOff > Offset) Offset = FixedOff;
985 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
986 if (FFI->isDeadObjectIndex(i))
988 Offset += FFI->getObjectSize(i);
989 unsigned Align = FFI->getObjectAlignment(i);
990 // Adjust to alignment boundary
991 Offset = (Offset+Align-1)/Align*Align;
993 return (unsigned)Offset;
997 ARMRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
998 RegScavenger *RS) const {
999 // This tells PEI to spill the FP as if it is any other callee-save register
1000 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1001 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1002 // to combine multiple loads / stores.
1003 bool CanEliminateFrame = true;
1004 bool CS1Spilled = false;
1005 bool LRSpilled = false;
1006 unsigned NumGPRSpills = 0;
1007 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1008 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1009 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1011 // Don't spill FP if the frame can be eliminated. This is determined
1012 // by scanning the callee-save registers to see if any is used.
1013 const unsigned *CSRegs = getCalleeSavedRegs();
1014 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
1015 for (unsigned i = 0; CSRegs[i]; ++i) {
1016 unsigned Reg = CSRegs[i];
1017 bool Spilled = false;
1018 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
1019 AFI->setCSRegisterIsSpilled(Reg);
1021 CanEliminateFrame = false;
1023 // Check alias registers too.
1024 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
1025 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
1027 CanEliminateFrame = false;
1032 if (CSRegClasses[i] == &ARM::GPRRegClass) {
1036 if (!STI.isTargetDarwin()) {
1043 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1058 if (!STI.isTargetDarwin()) {
1059 UnspilledCS1GPRs.push_back(Reg);
1069 UnspilledCS1GPRs.push_back(Reg);
1072 UnspilledCS2GPRs.push_back(Reg);
1079 bool ForceLRSpill = false;
1080 if (!LRSpilled && AFI->isThumbFunction()) {
1081 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
1082 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1083 // use of BL to implement far jump. If it turns out that it's not needed
1084 // then the branch fix up path will undo it.
1085 if (FnSize >= (1 << 11)) {
1086 CanEliminateFrame = false;
1087 ForceLRSpill = true;
1091 bool ExtraCSSpill = false;
1092 if (!CanEliminateFrame || hasFP(MF)) {
1093 AFI->setHasStackFrame(true);
1095 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1096 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1097 if (!LRSpilled && CS1Spilled) {
1098 MF.getRegInfo().setPhysRegUsed(ARM::LR);
1099 AFI->setCSRegisterIsSpilled(ARM::LR);
1101 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1102 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1103 ForceLRSpill = false;
1104 ExtraCSSpill = true;
1107 // Darwin ABI requires FP to point to the stack slot that contains the
1109 if (STI.isTargetDarwin() || hasFP(MF)) {
1110 MF.getRegInfo().setPhysRegUsed(FramePtr);
1114 // If stack and double are 8-byte aligned and we are spilling an odd number
1115 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
1116 // the integer and double callee save areas.
1117 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1118 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1119 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1120 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1121 unsigned Reg = UnspilledCS1GPRs[i];
1122 // Don't spiil high register if the function is thumb
1123 if (!AFI->isThumbFunction() || isLowRegister(Reg) || Reg == ARM::LR) {
1124 MF.getRegInfo().setPhysRegUsed(Reg);
1125 AFI->setCSRegisterIsSpilled(Reg);
1126 if (!isReservedReg(MF, Reg))
1127 ExtraCSSpill = true;
1131 } else if (!UnspilledCS2GPRs.empty() &&
1132 !AFI->isThumbFunction()) {
1133 unsigned Reg = UnspilledCS2GPRs.front();
1134 MF.getRegInfo().setPhysRegUsed(Reg);
1135 AFI->setCSRegisterIsSpilled(Reg);
1136 if (!isReservedReg(MF, Reg))
1137 ExtraCSSpill = true;
1141 // Estimate if we might need to scavenge a register at some point in order
1142 // to materialize a stack offset. If so, either spill one additiona
1143 // callee-saved register or reserve a special spill slot to facilitate
1144 // register scavenging.
1145 if (RS && !ExtraCSSpill && !AFI->isThumbFunction()) {
1146 MachineFrameInfo *MFI = MF.getFrameInfo();
1147 unsigned Size = estimateStackSize(MF, MFI);
1148 unsigned Limit = (1 << 12) - 1;
1149 for (MachineFunction::iterator BB = MF.begin(),E = MF.end();BB != E; ++BB)
1150 for (MachineBasicBlock::iterator I= BB->begin(); I != BB->end(); ++I) {
1151 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1152 if (I->getOperand(i).isFI()) {
1153 unsigned Opcode = I->getOpcode();
1154 const TargetInstrDesc &Desc = TII.get(Opcode);
1155 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1156 if (AddrMode == ARMII::AddrMode3) {
1157 Limit = (1 << 8) - 1;
1158 goto DoneEstimating;
1159 } else if (AddrMode == ARMII::AddrMode5) {
1160 unsigned ThisLimit = ((1 << 8) - 1) * 4;
1161 if (ThisLimit < Limit)
1167 if (Size >= Limit) {
1168 // If any non-reserved CS register isn't spilled, just spill one or two
1169 // extra. That should take care of it!
1170 unsigned NumExtras = TargetAlign / 4;
1171 SmallVector<unsigned, 2> Extras;
1172 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1173 unsigned Reg = UnspilledCS1GPRs.back();
1174 UnspilledCS1GPRs.pop_back();
1175 if (!isReservedReg(MF, Reg)) {
1176 Extras.push_back(Reg);
1180 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1181 unsigned Reg = UnspilledCS2GPRs.back();
1182 UnspilledCS2GPRs.pop_back();
1183 if (!isReservedReg(MF, Reg)) {
1184 Extras.push_back(Reg);
1188 if (Extras.size() && NumExtras == 0) {
1189 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1190 MF.getRegInfo().setPhysRegUsed(Extras[i]);
1191 AFI->setCSRegisterIsSpilled(Extras[i]);
1194 // Reserve a slot closest to SP or frame pointer.
1195 const TargetRegisterClass *RC = &ARM::GPRRegClass;
1196 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1197 RC->getAlignment()));
1204 MF.getRegInfo().setPhysRegUsed(ARM::LR);
1205 AFI->setCSRegisterIsSpilled(ARM::LR);
1206 AFI->setLRIsSpilledForFarJump(true);
1210 /// Move iterator pass the next bunch of callee save load / store ops for
1211 /// the particular spill area (1: integer area 1, 2: integer area 2,
1212 /// 3: fp area, 0: don't care).
1213 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1214 MachineBasicBlock::iterator &MBBI,
1215 int Opc, unsigned Area,
1216 const ARMSubtarget &STI) {
1217 while (MBBI != MBB.end() &&
1218 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFI()) {
1221 unsigned Category = 0;
1222 switch (MBBI->getOperand(0).getReg()) {
1223 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1227 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1228 Category = STI.isTargetDarwin() ? 2 : 1;
1230 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1231 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1238 if (Done || Category != Area)
1246 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
1247 MachineBasicBlock &MBB = MF.front();
1248 MachineBasicBlock::iterator MBBI = MBB.begin();
1249 MachineFrameInfo *MFI = MF.getFrameInfo();
1250 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1251 bool isThumb = AFI->isThumbFunction();
1252 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1253 unsigned NumBytes = MFI->getStackSize();
1254 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1255 DebugLoc dl = (MBBI != MBB.end() ?
1256 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1259 // Check if R3 is live in. It might have to be used as a scratch register.
1260 for (MachineRegisterInfo::livein_iterator I =MF.getRegInfo().livein_begin(),
1261 E = MF.getRegInfo().livein_end(); I != E; ++I) {
1262 if (I->first == ARM::R3) {
1263 AFI->setR3IsLiveIn(true);
1268 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1269 NumBytes = (NumBytes + 3) & ~3;
1270 MFI->setStackSize(NumBytes);
1273 // Determine the sizes of each callee-save spill areas and record which frame
1274 // belongs to which callee-save spill areas.
1275 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1276 int FramePtrSpillFI = 0;
1279 emitSPUpdate(MBB, MBBI, -VARegSaveSize, ARMCC::AL, 0, isThumb, TII,
1282 if (!AFI->hasStackFrame()) {
1284 emitSPUpdate(MBB, MBBI, -NumBytes, ARMCC::AL, 0, isThumb, TII, *this, dl);
1288 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1289 unsigned Reg = CSI[i].getReg();
1290 int FI = CSI[i].getFrameIdx();
1297 if (Reg == FramePtr)
1298 FramePtrSpillFI = FI;
1299 AFI->addGPRCalleeSavedArea1Frame(FI);
1306 if (Reg == FramePtr)
1307 FramePtrSpillFI = FI;
1308 if (STI.isTargetDarwin()) {
1309 AFI->addGPRCalleeSavedArea2Frame(FI);
1312 AFI->addGPRCalleeSavedArea1Frame(FI);
1317 AFI->addDPRCalleeSavedAreaFrame(FI);
1323 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1324 emitSPUpdate(MBB, MBBI, -GPRCS1Size, ARMCC::AL, 0, isThumb, TII, *this, dl);
1325 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1326 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
1328 if (MBBI != MBB.end())
1329 dl = MBBI->getDebugLoc();
1332 // Darwin ABI requires FP to point to the stack slot that contains the
1334 if (STI.isTargetDarwin() || hasFP(MF)) {
1335 MachineInstrBuilder MIB =
1336 BuildMI(MBB, MBBI, dl, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri),
1338 .addFrameIndex(FramePtrSpillFI).addImm(0);
1339 if (!isThumb) AddDefaultCC(AddDefaultPred(MIB));
1343 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1344 emitSPUpdate(MBB, MBBI, -GPRCS2Size, ARMCC::AL, 0, false, TII, *this, dl);
1346 // Build the new SUBri to adjust SP for FP callee-save spill area.
1347 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1348 emitSPUpdate(MBB, MBBI, -DPRCSSize, ARMCC::AL, 0, false, TII, *this, dl);
1351 // Determine starting offsets of spill areas.
1352 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1353 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1354 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1355 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1356 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1357 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1358 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1360 NumBytes = DPRCSOffset;
1362 // Insert it after all the callee-save spills.
1364 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1365 emitSPUpdate(MBB, MBBI, -NumBytes, ARMCC::AL, 0, isThumb, TII, *this, dl);
1368 if(STI.isTargetELF() && hasFP(MF)) {
1369 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1370 AFI->getFramePtrSpillOffset());
1373 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1374 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1375 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1378 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1379 for (unsigned i = 0; CSRegs[i]; ++i)
1380 if (Reg == CSRegs[i])
1385 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1386 return ((MI->getOpcode() == ARM::FLDD ||
1387 MI->getOpcode() == ARM::LDR ||
1388 MI->getOpcode() == ARM::tRestore) &&
1389 MI->getOperand(1).isFI() &&
1390 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1393 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1394 MachineBasicBlock &MBB) const {
1395 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1396 assert((MBBI->getOpcode() == ARM::BX_RET ||
1397 MBBI->getOpcode() == ARM::tBX_RET ||
1398 MBBI->getOpcode() == ARM::tPOP_RET) &&
1399 "Can only insert epilog into returning blocks");
1400 DebugLoc dl = MBBI->getDebugLoc();
1401 MachineFrameInfo *MFI = MF.getFrameInfo();
1402 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1403 bool isThumb = AFI->isThumbFunction();
1404 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1405 int NumBytes = (int)MFI->getStackSize();
1407 if (!AFI->hasStackFrame()) {
1409 emitSPUpdate(MBB, MBBI, NumBytes, ARMCC::AL, 0, isThumb, TII, *this, dl);
1411 // Unwind MBBI to point to first LDR / FLDD.
1412 const unsigned *CSRegs = getCalleeSavedRegs();
1413 if (MBBI != MBB.begin()) {
1416 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1417 if (!isCSRestore(MBBI, CSRegs))
1421 // Move SP to start of FP callee save spill area.
1422 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1423 AFI->getGPRCalleeSavedArea2Size() +
1424 AFI->getDPRCalleeSavedAreaSize());
1427 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1428 // Reset SP based on frame pointer only if the stack frame extends beyond
1429 // frame pointer stack slot or target is ELF and the function has FP.
1431 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes,
1434 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVlor2hir), ARM::SP)
1437 if (MBBI->getOpcode() == ARM::tBX_RET &&
1438 &MBB.front() != MBBI &&
1439 prior(MBBI)->getOpcode() == ARM::tPOP) {
1440 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1441 emitSPUpdate(MBB, PMBBI, NumBytes, ARMCC::AL, 0, isThumb, TII,
1444 emitSPUpdate(MBB, MBBI, NumBytes, ARMCC::AL, 0, isThumb, TII,
1448 // Darwin ABI requires FP to point to the stack slot that contains the
1450 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1451 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1452 // Reset SP based on frame pointer only if the stack frame extends beyond
1453 // frame pointer stack slot or target is ELF and the function has FP.
1454 if (AFI->getGPRCalleeSavedArea2Size() ||
1455 AFI->getDPRCalleeSavedAreaSize() ||
1456 AFI->getDPRCalleeSavedAreaOffset()||
1459 BuildMI(MBB, MBBI, dl, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1461 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1463 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP).addReg(FramePtr)
1464 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1466 } else if (NumBytes) {
1467 emitSPUpdate(MBB, MBBI, NumBytes, ARMCC::AL, 0, false, TII, *this, dl);
1470 // Move SP to start of integer callee save spill area 2.
1471 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1472 emitSPUpdate(MBB, MBBI, AFI->getDPRCalleeSavedAreaSize(), ARMCC::AL, 0,
1473 false, TII, *this, dl);
1475 // Move SP to start of integer callee save spill area 1.
1476 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1477 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea2Size(), ARMCC::AL, 0,
1478 false, TII, *this, dl);
1480 // Move SP to SP upon entry to the function.
1481 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1482 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea1Size(), ARMCC::AL, 0,
1483 false, TII, *this, dl);
1487 if (VARegSaveSize) {
1489 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1490 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1491 BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)).addReg(ARM::R3);
1493 emitSPUpdate(MBB, MBBI, VARegSaveSize, ARMCC::AL, 0, isThumb, TII,
1497 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1503 unsigned ARMRegisterInfo::getRARegister() const {
1507 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1508 if (STI.isTargetDarwin() || hasFP(MF))
1509 return (STI.useThumbBacktraces() || STI.isThumb()) ? ARM::R7 : ARM::R11;
1514 unsigned ARMRegisterInfo::getEHExceptionRegister() const {
1515 assert(0 && "What is the exception register");
1519 unsigned ARMRegisterInfo::getEHHandlerRegister() const {
1520 assert(0 && "What is the exception handler register");
1524 int ARMRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
1525 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
1528 #include "ARMGenRegisterInfo.inc"