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> 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 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii,
85 const ARMSubtarget &sti)
86 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
88 FramePtr(STI.useThumbBacktraces() ? ARM::R7 : ARM::R11) {
91 bool ARMRegisterInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
92 MachineBasicBlock::iterator MI,
93 const std::vector<CalleeSavedInfo> &CSI) const {
94 MachineFunction &MF = *MBB.getParent();
95 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
96 if (!AFI->isThumbFunction() || CSI.empty())
99 MachineInstrBuilder MIB = BuildMI(MBB, MI, TII.get(ARM::tPUSH));
100 for (unsigned i = CSI.size(); i != 0; --i) {
101 unsigned Reg = CSI[i-1].getReg();
102 // Add the callee-saved register as live-in. It's killed at the spill.
104 MIB.addReg(Reg, false/*isDef*/,false/*isImp*/,true/*isKill*/);
109 bool ARMRegisterInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
110 MachineBasicBlock::iterator MI,
111 const std::vector<CalleeSavedInfo> &CSI) const {
112 MachineFunction &MF = *MBB.getParent();
113 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
114 if (!AFI->isThumbFunction() || CSI.empty())
117 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
118 MachineInstr *PopMI = new MachineInstr(TII.get(ARM::tPOP));
119 MBB.insert(MI, PopMI);
120 for (unsigned i = CSI.size(); i != 0; --i) {
121 unsigned Reg = CSI[i-1].getReg();
122 if (Reg == ARM::LR) {
123 // Special epilogue for vararg functions. See emitEpilogue
127 PopMI->setInstrDescriptor(TII.get(ARM::tPOP_RET));
130 PopMI->addRegOperand(Reg, true);
135 void ARMRegisterInfo::
136 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
137 unsigned SrcReg, int FI,
138 const TargetRegisterClass *RC) const {
139 if (RC == ARM::GPRRegisterClass) {
140 MachineFunction &MF = *MBB.getParent();
141 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
142 if (AFI->isThumbFunction())
143 BuildMI(MBB, I, TII.get(ARM::tSpill)).addReg(SrcReg, false, false, true)
144 .addFrameIndex(FI).addImm(0);
146 BuildMI(MBB, I, TII.get(ARM::STR)).addReg(SrcReg, false, false, true)
147 .addFrameIndex(FI).addReg(0).addImm(0);
148 } else if (RC == ARM::DPRRegisterClass) {
149 BuildMI(MBB, I, TII.get(ARM::FSTD)).addReg(SrcReg, false, false, true)
150 .addFrameIndex(FI).addImm(0);
152 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
153 BuildMI(MBB, I, TII.get(ARM::FSTS)).addReg(SrcReg, false, false, true)
154 .addFrameIndex(FI).addImm(0);
158 void ARMRegisterInfo::
159 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
160 unsigned DestReg, int FI,
161 const TargetRegisterClass *RC) const {
162 if (RC == ARM::GPRRegisterClass) {
163 MachineFunction &MF = *MBB.getParent();
164 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
165 if (AFI->isThumbFunction())
166 BuildMI(MBB, I, TII.get(ARM::tRestore), DestReg)
167 .addFrameIndex(FI).addImm(0);
169 BuildMI(MBB, I, TII.get(ARM::LDR), DestReg)
170 .addFrameIndex(FI).addReg(0).addImm(0);
171 } else if (RC == ARM::DPRRegisterClass) {
172 BuildMI(MBB, I, TII.get(ARM::FLDD), DestReg)
173 .addFrameIndex(FI).addImm(0);
175 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
176 BuildMI(MBB, I, TII.get(ARM::FLDS), DestReg)
177 .addFrameIndex(FI).addImm(0);
181 void ARMRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
182 MachineBasicBlock::iterator I,
183 unsigned DestReg, unsigned SrcReg,
184 const TargetRegisterClass *RC) const {
185 if (RC == ARM::GPRRegisterClass) {
186 MachineFunction &MF = *MBB.getParent();
187 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
188 BuildMI(MBB, I, TII.get(AFI->isThumbFunction() ? ARM::tMOVr : ARM::MOVr),
189 DestReg).addReg(SrcReg);
190 } else if (RC == ARM::SPRRegisterClass)
191 BuildMI(MBB, I, TII.get(ARM::FCPYS), DestReg).addReg(SrcReg);
192 else if (RC == ARM::DPRRegisterClass)
193 BuildMI(MBB, I, TII.get(ARM::FCPYD), DestReg).addReg(SrcReg);
198 /// isLowRegister - Returns true if the register is low register r0-r7.
200 static bool isLowRegister(unsigned Reg) {
203 case R0: case R1: case R2: case R3:
204 case R4: case R5: case R6: case R7:
211 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr *MI,
212 unsigned OpNum, int FI) const {
213 unsigned Opc = MI->getOpcode();
214 MachineInstr *NewMI = NULL;
218 if (OpNum == 0) { // move -> store
219 unsigned SrcReg = MI->getOperand(1).getReg();
220 NewMI = BuildMI(TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
221 .addReg(0).addImm(0);
222 } else { // move -> load
223 unsigned DstReg = MI->getOperand(0).getReg();
224 NewMI = BuildMI(TII.get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
230 if (OpNum == 0) { // move -> store
231 unsigned SrcReg = MI->getOperand(1).getReg();
232 if (isPhysicalRegister(SrcReg) && !isLowRegister(SrcReg))
233 // tSpill cannot take a high register operand.
235 NewMI = BuildMI(TII.get(ARM::tSpill)).addReg(SrcReg).addFrameIndex(FI)
237 } else { // move -> load
238 unsigned DstReg = MI->getOperand(0).getReg();
239 if (isPhysicalRegister(DstReg) && !isLowRegister(DstReg))
240 // tRestore cannot target a high register operand.
242 NewMI = BuildMI(TII.get(ARM::tRestore), DstReg).addFrameIndex(FI)
248 if (OpNum == 0) { // move -> store
249 unsigned SrcReg = MI->getOperand(1).getReg();
250 NewMI = BuildMI(TII.get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
252 } else { // move -> load
253 unsigned DstReg = MI->getOperand(0).getReg();
254 NewMI = BuildMI(TII.get(ARM::FLDS), DstReg).addFrameIndex(FI).addImm(0);
259 if (OpNum == 0) { // move -> store
260 unsigned SrcReg = MI->getOperand(1).getReg();
261 NewMI = BuildMI(TII.get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
263 } else { // move -> load
264 unsigned DstReg = MI->getOperand(0).getReg();
265 NewMI = BuildMI(TII.get(ARM::FLDD), DstReg).addFrameIndex(FI).addImm(0);
272 NewMI->copyKillDeadInfo(MI);
276 const unsigned* ARMRegisterInfo::getCalleeSavedRegs() const {
277 static const unsigned CalleeSavedRegs[] = {
278 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
279 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
281 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
282 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
286 static const unsigned DarwinCalleeSavedRegs[] = {
287 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
288 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
290 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
291 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
294 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
297 const TargetRegisterClass* const *
298 ARMRegisterInfo::getCalleeSavedRegClasses() const {
299 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
300 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
301 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
302 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
304 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
305 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
308 return CalleeSavedRegClasses;
311 BitVector ARMRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
312 // FIXME: avoid re-calculating this everytime.
313 BitVector Reserved(getNumRegs());
314 Reserved.set(ARM::SP);
315 Reserved.set(ARM::PC);
316 if (STI.isTargetDarwin() || hasFP(MF))
317 Reserved.set(FramePtr);
318 // Some targets reserve R9.
319 if (STI.isR9Reserved())
320 Reserved.set(ARM::R9);
321 // At PEI time, if LR is used, it will be spilled upon entry.
322 if (MF.getUsedPhysregs() && !MF.isPhysRegUsed((unsigned)ARM::LR))
323 Reserved.set(ARM::LR);
328 ARMRegisterInfo::isReservedReg(const MachineFunction &MF, unsigned Reg) const {
336 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
340 return STI.isR9Reserved();
347 ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
348 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
349 return ThumbRegScavenging || !AFI->isThumbFunction();
352 /// hasFP - Return true if the specified function should have a dedicated frame
353 /// pointer register. This is true if the function has variable sized allocas
354 /// or if frame pointer elimination is disabled.
356 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
357 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
360 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
361 /// a destreg = basereg + immediate in ARM code.
363 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
364 MachineBasicBlock::iterator &MBBI,
365 unsigned DestReg, unsigned BaseReg,
366 int NumBytes, const TargetInstrInfo &TII) {
367 bool isSub = NumBytes < 0;
368 if (isSub) NumBytes = -NumBytes;
371 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
372 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
373 assert(ThisVal && "Didn't extract field correctly");
375 // We will handle these bits from offset, clear them.
376 NumBytes &= ~ThisVal;
378 // Get the properly encoded SOImmVal field.
379 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
380 assert(SOImmVal != -1 && "Bit extraction didn't work?");
382 // Build the new ADD / SUB.
383 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
384 .addReg(BaseReg, false, false, true).addImm(SOImmVal);
389 /// calcNumMI - Returns the number of instructions required to materialize
390 /// the specific add / sub r, c instruction.
391 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
392 unsigned NumBits, unsigned Scale) {
394 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
396 if (Opc == ARM::tADDrSPi) {
397 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
401 Scale = 1; // Followed by a number of tADDi8.
402 Chunk = ((1 << NumBits) - 1) * Scale;
405 NumMIs += Bytes / Chunk;
406 if ((Bytes % Chunk) != 0)
413 /// emitLoadConstPool - Emits a load from constpool to materialize NumBytes
415 static void emitLoadConstPool(MachineBasicBlock &MBB,
416 MachineBasicBlock::iterator &MBBI,
417 unsigned DestReg, int NumBytes,
418 const TargetInstrInfo &TII) {
419 MachineFunction &MF = *MBB.getParent();
420 MachineConstantPool *ConstantPool = MF.getConstantPool();
421 Constant *C = ConstantInt::get(Type::Int32Ty, NumBytes);
422 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 2);
423 BuildMI(MBB, MBBI, TII.get(ARM::tLDRpci), DestReg).addConstantPoolIndex(Idx);
426 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
427 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
428 /// in a register using mov / mvn sequences or load the immediate from a
431 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
432 MachineBasicBlock::iterator &MBBI,
433 unsigned DestReg, unsigned BaseReg,
434 int NumBytes, bool CanChangeCC,
435 const TargetInstrInfo &TII) {
436 bool isHigh = !isLowRegister(DestReg) ||
437 (BaseReg != 0 && !isLowRegister(BaseReg));
439 // Subtract doesn't have high register version. Load the negative value
440 // if either base or dest register is a high register. Also, if do not
441 // issue sub as part of the sequence if condition register is to be
443 if (NumBytes < 0 && !isHigh && CanChangeCC) {
445 NumBytes = -NumBytes;
447 unsigned LdReg = DestReg;
448 if (DestReg == ARM::SP) {
449 assert(BaseReg == ARM::SP && "Unexpected!");
451 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::R12)
452 .addReg(ARM::R3, false, false, true);
455 if (NumBytes <= 255 && NumBytes >= 0)
456 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
457 else if (NumBytes < 0 && NumBytes >= -255) {
458 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
459 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), LdReg)
460 .addReg(LdReg, false, false, true);
462 emitLoadConstPool(MBB, MBBI, LdReg, NumBytes, TII);
465 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
466 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, TII.get(Opc), DestReg);
467 if (DestReg == ARM::SP || isSub)
468 MIB.addReg(BaseReg).addReg(LdReg, false, false, true);
470 MIB.addReg(LdReg).addReg(BaseReg, false, false, true);
471 if (DestReg == ARM::SP)
472 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::R3)
473 .addReg(ARM::R12, false, false, true);
476 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
477 /// a destreg = basereg + immediate in Thumb code.
479 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
480 MachineBasicBlock::iterator &MBBI,
481 unsigned DestReg, unsigned BaseReg,
482 int NumBytes, const TargetInstrInfo &TII) {
483 bool isSub = NumBytes < 0;
484 unsigned Bytes = (unsigned)NumBytes;
485 if (isSub) Bytes = -NumBytes;
486 bool isMul4 = (Bytes & 3) == 0;
487 bool isTwoAddr = false;
488 bool DstNotEqBase = false;
489 unsigned NumBits = 1;
494 if (DestReg == BaseReg && BaseReg == ARM::SP) {
495 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
498 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
500 } else if (!isSub && BaseReg == ARM::SP) {
503 // r1 = add sp, 100 * 4
507 ExtraOpc = ARM::tADDi3;
516 if (DestReg != BaseReg)
519 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
523 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
524 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
525 if (NumMIs > Threshold) {
526 // This will expand into too many instructions. Load the immediate from a
528 emitThumbRegPlusImmInReg(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII);
533 if (isLowRegister(DestReg) && isLowRegister(BaseReg)) {
534 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
535 unsigned Chunk = (1 << 3) - 1;
536 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
538 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
539 .addReg(BaseReg, false, false, true).addImm(ThisVal);
541 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), DestReg)
542 .addReg(BaseReg, false, false, true);
547 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
549 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
552 // Build the new tADD / tSUB.
554 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(DestReg).addImm(ThisVal);
556 bool isKill = BaseReg != ARM::SP;
557 BuildMI(MBB, MBBI, TII.get(Opc), DestReg)
558 .addReg(BaseReg, false, false, isKill).addImm(ThisVal);
561 if (Opc == ARM::tADDrSPi) {
567 Chunk = ((1 << NumBits) - 1) * Scale;
568 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
575 BuildMI(MBB, MBBI, TII.get(ExtraOpc), DestReg)
576 .addReg(DestReg, false, false, true)
577 .addImm(((unsigned)NumBytes) & 3);
581 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
582 int NumBytes, bool isThumb, const TargetInstrInfo &TII) {
584 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
586 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
589 void ARMRegisterInfo::
590 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
591 MachineBasicBlock::iterator I) const {
593 // If we have alloca, convert as follows:
594 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
595 // ADJCALLSTACKUP -> add, sp, sp, amount
596 MachineInstr *Old = I;
597 unsigned Amount = Old->getOperand(0).getImmedValue();
599 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
600 // We need to keep the stack aligned properly. To do this, we round the
601 // amount of space needed for the outgoing arguments up to the next
602 // alignment boundary.
603 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
604 Amount = (Amount+Align-1)/Align*Align;
606 // Replace the pseudo instruction with a new instruction...
607 if (Old->getOpcode() == ARM::ADJCALLSTACKDOWN) {
608 emitSPUpdate(MBB, I, -Amount, AFI->isThumbFunction(), TII);
610 assert(Old->getOpcode() == ARM::ADJCALLSTACKUP);
611 emitSPUpdate(MBB, I, Amount, AFI->isThumbFunction(), TII);
618 /// emitThumbConstant - Emit a series of instructions to materialize a
620 static void emitThumbConstant(MachineBasicBlock &MBB,
621 MachineBasicBlock::iterator &MBBI,
622 unsigned DestReg, int Imm,
623 const TargetInstrInfo &TII) {
624 bool isSub = Imm < 0;
625 if (isSub) Imm = -Imm;
627 int Chunk = (1 << 8) - 1;
628 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
630 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), DestReg).addImm(ThisVal);
632 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII);
634 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), DestReg)
635 .addReg(DestReg, false, false, true);
638 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
639 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
640 /// register first and then a spilled callee-saved register if that fails.
642 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
643 ARMFunctionInfo *AFI) {
644 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
646 // Try a already spilled CS register.
647 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
652 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
653 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();
661 while (!MI.getOperand(i).isFrameIndex()) {
663 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
666 unsigned FrameReg = ARM::SP;
667 int FrameIndex = MI.getOperand(i).getFrameIndex();
668 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
669 MF.getFrameInfo()->getStackSize();
671 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
672 Offset -= AFI->getGPRCalleeSavedArea1Offset();
673 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
674 Offset -= AFI->getGPRCalleeSavedArea2Offset();
675 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
676 Offset -= AFI->getDPRCalleeSavedAreaOffset();
677 else if (hasFP(MF)) {
678 // There is alloca()'s in this function, must reference off the frame
680 FrameReg = getFrameRegister(MF);
681 Offset -= AFI->getFramePtrSpillOffset();
684 unsigned Opcode = MI.getOpcode();
685 const TargetInstrDescriptor &Desc = TII.get(Opcode);
686 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
689 if (Opcode == ARM::ADDri) {
690 Offset += MI.getOperand(i+1).getImm();
692 // Turn it into a move.
693 MI.setInstrDescriptor(TII.get(ARM::MOVr));
694 MI.getOperand(i).ChangeToRegister(FrameReg, false);
695 MI.RemoveOperand(i+1);
697 } else if (Offset < 0) {
700 MI.setInstrDescriptor(TII.get(ARM::SUBri));
703 // Common case: small offset, fits into instruction.
704 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
705 if (ImmedOffset != -1) {
706 // Replace the FrameIndex with sp / fp
707 MI.getOperand(i).ChangeToRegister(FrameReg, false);
708 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
712 // Otherwise, we fallback to common code below to form the imm offset with
713 // a sequence of ADDri instructions. First though, pull as much of the imm
714 // into this ADDri as possible.
715 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
716 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, (32-RotAmt) & 31);
718 // We will handle these bits from offset, clear them.
719 Offset &= ~ThisImmVal;
721 // Get the properly encoded SOImmVal field.
722 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
723 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
724 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
725 } else if (Opcode == ARM::tADDrSPi) {
726 Offset += MI.getOperand(i+1).getImm();
728 // Can't use tADDrSPi if it's based off the frame pointer.
729 unsigned NumBits = 0;
731 if (FrameReg != ARM::SP) {
732 Opcode = ARM::tADDi3;
733 MI.setInstrDescriptor(TII.get(ARM::tADDi3));
738 assert((Offset & 3) == 0 &&
739 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
743 // Turn it into a move.
744 MI.setInstrDescriptor(TII.get(ARM::tMOVr));
745 MI.getOperand(i).ChangeToRegister(FrameReg, false);
746 MI.RemoveOperand(i+1);
750 // Common case: small offset, fits into instruction.
751 unsigned Mask = (1 << NumBits) - 1;
752 if (((Offset / Scale) & ~Mask) == 0) {
753 // Replace the FrameIndex with sp / fp
754 MI.getOperand(i).ChangeToRegister(FrameReg, false);
755 MI.getOperand(i+1).ChangeToImmediate(Offset / Scale);
759 unsigned DestReg = MI.getOperand(0).getReg();
760 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
761 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, NumBits, Scale);
762 // MI would expand into a large number of instructions. Don't try to
763 // simplify the immediate.
765 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII);
771 // Translate r0 = add sp, imm to
772 // r0 = add sp, 255*4
773 // r0 = add r0, (imm - 255*4)
774 MI.getOperand(i).ChangeToRegister(FrameReg, false);
775 MI.getOperand(i+1).ChangeToImmediate(Mask);
776 Offset = (Offset - Mask * Scale);
777 MachineBasicBlock::iterator NII = next(II);
778 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII);
780 // Translate r0 = add sp, -imm to
781 // r0 = -imm (this is then translated into a series of instructons)
783 emitThumbConstant(MBB, II, DestReg, Offset, TII);
784 MI.setInstrDescriptor(TII.get(ARM::tADDhirr));
785 MI.getOperand(i).ChangeToRegister(DestReg, false, false, true);
786 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
792 unsigned NumBits = 0;
795 case ARMII::AddrMode2: {
797 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
798 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
803 case ARMII::AddrMode3: {
805 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
806 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
811 case ARMII::AddrMode5: {
813 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
814 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
820 case ARMII::AddrModeTs: {
822 InstrOffs = MI.getOperand(ImmIdx).getImm();
823 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
828 assert(0 && "Unsupported addressing mode!");
833 Offset += InstrOffs * Scale;
834 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
835 if (Offset < 0 && !isThumb) {
840 // Common case: small offset, fits into instruction.
841 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
842 int ImmedOffset = Offset / Scale;
843 unsigned Mask = (1 << NumBits) - 1;
844 if ((unsigned)Offset <= Mask * Scale) {
845 // Replace the FrameIndex with sp
846 MI.getOperand(i).ChangeToRegister(FrameReg, false);
848 ImmedOffset |= 1 << NumBits;
849 ImmOp.ChangeToImmediate(ImmedOffset);
853 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
854 if (AddrMode == ARMII::AddrModeTs) {
855 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
856 // a different base register.
858 Mask = (1 << NumBits) - 1;
860 // If this is a thumb spill / restore, we will be using a constpool load to
861 // materialize the offset.
862 if (AddrMode == ARMII::AddrModeTs && isThumSpillRestore)
863 ImmOp.ChangeToImmediate(0);
865 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
866 ImmedOffset = ImmedOffset & Mask;
868 ImmedOffset |= 1 << NumBits;
869 ImmOp.ChangeToImmediate(ImmedOffset);
870 Offset &= ~(Mask*Scale);
874 // If we get here, the immediate doesn't fit into the instruction. We folded
875 // as much as possible above, handle the rest, providing a register that is
877 assert(Offset && "This code isn't needed if offset already handled!");
880 if (TII.isLoad(Opcode)) {
881 // Use the destination register to materialize sp + offset.
882 unsigned TmpReg = MI.getOperand(0).getReg();
884 if (Opcode == ARM::tRestore) {
885 if (FrameReg == ARM::SP)
886 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
888 emitLoadConstPool(MBB, II, TmpReg, Offset, TII);
892 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
893 MI.setInstrDescriptor(TII.get(ARM::tLDR));
894 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
896 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
898 MI.addRegOperand(0, false); // tLDR has an extra register operand.
899 } else if (TII.isStore(Opcode)) {
900 // FIXME! This is horrific!!! We need register scavenging.
901 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
902 // also a ABI register so it's possible that is is the register that is
903 // being storing here. If that's the case, we do the following:
905 // Use r2 to materialize sp + offset
908 unsigned ValReg = MI.getOperand(0).getReg();
909 unsigned TmpReg = ARM::R3;
911 if (ValReg == ARM::R3) {
912 BuildMI(MBB, II, TII.get(ARM::tMOVr), ARM::R12)
913 .addReg(ARM::R2, false, false, true);
916 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
917 BuildMI(MBB, II, TII.get(ARM::tMOVr), ARM::R12)
918 .addReg(ARM::R3, false, false, true);
919 if (Opcode == ARM::tSpill) {
920 if (FrameReg == ARM::SP)
921 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
923 emitLoadConstPool(MBB, II, TmpReg, Offset, TII);
927 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
928 MI.setInstrDescriptor(TII.get(ARM::tSTR));
929 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
931 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
933 MI.addRegOperand(0, false); // tSTR has an extra register operand.
935 MachineBasicBlock::iterator NII = next(II);
936 if (ValReg == ARM::R3)
937 BuildMI(MBB, NII, TII.get(ARM::tMOVr), ARM::R2)
938 .addReg(ARM::R12, false, false, true);
939 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
940 BuildMI(MBB, NII, TII.get(ARM::tMOVr), ARM::R3)
941 .addReg(ARM::R12, false, false, true);
943 assert(false && "Unexpected opcode!");
945 // Insert a set of r12 with the full address: r12 = sp + offset
946 // If the offset we have is too large to fit into the instruction, we need
947 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
949 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
951 // No register is "free". Scavenge a register.
952 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II);
953 emitARMRegPlusImmediate(MBB, II, ScratchReg, FrameReg,
954 isSub ? -Offset : Offset, TII);
955 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
959 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
960 const MachineFrameInfo *FFI = MF.getFrameInfo();
962 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
963 int FixedOff = -FFI->getObjectOffset(i);
964 if (FixedOff > Offset) Offset = FixedOff;
966 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
967 Offset += FFI->getObjectSize(i);
968 unsigned Align = FFI->getObjectAlignment(i);
969 // Adjust to alignment boundary
970 Offset = (Offset+Align-1)/Align*Align;
972 return (unsigned)Offset;
976 ARMRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
977 RegScavenger *RS) const {
978 // This tells PEI to spill the FP as if it is any other callee-save register
979 // to take advantage the eliminateFrameIndex machinery. This also ensures it
980 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
981 // to combine multiple loads / stores.
982 bool CanEliminateFrame = true;
983 bool CS1Spilled = false;
984 bool LRSpilled = false;
985 unsigned NumGPRSpills = 0;
986 SmallVector<unsigned, 4> UnspilledCS1GPRs;
987 SmallVector<unsigned, 4> UnspilledCS2GPRs;
988 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
990 // Don't spill FP if the frame can be eliminated. This is determined
991 // by scanning the callee-save registers to see if any is used.
992 const unsigned *CSRegs = getCalleeSavedRegs();
993 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
994 for (unsigned i = 0; CSRegs[i]; ++i) {
995 unsigned Reg = CSRegs[i];
996 bool Spilled = false;
997 if (MF.isPhysRegUsed(Reg)) {
998 AFI->setCSRegisterIsSpilled(Reg);
1000 CanEliminateFrame = false;
1002 // Check alias registers too.
1003 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
1004 if (MF.isPhysRegUsed(*Aliases)) {
1006 CanEliminateFrame = false;
1011 if (CSRegClasses[i] == &ARM::GPRRegClass) {
1015 if (!STI.isTargetDarwin()) {
1023 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1038 if (!STI.isTargetDarwin()) {
1039 UnspilledCS1GPRs.push_back(Reg);
1049 UnspilledCS1GPRs.push_back(Reg);
1052 UnspilledCS2GPRs.push_back(Reg);
1059 bool ForceLRSpill = false;
1060 if (!LRSpilled && AFI->isThumbFunction()) {
1061 unsigned FnSize = ARM::GetFunctionSize(MF);
1062 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1063 // use of BL to implement far jump. If it turns out that it's not needed
1064 // then the branch fix up path will undo it.
1065 if (FnSize >= (1 << 11)) {
1066 CanEliminateFrame = false;
1067 ForceLRSpill = true;
1071 bool ExtraCSSpill = false;
1072 if (!CanEliminateFrame || hasFP(MF)) {
1073 AFI->setHasStackFrame(true);
1075 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1076 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1077 if (!LRSpilled && CS1Spilled) {
1078 MF.changePhyRegUsed(ARM::LR, true);
1079 AFI->setCSRegisterIsSpilled(ARM::LR);
1081 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1082 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1083 ForceLRSpill = false;
1084 ExtraCSSpill = true;
1087 // Darwin ABI requires FP to point to the stack slot that contains the
1089 if (STI.isTargetDarwin() || hasFP(MF)) {
1090 MF.changePhyRegUsed(FramePtr, true);
1094 // If stack and double are 8-byte aligned and we are spilling an odd number
1095 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
1096 // the integer and double callee save areas.
1097 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1098 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1099 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1100 unsigned Reg = UnspilledCS1GPRs.front();
1101 MF.changePhyRegUsed(Reg, true);
1102 AFI->setCSRegisterIsSpilled(Reg);
1103 if (!isReservedReg(MF, Reg))
1104 ExtraCSSpill = true;
1105 } else if (!UnspilledCS2GPRs.empty()) {
1106 unsigned Reg = UnspilledCS2GPRs.front();
1107 MF.changePhyRegUsed(Reg, true);
1108 AFI->setCSRegisterIsSpilled(Reg);
1109 if (!isReservedReg(MF, Reg))
1110 ExtraCSSpill = true;
1114 // Estimate if we might need to scavenge a register at some point in order
1115 // to materialize a stack offset. If so, either spill one additiona
1116 // callee-saved register or reserve a special spill slot to facilitate
1117 // register scavenging.
1118 if (RS && !ExtraCSSpill && !AFI->isThumbFunction()) {
1119 MachineFrameInfo *MFI = MF.getFrameInfo();
1120 unsigned Size = estimateStackSize(MF, MFI);
1121 unsigned Limit = (1 << 12) - 1;
1122 for (MachineFunction::iterator BB = MF.begin(),E = MF.end();BB != E; ++BB)
1123 for (MachineBasicBlock::iterator I= BB->begin(); I != BB->end(); ++I) {
1124 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1125 if (I->getOperand(i).isFrameIndex()) {
1126 unsigned Opcode = I->getOpcode();
1127 const TargetInstrDescriptor &Desc = TII.get(Opcode);
1128 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1129 if (AddrMode == ARMII::AddrMode3) {
1130 Limit = (1 << 8) - 1;
1131 goto DoneEstimating;
1132 } else if (AddrMode == ARMII::AddrMode5) {
1133 Limit = ((1 << 8) - 1) * 4;
1134 goto DoneEstimating;
1139 if (Size >= Limit) {
1140 // If any non-reserved CS register isn't spilled, just spill one or two
1141 // extra. That should take care of it!
1142 unsigned NumExtras = TargetAlign / 4;
1143 SmallVector<unsigned, 2> Extras;
1144 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1145 unsigned Reg = UnspilledCS1GPRs.back();
1146 UnspilledCS1GPRs.pop_back();
1147 if (!isReservedReg(MF, Reg)) {
1148 Extras.push_back(Reg);
1152 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1153 unsigned Reg = UnspilledCS2GPRs.back();
1154 UnspilledCS2GPRs.pop_back();
1155 if (!isReservedReg(MF, Reg)) {
1156 Extras.push_back(Reg);
1160 if (Extras.size() && NumExtras == 0) {
1161 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1162 MF.changePhyRegUsed(Extras[i], true);
1163 AFI->setCSRegisterIsSpilled(Extras[i]);
1166 // Reserve a slot closest to SP or frame pointer.
1167 const TargetRegisterClass *RC = &ARM::GPRRegClass;
1168 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1169 RC->getAlignment()));
1176 MF.changePhyRegUsed(ARM::LR, true);
1177 AFI->setCSRegisterIsSpilled(ARM::LR);
1178 AFI->setLRIsSpilledForFarJump(true);
1182 /// Move iterator pass the next bunch of callee save load / store ops for
1183 /// the particular spill area (1: integer area 1, 2: integer area 2,
1184 /// 3: fp area, 0: don't care).
1185 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1186 MachineBasicBlock::iterator &MBBI,
1187 int Opc, unsigned Area,
1188 const ARMSubtarget &STI) {
1189 while (MBBI != MBB.end() &&
1190 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFrameIndex()) {
1193 unsigned Category = 0;
1194 switch (MBBI->getOperand(0).getReg()) {
1195 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1199 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1200 Category = STI.isTargetDarwin() ? 2 : 1;
1202 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1203 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1210 if (Done || Category != Area)
1218 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
1219 MachineBasicBlock &MBB = MF.front();
1220 MachineBasicBlock::iterator MBBI = MBB.begin();
1221 MachineFrameInfo *MFI = MF.getFrameInfo();
1222 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1223 bool isThumb = AFI->isThumbFunction();
1224 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1225 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1226 unsigned NumBytes = MFI->getStackSize();
1227 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1230 // Check if R3 is live in. It might have to be used as a scratch register.
1231 for (MachineFunction::livein_iterator I=MF.livein_begin(),E=MF.livein_end();
1233 if ((*I).first == ARM::R3) {
1234 AFI->setR3IsLiveIn(true);
1239 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1240 NumBytes = (NumBytes + 3) & ~3;
1241 MFI->setStackSize(NumBytes);
1244 // Determine the sizes of each callee-save spill areas and record which frame
1245 // belongs to which callee-save spill areas.
1246 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1247 int FramePtrSpillFI = 0;
1250 emitSPUpdate(MBB, MBBI, -VARegSaveSize, isThumb, TII);
1252 if (!AFI->hasStackFrame()) {
1254 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1258 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1259 unsigned Reg = CSI[i].getReg();
1260 int FI = CSI[i].getFrameIdx();
1267 if (Reg == FramePtr)
1268 FramePtrSpillFI = FI;
1269 AFI->addGPRCalleeSavedArea1Frame(FI);
1276 if (Reg == FramePtr)
1277 FramePtrSpillFI = FI;
1278 if (STI.isTargetDarwin()) {
1279 AFI->addGPRCalleeSavedArea2Frame(FI);
1282 AFI->addGPRCalleeSavedArea1Frame(FI);
1287 AFI->addDPRCalleeSavedAreaFrame(FI);
1292 if (Align == 8 && (GPRCS1Size & 7) != 0)
1293 // Pad CS1 to ensure proper alignment.
1297 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1298 emitSPUpdate(MBB, MBBI, -GPRCS1Size, isThumb, TII);
1299 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1300 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH)
1303 // Darwin ABI requires FP to point to the stack slot that contains the
1305 if (STI.isTargetDarwin() || hasFP(MF))
1306 BuildMI(MBB, MBBI, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri), FramePtr)
1307 .addFrameIndex(FramePtrSpillFI).addImm(0);
1310 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1311 emitSPUpdate(MBB, MBBI, -GPRCS2Size, false, TII);
1313 // Build the new SUBri to adjust SP for FP callee-save spill area.
1314 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1315 emitSPUpdate(MBB, MBBI, -DPRCSSize, false, TII);
1318 // Determine starting offsets of spill areas.
1319 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1320 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1321 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1322 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1323 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1324 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1325 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1327 NumBytes = DPRCSOffset;
1329 // Insert it after all the callee-save spills.
1331 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1332 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1335 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1336 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1337 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1340 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1341 for (unsigned i = 0; CSRegs[i]; ++i)
1342 if (Reg == CSRegs[i])
1347 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1348 return ((MI->getOpcode() == ARM::FLDD ||
1349 MI->getOpcode() == ARM::LDR ||
1350 MI->getOpcode() == ARM::tRestore) &&
1351 MI->getOperand(1).isFrameIndex() &&
1352 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1355 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1356 MachineBasicBlock &MBB) const {
1357 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1358 assert((MBBI->getOpcode() == ARM::BX_RET ||
1359 MBBI->getOpcode() == ARM::tBX_RET ||
1360 MBBI->getOpcode() == ARM::tPOP_RET) &&
1361 "Can only insert epilog into returning blocks");
1363 MachineFrameInfo *MFI = MF.getFrameInfo();
1364 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1365 bool isThumb = AFI->isThumbFunction();
1366 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1367 int NumBytes = (int)MFI->getStackSize();
1368 if (!AFI->hasStackFrame()) {
1370 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1372 // Unwind MBBI to point to first LDR / FLDD.
1373 const unsigned *CSRegs = getCalleeSavedRegs();
1374 if (MBBI != MBB.begin()) {
1377 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1378 if (!isCSRestore(MBBI, CSRegs))
1382 // Move SP to start of FP callee save spill area.
1383 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1384 AFI->getGPRCalleeSavedArea2Size() +
1385 AFI->getDPRCalleeSavedAreaSize());
1388 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1389 // Reset SP based on frame pointer only if the stack frame extends beyond
1390 // frame pointer stack slot or target is ELF and the function has FP.
1392 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes, TII);
1394 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::SP).addReg(FramePtr);
1396 if (MBBI->getOpcode() == ARM::tBX_RET &&
1397 &MBB.front() != MBBI &&
1398 prior(MBBI)->getOpcode() == ARM::tPOP) {
1399 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1400 emitSPUpdate(MBB, PMBBI, NumBytes, isThumb, TII);
1402 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1405 // Darwin ABI requires FP to point to the stack slot that contains the
1407 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1408 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1409 // Reset SP based on frame pointer only if the stack frame extends beyond
1410 // frame pointer stack slot or target is ELF and the function has FP.
1411 if (AFI->getGPRCalleeSavedArea2Size() ||
1412 AFI->getDPRCalleeSavedAreaSize() ||
1413 AFI->getDPRCalleeSavedAreaOffset()||
1416 BuildMI(MBB, MBBI, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1419 BuildMI(MBB, MBBI, TII.get(ARM::MOVr), ARM::SP).addReg(FramePtr);
1420 } else if (NumBytes) {
1421 emitSPUpdate(MBB, MBBI, NumBytes, false, TII);
1424 // Move SP to start of integer callee save spill area 2.
1425 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1426 emitSPUpdate(MBB, MBBI, AFI->getDPRCalleeSavedAreaSize(), false, TII);
1428 // Move SP to start of integer callee save spill area 1.
1429 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1430 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea2Size(), false, TII);
1432 // Move SP to SP upon entry to the function.
1433 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1434 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea1Size(), false, TII);
1438 if (VARegSaveSize) {
1440 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1441 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1442 BuildMI(MBB, MBBI, TII.get(ARM::tPOP)).addReg(ARM::R3);
1444 emitSPUpdate(MBB, MBBI, VARegSaveSize, isThumb, TII);
1447 BuildMI(MBB, MBBI, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1453 unsigned ARMRegisterInfo::getRARegister() const {
1457 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1458 return STI.useThumbBacktraces() ? ARM::R7 : ARM::R11;
1461 unsigned ARMRegisterInfo::getEHExceptionRegister() const {
1462 assert(0 && "What is the exception register");
1466 unsigned ARMRegisterInfo::getEHHandlerRegister() const {
1467 assert(0 && "What is the exception handler register");
1471 #include "ARMGenRegisterInfo.inc"