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 /// emitLoadConstPool - Emits a load from constpool to materialize the
199 /// specified immediate.
200 static void emitLoadConstPool(MachineBasicBlock &MBB,
201 MachineBasicBlock::iterator &MBBI,
202 unsigned DestReg, int Val,
203 const TargetInstrInfo &TII, bool isThumb) {
204 MachineFunction &MF = *MBB.getParent();
205 MachineConstantPool *ConstantPool = MF.getConstantPool();
206 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
207 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 2);
209 BuildMI(MBB, MBBI, TII.get(ARM::tLDRcp), DestReg).addConstantPoolIndex(Idx);
211 BuildMI(MBB, MBBI, TII.get(ARM::LDRcp), DestReg).addConstantPoolIndex(Idx)
212 .addReg(0).addImm(0);
215 void ARMRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
216 MachineBasicBlock::iterator I,
218 const MachineInstr *Orig) const {
219 if (Orig->getOpcode() == ARM::MOVi2pieces) {
220 emitLoadConstPool(MBB, I, DestReg, Orig->getOperand(1).getImmedValue(),
225 MachineInstr *MI = Orig->clone();
226 MI->getOperand(0).setReg(DestReg);
230 /// isLowRegister - Returns true if the register is low register r0-r7.
232 static bool isLowRegister(unsigned Reg) {
235 case R0: case R1: case R2: case R3:
236 case R4: case R5: case R6: case R7:
243 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr *MI,
244 unsigned OpNum, int FI) const {
245 unsigned Opc = MI->getOpcode();
246 MachineInstr *NewMI = NULL;
250 if (OpNum == 0) { // move -> store
251 unsigned SrcReg = MI->getOperand(1).getReg();
252 NewMI = BuildMI(TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
253 .addReg(0).addImm(0);
254 } else { // move -> load
255 unsigned DstReg = MI->getOperand(0).getReg();
256 NewMI = BuildMI(TII.get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
262 if (OpNum == 0) { // move -> store
263 unsigned SrcReg = MI->getOperand(1).getReg();
264 if (isPhysicalRegister(SrcReg) && !isLowRegister(SrcReg))
265 // tSpill cannot take a high register operand.
267 NewMI = BuildMI(TII.get(ARM::tSpill)).addReg(SrcReg).addFrameIndex(FI)
269 } else { // move -> load
270 unsigned DstReg = MI->getOperand(0).getReg();
271 if (isPhysicalRegister(DstReg) && !isLowRegister(DstReg))
272 // tRestore cannot target a high register operand.
274 NewMI = BuildMI(TII.get(ARM::tRestore), DstReg).addFrameIndex(FI)
280 if (OpNum == 0) { // move -> store
281 unsigned SrcReg = MI->getOperand(1).getReg();
282 NewMI = BuildMI(TII.get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
284 } else { // move -> load
285 unsigned DstReg = MI->getOperand(0).getReg();
286 NewMI = BuildMI(TII.get(ARM::FLDS), DstReg).addFrameIndex(FI).addImm(0);
291 if (OpNum == 0) { // move -> store
292 unsigned SrcReg = MI->getOperand(1).getReg();
293 NewMI = BuildMI(TII.get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
295 } else { // move -> load
296 unsigned DstReg = MI->getOperand(0).getReg();
297 NewMI = BuildMI(TII.get(ARM::FLDD), DstReg).addFrameIndex(FI).addImm(0);
304 NewMI->copyKillDeadInfo(MI);
308 const unsigned* ARMRegisterInfo::getCalleeSavedRegs() const {
309 static const unsigned CalleeSavedRegs[] = {
310 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
311 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
313 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
314 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
318 static const unsigned DarwinCalleeSavedRegs[] = {
319 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
320 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
322 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
323 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
326 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
329 const TargetRegisterClass* const *
330 ARMRegisterInfo::getCalleeSavedRegClasses() const {
331 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
332 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
333 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
334 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
336 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
337 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
340 return CalleeSavedRegClasses;
343 BitVector ARMRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
344 // FIXME: avoid re-calculating this everytime.
345 BitVector Reserved(getNumRegs());
346 Reserved.set(ARM::SP);
347 Reserved.set(ARM::PC);
348 if (STI.isTargetDarwin() || hasFP(MF))
349 Reserved.set(FramePtr);
350 // Some targets reserve R9.
351 if (STI.isR9Reserved())
352 Reserved.set(ARM::R9);
357 ARMRegisterInfo::isReservedReg(const MachineFunction &MF, unsigned Reg) const {
365 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
369 return STI.isR9Reserved();
376 ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
377 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
378 return ThumbRegScavenging || !AFI->isThumbFunction();
381 /// hasFP - Return true if the specified function should have a dedicated frame
382 /// pointer register. This is true if the function has variable sized allocas
383 /// or if frame pointer elimination is disabled.
385 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
386 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
389 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
390 /// a destreg = basereg + immediate in ARM code.
392 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
393 MachineBasicBlock::iterator &MBBI,
394 unsigned DestReg, unsigned BaseReg,
395 int NumBytes, const TargetInstrInfo &TII) {
396 bool isSub = NumBytes < 0;
397 if (isSub) NumBytes = -NumBytes;
400 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
401 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
402 assert(ThisVal && "Didn't extract field correctly");
404 // We will handle these bits from offset, clear them.
405 NumBytes &= ~ThisVal;
407 // Get the properly encoded SOImmVal field.
408 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
409 assert(SOImmVal != -1 && "Bit extraction didn't work?");
411 // Build the new ADD / SUB.
412 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
413 .addReg(BaseReg, false, false, true).addImm(SOImmVal);
418 /// calcNumMI - Returns the number of instructions required to materialize
419 /// the specific add / sub r, c instruction.
420 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
421 unsigned NumBits, unsigned Scale) {
423 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
425 if (Opc == ARM::tADDrSPi) {
426 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
430 Scale = 1; // Followed by a number of tADDi8.
431 Chunk = ((1 << NumBits) - 1) * Scale;
434 NumMIs += Bytes / Chunk;
435 if ((Bytes % Chunk) != 0)
442 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
443 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
444 /// in a register using mov / mvn sequences or load the immediate from a
447 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
448 MachineBasicBlock::iterator &MBBI,
449 unsigned DestReg, unsigned BaseReg,
450 int NumBytes, bool CanChangeCC,
451 const TargetInstrInfo &TII) {
452 bool isHigh = !isLowRegister(DestReg) ||
453 (BaseReg != 0 && !isLowRegister(BaseReg));
455 // Subtract doesn't have high register version. Load the negative value
456 // if either base or dest register is a high register. Also, if do not
457 // issue sub as part of the sequence if condition register is to be
459 if (NumBytes < 0 && !isHigh && CanChangeCC) {
461 NumBytes = -NumBytes;
463 unsigned LdReg = DestReg;
464 if (DestReg == ARM::SP) {
465 assert(BaseReg == ARM::SP && "Unexpected!");
467 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::R12)
468 .addReg(ARM::R3, false, false, true);
471 if (NumBytes <= 255 && NumBytes >= 0)
472 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
473 else if (NumBytes < 0 && NumBytes >= -255) {
474 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
475 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), LdReg)
476 .addReg(LdReg, false, false, true);
478 emitLoadConstPool(MBB, MBBI, LdReg, NumBytes, TII, true);
481 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
482 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, TII.get(Opc), DestReg);
483 if (DestReg == ARM::SP || isSub)
484 MIB.addReg(BaseReg).addReg(LdReg, false, false, true);
486 MIB.addReg(LdReg).addReg(BaseReg, false, false, true);
487 if (DestReg == ARM::SP)
488 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::R3)
489 .addReg(ARM::R12, false, false, true);
492 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
493 /// a destreg = basereg + immediate in Thumb code.
495 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
496 MachineBasicBlock::iterator &MBBI,
497 unsigned DestReg, unsigned BaseReg,
498 int NumBytes, const TargetInstrInfo &TII) {
499 bool isSub = NumBytes < 0;
500 unsigned Bytes = (unsigned)NumBytes;
501 if (isSub) Bytes = -NumBytes;
502 bool isMul4 = (Bytes & 3) == 0;
503 bool isTwoAddr = false;
504 bool DstNotEqBase = false;
505 unsigned NumBits = 1;
510 if (DestReg == BaseReg && BaseReg == ARM::SP) {
511 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
514 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
516 } else if (!isSub && BaseReg == ARM::SP) {
519 // r1 = add sp, 100 * 4
523 ExtraOpc = ARM::tADDi3;
532 if (DestReg != BaseReg)
535 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
539 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
540 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
541 if (NumMIs > Threshold) {
542 // This will expand into too many instructions. Load the immediate from a
544 emitThumbRegPlusImmInReg(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII);
549 if (isLowRegister(DestReg) && isLowRegister(BaseReg)) {
550 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
551 unsigned Chunk = (1 << 3) - 1;
552 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
554 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
555 .addReg(BaseReg, false, false, true).addImm(ThisVal);
557 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), DestReg)
558 .addReg(BaseReg, false, false, true);
563 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
565 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
568 // Build the new tADD / tSUB.
570 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(DestReg).addImm(ThisVal);
572 bool isKill = BaseReg != ARM::SP;
573 BuildMI(MBB, MBBI, TII.get(Opc), DestReg)
574 .addReg(BaseReg, false, false, isKill).addImm(ThisVal);
577 if (Opc == ARM::tADDrSPi) {
583 Chunk = ((1 << NumBits) - 1) * Scale;
584 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
591 BuildMI(MBB, MBBI, TII.get(ExtraOpc), DestReg)
592 .addReg(DestReg, false, false, true)
593 .addImm(((unsigned)NumBytes) & 3);
597 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
598 int NumBytes, bool isThumb, const TargetInstrInfo &TII) {
600 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
602 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
605 void ARMRegisterInfo::
606 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
607 MachineBasicBlock::iterator I) const {
609 // If we have alloca, convert as follows:
610 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
611 // ADJCALLSTACKUP -> add, sp, sp, amount
612 MachineInstr *Old = I;
613 unsigned Amount = Old->getOperand(0).getImmedValue();
615 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
616 // We need to keep the stack aligned properly. To do this, we round the
617 // amount of space needed for the outgoing arguments up to the next
618 // alignment boundary.
619 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
620 Amount = (Amount+Align-1)/Align*Align;
622 // Replace the pseudo instruction with a new instruction...
623 if (Old->getOpcode() == ARM::ADJCALLSTACKDOWN) {
624 emitSPUpdate(MBB, I, -Amount, AFI->isThumbFunction(), TII);
626 assert(Old->getOpcode() == ARM::ADJCALLSTACKUP);
627 emitSPUpdate(MBB, I, Amount, AFI->isThumbFunction(), TII);
634 /// emitThumbConstant - Emit a series of instructions to materialize a
636 static void emitThumbConstant(MachineBasicBlock &MBB,
637 MachineBasicBlock::iterator &MBBI,
638 unsigned DestReg, int Imm,
639 const TargetInstrInfo &TII) {
640 bool isSub = Imm < 0;
641 if (isSub) Imm = -Imm;
643 int Chunk = (1 << 8) - 1;
644 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
646 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), DestReg).addImm(ThisVal);
648 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII);
650 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), DestReg)
651 .addReg(DestReg, false, false, true);
654 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
655 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
656 /// register first and then a spilled callee-saved register if that fails.
658 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
659 ARMFunctionInfo *AFI) {
660 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
662 // Try a already spilled CS register.
663 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
668 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
669 RegScavenger *RS) const{
671 MachineInstr &MI = *II;
672 MachineBasicBlock &MBB = *MI.getParent();
673 MachineFunction &MF = *MBB.getParent();
674 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
675 bool isThumb = AFI->isThumbFunction();
677 while (!MI.getOperand(i).isFrameIndex()) {
679 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
682 unsigned FrameReg = ARM::SP;
683 int FrameIndex = MI.getOperand(i).getFrameIndex();
684 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
685 MF.getFrameInfo()->getStackSize();
687 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
688 Offset -= AFI->getGPRCalleeSavedArea1Offset();
689 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
690 Offset -= AFI->getGPRCalleeSavedArea2Offset();
691 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
692 Offset -= AFI->getDPRCalleeSavedAreaOffset();
693 else if (hasFP(MF)) {
694 // There is alloca()'s in this function, must reference off the frame
696 FrameReg = getFrameRegister(MF);
697 Offset -= AFI->getFramePtrSpillOffset();
700 unsigned Opcode = MI.getOpcode();
701 const TargetInstrDescriptor &Desc = TII.get(Opcode);
702 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
705 if (Opcode == ARM::ADDri) {
706 Offset += MI.getOperand(i+1).getImm();
708 // Turn it into a move.
709 MI.setInstrDescriptor(TII.get(ARM::MOVr));
710 MI.getOperand(i).ChangeToRegister(FrameReg, false);
711 MI.RemoveOperand(i+1);
713 } else if (Offset < 0) {
716 MI.setInstrDescriptor(TII.get(ARM::SUBri));
719 // Common case: small offset, fits into instruction.
720 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
721 if (ImmedOffset != -1) {
722 // Replace the FrameIndex with sp / fp
723 MI.getOperand(i).ChangeToRegister(FrameReg, false);
724 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
728 // Otherwise, we fallback to common code below to form the imm offset with
729 // a sequence of ADDri instructions. First though, pull as much of the imm
730 // into this ADDri as possible.
731 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
732 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
734 // We will handle these bits from offset, clear them.
735 Offset &= ~ThisImmVal;
737 // Get the properly encoded SOImmVal field.
738 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
739 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
740 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
741 } else if (Opcode == ARM::tADDrSPi) {
742 Offset += MI.getOperand(i+1).getImm();
744 // Can't use tADDrSPi if it's based off the frame pointer.
745 unsigned NumBits = 0;
747 if (FrameReg != ARM::SP) {
748 Opcode = ARM::tADDi3;
749 MI.setInstrDescriptor(TII.get(ARM::tADDi3));
754 assert((Offset & 3) == 0 &&
755 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
759 // Turn it into a move.
760 MI.setInstrDescriptor(TII.get(ARM::tMOVr));
761 MI.getOperand(i).ChangeToRegister(FrameReg, false);
762 MI.RemoveOperand(i+1);
766 // Common case: small offset, fits into instruction.
767 unsigned Mask = (1 << NumBits) - 1;
768 if (((Offset / Scale) & ~Mask) == 0) {
769 // Replace the FrameIndex with sp / fp
770 MI.getOperand(i).ChangeToRegister(FrameReg, false);
771 MI.getOperand(i+1).ChangeToImmediate(Offset / Scale);
775 unsigned DestReg = MI.getOperand(0).getReg();
776 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
777 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, NumBits, Scale);
778 // MI would expand into a large number of instructions. Don't try to
779 // simplify the immediate.
781 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII);
787 // Translate r0 = add sp, imm to
788 // r0 = add sp, 255*4
789 // r0 = add r0, (imm - 255*4)
790 MI.getOperand(i).ChangeToRegister(FrameReg, false);
791 MI.getOperand(i+1).ChangeToImmediate(Mask);
792 Offset = (Offset - Mask * Scale);
793 MachineBasicBlock::iterator NII = next(II);
794 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII);
796 // Translate r0 = add sp, -imm to
797 // r0 = -imm (this is then translated into a series of instructons)
799 emitThumbConstant(MBB, II, DestReg, Offset, TII);
800 MI.setInstrDescriptor(TII.get(ARM::tADDhirr));
801 MI.getOperand(i).ChangeToRegister(DestReg, false, false, true);
802 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
808 unsigned NumBits = 0;
811 case ARMII::AddrMode2: {
813 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
814 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
819 case ARMII::AddrMode3: {
821 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
822 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
827 case ARMII::AddrMode5: {
829 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
830 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
836 case ARMII::AddrModeTs: {
838 InstrOffs = MI.getOperand(ImmIdx).getImm();
839 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
844 assert(0 && "Unsupported addressing mode!");
849 Offset += InstrOffs * Scale;
850 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
851 if (Offset < 0 && !isThumb) {
856 // Common case: small offset, fits into instruction.
857 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
858 int ImmedOffset = Offset / Scale;
859 unsigned Mask = (1 << NumBits) - 1;
860 if ((unsigned)Offset <= Mask * Scale) {
861 // Replace the FrameIndex with sp
862 MI.getOperand(i).ChangeToRegister(FrameReg, false);
864 ImmedOffset |= 1 << NumBits;
865 ImmOp.ChangeToImmediate(ImmedOffset);
869 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
870 if (AddrMode == ARMII::AddrModeTs) {
871 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
872 // a different base register.
874 Mask = (1 << NumBits) - 1;
876 // If this is a thumb spill / restore, we will be using a constpool load to
877 // materialize the offset.
878 if (AddrMode == ARMII::AddrModeTs && isThumSpillRestore)
879 ImmOp.ChangeToImmediate(0);
881 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
882 ImmedOffset = ImmedOffset & Mask;
884 ImmedOffset |= 1 << NumBits;
885 ImmOp.ChangeToImmediate(ImmedOffset);
886 Offset &= ~(Mask*Scale);
890 // If we get here, the immediate doesn't fit into the instruction. We folded
891 // as much as possible above, handle the rest, providing a register that is
893 assert(Offset && "This code isn't needed if offset already handled!");
896 if (TII.isLoad(Opcode)) {
897 // Use the destination register to materialize sp + offset.
898 unsigned TmpReg = MI.getOperand(0).getReg();
900 if (Opcode == ARM::tRestore) {
901 if (FrameReg == ARM::SP)
902 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
904 emitLoadConstPool(MBB, II, TmpReg, Offset, TII, true);
908 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
909 MI.setInstrDescriptor(TII.get(ARM::tLDR));
910 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
912 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
914 MI.addRegOperand(0, false); // tLDR has an extra register operand.
915 } else if (TII.isStore(Opcode)) {
916 // FIXME! This is horrific!!! We need register scavenging.
917 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
918 // also a ABI register so it's possible that is is the register that is
919 // being storing here. If that's the case, we do the following:
921 // Use r2 to materialize sp + offset
924 unsigned ValReg = MI.getOperand(0).getReg();
925 unsigned TmpReg = ARM::R3;
927 if (ValReg == ARM::R3) {
928 BuildMI(MBB, II, TII.get(ARM::tMOVr), ARM::R12)
929 .addReg(ARM::R2, false, false, true);
932 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
933 BuildMI(MBB, II, TII.get(ARM::tMOVr), ARM::R12)
934 .addReg(ARM::R3, false, false, true);
935 if (Opcode == ARM::tSpill) {
936 if (FrameReg == ARM::SP)
937 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
939 emitLoadConstPool(MBB, II, TmpReg, Offset, TII, true);
943 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
944 MI.setInstrDescriptor(TII.get(ARM::tSTR));
945 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
947 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
949 MI.addRegOperand(0, false); // tSTR has an extra register operand.
951 MachineBasicBlock::iterator NII = next(II);
952 if (ValReg == ARM::R3)
953 BuildMI(MBB, NII, TII.get(ARM::tMOVr), ARM::R2)
954 .addReg(ARM::R12, false, false, true);
955 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
956 BuildMI(MBB, NII, TII.get(ARM::tMOVr), ARM::R3)
957 .addReg(ARM::R12, false, false, true);
959 assert(false && "Unexpected opcode!");
961 // Insert a set of r12 with the full address: r12 = sp + offset
962 // If the offset we have is too large to fit into the instruction, we need
963 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
965 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
967 // No register is "free". Scavenge a register.
968 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II);
969 emitARMRegPlusImmediate(MBB, II, ScratchReg, FrameReg,
970 isSub ? -Offset : Offset, TII);
971 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
975 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
976 const MachineFrameInfo *FFI = MF.getFrameInfo();
978 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
979 int FixedOff = -FFI->getObjectOffset(i);
980 if (FixedOff > Offset) Offset = FixedOff;
982 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
983 Offset += FFI->getObjectSize(i);
984 unsigned Align = FFI->getObjectAlignment(i);
985 // Adjust to alignment boundary
986 Offset = (Offset+Align-1)/Align*Align;
988 return (unsigned)Offset;
992 ARMRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
993 RegScavenger *RS) const {
994 // This tells PEI to spill the FP as if it is any other callee-save register
995 // to take advantage the eliminateFrameIndex machinery. This also ensures it
996 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
997 // to combine multiple loads / stores.
998 bool CanEliminateFrame = true;
999 bool CS1Spilled = false;
1000 bool LRSpilled = false;
1001 unsigned NumGPRSpills = 0;
1002 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1003 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1004 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1006 // Don't spill FP if the frame can be eliminated. This is determined
1007 // by scanning the callee-save registers to see if any is used.
1008 const unsigned *CSRegs = getCalleeSavedRegs();
1009 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
1010 for (unsigned i = 0; CSRegs[i]; ++i) {
1011 unsigned Reg = CSRegs[i];
1012 bool Spilled = false;
1013 if (MF.isPhysRegUsed(Reg)) {
1014 AFI->setCSRegisterIsSpilled(Reg);
1016 CanEliminateFrame = false;
1018 // Check alias registers too.
1019 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
1020 if (MF.isPhysRegUsed(*Aliases)) {
1022 CanEliminateFrame = false;
1027 if (CSRegClasses[i] == &ARM::GPRRegClass) {
1031 if (!STI.isTargetDarwin()) {
1039 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1054 if (!STI.isTargetDarwin()) {
1055 UnspilledCS1GPRs.push_back(Reg);
1065 UnspilledCS1GPRs.push_back(Reg);
1068 UnspilledCS2GPRs.push_back(Reg);
1075 bool ForceLRSpill = false;
1076 if (!LRSpilled && AFI->isThumbFunction()) {
1077 unsigned FnSize = ARM::GetFunctionSize(MF);
1078 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1079 // use of BL to implement far jump. If it turns out that it's not needed
1080 // then the branch fix up path will undo it.
1081 if (FnSize >= (1 << 11)) {
1082 CanEliminateFrame = false;
1083 ForceLRSpill = true;
1087 bool ExtraCSSpill = false;
1088 if (!CanEliminateFrame || hasFP(MF)) {
1089 AFI->setHasStackFrame(true);
1091 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1092 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1093 if (!LRSpilled && CS1Spilled) {
1094 MF.setPhysRegUsed(ARM::LR);
1095 AFI->setCSRegisterIsSpilled(ARM::LR);
1097 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1098 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1099 ForceLRSpill = false;
1100 ExtraCSSpill = true;
1103 // Darwin ABI requires FP to point to the stack slot that contains the
1105 if (STI.isTargetDarwin() || hasFP(MF)) {
1106 MF.setPhysRegUsed(FramePtr);
1110 // If stack and double are 8-byte aligned and we are spilling an odd number
1111 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
1112 // the integer and double callee save areas.
1113 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1114 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1115 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1116 unsigned Reg = UnspilledCS1GPRs.front();
1117 MF.setPhysRegUsed(Reg);
1118 AFI->setCSRegisterIsSpilled(Reg);
1119 if (!isReservedReg(MF, Reg))
1120 ExtraCSSpill = true;
1121 } else if (!UnspilledCS2GPRs.empty()) {
1122 unsigned Reg = UnspilledCS2GPRs.front();
1123 MF.setPhysRegUsed(Reg);
1124 AFI->setCSRegisterIsSpilled(Reg);
1125 if (!isReservedReg(MF, Reg))
1126 ExtraCSSpill = true;
1130 // Estimate if we might need to scavenge a register at some point in order
1131 // to materialize a stack offset. If so, either spill one additiona
1132 // callee-saved register or reserve a special spill slot to facilitate
1133 // register scavenging.
1134 if (RS && !ExtraCSSpill && !AFI->isThumbFunction()) {
1135 MachineFrameInfo *MFI = MF.getFrameInfo();
1136 unsigned Size = estimateStackSize(MF, MFI);
1137 unsigned Limit = (1 << 12) - 1;
1138 for (MachineFunction::iterator BB = MF.begin(),E = MF.end();BB != E; ++BB)
1139 for (MachineBasicBlock::iterator I= BB->begin(); I != BB->end(); ++I) {
1140 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1141 if (I->getOperand(i).isFrameIndex()) {
1142 unsigned Opcode = I->getOpcode();
1143 const TargetInstrDescriptor &Desc = TII.get(Opcode);
1144 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1145 if (AddrMode == ARMII::AddrMode3) {
1146 Limit = (1 << 8) - 1;
1147 goto DoneEstimating;
1148 } else if (AddrMode == ARMII::AddrMode5) {
1149 Limit = ((1 << 8) - 1) * 4;
1150 goto DoneEstimating;
1155 if (Size >= Limit) {
1156 // If any non-reserved CS register isn't spilled, just spill one or two
1157 // extra. That should take care of it!
1158 unsigned NumExtras = TargetAlign / 4;
1159 SmallVector<unsigned, 2> Extras;
1160 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1161 unsigned Reg = UnspilledCS1GPRs.back();
1162 UnspilledCS1GPRs.pop_back();
1163 if (!isReservedReg(MF, Reg)) {
1164 Extras.push_back(Reg);
1168 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1169 unsigned Reg = UnspilledCS2GPRs.back();
1170 UnspilledCS2GPRs.pop_back();
1171 if (!isReservedReg(MF, Reg)) {
1172 Extras.push_back(Reg);
1176 if (Extras.size() && NumExtras == 0) {
1177 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1178 MF.setPhysRegUsed(Extras[i]);
1179 AFI->setCSRegisterIsSpilled(Extras[i]);
1182 // Reserve a slot closest to SP or frame pointer.
1183 const TargetRegisterClass *RC = &ARM::GPRRegClass;
1184 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1185 RC->getAlignment()));
1192 MF.setPhysRegUsed(ARM::LR);
1193 AFI->setCSRegisterIsSpilled(ARM::LR);
1194 AFI->setLRIsSpilledForFarJump(true);
1198 /// Move iterator pass the next bunch of callee save load / store ops for
1199 /// the particular spill area (1: integer area 1, 2: integer area 2,
1200 /// 3: fp area, 0: don't care).
1201 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1202 MachineBasicBlock::iterator &MBBI,
1203 int Opc, unsigned Area,
1204 const ARMSubtarget &STI) {
1205 while (MBBI != MBB.end() &&
1206 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFrameIndex()) {
1209 unsigned Category = 0;
1210 switch (MBBI->getOperand(0).getReg()) {
1211 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1215 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1216 Category = STI.isTargetDarwin() ? 2 : 1;
1218 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1219 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1226 if (Done || Category != Area)
1234 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
1235 MachineBasicBlock &MBB = MF.front();
1236 MachineBasicBlock::iterator MBBI = MBB.begin();
1237 MachineFrameInfo *MFI = MF.getFrameInfo();
1238 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1239 bool isThumb = AFI->isThumbFunction();
1240 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1241 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1242 unsigned NumBytes = MFI->getStackSize();
1243 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1246 // Check if R3 is live in. It might have to be used as a scratch register.
1247 for (MachineFunction::livein_iterator I=MF.livein_begin(),E=MF.livein_end();
1249 if ((*I).first == ARM::R3) {
1250 AFI->setR3IsLiveIn(true);
1255 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1256 NumBytes = (NumBytes + 3) & ~3;
1257 MFI->setStackSize(NumBytes);
1260 // Determine the sizes of each callee-save spill areas and record which frame
1261 // belongs to which callee-save spill areas.
1262 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1263 int FramePtrSpillFI = 0;
1266 emitSPUpdate(MBB, MBBI, -VARegSaveSize, isThumb, TII);
1268 if (!AFI->hasStackFrame()) {
1270 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1274 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1275 unsigned Reg = CSI[i].getReg();
1276 int FI = CSI[i].getFrameIdx();
1283 if (Reg == FramePtr)
1284 FramePtrSpillFI = FI;
1285 AFI->addGPRCalleeSavedArea1Frame(FI);
1292 if (Reg == FramePtr)
1293 FramePtrSpillFI = FI;
1294 if (STI.isTargetDarwin()) {
1295 AFI->addGPRCalleeSavedArea2Frame(FI);
1298 AFI->addGPRCalleeSavedArea1Frame(FI);
1303 AFI->addDPRCalleeSavedAreaFrame(FI);
1308 if (Align == 8 && (GPRCS1Size & 7) != 0)
1309 // Pad CS1 to ensure proper alignment.
1313 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1314 emitSPUpdate(MBB, MBBI, -GPRCS1Size, isThumb, TII);
1315 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1316 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH)
1319 // Darwin ABI requires FP to point to the stack slot that contains the
1321 if (STI.isTargetDarwin() || hasFP(MF))
1322 BuildMI(MBB, MBBI, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri), FramePtr)
1323 .addFrameIndex(FramePtrSpillFI).addImm(0);
1326 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1327 emitSPUpdate(MBB, MBBI, -GPRCS2Size, false, TII);
1329 // Build the new SUBri to adjust SP for FP callee-save spill area.
1330 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1331 emitSPUpdate(MBB, MBBI, -DPRCSSize, false, TII);
1334 // Determine starting offsets of spill areas.
1335 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1336 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1337 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1338 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1339 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1340 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1341 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1343 NumBytes = DPRCSOffset;
1345 // Insert it after all the callee-save spills.
1347 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1348 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1351 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1352 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1353 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1356 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1357 for (unsigned i = 0; CSRegs[i]; ++i)
1358 if (Reg == CSRegs[i])
1363 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1364 return ((MI->getOpcode() == ARM::FLDD ||
1365 MI->getOpcode() == ARM::LDR ||
1366 MI->getOpcode() == ARM::tRestore) &&
1367 MI->getOperand(1).isFrameIndex() &&
1368 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1371 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1372 MachineBasicBlock &MBB) const {
1373 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1374 assert((MBBI->getOpcode() == ARM::BX_RET ||
1375 MBBI->getOpcode() == ARM::tBX_RET ||
1376 MBBI->getOpcode() == ARM::tPOP_RET) &&
1377 "Can only insert epilog into returning blocks");
1379 MachineFrameInfo *MFI = MF.getFrameInfo();
1380 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1381 bool isThumb = AFI->isThumbFunction();
1382 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1383 int NumBytes = (int)MFI->getStackSize();
1384 if (!AFI->hasStackFrame()) {
1386 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1388 // Unwind MBBI to point to first LDR / FLDD.
1389 const unsigned *CSRegs = getCalleeSavedRegs();
1390 if (MBBI != MBB.begin()) {
1393 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1394 if (!isCSRestore(MBBI, CSRegs))
1398 // Move SP to start of FP callee save spill area.
1399 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1400 AFI->getGPRCalleeSavedArea2Size() +
1401 AFI->getDPRCalleeSavedAreaSize());
1404 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1405 // Reset SP based on frame pointer only if the stack frame extends beyond
1406 // frame pointer stack slot or target is ELF and the function has FP.
1408 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes, TII);
1410 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::SP).addReg(FramePtr);
1412 if (MBBI->getOpcode() == ARM::tBX_RET &&
1413 &MBB.front() != MBBI &&
1414 prior(MBBI)->getOpcode() == ARM::tPOP) {
1415 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1416 emitSPUpdate(MBB, PMBBI, NumBytes, isThumb, TII);
1418 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1421 // Darwin ABI requires FP to point to the stack slot that contains the
1423 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1424 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1425 // Reset SP based on frame pointer only if the stack frame extends beyond
1426 // frame pointer stack slot or target is ELF and the function has FP.
1427 if (AFI->getGPRCalleeSavedArea2Size() ||
1428 AFI->getDPRCalleeSavedAreaSize() ||
1429 AFI->getDPRCalleeSavedAreaOffset()||
1432 BuildMI(MBB, MBBI, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1435 BuildMI(MBB, MBBI, TII.get(ARM::MOVr), ARM::SP).addReg(FramePtr);
1436 } else if (NumBytes) {
1437 emitSPUpdate(MBB, MBBI, NumBytes, false, TII);
1440 // Move SP to start of integer callee save spill area 2.
1441 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1442 emitSPUpdate(MBB, MBBI, AFI->getDPRCalleeSavedAreaSize(), false, TII);
1444 // Move SP to start of integer callee save spill area 1.
1445 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1446 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea2Size(), false, TII);
1448 // Move SP to SP upon entry to the function.
1449 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1450 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea1Size(), false, TII);
1454 if (VARegSaveSize) {
1456 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1457 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1458 BuildMI(MBB, MBBI, TII.get(ARM::tPOP)).addReg(ARM::R3);
1460 emitSPUpdate(MBB, MBBI, VARegSaveSize, isThumb, TII);
1463 BuildMI(MBB, MBBI, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1469 unsigned ARMRegisterInfo::getRARegister() const {
1473 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1474 if (STI.isTargetDarwin() || hasFP(MF))
1475 return STI.useThumbBacktraces() ? ARM::R7 : ARM::R11;
1480 unsigned ARMRegisterInfo::getEHExceptionRegister() const {
1481 assert(0 && "What is the exception register");
1485 unsigned ARMRegisterInfo::getEHHandlerRegister() const {
1486 assert(0 && "What is the exception handler register");
1490 #include "ARMGenRegisterInfo.inc"