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/Target/TargetFrameInfo.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetOptions.h"
31 #include "llvm/ADT/SmallVector.h"
32 #include "llvm/ADT/STLExtras.h"
36 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
39 case R0: case S0: case D0: return 0;
40 case R1: case S1: case D1: return 1;
41 case R2: case S2: case D2: return 2;
42 case R3: case S3: case D3: return 3;
43 case R4: case S4: case D4: return 4;
44 case R5: case S5: case D5: return 5;
45 case R6: case S6: case D6: return 6;
46 case R7: case S7: case D7: return 7;
47 case R8: case S8: case D8: return 8;
48 case R9: case S9: case D9: return 9;
49 case R10: case S10: case D10: return 10;
50 case R11: case S11: case D11: return 11;
51 case R12: case S12: case D12: return 12;
52 case SP: case S13: case D13: return 13;
53 case LR: case S14: case D14: return 14;
54 case PC: case S15: case D15: return 15;
72 assert(0 && "Unknown ARM register!");
77 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii,
78 const ARMSubtarget &sti)
79 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
81 FramePtr(STI.useThumbBacktraces() ? ARM::R7 : ARM::R11) {
84 bool ARMRegisterInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
85 MachineBasicBlock::iterator MI,
86 const std::vector<CalleeSavedInfo> &CSI) const {
87 MachineFunction &MF = *MBB.getParent();
88 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
89 if (!AFI->isThumbFunction() || CSI.empty())
92 MachineInstrBuilder MIB = BuildMI(MBB, MI, TII.get(ARM::tPUSH));
93 for (unsigned i = CSI.size(); i != 0; --i)
94 MIB.addReg(CSI[i-1].getReg());
98 bool ARMRegisterInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
99 MachineBasicBlock::iterator MI,
100 const std::vector<CalleeSavedInfo> &CSI) const {
101 MachineFunction &MF = *MBB.getParent();
102 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
103 if (!AFI->isThumbFunction() || CSI.empty())
106 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
107 MachineInstr *PopMI = new MachineInstr(TII.get(ARM::tPOP));
108 MBB.insert(MI, PopMI);
109 for (unsigned i = CSI.size(); i != 0; --i) {
110 unsigned Reg = CSI[i-1].getReg();
111 if (Reg == ARM::LR) {
112 // Special epilogue for vararg functions. See emitEpilogue
116 PopMI->setInstrDescriptor(TII.get(ARM::tPOP_RET));
119 PopMI->addRegOperand(Reg, true);
124 void ARMRegisterInfo::
125 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
126 unsigned SrcReg, int FI,
127 const TargetRegisterClass *RC) const {
128 if (RC == ARM::GPRRegisterClass) {
129 MachineFunction &MF = *MBB.getParent();
130 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
131 if (AFI->isThumbFunction())
132 BuildMI(MBB, I, TII.get(ARM::tSpill)).addReg(SrcReg)
133 .addFrameIndex(FI).addImm(0);
135 BuildMI(MBB, I, TII.get(ARM::STR)).addReg(SrcReg)
136 .addFrameIndex(FI).addReg(0).addImm(0);
137 } else if (RC == ARM::DPRRegisterClass) {
138 BuildMI(MBB, I, TII.get(ARM::FSTD)).addReg(SrcReg)
139 .addFrameIndex(FI).addImm(0);
141 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
142 BuildMI(MBB, I, TII.get(ARM::FSTS)).addReg(SrcReg)
143 .addFrameIndex(FI).addImm(0);
147 void ARMRegisterInfo::
148 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
149 unsigned DestReg, int FI,
150 const TargetRegisterClass *RC) const {
151 if (RC == ARM::GPRRegisterClass) {
152 MachineFunction &MF = *MBB.getParent();
153 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
154 if (AFI->isThumbFunction())
155 BuildMI(MBB, I, TII.get(ARM::tRestore), DestReg)
156 .addFrameIndex(FI).addImm(0);
158 BuildMI(MBB, I, TII.get(ARM::LDR), DestReg)
159 .addFrameIndex(FI).addReg(0).addImm(0);
160 } else if (RC == ARM::DPRRegisterClass) {
161 BuildMI(MBB, I, TII.get(ARM::FLDD), DestReg)
162 .addFrameIndex(FI).addImm(0);
164 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
165 BuildMI(MBB, I, TII.get(ARM::FLDS), DestReg)
166 .addFrameIndex(FI).addImm(0);
170 void ARMRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
171 MachineBasicBlock::iterator I,
172 unsigned DestReg, unsigned SrcReg,
173 const TargetRegisterClass *RC) const {
174 if (RC == ARM::GPRRegisterClass) {
175 MachineFunction &MF = *MBB.getParent();
176 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
177 BuildMI(MBB, I, TII.get(AFI->isThumbFunction() ? ARM::tMOVrr : ARM::MOVrr),
178 DestReg).addReg(SrcReg);
179 } else if (RC == ARM::SPRRegisterClass)
180 BuildMI(MBB, I, TII.get(ARM::FCPYS), DestReg).addReg(SrcReg);
181 else if (RC == ARM::DPRRegisterClass)
182 BuildMI(MBB, I, TII.get(ARM::FCPYD), DestReg).addReg(SrcReg);
187 /// isLowRegister - Returns true if the register is low register r0-r7.
189 static bool isLowRegister(unsigned Reg) {
192 case R0: case R1: case R2: case R3:
193 case R4: case R5: case R6: case R7:
200 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr *MI,
201 unsigned OpNum, int FI) const {
202 unsigned Opc = MI->getOpcode();
203 MachineInstr *NewMI = NULL;
207 if (OpNum == 0) { // move -> store
208 unsigned SrcReg = MI->getOperand(1).getReg();
209 NewMI = BuildMI(TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
210 .addReg(0).addImm(0);
211 } else { // move -> load
212 unsigned DstReg = MI->getOperand(0).getReg();
213 NewMI = BuildMI(TII.get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
219 if (OpNum == 0) { // move -> store
220 unsigned SrcReg = MI->getOperand(1).getReg();
221 if (!isLowRegister(SrcReg))
222 // tSpill cannot take a high register operand.
224 NewMI = BuildMI(TII.get(ARM::tSpill)).addReg(SrcReg).addFrameIndex(FI)
226 } else { // move -> load
227 unsigned DstReg = MI->getOperand(0).getReg();
228 if (!isLowRegister(DstReg))
229 // tRestore cannot target a high register operand.
231 NewMI = BuildMI(TII.get(ARM::tRestore), DstReg).addFrameIndex(FI)
237 if (OpNum == 0) { // move -> store
238 unsigned SrcReg = MI->getOperand(1).getReg();
239 NewMI = BuildMI(TII.get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
241 } else { // move -> load
242 unsigned DstReg = MI->getOperand(0).getReg();
243 NewMI = BuildMI(TII.get(ARM::FLDS), DstReg).addFrameIndex(FI).addImm(0);
248 if (OpNum == 0) { // move -> store
249 unsigned SrcReg = MI->getOperand(1).getReg();
250 NewMI = BuildMI(TII.get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
252 } else { // move -> load
253 unsigned DstReg = MI->getOperand(0).getReg();
254 NewMI = BuildMI(TII.get(ARM::FLDD), DstReg).addFrameIndex(FI).addImm(0);
261 NewMI->copyKillDeadInfo(MI);
265 const unsigned* ARMRegisterInfo::getCalleeSavedRegs() const {
266 static const unsigned CalleeSavedRegs[] = {
267 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
268 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
270 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
271 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
275 static const unsigned DarwinCalleeSavedRegs[] = {
276 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
277 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
279 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
280 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
283 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
286 const TargetRegisterClass* const *
287 ARMRegisterInfo::getCalleeSavedRegClasses() const {
288 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
289 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
290 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
291 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
293 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
294 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
297 return CalleeSavedRegClasses;
300 /// hasFP - Return true if the specified function should have a dedicated frame
301 /// pointer register. This is true if the function has variable sized allocas
302 /// or if frame pointer elimination is disabled.
304 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
305 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
308 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
309 /// a destreg = basereg + immediate in ARM code.
311 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
312 MachineBasicBlock::iterator &MBBI,
313 unsigned DestReg, unsigned BaseReg,
314 int NumBytes, const TargetInstrInfo &TII) {
315 bool isSub = NumBytes < 0;
316 if (isSub) NumBytes = -NumBytes;
319 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
320 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
321 assert(ThisVal && "Didn't extract field correctly");
323 // We will handle these bits from offset, clear them.
324 NumBytes &= ~ThisVal;
326 // Get the properly encoded SOImmVal field.
327 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
328 assert(SOImmVal != -1 && "Bit extraction didn't work?");
330 // Build the new ADD / SUB.
331 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
332 .addReg(BaseReg).addImm(SOImmVal);
337 /// calcNumMI - Returns the number of instructions required to materialize
338 /// the specific add / sub r, c instruction.
339 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
340 unsigned NumBits, unsigned Scale) {
342 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
344 if (Opc == ARM::tADDrSPi) {
345 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
350 Chunk = ((1 << NumBits) - 1) * Scale;
353 NumMIs += Bytes / Chunk;
354 if ((Bytes % Chunk) != 0)
361 /// emitLoadConstPool - Emits a load from constpool to materialize NumBytes
363 static void emitLoadConstPool(MachineBasicBlock &MBB,
364 MachineBasicBlock::iterator &MBBI,
365 unsigned DestReg, int NumBytes,
366 const TargetInstrInfo &TII) {
367 MachineFunction &MF = *MBB.getParent();
368 MachineConstantPool *ConstantPool = MF.getConstantPool();
369 Constant *C = ConstantInt::get(Type::Int32Ty, NumBytes);
370 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 2);
371 BuildMI(MBB, MBBI, TII.get(ARM::tLDRpci), DestReg).addConstantPoolIndex(Idx);
374 /// emitThumbRegPlusConstPool - Emits a series of instructions to materialize
375 /// a destreg = basereg + immediate in Thumb code. Load the immediate from a
378 void emitThumbRegPlusConstPool(MachineBasicBlock &MBB,
379 MachineBasicBlock::iterator &MBBI,
380 unsigned DestReg, unsigned BaseReg,
381 int NumBytes, bool CanChangeCC,
382 const TargetInstrInfo &TII) {
383 bool isHigh = !isLowRegister(DestReg) ||
384 (BaseReg != 0 && !isLowRegister(BaseReg));
386 // Subtract doesn't have high register version. Load the negative value
387 // if either base or dest register is a high register. Also, if do not
388 // issue sub as part of the sequence if condition register is to be
390 if (NumBytes < 0 && !isHigh && CanChangeCC) {
392 NumBytes = -NumBytes;
394 unsigned LdReg = DestReg;
395 if (DestReg == ARM::SP) {
396 assert(BaseReg == ARM::SP && "Unexpected!");
398 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::R12).addReg(ARM::R3);
401 if (NumBytes <= 255 && NumBytes >= 0)
402 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), LdReg).addImm(NumBytes);
403 else if (NumBytes < 0 && NumBytes >= -255) {
404 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), LdReg).addImm(NumBytes);
405 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), LdReg).addReg(LdReg);
407 emitLoadConstPool(MBB, MBBI, LdReg, NumBytes, TII);
410 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
411 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, TII.get(Opc), DestReg);
412 if (DestReg == ARM::SP)
413 MIB.addReg(BaseReg).addReg(LdReg);
415 MIB.addReg(BaseReg).addReg(LdReg);
417 MIB.addReg(LdReg).addReg(BaseReg);
418 if (DestReg == ARM::SP)
419 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::R3).addReg(ARM::R12);
422 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
423 /// a destreg = basereg + immediate in Thumb code.
425 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
426 MachineBasicBlock::iterator &MBBI,
427 unsigned DestReg, unsigned BaseReg,
428 int NumBytes, const TargetInstrInfo &TII) {
429 bool isSub = NumBytes < 0;
430 unsigned Bytes = (unsigned)NumBytes;
431 if (isSub) Bytes = -NumBytes;
432 bool isMul4 = (Bytes & 3) == 0;
433 bool isTwoAddr = false;
434 bool DstNotEqBase = false;
435 unsigned NumBits = 1;
440 if (DestReg == BaseReg && BaseReg == ARM::SP) {
441 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
444 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
446 } else if (!isSub && BaseReg == ARM::SP) {
449 // r1 = add sp, 100 * 4
453 ExtraOpc = ARM::tADDi3;
462 if (DestReg != BaseReg)
465 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
469 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
470 unsigned Threshold = (DestReg == ARM::SP) ? 3 : 2;
471 if (NumMIs > Threshold) {
472 // This will expand into too many instructions. Load the immediate from a
474 emitThumbRegPlusConstPool(MBB, MBBI, DestReg, BaseReg, NumBytes, true, TII);
479 if (isLowRegister(DestReg) && isLowRegister(BaseReg)) {
480 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
481 unsigned Chunk = (1 << 3) - 1;
482 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
484 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
485 .addReg(BaseReg).addImm(ThisVal);
487 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), DestReg).addReg(BaseReg);
492 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
494 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
497 // Build the new tADD / tSUB.
499 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(DestReg).addImm(ThisVal);
501 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(BaseReg).addImm(ThisVal);
504 if (Opc == ARM::tADDrSPi) {
510 Chunk = ((1 << NumBits) - 1) * Scale;
511 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
518 BuildMI(MBB, MBBI, TII.get(ExtraOpc), DestReg).addReg(DestReg)
519 .addImm(((unsigned)NumBytes) & 3);
523 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
524 int NumBytes, bool isThumb, const TargetInstrInfo &TII) {
526 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
528 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
531 void ARMRegisterInfo::
532 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
533 MachineBasicBlock::iterator I) const {
535 // If we have alloca, convert as follows:
536 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
537 // ADJCALLSTACKUP -> add, sp, sp, amount
538 MachineInstr *Old = I;
539 unsigned Amount = Old->getOperand(0).getImmedValue();
541 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
542 // We need to keep the stack aligned properly. To do this, we round the
543 // amount of space needed for the outgoing arguments up to the next
544 // alignment boundary.
545 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
546 Amount = (Amount+Align-1)/Align*Align;
548 // Replace the pseudo instruction with a new instruction...
549 if (Old->getOpcode() == ARM::ADJCALLSTACKDOWN) {
550 emitSPUpdate(MBB, I, -Amount, AFI->isThumbFunction(), TII);
552 assert(Old->getOpcode() == ARM::ADJCALLSTACKUP);
553 emitSPUpdate(MBB, I, Amount, AFI->isThumbFunction(), TII);
560 /// emitThumbConstant - Emit a series of instructions to materialize a
562 static void emitThumbConstant(MachineBasicBlock &MBB,
563 MachineBasicBlock::iterator &MBBI,
564 unsigned DestReg, int Imm,
565 const TargetInstrInfo &TII) {
566 bool isSub = Imm < 0;
567 if (isSub) Imm = -Imm;
569 int Chunk = (1 << 8) - 1;
570 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
572 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), DestReg).addImm(ThisVal);
574 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII);
576 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), DestReg).addReg(DestReg);
579 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const{
581 MachineInstr &MI = *II;
582 MachineBasicBlock &MBB = *MI.getParent();
583 MachineFunction &MF = *MBB.getParent();
584 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
585 bool isThumb = AFI->isThumbFunction();
587 while (!MI.getOperand(i).isFrameIndex()) {
589 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
592 unsigned FrameReg = ARM::SP;
593 int FrameIndex = MI.getOperand(i).getFrameIndex();
594 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
595 MF.getFrameInfo()->getStackSize();
597 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
598 Offset -= AFI->getGPRCalleeSavedArea1Offset();
599 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
600 Offset -= AFI->getGPRCalleeSavedArea2Offset();
601 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
602 Offset -= AFI->getDPRCalleeSavedAreaOffset();
603 else if (hasFP(MF)) {
604 // There is alloca()'s in this function, must reference off the frame
606 FrameReg = getFrameRegister(MF);
607 Offset -= AFI->getFramePtrSpillOffset();
610 unsigned Opcode = MI.getOpcode();
611 const TargetInstrDescriptor &Desc = TII.get(Opcode);
612 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
615 if (Opcode == ARM::ADDri) {
616 Offset += MI.getOperand(i+1).getImm();
618 // Turn it into a move.
619 MI.setInstrDescriptor(TII.get(ARM::MOVrr));
620 MI.getOperand(i).ChangeToRegister(FrameReg, false);
621 MI.RemoveOperand(i+1);
623 } else if (Offset < 0) {
626 MI.setInstrDescriptor(TII.get(ARM::SUBri));
629 // Common case: small offset, fits into instruction.
630 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
631 if (ImmedOffset != -1) {
632 // Replace the FrameIndex with sp / fp
633 MI.getOperand(i).ChangeToRegister(FrameReg, false);
634 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
638 // Otherwise, we fallback to common code below to form the imm offset with
639 // a sequence of ADDri instructions. First though, pull as much of the imm
640 // into this ADDri as possible.
641 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
642 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, (32-RotAmt) & 31);
644 // We will handle these bits from offset, clear them.
645 Offset &= ~ThisImmVal;
647 // Get the properly encoded SOImmVal field.
648 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
649 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
650 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
651 } else if (Opcode == ARM::tADDrSPi) {
652 Offset += MI.getOperand(i+1).getImm();
653 assert((Offset & 3) == 0 &&
654 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
656 // Turn it into a move.
657 MI.setInstrDescriptor(TII.get(ARM::tMOVrr));
658 MI.getOperand(i).ChangeToRegister(FrameReg, false);
659 MI.RemoveOperand(i+1);
663 // Common case: small offset, fits into instruction.
664 if (((Offset >> 2) & ~255U) == 0) {
665 // Replace the FrameIndex with sp / fp
666 MI.getOperand(i).ChangeToRegister(FrameReg, false);
667 MI.getOperand(i+1).ChangeToImmediate(Offset >> 2);
671 unsigned DestReg = MI.getOperand(0).getReg();
672 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
673 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, 8, 1);
674 // MI would expand into a large number of instructions. Don't try to
675 // simplify the immediate.
677 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII);
683 // Translate r0 = add sp, imm to
684 // r0 = add sp, 255*4
685 // r0 = add r0, (imm - 255*4)
686 MI.getOperand(i).ChangeToRegister(FrameReg, false);
687 MI.getOperand(i+1).ChangeToImmediate(255);
688 Offset = (Offset - 255 * 4);
689 MachineBasicBlock::iterator NII = next(II);
690 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII);
692 // Translate r0 = add sp, -imm to
693 // r0 = -imm (this is then translated into a series of instructons)
695 emitThumbConstant(MBB, II, DestReg, Offset, TII);
696 MI.setInstrDescriptor(TII.get(ARM::tADDhirr));
697 MI.getOperand(i).ChangeToRegister(DestReg, false);
698 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
704 unsigned NumBits = 0;
707 case ARMII::AddrMode2: {
709 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
710 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
715 case ARMII::AddrMode3: {
717 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
718 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
723 case ARMII::AddrMode5: {
725 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
726 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
732 case ARMII::AddrModeTs: {
734 InstrOffs = MI.getOperand(ImmIdx).getImm();
735 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
740 assert(0 && "Unsupported addressing mode!");
745 Offset += InstrOffs * Scale;
746 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
747 if (Offset < 0 && !isThumb) {
752 // Common case: small offset, fits into instruction.
753 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
754 int ImmedOffset = Offset / Scale;
755 unsigned Mask = (1 << NumBits) - 1;
756 if ((unsigned)Offset <= Mask * Scale) {
757 // Replace the FrameIndex with sp
758 MI.getOperand(i).ChangeToRegister(FrameReg, false);
760 ImmedOffset |= 1 << NumBits;
761 ImmOp.ChangeToImmediate(ImmedOffset);
765 // If this is a thumb spill / restore, we will be using a constpool load to
766 // materialize the offset.
767 bool isThumSpillRestore = Opcode == ARM::tRestore || Opcode == ARM::tSpill;
768 if (AddrMode == ARMII::AddrModeTs && !isThumSpillRestore) {
769 if (AddrMode == ARMII::AddrModeTs) {
770 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
771 // a different base register.
773 Mask = (1 << NumBits) - 1;
775 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
776 ImmedOffset = ImmedOffset & Mask;
778 ImmedOffset |= 1 << NumBits;
779 ImmOp.ChangeToImmediate(ImmedOffset);
780 Offset &= ~(Mask*Scale);
784 // If we get here, the immediate doesn't fit into the instruction. We folded
785 // as much as possible above, handle the rest, providing a register that is
787 assert(Offset && "This code isn't needed if offset already handled!");
790 if (TII.isLoad(Opcode)) {
791 // Use the destination register to materialize sp + offset.
792 unsigned TmpReg = MI.getOperand(0).getReg();
794 if (Opcode == ARM::tRestore) {
795 if (FrameReg == ARM::SP)
796 emitThumbRegPlusConstPool(MBB, II, TmpReg, FrameReg,Offset,false,TII);
798 emitLoadConstPool(MBB, II, TmpReg, Offset, TII);
802 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
803 MI.setInstrDescriptor(TII.get(ARM::tLDR));
804 MI.getOperand(i).ChangeToRegister(TmpReg, false);
806 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
808 MI.addRegOperand(0, false); // tLDR has an extra register operand.
809 } else if (TII.isStore(Opcode)) {
810 // FIXME! This is horrific!!! We need register scavenging.
811 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
812 // also a ABI register so it's possible that is is the register that is
813 // being storing here. If that's the case, we do the following:
815 // Use r2 to materialize sp + offset
818 unsigned ValReg = MI.getOperand(0).getReg();
819 unsigned TmpReg = ARM::R3;
821 if (ValReg == ARM::R3) {
822 BuildMI(MBB, II, TII.get(ARM::tMOVrr), ARM::R12).addReg(ARM::R2);
825 if (TmpReg == ARM::R3 && AFI->isR3IsLiveIn())
826 BuildMI(MBB, II, TII.get(ARM::tMOVrr), ARM::R12).addReg(ARM::R3);
827 if (Opcode == ARM::tSpill) {
828 if (FrameReg == ARM::SP)
829 emitThumbRegPlusConstPool(MBB, II, TmpReg, FrameReg,Offset,false,TII);
831 emitLoadConstPool(MBB, II, TmpReg, Offset, TII);
835 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg, Offset, TII);
836 MI.setInstrDescriptor(TII.get(ARM::tSTR));
837 MI.getOperand(i).ChangeToRegister(TmpReg, false);
839 MI.addRegOperand(FrameReg, false); // Use [reg, reg] addrmode.
841 MI.addRegOperand(0, false); // tSTR has an extra register operand.
843 MachineBasicBlock::iterator NII = next(II);
844 if (ValReg == ARM::R3)
845 BuildMI(MBB, NII, TII.get(ARM::tMOVrr), ARM::R2).addReg(ARM::R12);
846 if (TmpReg == ARM::R3 && AFI->isR3IsLiveIn())
847 BuildMI(MBB, NII, TII.get(ARM::tMOVrr), ARM::R3).addReg(ARM::R12);
849 assert(false && "Unexpected opcode!");
851 // Insert a set of r12 with the full address: r12 = sp + offset
852 // If the offset we have is too large to fit into the instruction, we need
853 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
855 emitARMRegPlusImmediate(MBB, II, ARM::R12, FrameReg,
856 isSub ? -Offset : Offset, TII);
857 MI.getOperand(i).ChangeToRegister(ARM::R12, false);
861 void ARMRegisterInfo::
862 processFunctionBeforeCalleeSavedScan(MachineFunction &MF) const {
863 // This tells PEI to spill the FP as if it is any other callee-save register
864 // to take advantage the eliminateFrameIndex machinery. This also ensures it
865 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
866 // to combine multiple loads / stores.
867 bool CanEliminateFrame = true;
868 bool CS1Spilled = false;
869 bool LRSpilled = false;
870 unsigned NumGPRSpills = 0;
871 SmallVector<unsigned, 4> UnspilledCS1GPRs;
872 SmallVector<unsigned, 4> UnspilledCS2GPRs;
874 // Don't spill FP if the frame can be eliminated. This is determined
875 // by scanning the callee-save registers to see if any is used.
876 const unsigned *CSRegs = getCalleeSavedRegs();
877 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
878 for (unsigned i = 0; CSRegs[i]; ++i) {
879 unsigned Reg = CSRegs[i];
880 bool Spilled = false;
881 if (MF.isPhysRegUsed(Reg)) {
883 CanEliminateFrame = false;
885 // Check alias registers too.
886 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
887 if (MF.isPhysRegUsed(*Aliases)) {
889 CanEliminateFrame = false;
894 if (CSRegClasses[i] == &ARM::GPRRegClass) {
898 if (!STI.isTargetDarwin()) {
906 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
921 if (!STI.isTargetDarwin()) {
922 UnspilledCS1GPRs.push_back(Reg);
932 UnspilledCS1GPRs.push_back(Reg);
935 UnspilledCS2GPRs.push_back(Reg);
942 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
943 bool ForceLRSpill = false;
944 if (!LRSpilled && AFI->isThumbFunction()) {
945 unsigned FnSize = ARM::GetFunctionSize(MF);
946 // Force LR spill if the Thumb function size is > 2048. This enables the
947 // use of BL to implement far jump. If it turns out that it's not needed
948 // the branch fix up path will undo it.
949 if (FnSize >= (1 << 11)) {
950 CanEliminateFrame = false;
955 if (!CanEliminateFrame || hasFP(MF)) {
956 AFI->setHasStackFrame(true);
958 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
959 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
960 if (!LRSpilled && CS1Spilled) {
961 MF.changePhyRegUsed(ARM::LR, true);
963 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
964 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
965 ForceLRSpill = false;
968 // Darwin ABI requires FP to point to the stack slot that contains the
970 if (STI.isTargetDarwin() || hasFP(MF)) {
971 MF.changePhyRegUsed(FramePtr, true);
975 // If stack and double are 8-byte aligned and we are spilling an odd number
976 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
977 // the integer and double callee save areas.
978 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
979 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
980 if (CS1Spilled && !UnspilledCS1GPRs.empty())
981 MF.changePhyRegUsed(UnspilledCS1GPRs.front(), true);
982 else if (!UnspilledCS2GPRs.empty())
983 MF.changePhyRegUsed(UnspilledCS2GPRs.front(), true);
988 MF.changePhyRegUsed(ARM::LR, true);
989 AFI->setLRIsForceSpilled(true);
993 /// Move iterator pass the next bunch of callee save load / store ops for
994 /// the particular spill area (1: integer area 1, 2: integer area 2,
995 /// 3: fp area, 0: don't care).
996 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
997 MachineBasicBlock::iterator &MBBI,
998 int Opc, unsigned Area,
999 const ARMSubtarget &STI) {
1000 while (MBBI != MBB.end() &&
1001 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFrameIndex()) {
1004 unsigned Category = 0;
1005 switch (MBBI->getOperand(0).getReg()) {
1006 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1010 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1011 Category = STI.isTargetDarwin() ? 2 : 1;
1013 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1014 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1021 if (Done || Category != Area)
1029 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
1030 MachineBasicBlock &MBB = MF.front();
1031 MachineBasicBlock::iterator MBBI = MBB.begin();
1032 MachineFrameInfo *MFI = MF.getFrameInfo();
1033 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1034 bool isThumb = AFI->isThumbFunction();
1035 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1036 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1037 unsigned NumBytes = MFI->getStackSize();
1038 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1041 // Check if R3 is live in. It might have to be used as a scratch register.
1042 for (MachineFunction::livein_iterator I=MF.livein_begin(),E=MF.livein_end();
1044 if ((*I).first == ARM::R3) {
1045 AFI->setR3IsLiveIn(true);
1050 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
1051 NumBytes = (NumBytes + 3) & ~3;
1052 MFI->setStackSize(NumBytes);
1055 // Determine the sizes of each callee-save spill areas and record which frame
1056 // belongs to which callee-save spill areas.
1057 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1058 int FramePtrSpillFI = 0;
1059 if (!AFI->hasStackFrame()) {
1061 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1066 emitSPUpdate(MBB, MBBI, -VARegSaveSize, isThumb, TII);
1068 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1069 unsigned Reg = CSI[i].getReg();
1070 int FI = CSI[i].getFrameIdx();
1077 if (Reg == FramePtr)
1078 FramePtrSpillFI = FI;
1079 AFI->addGPRCalleeSavedArea1Frame(FI);
1086 if (Reg == FramePtr)
1087 FramePtrSpillFI = FI;
1088 if (STI.isTargetDarwin()) {
1089 AFI->addGPRCalleeSavedArea2Frame(FI);
1092 AFI->addGPRCalleeSavedArea1Frame(FI);
1097 AFI->addDPRCalleeSavedAreaFrame(FI);
1102 if (Align == 8 && (GPRCS1Size & 7) != 0)
1103 // Pad CS1 to ensure proper alignment.
1107 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1108 emitSPUpdate(MBB, MBBI, -GPRCS1Size, isThumb, TII);
1109 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1110 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH)
1113 // Darwin ABI requires FP to point to the stack slot that contains the
1115 if (STI.isTargetDarwin() || hasFP(MF))
1116 BuildMI(MBB, MBBI, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri), FramePtr)
1117 .addFrameIndex(FramePtrSpillFI).addImm(0);
1120 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1121 emitSPUpdate(MBB, MBBI, -GPRCS2Size, false, TII);
1123 // Build the new SUBri to adjust SP for FP callee-save spill area.
1124 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1125 emitSPUpdate(MBB, MBBI, -DPRCSSize, false, TII);
1128 // Determine starting offsets of spill areas.
1129 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1130 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1131 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1132 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1133 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1134 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1135 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1137 NumBytes = DPRCSOffset;
1139 // Insert it after all the callee-save spills.
1141 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1142 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1145 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1146 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1147 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1150 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1151 for (unsigned i = 0; CSRegs[i]; ++i)
1152 if (Reg == CSRegs[i])
1157 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1158 return ((MI->getOpcode() == ARM::FLDD ||
1159 MI->getOpcode() == ARM::LDR ||
1160 MI->getOpcode() == ARM::tRestore) &&
1161 MI->getOperand(1).isFrameIndex() &&
1162 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1165 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1166 MachineBasicBlock &MBB) const {
1167 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1168 assert((MBBI->getOpcode() == ARM::BX_RET ||
1169 MBBI->getOpcode() == ARM::tBX_RET ||
1170 MBBI->getOpcode() == ARM::tPOP_RET) &&
1171 "Can only insert epilog into returning blocks");
1173 MachineFrameInfo *MFI = MF.getFrameInfo();
1174 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1175 bool isThumb = AFI->isThumbFunction();
1176 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1177 int NumBytes = (int)MFI->getStackSize();
1178 if (!AFI->hasStackFrame()) {
1180 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1184 // Unwind MBBI to point to first LDR / FLDD.
1185 const unsigned *CSRegs = getCalleeSavedRegs();
1186 if (MBBI != MBB.begin()) {
1189 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1190 if (!isCSRestore(MBBI, CSRegs))
1194 // Move SP to start of FP callee save spill area.
1195 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1196 AFI->getGPRCalleeSavedArea2Size() +
1197 AFI->getDPRCalleeSavedAreaSize());
1200 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1201 // Reset SP based on frame pointer only if the stack frame extends beyond
1202 // frame pointer stack slot or target is ELF and the function has FP.
1204 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, FramePtr, -NumBytes, TII);
1206 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::SP).addReg(FramePtr);
1208 if (MBBI->getOpcode() == ARM::tBX_RET &&
1209 &MBB.front() != MBBI &&
1210 prior(MBBI)->getOpcode() == ARM::tPOP) {
1211 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1212 emitSPUpdate(MBB, PMBBI, NumBytes, isThumb, TII);
1214 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1217 // Darwin ABI requires FP to point to the stack slot that contains the
1219 if (STI.isTargetDarwin() || hasFP(MF)) {
1220 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1221 // Reset SP based on frame pointer only if the stack frame extends beyond
1222 // frame pointer stack slot or target is ELF and the function has FP.
1223 if (AFI->getGPRCalleeSavedArea2Size() ||
1224 AFI->getDPRCalleeSavedAreaSize() ||
1225 AFI->getDPRCalleeSavedAreaOffset()||
1228 BuildMI(MBB, MBBI, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1231 BuildMI(MBB, MBBI, TII.get(ARM::MOVrr), ARM::SP).addReg(FramePtr);
1232 } else if (NumBytes) {
1233 emitSPUpdate(MBB, MBBI, NumBytes, false, TII);
1236 // Move SP to start of integer callee save spill area 2.
1237 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1238 emitSPUpdate(MBB, MBBI, AFI->getDPRCalleeSavedAreaSize(), false, TII);
1240 // Move SP to start of integer callee save spill area 1.
1241 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1242 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea2Size(), false, TII);
1244 // Move SP to SP upon entry to the function.
1245 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1246 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea1Size(), false, TII);
1249 if (VARegSaveSize) {
1251 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1252 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1253 BuildMI(MBB, MBBI, TII.get(ARM::tPOP)).addReg(ARM::R3);
1255 emitSPUpdate(MBB, MBBI, VARegSaveSize, isThumb, TII);
1258 BuildMI(MBB, MBBI, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1264 unsigned ARMRegisterInfo::getRARegister() const {
1268 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1269 return STI.useThumbBacktraces() ? ARM::R7 : ARM::R11;
1272 #include "ARMGenRegisterInfo.inc"