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() || STI.isThumb()) ? 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).addImm((int64_t)ARMCC::AL);
148 } else if (RC == ARM::DPRRegisterClass) {
149 BuildMI(MBB, I, TII.get(ARM::FSTD)).addReg(SrcReg, false, false, true)
150 .addFrameIndex(FI).addImm(0).addImm((int64_t)ARMCC::AL);
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).addImm((int64_t)ARMCC::AL);
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).addImm((int64_t)ARMCC::AL);
171 } else if (RC == ARM::DPRRegisterClass) {
172 BuildMI(MBB, I, TII.get(ARM::FLDD), DestReg)
173 .addFrameIndex(FI).addImm(0).addImm((int64_t)ARMCC::AL);
175 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
176 BuildMI(MBB, I, TII.get(ARM::FLDS), DestReg)
177 .addFrameIndex(FI).addImm(0).addImm((int64_t)ARMCC::AL);
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 if (AFI->isThumbFunction())
189 BuildMI(MBB, I, TII.get(ARM::tMOVr), DestReg).addReg(SrcReg);
191 BuildMI(MBB, I, TII.get(ARM::MOVr), DestReg).addReg(SrcReg)
192 .addImm((int64_t)ARMCC::AL);
193 } else if (RC == ARM::SPRRegisterClass)
194 BuildMI(MBB, I, TII.get(ARM::FCPYS), DestReg).addReg(SrcReg)
195 .addImm((int64_t)ARMCC::AL);
196 else if (RC == ARM::DPRRegisterClass)
197 BuildMI(MBB, I, TII.get(ARM::FCPYD), DestReg).addReg(SrcReg)
198 .addImm((int64_t)ARMCC::AL);
203 /// emitLoadConstPool - Emits a load from constpool to materialize the
204 /// specified immediate.
205 static void emitLoadConstPool(MachineBasicBlock &MBB,
206 MachineBasicBlock::iterator &MBBI,
207 unsigned DestReg, ARMCC::CondCodes Pred, int Val,
208 const TargetInstrInfo &TII, bool isThumb) {
209 MachineFunction &MF = *MBB.getParent();
210 MachineConstantPool *ConstantPool = MF.getConstantPool();
211 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
212 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 2);
214 BuildMI(MBB, MBBI, TII.get(ARM::tLDRcp), DestReg).addConstantPoolIndex(Idx);
216 BuildMI(MBB, MBBI, TII.get(ARM::LDRcp), DestReg).addConstantPoolIndex(Idx)
217 .addReg(0).addImm(0).addImm((unsigned)Pred);
220 void ARMRegisterInfo::reMaterialize(MachineBasicBlock &MBB,
221 MachineBasicBlock::iterator I,
223 const MachineInstr *Orig) const {
224 if (Orig->getOpcode() == ARM::MOVi2pieces) {
225 emitLoadConstPool(MBB, I, DestReg,
226 (ARMCC::CondCodes)Orig->getOperand(2).getImmedValue(),
227 Orig->getOperand(1).getImmedValue(),
232 MachineInstr *MI = Orig->clone();
233 MI->getOperand(0).setReg(DestReg);
237 /// isLowRegister - Returns true if the register is low register r0-r7.
239 static bool isLowRegister(unsigned Reg) {
242 case R0: case R1: case R2: case R3:
243 case R4: case R5: case R6: case R7:
250 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr *MI,
251 unsigned OpNum, int FI) const {
252 unsigned Opc = MI->getOpcode();
253 MachineInstr *NewMI = NULL;
257 unsigned Pred = MI->getOperand(2).getImmedValue();
258 if (OpNum == 0) { // move -> store
259 unsigned SrcReg = MI->getOperand(1).getReg();
260 NewMI = BuildMI(TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
261 .addReg(0).addImm(0).addImm(Pred);
262 } else { // move -> load
263 unsigned DstReg = MI->getOperand(0).getReg();
264 NewMI = BuildMI(TII.get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
265 .addImm(0).addImm(Pred);
270 if (OpNum == 0) { // move -> store
271 unsigned SrcReg = MI->getOperand(1).getReg();
272 if (isPhysicalRegister(SrcReg) && !isLowRegister(SrcReg))
273 // tSpill cannot take a high register operand.
275 NewMI = BuildMI(TII.get(ARM::tSpill)).addReg(SrcReg).addFrameIndex(FI)
277 } else { // move -> load
278 unsigned DstReg = MI->getOperand(0).getReg();
279 if (isPhysicalRegister(DstReg) && !isLowRegister(DstReg))
280 // tRestore cannot target a high register operand.
282 NewMI = BuildMI(TII.get(ARM::tRestore), DstReg).addFrameIndex(FI)
288 unsigned Pred = MI->getOperand(2).getImmedValue();
289 if (OpNum == 0) { // move -> store
290 unsigned SrcReg = MI->getOperand(1).getReg();
291 NewMI = BuildMI(TII.get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
292 .addImm(0).addImm(Pred);
293 } else { // move -> load
294 unsigned DstReg = MI->getOperand(0).getReg();
295 NewMI = BuildMI(TII.get(ARM::FLDS), DstReg).addFrameIndex(FI)
296 .addImm(0).addImm(Pred);
301 unsigned Pred = MI->getOperand(2).getImmedValue();
302 if (OpNum == 0) { // move -> store
303 unsigned SrcReg = MI->getOperand(1).getReg();
304 NewMI = BuildMI(TII.get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
305 .addImm(0).addImm(Pred);
306 } else { // move -> load
307 unsigned DstReg = MI->getOperand(0).getReg();
308 NewMI = BuildMI(TII.get(ARM::FLDD), DstReg).addFrameIndex(FI)
309 .addImm(0).addImm(Pred);
316 NewMI->copyKillDeadInfo(MI);
320 const unsigned* ARMRegisterInfo::getCalleeSavedRegs() const {
321 static const unsigned CalleeSavedRegs[] = {
322 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
323 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
325 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
326 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
330 static const unsigned DarwinCalleeSavedRegs[] = {
331 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
332 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
334 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
335 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
338 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
341 const TargetRegisterClass* const *
342 ARMRegisterInfo::getCalleeSavedRegClasses() const {
343 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
344 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
345 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
346 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
348 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
349 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
352 return CalleeSavedRegClasses;
355 BitVector ARMRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
356 // FIXME: avoid re-calculating this everytime.
357 BitVector Reserved(getNumRegs());
358 Reserved.set(ARM::SP);
359 Reserved.set(ARM::PC);
360 if (STI.isTargetDarwin() || hasFP(MF))
361 Reserved.set(FramePtr);
362 // Some targets reserve R9.
363 if (STI.isR9Reserved())
364 Reserved.set(ARM::R9);
369 ARMRegisterInfo::isReservedReg(const MachineFunction &MF, unsigned Reg) const {
377 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
381 return STI.isR9Reserved();
388 ARMRegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
389 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
390 return ThumbRegScavenging || !AFI->isThumbFunction();
393 /// hasFP - Return true if the specified function should have a dedicated frame
394 /// pointer register. This is true if the function has variable sized allocas
395 /// or if frame pointer elimination is disabled.
397 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
398 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
401 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
402 // not required, we reserve argument space for call sites in the function
403 // immediately on entry to the current function. This eliminates the need for
404 // add/sub sp brackets around call sites. Returns true if the call frame is
405 // included as part of the stack frame.
406 bool ARMRegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
407 const MachineFrameInfo *FFI = MF.getFrameInfo();
408 unsigned CFSize = FFI->getMaxCallFrameSize();
409 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
410 // It's not always a good idea to include the call frame as part of the
411 // stack frame. ARM (especially Thumb) has small immediate offset to
412 // address the stack frame. So a large call frame can cause poor codegen
413 // and may even makes it impossible to scavenge a register.
414 if (AFI->isThumbFunction()) {
415 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
418 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
424 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
425 /// a destreg = basereg + immediate in ARM code.
427 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
428 MachineBasicBlock::iterator &MBBI,
429 unsigned DestReg, unsigned BaseReg,
430 ARMCC::CondCodes Pred,
431 int NumBytes, const TargetInstrInfo &TII) {
432 bool isSub = NumBytes < 0;
433 if (isSub) NumBytes = -NumBytes;
436 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
437 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
438 assert(ThisVal && "Didn't extract field correctly");
440 // We will handle these bits from offset, clear them.
441 NumBytes &= ~ThisVal;
443 // Get the properly encoded SOImmVal field.
444 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
445 assert(SOImmVal != -1 && "Bit extraction didn't work?");
447 // Build the new ADD / SUB.
448 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
449 .addReg(BaseReg, false, false, true).addImm(SOImmVal)
450 .addImm((unsigned)Pred);
455 /// calcNumMI - Returns the number of instructions required to materialize
456 /// the specific add / sub r, c instruction.
457 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
458 unsigned NumBits, unsigned Scale) {
460 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
462 if (Opc == ARM::tADDrSPi) {
463 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
467 Scale = 1; // Followed by a number of tADDi8.
468 Chunk = ((1 << NumBits) - 1) * Scale;
471 NumMIs += Bytes / Chunk;
472 if ((Bytes % Chunk) != 0)
479 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
480 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
481 /// in a register using mov / mvn sequences or load the immediate from a
484 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
485 MachineBasicBlock::iterator &MBBI,
486 unsigned DestReg, unsigned BaseReg,
487 int NumBytes, bool CanChangeCC,
488 const TargetInstrInfo &TII) {
489 bool isHigh = !isLowRegister(DestReg) ||
490 (BaseReg != 0 && !isLowRegister(BaseReg));
492 // Subtract doesn't have high register version. Load the negative value
493 // if either base or dest register is a high register. Also, if do not
494 // issue sub as part of the sequence if condition register is to be
496 if (NumBytes < 0 && !isHigh && CanChangeCC) {
498 NumBytes = -NumBytes;
500 unsigned LdReg = DestReg;
501 if (DestReg == ARM::SP) {
502 assert(BaseReg == ARM::SP && "Unexpected!");
504 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::R12)
505 .addReg(ARM::R3, false, false, true);
508 if (NumBytes <= 255 && NumBytes >= 0)
509 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
510 else if (NumBytes < 0 && NumBytes >= -255) {
511 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), LdReg).addImm(NumBytes);
512 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), LdReg)
513 .addReg(LdReg, false, false, true);
515 emitLoadConstPool(MBB, MBBI, LdReg, ARMCC::AL, NumBytes, TII, true);
518 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
519 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, TII.get(Opc), DestReg);
520 if (DestReg == ARM::SP || isSub)
521 MIB.addReg(BaseReg).addReg(LdReg, false, false, true);
523 MIB.addReg(LdReg).addReg(BaseReg, false, false, true);
524 if (DestReg == ARM::SP)
525 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::R3)
526 .addReg(ARM::R12, false, false, true);
529 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
530 /// a destreg = basereg + immediate in Thumb code.
532 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
533 MachineBasicBlock::iterator &MBBI,
534 unsigned DestReg, unsigned BaseReg,
535 int NumBytes, const TargetInstrInfo &TII) {
536 bool isSub = NumBytes < 0;
537 unsigned Bytes = (unsigned)NumBytes;
538 if (isSub) Bytes = -NumBytes;
539 bool isMul4 = (Bytes & 3) == 0;
540 bool isTwoAddr = false;
541 bool DstNotEqBase = false;
542 unsigned NumBits = 1;
547 if (DestReg == BaseReg && BaseReg == ARM::SP) {
548 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
551 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
553 } else if (!isSub && BaseReg == ARM::SP) {
556 // r1 = add sp, 100 * 4
560 ExtraOpc = ARM::tADDi3;
569 if (DestReg != BaseReg)
572 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
576 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
577 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
578 if (NumMIs > Threshold) {
579 // This will expand into too many instructions. Load the immediate from a
581 emitThumbRegPlusImmInReg(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII);
586 if (isLowRegister(DestReg) && isLowRegister(BaseReg)) {
587 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
588 unsigned Chunk = (1 << 3) - 1;
589 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
591 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
592 .addReg(BaseReg, false, false, true).addImm(ThisVal);
594 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), DestReg)
595 .addReg(BaseReg, false, false, true);
600 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
602 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
605 // Build the new tADD / tSUB.
607 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(DestReg).addImm(ThisVal);
609 bool isKill = BaseReg != ARM::SP;
610 BuildMI(MBB, MBBI, TII.get(Opc), DestReg)
611 .addReg(BaseReg, false, false, isKill).addImm(ThisVal);
614 if (Opc == ARM::tADDrSPi) {
620 Chunk = ((1 << NumBits) - 1) * Scale;
621 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
628 BuildMI(MBB, MBBI, TII.get(ExtraOpc), DestReg)
629 .addReg(DestReg, false, false, true)
630 .addImm(((unsigned)NumBytes) & 3);
634 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
635 ARMCC::CondCodes Pred, int NumBytes, bool isThumb,
636 const TargetInstrInfo &TII) {
638 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
640 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, Pred, NumBytes, TII);
643 void ARMRegisterInfo::
644 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
645 MachineBasicBlock::iterator I) const {
646 if (!hasReservedCallFrame(MF)) {
647 // If we have alloca, convert as follows:
648 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
649 // ADJCALLSTACKUP -> add, sp, sp, amount
650 MachineInstr *Old = I;
651 unsigned Amount = Old->getOperand(0).getImmedValue();
653 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
654 // We need to keep the stack aligned properly. To do this, we round the
655 // amount of space needed for the outgoing arguments up to the next
656 // alignment boundary.
657 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
658 Amount = (Amount+Align-1)/Align*Align;
660 // Replace the pseudo instruction with a new instruction...
661 unsigned Opc = Old->getOpcode();
662 bool isThumb = AFI->isThumbFunction();
663 ARMCC::CondCodes Pred = isThumb
664 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(1).getImmedValue();
665 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
666 emitSPUpdate(MBB, I, Pred, -Amount, isThumb, TII);
668 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
669 emitSPUpdate(MBB, I, Pred, Amount, isThumb, TII);
676 /// emitThumbConstant - Emit a series of instructions to materialize a
678 static void emitThumbConstant(MachineBasicBlock &MBB,
679 MachineBasicBlock::iterator &MBBI,
680 unsigned DestReg, int Imm,
681 const TargetInstrInfo &TII) {
682 bool isSub = Imm < 0;
683 if (isSub) Imm = -Imm;
685 int Chunk = (1 << 8) - 1;
686 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
688 BuildMI(MBB, MBBI, TII.get(ARM::tMOVi8), DestReg).addImm(ThisVal);
690 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII);
692 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), DestReg)
693 .addReg(DestReg, false, false, true);
696 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
697 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
698 /// register first and then a spilled callee-saved register if that fails.
700 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
701 ARMFunctionInfo *AFI) {
702 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
704 // Try a already spilled CS register.
705 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
710 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
711 int SPAdj, RegScavenger *RS) const{
713 MachineInstr &MI = *II;
714 MachineBasicBlock &MBB = *MI.getParent();
715 MachineFunction &MF = *MBB.getParent();
716 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
717 bool isThumb = AFI->isThumbFunction();
719 while (!MI.getOperand(i).isFrameIndex()) {
721 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
724 unsigned FrameReg = ARM::SP;
725 int FrameIndex = MI.getOperand(i).getFrameIndex();
726 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
727 MF.getFrameInfo()->getStackSize() + SPAdj;
729 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
730 Offset -= AFI->getGPRCalleeSavedArea1Offset();
731 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
732 Offset -= AFI->getGPRCalleeSavedArea2Offset();
733 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
734 Offset -= AFI->getDPRCalleeSavedAreaOffset();
735 else if (hasFP(MF)) {
736 assert(SPAdj == 0 && "Unexpected");
737 // There is alloca()'s in this function, must reference off the frame
739 FrameReg = getFrameRegister(MF);
740 Offset -= AFI->getFramePtrSpillOffset();
743 unsigned Opcode = MI.getOpcode();
744 const TargetInstrDescriptor &Desc = TII.get(Opcode);
745 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
748 if (Opcode == ARM::ADDri) {
749 Offset += MI.getOperand(i+1).getImm();
751 // Turn it into a move.
752 MI.setInstrDescriptor(TII.get(ARM::MOVr));
753 MI.getOperand(i).ChangeToRegister(FrameReg, false);
754 MI.RemoveOperand(i+1);
756 } else if (Offset < 0) {
759 MI.setInstrDescriptor(TII.get(ARM::SUBri));
762 // Common case: small offset, fits into instruction.
763 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
764 if (ImmedOffset != -1) {
765 // Replace the FrameIndex with sp / fp
766 MI.getOperand(i).ChangeToRegister(FrameReg, false);
767 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
771 // Otherwise, we fallback to common code below to form the imm offset with
772 // a sequence of ADDri instructions. First though, pull as much of the imm
773 // into this ADDri as possible.
774 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
775 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, RotAmt);
777 // We will handle these bits from offset, clear them.
778 Offset &= ~ThisImmVal;
780 // Get the properly encoded SOImmVal field.
781 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
782 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
783 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
784 } else if (Opcode == ARM::tADDrSPi) {
785 Offset += MI.getOperand(i+1).getImm();
787 // Can't use tADDrSPi if it's based off the frame pointer.
788 unsigned NumBits = 0;
790 if (FrameReg != ARM::SP) {
791 Opcode = ARM::tADDi3;
792 MI.setInstrDescriptor(TII.get(ARM::tADDi3));
797 assert((Offset & 3) == 0 &&
798 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
802 // Turn it into a move.
803 MI.setInstrDescriptor(TII.get(ARM::tMOVr));
804 MI.getOperand(i).ChangeToRegister(FrameReg, false);
805 MI.RemoveOperand(i+1);
809 // Common case: small offset, fits into instruction.
810 unsigned Mask = (1 << NumBits) - 1;
811 if (((Offset / Scale) & ~Mask) == 0) {
812 // Replace the FrameIndex with sp / fp
813 MI.getOperand(i).ChangeToRegister(FrameReg, false);
814 MI.getOperand(i+1).ChangeToImmediate(Offset / Scale);
818 unsigned DestReg = MI.getOperand(0).getReg();
819 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
820 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, NumBits, Scale);
821 // MI would expand into a large number of instructions. Don't try to
822 // simplify the immediate.
824 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII);
830 // Translate r0 = add sp, imm to
831 // r0 = add sp, 255*4
832 // r0 = add r0, (imm - 255*4)
833 MI.getOperand(i).ChangeToRegister(FrameReg, false);
834 MI.getOperand(i+1).ChangeToImmediate(Mask);
835 Offset = (Offset - Mask * Scale);
836 MachineBasicBlock::iterator NII = next(II);
837 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII);
839 // Translate r0 = add sp, -imm to
840 // r0 = -imm (this is then translated into a series of instructons)
842 emitThumbConstant(MBB, II, DestReg, Offset, TII);
843 MI.setInstrDescriptor(TII.get(ARM::tADDhirr));
844 MI.getOperand(i).ChangeToRegister(DestReg, false, false, true);
845 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
851 unsigned NumBits = 0;
854 case ARMII::AddrMode2: {
856 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
857 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
862 case ARMII::AddrMode3: {
864 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
865 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
870 case ARMII::AddrMode5: {
872 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
873 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
879 case ARMII::AddrModeTs: {
881 InstrOffs = MI.getOperand(ImmIdx).getImm();
882 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
887 assert(0 && "Unsupported addressing mode!");
892 Offset += InstrOffs * Scale;
893 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
894 if (Offset < 0 && !isThumb) {
899 // Common case: small offset, fits into instruction.
900 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
901 int ImmedOffset = Offset / Scale;
902 unsigned Mask = (1 << NumBits) - 1;
903 if ((unsigned)Offset <= Mask * Scale) {
904 // Replace the FrameIndex with sp
905 MI.getOperand(i).ChangeToRegister(FrameReg, false);
907 ImmedOffset |= 1 << NumBits;
908 ImmOp.ChangeToImmediate(ImmedOffset);
912 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
913 if (AddrMode == ARMII::AddrModeTs) {
914 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
915 // a different base register.
917 Mask = (1 << NumBits) - 1;
919 // If this is a thumb spill / restore, we will be using a constpool load to
920 // materialize the offset.
921 if (AddrMode == ARMII::AddrModeTs && isThumSpillRestore)
922 ImmOp.ChangeToImmediate(0);
924 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
925 ImmedOffset = ImmedOffset & Mask;
927 ImmedOffset |= 1 << NumBits;
928 ImmOp.ChangeToImmediate(ImmedOffset);
929 Offset &= ~(Mask*Scale);
933 // If we get here, the immediate doesn't fit into the instruction. We folded
934 // as much as possible above, handle the rest, providing a register that is
936 assert(Offset && "This code isn't needed if offset already handled!");
939 if (TII.isLoad(Opcode)) {
940 // Use the destination register to materialize sp + offset.
941 unsigned TmpReg = MI.getOperand(0).getReg();
943 if (Opcode == ARM::tRestore) {
944 if (FrameReg == ARM::SP)
945 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
947 emitLoadConstPool(MBB, II, TmpReg, ARMCC::AL, Offset, TII, true);
951 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
952 MI.setInstrDescriptor(TII.get(ARM::tLDR));
953 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
955 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
957 MI.addRegOperand(0, false); // tLDR has an extra register operand.
958 } else if (TII.isStore(Opcode)) {
959 // FIXME! This is horrific!!! We need register scavenging.
960 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
961 // also a ABI register so it's possible that is is the register that is
962 // being storing here. If that's the case, we do the following:
964 // Use r2 to materialize sp + offset
967 unsigned ValReg = MI.getOperand(0).getReg();
968 unsigned TmpReg = ARM::R3;
970 if (ValReg == ARM::R3) {
971 BuildMI(MBB, II, TII.get(ARM::tMOVr), ARM::R12)
972 .addReg(ARM::R2, false, false, true);
975 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
976 BuildMI(MBB, II, TII.get(ARM::tMOVr), ARM::R12)
977 .addReg(ARM::R3, false, false, true);
978 if (Opcode == ARM::tSpill) {
979 if (FrameReg == ARM::SP)
980 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,Offset,false,TII);
982 emitLoadConstPool(MBB, II, TmpReg, ARMCC::AL, Offset, TII, true);
986 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
987 MI.setInstrDescriptor(TII.get(ARM::tSTR));
988 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
990 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
992 MI.addRegOperand(0, false); // tSTR has an extra register operand.
994 MachineBasicBlock::iterator NII = next(II);
995 if (ValReg == ARM::R3)
996 BuildMI(MBB, NII, TII.get(ARM::tMOVr), ARM::R2)
997 .addReg(ARM::R12, false, false, true);
998 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
999 BuildMI(MBB, NII, TII.get(ARM::tMOVr), ARM::R3)
1000 .addReg(ARM::R12, false, false, true);
1002 assert(false && "Unexpected opcode!");
1004 // Insert a set of r12 with the full address: r12 = sp + offset
1005 // If the offset we have is too large to fit into the instruction, we need
1006 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
1008 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
1009 if (ScratchReg == 0)
1010 // No register is "free". Scavenge a register.
1011 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II, SPAdj);
1012 MachineOperand *MO = MI.findFirstPredOperand();
1013 ARMCC::CondCodes Pred = MO ?
1014 (ARMCC::CondCodes)MO->getImmedValue() : ARMCC::AL;
1015 emitARMRegPlusImmediate(MBB, II, ScratchReg, FrameReg, Pred,
1016 isSub ? -Offset : Offset, TII);
1017 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1021 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
1022 const MachineFrameInfo *FFI = MF.getFrameInfo();
1024 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
1025 int FixedOff = -FFI->getObjectOffset(i);
1026 if (FixedOff > Offset) Offset = FixedOff;
1028 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
1029 Offset += FFI->getObjectSize(i);
1030 unsigned Align = FFI->getObjectAlignment(i);
1031 // Adjust to alignment boundary
1032 Offset = (Offset+Align-1)/Align*Align;
1034 return (unsigned)Offset;
1038 ARMRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
1039 RegScavenger *RS) const {
1040 // This tells PEI to spill the FP as if it is any other callee-save register
1041 // to take advantage the eliminateFrameIndex machinery. This also ensures it
1042 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
1043 // to combine multiple loads / stores.
1044 bool CanEliminateFrame = true;
1045 bool CS1Spilled = false;
1046 bool LRSpilled = false;
1047 unsigned NumGPRSpills = 0;
1048 SmallVector<unsigned, 4> UnspilledCS1GPRs;
1049 SmallVector<unsigned, 4> UnspilledCS2GPRs;
1050 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1052 // Don't spill FP if the frame can be eliminated. This is determined
1053 // by scanning the callee-save registers to see if any is used.
1054 const unsigned *CSRegs = getCalleeSavedRegs();
1055 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
1056 for (unsigned i = 0; CSRegs[i]; ++i) {
1057 unsigned Reg = CSRegs[i];
1058 bool Spilled = false;
1059 if (MF.isPhysRegUsed(Reg)) {
1060 AFI->setCSRegisterIsSpilled(Reg);
1062 CanEliminateFrame = false;
1064 // Check alias registers too.
1065 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
1066 if (MF.isPhysRegUsed(*Aliases)) {
1068 CanEliminateFrame = false;
1073 if (CSRegClasses[i] == &ARM::GPRRegClass) {
1077 if (!STI.isTargetDarwin()) {
1084 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
1099 if (!STI.isTargetDarwin()) {
1100 UnspilledCS1GPRs.push_back(Reg);
1110 UnspilledCS1GPRs.push_back(Reg);
1113 UnspilledCS2GPRs.push_back(Reg);
1120 bool ForceLRSpill = false;
1121 if (!LRSpilled && AFI->isThumbFunction()) {
1122 unsigned FnSize = ARM::GetFunctionSize(MF);
1123 // Force LR to be spilled if the Thumb function size is > 2048. This enables
1124 // use of BL to implement far jump. If it turns out that it's not needed
1125 // then the branch fix up path will undo it.
1126 if (FnSize >= (1 << 11)) {
1127 CanEliminateFrame = false;
1128 ForceLRSpill = true;
1132 bool ExtraCSSpill = false;
1133 if (!CanEliminateFrame || hasFP(MF)) {
1134 AFI->setHasStackFrame(true);
1136 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
1137 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
1138 if (!LRSpilled && CS1Spilled) {
1139 MF.setPhysRegUsed(ARM::LR);
1140 AFI->setCSRegisterIsSpilled(ARM::LR);
1142 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
1143 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
1144 ForceLRSpill = false;
1145 ExtraCSSpill = true;
1148 // Darwin ABI requires FP to point to the stack slot that contains the
1150 if (STI.isTargetDarwin() || hasFP(MF)) {
1151 MF.setPhysRegUsed(FramePtr);
1155 // If stack and double are 8-byte aligned and we are spilling an odd number
1156 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
1157 // the integer and double callee save areas.
1158 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
1159 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
1160 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
1161 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
1162 unsigned Reg = UnspilledCS1GPRs[i];
1163 // Don't spiil high register if the function is thumb
1164 if (!AFI->isThumbFunction() || isLowRegister(Reg) || Reg == ARM::LR) {
1165 MF.setPhysRegUsed(Reg);
1166 AFI->setCSRegisterIsSpilled(Reg);
1167 if (!isReservedReg(MF, Reg))
1168 ExtraCSSpill = true;
1172 } else if (!UnspilledCS2GPRs.empty() &&
1173 !AFI->isThumbFunction()) {
1174 unsigned Reg = UnspilledCS2GPRs.front();
1175 MF.setPhysRegUsed(Reg);
1176 AFI->setCSRegisterIsSpilled(Reg);
1177 if (!isReservedReg(MF, Reg))
1178 ExtraCSSpill = true;
1182 // Estimate if we might need to scavenge a register at some point in order
1183 // to materialize a stack offset. If so, either spill one additiona
1184 // callee-saved register or reserve a special spill slot to facilitate
1185 // register scavenging.
1186 if (RS && !ExtraCSSpill && !AFI->isThumbFunction()) {
1187 MachineFrameInfo *MFI = MF.getFrameInfo();
1188 unsigned Size = estimateStackSize(MF, MFI);
1189 unsigned Limit = (1 << 12) - 1;
1190 for (MachineFunction::iterator BB = MF.begin(),E = MF.end();BB != E; ++BB)
1191 for (MachineBasicBlock::iterator I= BB->begin(); I != BB->end(); ++I) {
1192 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
1193 if (I->getOperand(i).isFrameIndex()) {
1194 unsigned Opcode = I->getOpcode();
1195 const TargetInstrDescriptor &Desc = TII.get(Opcode);
1196 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1197 if (AddrMode == ARMII::AddrMode3) {
1198 Limit = (1 << 8) - 1;
1199 goto DoneEstimating;
1200 } else if (AddrMode == ARMII::AddrMode5) {
1201 unsigned ThisLimit = ((1 << 8) - 1) * 4;
1202 if (ThisLimit < Limit)
1208 if (Size >= Limit) {
1209 // If any non-reserved CS register isn't spilled, just spill one or two
1210 // extra. That should take care of it!
1211 unsigned NumExtras = TargetAlign / 4;
1212 SmallVector<unsigned, 2> Extras;
1213 while (NumExtras && !UnspilledCS1GPRs.empty()) {
1214 unsigned Reg = UnspilledCS1GPRs.back();
1215 UnspilledCS1GPRs.pop_back();
1216 if (!isReservedReg(MF, Reg)) {
1217 Extras.push_back(Reg);
1221 while (NumExtras && !UnspilledCS2GPRs.empty()) {
1222 unsigned Reg = UnspilledCS2GPRs.back();
1223 UnspilledCS2GPRs.pop_back();
1224 if (!isReservedReg(MF, Reg)) {
1225 Extras.push_back(Reg);
1229 if (Extras.size() && NumExtras == 0) {
1230 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
1231 MF.setPhysRegUsed(Extras[i]);
1232 AFI->setCSRegisterIsSpilled(Extras[i]);
1235 // Reserve a slot closest to SP or frame pointer.
1236 const TargetRegisterClass *RC = &ARM::GPRRegClass;
1237 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
1238 RC->getAlignment()));
1245 MF.setPhysRegUsed(ARM::LR);
1246 AFI->setCSRegisterIsSpilled(ARM::LR);
1247 AFI->setLRIsSpilledForFarJump(true);
1251 /// Move iterator pass the next bunch of callee save load / store ops for
1252 /// the particular spill area (1: integer area 1, 2: integer area 2,
1253 /// 3: fp area, 0: don't care).
1254 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1255 MachineBasicBlock::iterator &MBBI,
1256 int Opc, unsigned Area,
1257 const ARMSubtarget &STI) {
1258 while (MBBI != MBB.end() &&
1259 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFrameIndex()) {
1262 unsigned Category = 0;
1263 switch (MBBI->getOperand(0).getReg()) {
1264 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1268 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1269 Category = STI.isTargetDarwin() ? 2 : 1;
1271 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1272 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1279 if (Done || Category != Area)
1287 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
1288 MachineBasicBlock &MBB = MF.front();
1289 MachineBasicBlock::iterator MBBI = MBB.begin();
1290 MachineFrameInfo *MFI = MF.getFrameInfo();
1291 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1292 bool isThumb = AFI->isThumbFunction();
1293 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1294 unsigned NumBytes = MFI->getStackSize();
1295 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1298 // Check if R3 is live in. It might have to be used as a scratch register.
1299 for (MachineFunction::livein_iterator I=MF.livein_begin(),E=MF.livein_end();
1301 if ((*I).first == ARM::R3) {
1302 AFI->setR3IsLiveIn(true);
1307 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1308 NumBytes = (NumBytes + 3) & ~3;
1309 MFI->setStackSize(NumBytes);
1312 // Determine the sizes of each callee-save spill areas and record which frame
1313 // belongs to which callee-save spill areas.
1314 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1315 int FramePtrSpillFI = 0;
1318 emitSPUpdate(MBB, MBBI, ARMCC::AL, -VARegSaveSize, isThumb, TII);
1320 if (!AFI->hasStackFrame()) {
1322 emitSPUpdate(MBB, MBBI, ARMCC::AL, -NumBytes, isThumb, TII);
1326 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1327 unsigned Reg = CSI[i].getReg();
1328 int FI = CSI[i].getFrameIdx();
1335 if (Reg == FramePtr)
1336 FramePtrSpillFI = FI;
1337 AFI->addGPRCalleeSavedArea1Frame(FI);
1344 if (Reg == FramePtr)
1345 FramePtrSpillFI = FI;
1346 if (STI.isTargetDarwin()) {
1347 AFI->addGPRCalleeSavedArea2Frame(FI);
1350 AFI->addGPRCalleeSavedArea1Frame(FI);
1355 AFI->addDPRCalleeSavedAreaFrame(FI);
1361 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1362 emitSPUpdate(MBB, MBBI, ARMCC::AL, -GPRCS1Size, isThumb, TII);
1363 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1364 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH)
1367 // Darwin ABI requires FP to point to the stack slot that contains the
1369 if (STI.isTargetDarwin() || hasFP(MF)) {
1370 MachineInstrBuilder MIB =
1371 BuildMI(MBB, MBBI, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri),FramePtr)
1372 .addFrameIndex(FramePtrSpillFI).addImm(0);
1373 if (!isThumb) MIB.addImm(ARMCC::AL);
1377 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1378 emitSPUpdate(MBB, MBBI, ARMCC::AL, -GPRCS2Size, false, TII);
1380 // Build the new SUBri to adjust SP for FP callee-save spill area.
1381 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1382 emitSPUpdate(MBB, MBBI, ARMCC::AL, -DPRCSSize, false, TII);
1385 // Determine starting offsets of spill areas.
1386 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1387 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1388 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1389 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1390 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1391 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1392 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1394 NumBytes = DPRCSOffset;
1396 // Insert it after all the callee-save spills.
1398 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1399 emitSPUpdate(MBB, MBBI, ARMCC::AL, -NumBytes, isThumb, TII);
1402 if(STI.isTargetELF() && hasFP(MF)) {
1403 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1404 AFI->getFramePtrSpillOffset());
1407 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1408 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1409 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1412 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1413 for (unsigned i = 0; CSRegs[i]; ++i)
1414 if (Reg == CSRegs[i])
1419 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1420 return ((MI->getOpcode() == ARM::FLDD ||
1421 MI->getOpcode() == ARM::LDR ||
1422 MI->getOpcode() == ARM::tRestore) &&
1423 MI->getOperand(1).isFrameIndex() &&
1424 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1427 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1428 MachineBasicBlock &MBB) const {
1429 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1430 assert((MBBI->getOpcode() == ARM::BX_RET ||
1431 MBBI->getOpcode() == ARM::tBX_RET ||
1432 MBBI->getOpcode() == ARM::tPOP_RET) &&
1433 "Can only insert epilog into returning blocks");
1435 MachineFrameInfo *MFI = MF.getFrameInfo();
1436 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1437 bool isThumb = AFI->isThumbFunction();
1438 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1439 int NumBytes = (int)MFI->getStackSize();
1440 if (!AFI->hasStackFrame()) {
1442 emitSPUpdate(MBB, MBBI, ARMCC::AL, NumBytes, isThumb, TII);
1444 // Unwind MBBI to point to first LDR / FLDD.
1445 const unsigned *CSRegs = getCalleeSavedRegs();
1446 if (MBBI != MBB.begin()) {
1449 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1450 if (!isCSRestore(MBBI, CSRegs))
1454 // Move SP to start of FP callee save spill area.
1455 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1456 AFI->getGPRCalleeSavedArea2Size() +
1457 AFI->getDPRCalleeSavedAreaSize());
1460 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1461 // Reset SP based on frame pointer only if the stack frame extends beyond
1462 // frame pointer stack slot or target is ELF and the function has FP.
1464 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes, TII);
1466 BuildMI(MBB, MBBI, TII.get(ARM::tMOVr), ARM::SP).addReg(FramePtr);
1468 if (MBBI->getOpcode() == ARM::tBX_RET &&
1469 &MBB.front() != MBBI &&
1470 prior(MBBI)->getOpcode() == ARM::tPOP) {
1471 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1472 emitSPUpdate(MBB, PMBBI, ARMCC::AL, NumBytes, isThumb, TII);
1474 emitSPUpdate(MBB, MBBI, ARMCC::AL, NumBytes, isThumb, TII);
1477 // Darwin ABI requires FP to point to the stack slot that contains the
1479 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1480 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1481 // Reset SP based on frame pointer only if the stack frame extends beyond
1482 // frame pointer stack slot or target is ELF and the function has FP.
1483 if (AFI->getGPRCalleeSavedArea2Size() ||
1484 AFI->getDPRCalleeSavedAreaSize() ||
1485 AFI->getDPRCalleeSavedAreaOffset()||
1488 BuildMI(MBB, MBBI, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1489 .addImm(NumBytes).addImm((unsigned)ARMCC::AL);
1491 BuildMI(MBB, MBBI, TII.get(ARM::MOVr), ARM::SP).addReg(FramePtr)
1492 .addImm((unsigned)ARMCC::AL);
1493 } else if (NumBytes) {
1494 emitSPUpdate(MBB, MBBI, ARMCC::AL, NumBytes, false, TII);
1497 // Move SP to start of integer callee save spill area 2.
1498 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1499 emitSPUpdate(MBB, MBBI, ARMCC::AL, AFI->getDPRCalleeSavedAreaSize(),
1502 // Move SP to start of integer callee save spill area 1.
1503 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1504 emitSPUpdate(MBB, MBBI, ARMCC::AL, AFI->getGPRCalleeSavedArea2Size(),
1507 // Move SP to SP upon entry to the function.
1508 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1509 emitSPUpdate(MBB, MBBI, ARMCC::AL, AFI->getGPRCalleeSavedArea1Size(),
1514 if (VARegSaveSize) {
1516 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1517 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1518 BuildMI(MBB, MBBI, TII.get(ARM::tPOP)).addReg(ARM::R3);
1520 emitSPUpdate(MBB, MBBI, ARMCC::AL, VARegSaveSize, isThumb, TII);
1523 BuildMI(MBB, MBBI, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1529 unsigned ARMRegisterInfo::getRARegister() const {
1533 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1534 if (STI.isTargetDarwin() || hasFP(MF))
1535 return (STI.useThumbBacktraces() || STI.isThumb()) ? ARM::R7 : ARM::R11;
1540 unsigned ARMRegisterInfo::getEHExceptionRegister() const {
1541 assert(0 && "What is the exception register");
1545 unsigned ARMRegisterInfo::getEHHandlerRegister() const {
1546 assert(0 && "What is the exception handler register");
1550 #include "ARMGenRegisterInfo.inc"