1 //===- Thumb1RegisterInfo.cpp - Thumb-1 Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the Thumb-1 implementation of the TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMMachineFunctionInfo.h"
18 #include "ARMSubtarget.h"
19 #include "Thumb1InstrInfo.h"
20 #include "Thumb1RegisterInfo.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/MachineRegisterInfo.h"
29 #include "llvm/Target/TargetFrameInfo.h"
30 #include "llvm/Target/TargetMachine.h"
31 #include "llvm/ADT/BitVector.h"
32 #include "llvm/ADT/SmallVector.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
39 ThumbRegScavenging("enable-thumb-reg-scavenging",
41 cl::desc("Enable register scavenging on Thumb"));
43 Thumb1RegisterInfo::Thumb1RegisterInfo(const ARMBaseInstrInfo &tii,
44 const ARMSubtarget &sti)
45 : ARMBaseRegisterInfo(tii, sti) {
49 const MachineInstrBuilder &AddDefaultPred(const MachineInstrBuilder &MIB) {
50 return MIB.addImm((int64_t)ARMCC::AL).addReg(0);
54 const MachineInstrBuilder &AddDefaultCC(const MachineInstrBuilder &MIB) {
55 return MIB.addReg(ARM::CPSR);
58 /// emitLoadConstPool - Emits a load from constpool to materialize the
59 /// specified immediate.
60 void Thumb1RegisterInfo::emitLoadConstPool(MachineBasicBlock &MBB,
61 MachineBasicBlock::iterator &MBBI,
63 unsigned DestReg, int Val,
64 ARMCC::CondCodes Pred,
65 unsigned PredReg) const {
66 MachineFunction &MF = *MBB.getParent();
67 MachineConstantPool *ConstantPool = MF.getConstantPool();
68 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
69 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
71 BuildMI(MBB, MBBI, dl, TII.get(ARM::tLDRcp), DestReg)
72 .addConstantPoolIndex(Idx).addImm(Pred).addReg(PredReg);
75 const TargetRegisterClass*
76 Thumb1RegisterInfo::getPhysicalRegisterRegClass(unsigned Reg, MVT VT) const {
77 if (isARMLowRegister(Reg))
78 return ARM::tGPRRegisterClass;
82 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
83 case ARM::R12: case ARM::SP: case ARM::LR: case ARM::PC:
84 return ARM::GPRRegisterClass;
87 return TargetRegisterInfo::getPhysicalRegisterRegClass(Reg, VT);
91 Thumb1RegisterInfo::requiresRegisterScavenging(const MachineFunction &MF) const {
92 return ThumbRegScavenging;
95 bool Thumb1RegisterInfo::hasReservedCallFrame(MachineFunction &MF) const {
96 const MachineFrameInfo *FFI = MF.getFrameInfo();
97 unsigned CFSize = FFI->getMaxCallFrameSize();
98 // It's not always a good idea to include the call frame as part of the
99 // stack frame. ARM (especially Thumb) has small immediate offset to
100 // address the stack frame. So a large call frame can cause poor codegen
101 // and may even makes it impossible to scavenge a register.
102 if (CFSize >= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
105 return !MF.getFrameInfo()->hasVarSizedObjects();
109 /// emitThumbRegPlusImmInReg - Emits a series of instructions to materialize
110 /// a destreg = basereg + immediate in Thumb code. Materialize the immediate
111 /// in a register using mov / mvn sequences or load the immediate from a
114 void emitThumbRegPlusImmInReg(MachineBasicBlock &MBB,
115 MachineBasicBlock::iterator &MBBI,
116 unsigned DestReg, unsigned BaseReg,
117 int NumBytes, bool CanChangeCC,
118 const TargetInstrInfo &TII,
119 const Thumb1RegisterInfo& MRI,
121 bool isHigh = !isARMLowRegister(DestReg) ||
122 (BaseReg != 0 && !isARMLowRegister(BaseReg));
124 // Subtract doesn't have high register version. Load the negative value
125 // if either base or dest register is a high register. Also, if do not
126 // issue sub as part of the sequence if condition register is to be
128 if (NumBytes < 0 && !isHigh && CanChangeCC) {
130 NumBytes = -NumBytes;
132 unsigned LdReg = DestReg;
133 if (DestReg == ARM::SP) {
134 assert(BaseReg == ARM::SP && "Unexpected!");
136 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVlor2hir), ARM::R12)
137 .addReg(ARM::R3, RegState::Kill);
140 if (NumBytes <= 255 && NumBytes >= 0)
141 AddDefaultCC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg))
143 else if (NumBytes < 0 && NumBytes >= -255) {
144 AddDefaultCC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8), LdReg))
146 AddDefaultCC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tRSB), LdReg))
147 .addReg(LdReg, RegState::Kill);
149 MRI.emitLoadConstPool(MBB, MBBI, dl, LdReg, NumBytes);
152 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
153 MachineInstrBuilder MIB =
154 BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg);
155 if (Opc != ARM::tADDhirr)
156 MIB = AddDefaultCC(MIB);
157 if (DestReg == ARM::SP || isSub)
158 MIB.addReg(BaseReg).addReg(LdReg, RegState::Kill);
160 MIB.addReg(LdReg).addReg(BaseReg, RegState::Kill);
163 if (DestReg == ARM::SP)
164 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVhir2lor), ARM::R3)
165 .addReg(ARM::R12, RegState::Kill);
168 /// calcNumMI - Returns the number of instructions required to materialize
169 /// the specific add / sub r, c instruction.
170 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
171 unsigned NumBits, unsigned Scale) {
173 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
175 if (Opc == ARM::tADDrSPi) {
176 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
180 Scale = 1; // Followed by a number of tADDi8.
181 Chunk = ((1 << NumBits) - 1) * Scale;
184 NumMIs += Bytes / Chunk;
185 if ((Bytes % Chunk) != 0)
192 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
193 /// a destreg = basereg + immediate in Thumb code.
195 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
196 MachineBasicBlock::iterator &MBBI,
197 unsigned DestReg, unsigned BaseReg,
198 int NumBytes, const TargetInstrInfo &TII,
199 const Thumb1RegisterInfo& MRI,
201 bool isSub = NumBytes < 0;
202 unsigned Bytes = (unsigned)NumBytes;
203 if (isSub) Bytes = -NumBytes;
204 bool isMul4 = (Bytes & 3) == 0;
205 bool isTwoAddr = false;
206 bool DstNotEqBase = false;
207 unsigned NumBits = 1;
212 bool NeedPred = false;
214 if (DestReg == BaseReg && BaseReg == ARM::SP) {
215 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
218 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
220 } else if (!isSub && BaseReg == ARM::SP) {
223 // r1 = add sp, 100 * 4
227 ExtraOpc = ARM::tADDi3;
236 if (DestReg != BaseReg)
239 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
240 NeedPred = NeedCC = true;
244 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
245 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
246 if (NumMIs > Threshold) {
247 // This will expand into too many instructions. Load the immediate from a
249 emitThumbRegPlusImmInReg(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII,
255 if (isARMLowRegister(DestReg) && isARMLowRegister(BaseReg)) {
256 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
257 unsigned Chunk = (1 << 3) - 1;
258 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
260 const TargetInstrDesc &TID = TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3);
261 const MachineInstrBuilder MIB =
262 AddDefaultCC(BuildMI(MBB, MBBI, dl, TID, DestReg));
263 AddDefaultPred(MIB.addReg(BaseReg, RegState::Kill).addImm(ThisVal));
265 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVr), DestReg)
266 .addReg(BaseReg, RegState::Kill);
271 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
273 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
276 // Build the new tADD / tSUB.
278 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg);
280 MIB = AddDefaultCC(MIB);
281 MIB .addReg(DestReg).addImm(ThisVal);
283 MIB = AddDefaultPred(MIB);
286 bool isKill = BaseReg != ARM::SP;
287 MachineInstrBuilder MIB = BuildMI(MBB, MBBI, dl, TII.get(Opc), DestReg);
289 MIB = AddDefaultCC(MIB);
290 MIB.addReg(BaseReg, getKillRegState(isKill)).addImm(ThisVal);
292 MIB = AddDefaultPred(MIB);
295 if (Opc == ARM::tADDrSPi) {
301 Chunk = ((1 << NumBits) - 1) * Scale;
302 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
303 NeedPred = NeedCC = isTwoAddr = true;
309 const TargetInstrDesc &TID = TII.get(ExtraOpc);
310 AddDefaultPred(AddDefaultCC(BuildMI(MBB, MBBI, dl, TID, DestReg))
311 .addReg(DestReg, RegState::Kill)
312 .addImm(((unsigned)NumBytes) & 3));
316 static void emitSPUpdate(MachineBasicBlock &MBB,
317 MachineBasicBlock::iterator &MBBI,
318 const TargetInstrInfo &TII, DebugLoc dl,
319 const Thumb1RegisterInfo &MRI,
321 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII,
325 void Thumb1RegisterInfo::
326 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
327 MachineBasicBlock::iterator I) const {
328 if (!hasReservedCallFrame(MF)) {
329 // If we have alloca, convert as follows:
330 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
331 // ADJCALLSTACKUP -> add, sp, sp, amount
332 MachineInstr *Old = I;
333 DebugLoc dl = Old->getDebugLoc();
334 unsigned Amount = Old->getOperand(0).getImm();
336 // We need to keep the stack aligned properly. To do this, we round the
337 // amount of space needed for the outgoing arguments up to the next
338 // alignment boundary.
339 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
340 Amount = (Amount+Align-1)/Align*Align;
342 // Replace the pseudo instruction with a new instruction...
343 unsigned Opc = Old->getOpcode();
344 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
345 emitSPUpdate(MBB, I, TII, dl, *this, -Amount);
347 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
348 emitSPUpdate(MBB, I, TII, dl, *this, Amount);
355 /// emitThumbConstant - Emit a series of instructions to materialize a
357 static void emitThumbConstant(MachineBasicBlock &MBB,
358 MachineBasicBlock::iterator &MBBI,
359 unsigned DestReg, int Imm,
360 const TargetInstrInfo &TII,
361 const Thumb1RegisterInfo& MRI,
363 bool isSub = Imm < 0;
364 if (isSub) Imm = -Imm;
366 int Chunk = (1 << 8) - 1;
367 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
369 AddDefaultPred(AddDefaultCC(BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVi8),
373 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII, MRI, dl);
375 const TargetInstrDesc &TID = TII.get(ARM::tRSB);
376 AddDefaultPred(AddDefaultCC(BuildMI(MBB, MBBI, dl, TID, DestReg))
377 .addReg(DestReg, RegState::Kill));
381 static void removeOperands(MachineInstr &MI, unsigned i) {
383 for (unsigned e = MI.getNumOperands(); i != e; ++i)
384 MI.RemoveOperand(Op);
387 void Thumb1RegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
388 int SPAdj, RegScavenger *RS) const{
390 MachineInstr &MI = *II;
391 MachineBasicBlock &MBB = *MI.getParent();
392 MachineFunction &MF = *MBB.getParent();
393 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
394 DebugLoc dl = MI.getDebugLoc();
396 while (!MI.getOperand(i).isFI()) {
398 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
401 unsigned FrameReg = ARM::SP;
402 int FrameIndex = MI.getOperand(i).getIndex();
403 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
404 MF.getFrameInfo()->getStackSize() + SPAdj;
406 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
407 Offset -= AFI->getGPRCalleeSavedArea1Offset();
408 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
409 Offset -= AFI->getGPRCalleeSavedArea2Offset();
410 else if (hasFP(MF)) {
411 assert(SPAdj == 0 && "Unexpected");
412 // There is alloca()'s in this function, must reference off the frame
414 FrameReg = getFrameRegister(MF);
415 Offset -= AFI->getFramePtrSpillOffset();
418 unsigned Opcode = MI.getOpcode();
419 const TargetInstrDesc &Desc = MI.getDesc();
420 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
422 if (Opcode == ARM::tADDrSPi) {
423 Offset += MI.getOperand(i+1).getImm();
425 // Can't use tADDrSPi if it's based off the frame pointer.
426 unsigned NumBits = 0;
428 if (FrameReg != ARM::SP) {
429 Opcode = ARM::tADDi3;
430 MI.setDesc(TII.get(Opcode));
435 assert((Offset & 3) == 0 &&
436 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
440 // Turn it into a move.
441 MI.setDesc(TII.get(ARM::tMOVhir2lor));
442 MI.getOperand(i).ChangeToRegister(FrameReg, false);
443 MI.RemoveOperand(i+1);
447 // Common case: small offset, fits into instruction.
448 unsigned Mask = (1 << NumBits) - 1;
449 if (((Offset / Scale) & ~Mask) == 0) {
450 // Replace the FrameIndex with sp / fp
451 if (Opcode == ARM::tADDi3) {
452 removeOperands(MI, i);
453 MachineInstrBuilder MIB(&MI);
454 AddDefaultPred(AddDefaultCC(MIB).addReg(FrameReg).addImm(Offset/Scale));
456 MI.getOperand(i).ChangeToRegister(FrameReg, false);
457 MI.getOperand(i+1).ChangeToImmediate(Offset / Scale);
462 unsigned DestReg = MI.getOperand(0).getReg();
463 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
464 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, NumBits, Scale);
465 // MI would expand into a large number of instructions. Don't try to
466 // simplify the immediate.
468 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII,
475 // Translate r0 = add sp, imm to
476 // r0 = add sp, 255*4
477 // r0 = add r0, (imm - 255*4)
478 if (Opcode == ARM::tADDi3) {
479 removeOperands(MI, i);
480 MachineInstrBuilder MIB(&MI);
481 AddDefaultPred(AddDefaultCC(MIB).addReg(FrameReg).addImm(Mask));
483 MI.getOperand(i).ChangeToRegister(FrameReg, false);
484 MI.getOperand(i+1).ChangeToImmediate(Mask);
486 Offset = (Offset - Mask * Scale);
487 MachineBasicBlock::iterator NII = next(II);
488 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII,
491 // Translate r0 = add sp, -imm to
492 // r0 = -imm (this is then translated into a series of instructons)
494 emitThumbConstant(MBB, II, DestReg, Offset, TII, *this, dl);
495 MI.setDesc(TII.get(ARM::tADDhirr));
496 MI.getOperand(i).ChangeToRegister(DestReg, false, false, true);
497 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
503 unsigned NumBits = 0;
506 case ARMII::AddrModeT1_s: {
508 InstrOffs = MI.getOperand(ImmIdx).getImm();
509 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
514 LLVM_UNREACHABLE("Unsupported addressing mode!");
518 Offset += InstrOffs * Scale;
519 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
521 // Common case: small offset, fits into instruction.
522 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
523 int ImmedOffset = Offset / Scale;
524 unsigned Mask = (1 << NumBits) - 1;
525 if ((unsigned)Offset <= Mask * Scale) {
526 // Replace the FrameIndex with sp
527 MI.getOperand(i).ChangeToRegister(FrameReg, false);
528 ImmOp.ChangeToImmediate(ImmedOffset);
532 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
533 if (AddrMode == ARMII::AddrModeT1_s) {
534 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
535 // a different base register.
537 Mask = (1 << NumBits) - 1;
539 // If this is a thumb spill / restore, we will be using a constpool load to
540 // materialize the offset.
541 if (AddrMode == ARMII::AddrModeT1_s && isThumSpillRestore)
542 ImmOp.ChangeToImmediate(0);
544 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
545 ImmedOffset = ImmedOffset & Mask;
546 ImmOp.ChangeToImmediate(ImmedOffset);
547 Offset &= ~(Mask*Scale);
551 // If we get here, the immediate doesn't fit into the instruction. We folded
552 // as much as possible above, handle the rest, providing a register that is
554 assert(Offset && "This code isn't needed if offset already handled!");
556 // Remove predicate first.
557 int PIdx = MI.findFirstPredOperandIdx();
559 removeOperands(MI, PIdx);
561 if (Desc.mayLoad()) {
562 // Use the destination register to materialize sp + offset.
563 unsigned TmpReg = MI.getOperand(0).getReg();
565 if (Opcode == ARM::tRestore) {
566 if (FrameReg == ARM::SP)
567 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,
568 Offset, false, TII, *this, dl);
570 emitLoadConstPool(MBB, II, dl, TmpReg, Offset);
574 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII,
578 MI.setDesc(TII.get(ARM::tLDR));
579 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
581 // Use [reg, reg] addrmode.
582 MI.addOperand(MachineOperand::CreateReg(FrameReg, false));
583 else // tLDR has an extra register operand.
584 MI.addOperand(MachineOperand::CreateReg(0, false));
585 } else if (Desc.mayStore()) {
586 // FIXME! This is horrific!!! We need register scavenging.
587 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
588 // also a ABI register so it's possible that is is the register that is
589 // being storing here. If that's the case, we do the following:
591 // Use r2 to materialize sp + offset
594 unsigned ValReg = MI.getOperand(0).getReg();
595 unsigned TmpReg = ARM::R3;
597 if (ValReg == ARM::R3) {
598 BuildMI(MBB, II, dl, TII.get(ARM::tMOVlor2hir), ARM::R12)
599 .addReg(ARM::R2, RegState::Kill);
602 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
603 BuildMI(MBB, II, dl, TII.get(ARM::tMOVlor2hir), ARM::R12)
604 .addReg(ARM::R3, RegState::Kill);
605 if (Opcode == ARM::tSpill) {
606 if (FrameReg == ARM::SP)
607 emitThumbRegPlusImmInReg(MBB, II, TmpReg, FrameReg,
608 Offset, false, TII, *this, dl);
610 emitLoadConstPool(MBB, II, dl, TmpReg, Offset);
614 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII,
616 MI.setDesc(TII.get(ARM::tSTR));
617 MI.getOperand(i).ChangeToRegister(TmpReg, false, false, true);
618 if (UseRR) // Use [reg, reg] addrmode.
619 MI.addOperand(MachineOperand::CreateReg(FrameReg, false));
620 else // tSTR has an extra register operand.
621 MI.addOperand(MachineOperand::CreateReg(0, false));
623 MachineBasicBlock::iterator NII = next(II);
624 if (ValReg == ARM::R3)
625 BuildMI(MBB, NII, dl, TII.get(ARM::tMOVhir2lor), ARM::R2)
626 .addReg(ARM::R12, RegState::Kill);
627 if (TmpReg == ARM::R3 && AFI->isR3LiveIn())
628 BuildMI(MBB, NII, dl, TII.get(ARM::tMOVhir2lor), ARM::R3)
629 .addReg(ARM::R12, RegState::Kill);
631 assert(false && "Unexpected opcode!");
633 // Add predicate back if it's needed.
634 if (MI.getDesc().isPredicable()) {
635 MachineInstrBuilder MIB(&MI);
640 void Thumb1RegisterInfo::emitPrologue(MachineFunction &MF) const {
641 MachineBasicBlock &MBB = MF.front();
642 MachineBasicBlock::iterator MBBI = MBB.begin();
643 MachineFrameInfo *MFI = MF.getFrameInfo();
644 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
645 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
646 unsigned NumBytes = MFI->getStackSize();
647 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
648 DebugLoc dl = (MBBI != MBB.end() ?
649 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
651 // Check if R3 is live in. It might have to be used as a scratch register.
652 for (MachineRegisterInfo::livein_iterator I =MF.getRegInfo().livein_begin(),
653 E = MF.getRegInfo().livein_end(); I != E; ++I) {
654 if (I->first == ARM::R3) {
655 AFI->setR3IsLiveIn(true);
660 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
661 NumBytes = (NumBytes + 3) & ~3;
662 MFI->setStackSize(NumBytes);
664 // Determine the sizes of each callee-save spill areas and record which frame
665 // belongs to which callee-save spill areas.
666 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
667 int FramePtrSpillFI = 0;
670 emitSPUpdate(MBB, MBBI, TII, dl, *this, -VARegSaveSize);
672 if (!AFI->hasStackFrame()) {
674 emitSPUpdate(MBB, MBBI, TII, dl, *this, -NumBytes);
678 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
679 unsigned Reg = CSI[i].getReg();
680 int FI = CSI[i].getFrameIdx();
688 FramePtrSpillFI = FI;
689 AFI->addGPRCalleeSavedArea1Frame(FI);
697 FramePtrSpillFI = FI;
698 if (STI.isTargetDarwin()) {
699 AFI->addGPRCalleeSavedArea2Frame(FI);
702 AFI->addGPRCalleeSavedArea1Frame(FI);
707 AFI->addDPRCalleeSavedAreaFrame(FI);
712 if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH) {
714 if (MBBI != MBB.end())
715 dl = MBBI->getDebugLoc();
718 // Darwin ABI requires FP to point to the stack slot that contains the
720 if (STI.isTargetDarwin() || hasFP(MF)) {
721 MachineInstrBuilder MIB =
722 BuildMI(MBB, MBBI, dl, TII.get(ARM::tADDrSPi), FramePtr)
723 .addFrameIndex(FramePtrSpillFI).addImm(0);
726 // Determine starting offsets of spill areas.
727 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
728 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
729 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
730 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
731 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
732 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
733 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
735 NumBytes = DPRCSOffset;
737 // Insert it after all the callee-save spills.
738 emitSPUpdate(MBB, MBBI, TII, dl, *this, -NumBytes);
741 if (STI.isTargetELF() && hasFP(MF)) {
742 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
743 AFI->getFramePtrSpillOffset());
746 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
747 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
748 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
751 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
752 for (unsigned i = 0; CSRegs[i]; ++i)
753 if (Reg == CSRegs[i])
758 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
759 return (MI->getOpcode() == ARM::tRestore &&
760 MI->getOperand(1).isFI() &&
761 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
764 void Thumb1RegisterInfo::emitEpilogue(MachineFunction &MF,
765 MachineBasicBlock &MBB) const {
766 MachineBasicBlock::iterator MBBI = prior(MBB.end());
767 assert((MBBI->getOpcode() == ARM::tBX_RET ||
768 MBBI->getOpcode() == ARM::tPOP_RET) &&
769 "Can only insert epilog into returning blocks");
770 DebugLoc dl = MBBI->getDebugLoc();
771 MachineFrameInfo *MFI = MF.getFrameInfo();
772 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
773 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
774 int NumBytes = (int)MFI->getStackSize();
776 if (!AFI->hasStackFrame()) {
778 emitSPUpdate(MBB, MBBI, TII, dl, *this, NumBytes);
780 // Unwind MBBI to point to first LDR / FLDD.
781 const unsigned *CSRegs = getCalleeSavedRegs();
782 if (MBBI != MBB.begin()) {
785 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
786 if (!isCSRestore(MBBI, CSRegs))
790 // Move SP to start of FP callee save spill area.
791 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
792 AFI->getGPRCalleeSavedArea2Size() +
793 AFI->getDPRCalleeSavedAreaSize());
796 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
797 // Reset SP based on frame pointer only if the stack frame extends beyond
798 // frame pointer stack slot or target is ELF and the function has FP.
800 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes,
803 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVlor2hir), ARM::SP)
806 if (MBBI->getOpcode() == ARM::tBX_RET &&
807 &MBB.front() != MBBI &&
808 prior(MBBI)->getOpcode() == ARM::tPOP) {
809 MachineBasicBlock::iterator PMBBI = prior(MBBI);
810 emitSPUpdate(MBB, PMBBI, TII, dl, *this, NumBytes);
812 emitSPUpdate(MBB, MBBI, TII, dl, *this, NumBytes);
817 // Epilogue for vararg functions: pop LR to R3 and branch off it.
818 // FIXME: Verify this is still ok when R3 is no longer being reserved.
819 BuildMI(MBB, MBBI, dl, TII.get(ARM::tPOP)).addReg(ARM::R3);
821 emitSPUpdate(MBB, MBBI, TII, dl, *this, VARegSaveSize);
823 BuildMI(MBB, MBBI, dl, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);