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"
37 unsigned ARMRegisterInfo::getRegisterNumbering(unsigned RegEnum) {
40 case R0: case S0: case D0: return 0;
41 case R1: case S1: case D1: return 1;
42 case R2: case S2: case D2: return 2;
43 case R3: case S3: case D3: return 3;
44 case R4: case S4: case D4: return 4;
45 case R5: case S5: case D5: return 5;
46 case R6: case S6: case D6: return 6;
47 case R7: case S7: case D7: return 7;
48 case R8: case S8: case D8: return 8;
49 case R9: case S9: case D9: return 9;
50 case R10: case S10: case D10: return 10;
51 case R11: case S11: case D11: return 11;
52 case R12: case S12: case D12: return 12;
53 case SP: case S13: case D13: return 13;
54 case LR: case S14: case D14: return 14;
55 case PC: case S15: case D15: return 15;
73 std::cerr << "Unknown ARM register!\n";
78 ARMRegisterInfo::ARMRegisterInfo(const TargetInstrInfo &tii,
79 const ARMSubtarget &sti)
80 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
82 FramePtr(STI.useThumbBacktraces() ? ARM::R7 : ARM::R11) {
85 bool ARMRegisterInfo::spillCalleeSavedRegisters(MachineBasicBlock &MBB,
86 MachineBasicBlock::iterator MI,
87 const std::vector<CalleeSavedInfo> &CSI) const {
88 MachineFunction &MF = *MBB.getParent();
89 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
90 if (!AFI->isThumbFunction() || CSI.empty())
93 MachineInstrBuilder MIB = BuildMI(MBB, MI, TII.get(ARM::tPUSH));
94 for (unsigned i = CSI.size(); i != 0; --i)
95 MIB.addReg(CSI[i-1].getReg());
99 bool ARMRegisterInfo::restoreCalleeSavedRegisters(MachineBasicBlock &MBB,
100 MachineBasicBlock::iterator MI,
101 const std::vector<CalleeSavedInfo> &CSI) const {
102 MachineFunction &MF = *MBB.getParent();
103 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
104 if (!AFI->isThumbFunction() || CSI.empty())
107 bool isVarArg = AFI->getVarArgsRegSaveSize() > 0;
108 MachineInstr *PopMI = new MachineInstr(TII.get(ARM::tPOP));
109 MBB.insert(MI, PopMI);
110 for (unsigned i = CSI.size(); i != 0; --i) {
111 unsigned Reg = CSI[i-1].getReg();
112 if (Reg == ARM::LR) {
113 // Special epilogue for vararg functions. See emitEpilogue
117 PopMI->setInstrDescriptor(TII.get(ARM::tPOP_RET));
120 PopMI->addRegOperand(Reg, true);
125 void ARMRegisterInfo::
126 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
127 unsigned SrcReg, int FI,
128 const TargetRegisterClass *RC) const {
129 if (RC == ARM::GPRRegisterClass) {
130 MachineFunction &MF = *MBB.getParent();
131 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
132 if (AFI->isThumbFunction())
133 BuildMI(MBB, I, TII.get(ARM::tSTRspi)).addReg(SrcReg)
134 .addFrameIndex(FI).addImm(0);
136 BuildMI(MBB, I, TII.get(ARM::STR)).addReg(SrcReg)
137 .addFrameIndex(FI).addReg(0).addImm(0);
138 } else if (RC == ARM::DPRRegisterClass) {
139 BuildMI(MBB, I, TII.get(ARM::FSTD)).addReg(SrcReg)
140 .addFrameIndex(FI).addImm(0);
142 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
143 BuildMI(MBB, I, TII.get(ARM::FSTS)).addReg(SrcReg)
144 .addFrameIndex(FI).addImm(0);
148 void ARMRegisterInfo::
149 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I,
150 unsigned DestReg, int FI,
151 const TargetRegisterClass *RC) const {
152 if (RC == ARM::GPRRegisterClass) {
153 MachineFunction &MF = *MBB.getParent();
154 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
155 if (AFI->isThumbFunction())
156 BuildMI(MBB, I, TII.get(ARM::tLDRspi), DestReg)
157 .addFrameIndex(FI).addImm(0);
159 BuildMI(MBB, I, TII.get(ARM::LDR), DestReg)
160 .addFrameIndex(FI).addReg(0).addImm(0);
161 } else if (RC == ARM::DPRRegisterClass) {
162 BuildMI(MBB, I, TII.get(ARM::FLDD), DestReg)
163 .addFrameIndex(FI).addImm(0);
165 assert(RC == ARM::SPRRegisterClass && "Unknown regclass!");
166 BuildMI(MBB, I, TII.get(ARM::FLDS), DestReg)
167 .addFrameIndex(FI).addImm(0);
171 void ARMRegisterInfo::copyRegToReg(MachineBasicBlock &MBB,
172 MachineBasicBlock::iterator I,
173 unsigned DestReg, unsigned SrcReg,
174 const TargetRegisterClass *RC) const {
175 if (RC == ARM::GPRRegisterClass) {
176 MachineFunction &MF = *MBB.getParent();
177 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
178 BuildMI(MBB, I, TII.get(AFI->isThumbFunction() ? ARM::tMOVrr : ARM::MOVrr),
179 DestReg).addReg(SrcReg);
180 } else if (RC == ARM::SPRRegisterClass)
181 BuildMI(MBB, I, TII.get(ARM::FCPYS), DestReg).addReg(SrcReg);
182 else if (RC == ARM::DPRRegisterClass)
183 BuildMI(MBB, I, TII.get(ARM::FCPYD), DestReg).addReg(SrcReg);
188 MachineInstr *ARMRegisterInfo::foldMemoryOperand(MachineInstr *MI,
189 unsigned OpNum, int FI) const {
190 unsigned Opc = MI->getOpcode();
191 MachineInstr *NewMI = NULL;
195 if (OpNum == 0) { // move -> store
196 unsigned SrcReg = MI->getOperand(1).getReg();
197 NewMI = BuildMI(TII.get(ARM::STR)).addReg(SrcReg).addFrameIndex(FI)
198 .addReg(0).addImm(0);
199 } else { // move -> load
200 unsigned DstReg = MI->getOperand(0).getReg();
201 NewMI = BuildMI(TII.get(ARM::LDR), DstReg).addFrameIndex(FI).addReg(0)
207 if (OpNum == 0) { // move -> store
208 unsigned SrcReg = MI->getOperand(1).getReg();
209 NewMI = BuildMI(TII.get(ARM::tSTRspi)).addReg(SrcReg).addFrameIndex(FI)
211 } else { // move -> load
212 unsigned DstReg = MI->getOperand(0).getReg();
213 NewMI = BuildMI(TII.get(ARM::tLDRspi), DstReg).addFrameIndex(FI)
219 if (OpNum == 0) { // move -> store
220 unsigned SrcReg = MI->getOperand(1).getReg();
221 NewMI = BuildMI(TII.get(ARM::FSTS)).addReg(SrcReg).addFrameIndex(FI)
223 } else { // move -> load
224 unsigned DstReg = MI->getOperand(0).getReg();
225 NewMI = BuildMI(TII.get(ARM::FLDS), DstReg).addFrameIndex(FI).addImm(0);
230 if (OpNum == 0) { // move -> store
231 unsigned SrcReg = MI->getOperand(1).getReg();
232 NewMI = BuildMI(TII.get(ARM::FSTD)).addReg(SrcReg).addFrameIndex(FI)
234 } else { // move -> load
235 unsigned DstReg = MI->getOperand(0).getReg();
236 NewMI = BuildMI(TII.get(ARM::FLDD), DstReg).addFrameIndex(FI).addImm(0);
243 NewMI->copyKillDeadInfo(MI);
247 const unsigned* ARMRegisterInfo::getCalleeSavedRegs() const {
248 static const unsigned CalleeSavedRegs[] = {
249 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
250 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
252 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
253 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
257 static const unsigned DarwinCalleeSavedRegs[] = {
258 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
259 ARM::R11, ARM::R10, ARM::R9, ARM::R8,
261 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
262 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
265 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
268 const TargetRegisterClass* const *
269 ARMRegisterInfo::getCalleeSavedRegClasses() const {
270 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
271 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
272 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
273 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
275 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
276 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
279 return CalleeSavedRegClasses;
282 /// hasFP - Return true if the specified function should have a dedicated frame
283 /// pointer register. This is true if the function has variable sized allocas
284 /// or if frame pointer elimination is disabled.
286 bool ARMRegisterInfo::hasFP(const MachineFunction &MF) const {
287 return NoFramePointerElim || MF.getFrameInfo()->hasVarSizedObjects();
290 /// emitARMRegPlusImmediate - Emits a series of instructions to materialize
291 /// a destreg = basereg + immediate in ARM code.
293 void emitARMRegPlusImmediate(MachineBasicBlock &MBB,
294 MachineBasicBlock::iterator &MBBI,
295 unsigned DestReg, unsigned BaseReg,
296 int NumBytes, const TargetInstrInfo &TII) {
297 bool isSub = NumBytes < 0;
298 if (isSub) NumBytes = -NumBytes;
301 unsigned RotAmt = ARM_AM::getSOImmValRotate(NumBytes);
302 unsigned ThisVal = NumBytes & ARM_AM::rotr32(0xFF, RotAmt);
303 assert(ThisVal && "Didn't extract field correctly");
305 // We will handle these bits from offset, clear them.
306 NumBytes &= ~ThisVal;
308 // Get the properly encoded SOImmVal field.
309 int SOImmVal = ARM_AM::getSOImmVal(ThisVal);
310 assert(SOImmVal != -1 && "Bit extraction didn't work?");
312 // Build the new ADD / SUB.
313 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::SUBri : ARM::ADDri), DestReg)
314 .addReg(BaseReg).addImm(SOImmVal);
319 /// isLowRegister - Returns true if the register is low register r0-r7.
321 static bool isLowRegister(unsigned Reg) {
324 case R0: case R1: case R2: case R3:
325 case R4: case R5: case R6: case R7:
332 /// calcNumMI - Returns the number of instructions required to materialize
333 /// the specific add / sub r, c instruction.
334 static unsigned calcNumMI(int Opc, int ExtraOpc, unsigned Bytes,
335 unsigned NumBits, unsigned Scale) {
337 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
339 if (Opc == ARM::tADDrSPi) {
340 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
345 Chunk = ((1 << NumBits) - 1) * Scale;
348 NumMIs += Bytes / Chunk;
349 if ((Bytes % Chunk) != 0)
356 /// emitThumbRegPlusConstPool - Emits a series of instructions to materialize
357 /// a destreg = basereg + immediate in Thumb code. Load the immediate from a
360 void emitThumbRegPlusConstPool(MachineBasicBlock &MBB,
361 MachineBasicBlock::iterator &MBBI,
362 unsigned DestReg, unsigned BaseReg,
363 int NumBytes, const TargetInstrInfo &TII) {
364 MachineFunction &MF = *MBB.getParent();
365 MachineConstantPool *ConstantPool = MF.getConstantPool();
366 bool isHigh = !isLowRegister(DestReg) || !isLowRegister(BaseReg);
368 // Subtract doesn't have high register version. Load the negative value
369 // if either base or dest register is a high register.
370 if (NumBytes < 0 && !isHigh) {
372 NumBytes = -NumBytes;
374 Constant *C = ConstantInt::get(Type::Int32Ty, NumBytes);
375 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 2);
376 unsigned LdReg = DestReg;
377 if (DestReg == ARM::SP) {
378 assert(BaseReg == ARM::SP && "Unexpected!");
380 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::R12).addReg(ARM::R3);
382 // Load the constant.
383 BuildMI(MBB, MBBI, TII.get(ARM::tLDRpci), LdReg).addConstantPoolIndex(Idx);
385 int Opc = (isSub) ? ARM::tSUBrr : (isHigh ? ARM::tADDhirr : ARM::tADDrr);
386 const MachineInstrBuilder MIB = BuildMI(MBB, MBBI, TII.get(Opc), DestReg);
387 if (DestReg == ARM::SP)
388 MIB.addReg(BaseReg).addReg(LdReg);
390 MIB.addReg(BaseReg).addReg(LdReg);
392 MIB.addReg(LdReg).addReg(BaseReg);
393 if (DestReg == ARM::SP)
394 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), ARM::R3).addReg(ARM::R12);
397 /// emitThumbRegPlusImmediate - Emits a series of instructions to materialize
398 /// a destreg = basereg + immediate in Thumb code.
400 void emitThumbRegPlusImmediate(MachineBasicBlock &MBB,
401 MachineBasicBlock::iterator &MBBI,
402 unsigned DestReg, unsigned BaseReg,
403 int NumBytes, const TargetInstrInfo &TII) {
404 bool isSub = NumBytes < 0;
405 unsigned Bytes = (unsigned)NumBytes;
406 if (isSub) Bytes = -NumBytes;
407 bool isMul4 = (Bytes & 3) == 0;
408 bool isTwoAddr = false;
409 bool DstNeBase = false;
410 unsigned NumBits = 1;
415 if (DestReg == BaseReg && BaseReg == ARM::SP) {
416 assert(isMul4 && "Thumb sp inc / dec size must be multiple of 4!");
419 Opc = isSub ? ARM::tSUBspi : ARM::tADDspi;
421 } else if (!isSub && BaseReg == ARM::SP) {
424 // r1 = add sp, 100 * 4
428 ExtraOpc = ARM::tADDi3;
437 if (DestReg != BaseReg)
440 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
444 unsigned NumMIs = calcNumMI(Opc, ExtraOpc, Bytes, NumBits, Scale);
445 unsigned Threshold = (DestReg == ARM::SP) ? 4 : 3;
446 if (NumMIs > Threshold) {
447 // This will expand into too many instructions. Load the immediate from a
449 emitThumbRegPlusConstPool(MBB, MBBI, DestReg, BaseReg, NumBytes, TII);
454 if (isLowRegister(DestReg) && isLowRegister(BaseReg)) {
455 // If both are low registers, emit DestReg = add BaseReg, max(Imm, 7)
456 unsigned Chunk = (1 << 3) - 1;
457 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
459 BuildMI(MBB, MBBI, TII.get(isSub ? ARM::tSUBi3 : ARM::tADDi3), DestReg)
460 .addReg(BaseReg).addImm(ThisVal);
462 BuildMI(MBB, MBBI, TII.get(ARM::tMOVrr), DestReg).addReg(BaseReg);
467 unsigned Chunk = ((1 << NumBits) - 1) * Scale;
469 unsigned ThisVal = (Bytes > Chunk) ? Chunk : Bytes;
472 // Build the new tADD / tSUB.
474 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(DestReg).addImm(ThisVal);
476 BuildMI(MBB, MBBI, TII.get(Opc), DestReg).addReg(BaseReg).addImm(ThisVal);
479 if (Opc == ARM::tADDrSPi) {
485 Chunk = ((1 << NumBits) - 1) * Scale;
486 Opc = isSub ? ARM::tSUBi8 : ARM::tADDi8;
493 BuildMI(MBB, MBBI, TII.get(ExtraOpc), DestReg).addReg(DestReg)
494 .addImm(((unsigned)NumBytes) & 3);
498 void emitSPUpdate(MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
499 int NumBytes, bool isThumb, const TargetInstrInfo &TII) {
501 emitThumbRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
503 emitARMRegPlusImmediate(MBB, MBBI, ARM::SP, ARM::SP, NumBytes, TII);
506 void ARMRegisterInfo::
507 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
508 MachineBasicBlock::iterator I) const {
510 // If we have alloca, convert as follows:
511 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
512 // ADJCALLSTACKUP -> add, sp, sp, amount
513 MachineInstr *Old = I;
514 unsigned Amount = Old->getOperand(0).getImmedValue();
516 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
517 // We need to keep the stack aligned properly. To do this, we round the
518 // amount of space needed for the outgoing arguments up to the next
519 // alignment boundary.
520 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
521 Amount = (Amount+Align-1)/Align*Align;
523 // Replace the pseudo instruction with a new instruction...
524 if (Old->getOpcode() == ARM::ADJCALLSTACKDOWN) {
525 emitSPUpdate(MBB, I, -Amount, AFI->isThumbFunction(), TII);
527 assert(Old->getOpcode() == ARM::ADJCALLSTACKUP);
528 emitSPUpdate(MBB, I, Amount, AFI->isThumbFunction(), TII);
535 /// emitThumbConstant - Emit a series of instructions to materialize a
537 static void emitThumbConstant(MachineBasicBlock &MBB,
538 MachineBasicBlock::iterator &MBBI,
539 unsigned DestReg, int Imm,
540 const TargetInstrInfo &TII) {
541 bool isSub = Imm < 0;
542 if (isSub) Imm = -Imm;
544 int Chunk = (1 << 8) - 1;
545 int ThisVal = (Imm > Chunk) ? Chunk : Imm;
547 BuildMI(MBB, MBBI, TII.get(ARM::tMOVri8), DestReg).addImm(ThisVal);
549 emitThumbRegPlusImmediate(MBB, MBBI, DestReg, DestReg, Imm, TII);
551 BuildMI(MBB, MBBI, TII.get(ARM::tNEG), DestReg).addReg(DestReg);
554 void ARMRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II) const{
556 MachineInstr &MI = *II;
557 MachineBasicBlock &MBB = *MI.getParent();
558 MachineFunction &MF = *MBB.getParent();
559 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
560 bool isThumb = AFI->isThumbFunction();
562 while (!MI.getOperand(i).isFrameIndex()) {
564 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
567 unsigned FrameReg = ARM::SP;
568 int FrameIndex = MI.getOperand(i).getFrameIndex();
569 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
570 MF.getFrameInfo()->getStackSize();
572 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
573 Offset -= AFI->getGPRCalleeSavedArea1Offset();
574 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
575 Offset -= AFI->getGPRCalleeSavedArea2Offset();
576 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
577 Offset -= AFI->getDPRCalleeSavedAreaOffset();
578 else if (hasFP(MF)) {
579 // There is alloca()'s in this function, must reference off the frame
581 FrameReg = getFrameRegister(MF);
582 Offset -= AFI->getFramePtrSpillOffset();
585 unsigned Opcode = MI.getOpcode();
586 const TargetInstrDescriptor &Desc = TII.get(Opcode);
587 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
590 if (Opcode == ARM::ADDri) {
591 Offset += MI.getOperand(i+1).getImm();
593 // Turn it into a move.
594 MI.setInstrDescriptor(TII.get(ARM::MOVrr));
595 MI.getOperand(i).ChangeToRegister(FrameReg, false);
596 MI.RemoveOperand(i+1);
598 } else if (Offset < 0) {
601 MI.setInstrDescriptor(TII.get(ARM::SUBri));
604 // Common case: small offset, fits into instruction.
605 int ImmedOffset = ARM_AM::getSOImmVal(Offset);
606 if (ImmedOffset != -1) {
607 // Replace the FrameIndex with sp / fp
608 MI.getOperand(i).ChangeToRegister(FrameReg, false);
609 MI.getOperand(i+1).ChangeToImmediate(ImmedOffset);
613 // Otherwise, we fallback to common code below to form the imm offset with
614 // a sequence of ADDri instructions. First though, pull as much of the imm
615 // into this ADDri as possible.
616 unsigned RotAmt = ARM_AM::getSOImmValRotate(Offset);
617 unsigned ThisImmVal = Offset & ARM_AM::rotr32(0xFF, (32-RotAmt) & 31);
619 // We will handle these bits from offset, clear them.
620 Offset &= ~ThisImmVal;
622 // Get the properly encoded SOImmVal field.
623 int ThisSOImmVal = ARM_AM::getSOImmVal(ThisImmVal);
624 assert(ThisSOImmVal != -1 && "Bit extraction didn't work?");
625 MI.getOperand(i+1).ChangeToImmediate(ThisSOImmVal);
626 } else if (Opcode == ARM::tADDrSPi) {
627 Offset += MI.getOperand(i+1).getImm();
628 assert((Offset & 3) == 0 &&
629 "Thumb add/sub sp, #imm immediate must be multiple of 4!");
631 // Turn it into a move.
632 MI.setInstrDescriptor(TII.get(ARM::tMOVrr));
633 MI.getOperand(i).ChangeToRegister(FrameReg, false);
634 MI.RemoveOperand(i+1);
638 // Common case: small offset, fits into instruction.
639 if (((Offset >> 2) & ~255U) == 0) {
640 // Replace the FrameIndex with sp / fp
641 MI.getOperand(i).ChangeToRegister(FrameReg, false);
642 MI.getOperand(i+1).ChangeToImmediate(Offset >> 2);
646 unsigned DestReg = MI.getOperand(0).getReg();
647 unsigned Bytes = (Offset > 0) ? Offset : -Offset;
648 unsigned NumMIs = calcNumMI(Opcode, 0, Bytes, 8, 1);
649 // MI would expand into a large number of instructions. Don't try to
650 // simplify the immediate.
652 emitThumbRegPlusImmediate(MBB, II, DestReg, FrameReg, Offset, TII);
658 // Translate r0 = add sp, imm to
659 // r0 = add sp, 255*4
660 // r0 = add r0, (imm - 255*4)
661 MI.getOperand(i).ChangeToRegister(FrameReg, false);
662 MI.getOperand(i+1).ChangeToImmediate(255);
663 Offset = (Offset - 255 * 4);
664 MachineBasicBlock::iterator NII = next(II);
665 emitThumbRegPlusImmediate(MBB, NII, DestReg, DestReg, Offset, TII);
667 // Translate r0 = add sp, -imm to
668 // r0 = -imm (this is then translated into a series of instructons)
670 emitThumbConstant(MBB, II, DestReg, Offset, TII);
671 MI.setInstrDescriptor(TII.get(ARM::tADDhirr));
672 MI.getOperand(i).ChangeToRegister(DestReg, false);
673 MI.getOperand(i+1).ChangeToRegister(FrameReg, false);
679 unsigned NumBits = 0;
682 case ARMII::AddrMode2: {
684 InstrOffs = ARM_AM::getAM2Offset(MI.getOperand(ImmIdx).getImm());
685 if (ARM_AM::getAM2Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
690 case ARMII::AddrMode3: {
692 InstrOffs = ARM_AM::getAM3Offset(MI.getOperand(ImmIdx).getImm());
693 if (ARM_AM::getAM3Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
698 case ARMII::AddrMode5: {
700 InstrOffs = ARM_AM::getAM5Offset(MI.getOperand(ImmIdx).getImm());
701 if (ARM_AM::getAM5Op(MI.getOperand(ImmIdx).getImm()) == ARM_AM::sub)
707 case ARMII::AddrModeTs: {
709 InstrOffs = MI.getOperand(ImmIdx).getImm();
710 NumBits = (FrameReg == ARM::SP) ? 8 : 5;
715 std::cerr << "Unsupported addressing mode!\n";
720 Offset += InstrOffs * Scale;
721 assert((Offset & (Scale-1)) == 0 && "Can't encode this offset!");
727 if (!isSub || !isThumb) {
728 MachineOperand &ImmOp = MI.getOperand(ImmIdx);
729 int ImmedOffset = Offset / Scale;
730 unsigned Mask = (1 << NumBits) - 1;
731 if ((unsigned)Offset <= Mask * Scale) {
732 // Replace the FrameIndex with sp
733 MI.getOperand(i).ChangeToRegister(FrameReg, false);
735 ImmedOffset |= 1 << NumBits;
736 ImmOp.ChangeToImmediate(ImmedOffset);
740 // Otherwise, it didn't fit. Pull in what we can to simplify the immed.
741 if (AddrMode == ARMII::AddrModeTs) {
742 // Thumb tLDRspi, tSTRspi. These will change to instructions that use
743 // a different base register.
745 Mask = (1 << NumBits) - 1;
748 ImmedOffset = ImmedOffset & Mask;
750 ImmedOffset |= 1 << NumBits;
751 ImmOp.ChangeToImmediate(ImmedOffset);
752 Offset &= ~(Mask*Scale);
756 // If we get here, the immediate doesn't fit into the instruction. We folded
757 // as much as possible above, handle the rest, providing a register that is
759 assert(Offset && "This code isn't needed if offset already handled!");
762 if (TII.isLoad(Opcode)) {
763 // Use the destination register to materialize sp + offset.
764 unsigned TmpReg = MI.getOperand(0).getReg();
765 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg,
766 isSub ? -Offset : Offset, TII);
767 MI.setInstrDescriptor(TII.get(ARM::tLDR));
768 MI.getOperand(i).ChangeToRegister(TmpReg, false);
769 MI.addRegOperand(0, false); // tLDR has an extra register operand.
770 } else if (TII.isStore(Opcode)) {
771 // FIXME! This is horrific!!! We need register scavenging.
772 // Our temporary workaround has marked r3 unavailable. Of course, r3 is
773 // also a ABI register so it's possible that is is the register that is
774 // being storing here. If that's the case, we do the following:
776 // Use r2 to materialize sp + offset
779 unsigned ValReg = MI.getOperand(0).getReg();
780 unsigned TmpReg = ARM::R3;
781 if (ValReg == ARM::R3) {
782 BuildMI(MBB, II, TII.get(ARM::tMOVrr), ARM::R12).addReg(ARM::R2);
785 emitThumbRegPlusImmediate(MBB, II, TmpReg, FrameReg,
786 isSub ? -Offset : Offset, TII);
787 MI.setInstrDescriptor(TII.get(ARM::tSTR));
788 MI.getOperand(i).ChangeToRegister(TmpReg, false);
789 MI.addRegOperand(0, false); // tSTR has an extra register operand.
790 if (ValReg == ARM::R3)
791 BuildMI(MBB, II, TII.get(ARM::tMOVrr), ARM::R2).addReg(ARM::R12);
793 assert(false && "Unexpected opcode!");
795 // Insert a set of r12 with the full address: r12 = sp + offset
796 // If the offset we have is too large to fit into the instruction, we need
797 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
799 emitARMRegPlusImmediate(MBB, II, ARM::R12, FrameReg,
800 isSub ? -Offset : Offset, TII);
801 MI.getOperand(i).ChangeToRegister(ARM::R12, false);
805 void ARMRegisterInfo::
806 processFunctionBeforeCalleeSavedScan(MachineFunction &MF) const {
807 // This tells PEI to spill the FP as if it is any other callee-save register
808 // to take advantage the eliminateFrameIndex machinery. This also ensures it
809 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
810 // to combine multiple loads / stores.
811 bool CanEliminateFrame = true;
812 bool CS1Spilled = false;
813 bool LRSpilled = false;
814 unsigned NumGPRSpills = 0;
815 SmallVector<unsigned, 4> UnspilledCS1GPRs;
816 SmallVector<unsigned, 4> UnspilledCS2GPRs;
818 // Don't spill FP if the frame can be eliminated. This is determined
819 // by scanning the callee-save registers to see if any is used.
820 const unsigned *CSRegs = getCalleeSavedRegs();
821 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
822 for (unsigned i = 0; CSRegs[i]; ++i) {
823 unsigned Reg = CSRegs[i];
824 bool Spilled = false;
825 if (MF.isPhysRegUsed(Reg)) {
827 CanEliminateFrame = false;
829 // Check alias registers too.
830 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
831 if (MF.isPhysRegUsed(*Aliases)) {
833 CanEliminateFrame = false;
838 if (CSRegClasses[i] == &ARM::GPRRegClass) {
842 if (!STI.isTargetDarwin()) {
850 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
865 if (!STI.isTargetDarwin()) {
866 UnspilledCS1GPRs.push_back(Reg);
876 UnspilledCS1GPRs.push_back(Reg);
879 UnspilledCS2GPRs.push_back(Reg);
886 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
887 bool ForceLRSpill = false;
888 if (!LRSpilled && AFI->isThumbFunction()) {
889 unsigned FnSize = ARM::GetFunctionSize(MF);
890 // Force LR spill if the Thumb function size is > 2048. This enables the
891 // use of BL to implement far jump. If it turns out that it's not needed
892 // the branch fix up path will undo it.
893 if (FnSize >= (1 << 11)) {
894 CanEliminateFrame = false;
899 if (!CanEliminateFrame || hasFP(MF)) {
900 AFI->setHasStackFrame(true);
902 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
903 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
904 if (!LRSpilled && CS1Spilled) {
905 MF.changePhyRegUsed(ARM::LR, true);
907 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
908 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
909 ForceLRSpill = false;
912 // Darwin ABI requires FP to point to the stack slot that contains the
914 if (STI.isTargetDarwin() || hasFP(MF)) {
915 MF.changePhyRegUsed(FramePtr, true);
919 // If stack and double are 8-byte aligned and we are spilling an odd number
920 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
921 // the integer and double callee save areas.
922 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
923 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
924 if (CS1Spilled && !UnspilledCS1GPRs.empty())
925 MF.changePhyRegUsed(UnspilledCS1GPRs.front(), true);
926 else if (!UnspilledCS2GPRs.empty())
927 MF.changePhyRegUsed(UnspilledCS2GPRs.front(), true);
932 MF.changePhyRegUsed(ARM::LR, true);
933 AFI->setLRIsForceSpilled(true);
937 /// Move iterator pass the next bunch of callee save load / store ops for
938 /// the particular spill area (1: integer area 1, 2: integer area 2,
939 /// 3: fp area, 0: don't care).
940 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
941 MachineBasicBlock::iterator &MBBI,
942 int Opc, unsigned Area,
943 const ARMSubtarget &STI) {
944 while (MBBI != MBB.end() &&
945 MBBI->getOpcode() == Opc && MBBI->getOperand(1).isFrameIndex()) {
948 unsigned Category = 0;
949 switch (MBBI->getOperand(0).getReg()) {
950 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
954 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
955 Category = STI.isTargetDarwin() ? 2 : 1;
957 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
958 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
965 if (Done || Category != Area)
973 void ARMRegisterInfo::emitPrologue(MachineFunction &MF) const {
974 MachineBasicBlock &MBB = MF.front();
975 MachineBasicBlock::iterator MBBI = MBB.begin();
976 MachineFrameInfo *MFI = MF.getFrameInfo();
977 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
978 bool isThumb = AFI->isThumbFunction();
979 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
980 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
981 unsigned NumBytes = MFI->getStackSize();
982 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
985 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
986 NumBytes = (NumBytes + 3) & ~3;
987 MFI->setStackSize(NumBytes);
990 // Determine the sizes of each callee-save spill areas and record which frame
991 // belongs to which callee-save spill areas.
992 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
993 int FramePtrSpillFI = 0;
994 if (!AFI->hasStackFrame()) {
996 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1001 emitSPUpdate(MBB, MBBI, -VARegSaveSize, isThumb, TII);
1003 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1004 unsigned Reg = CSI[i].getReg();
1005 int FI = CSI[i].getFrameIdx();
1012 if (Reg == FramePtr)
1013 FramePtrSpillFI = FI;
1014 AFI->addGPRCalleeSavedArea1Frame(FI);
1021 if (Reg == FramePtr)
1022 FramePtrSpillFI = FI;
1023 if (STI.isTargetDarwin()) {
1024 AFI->addGPRCalleeSavedArea2Frame(FI);
1027 AFI->addGPRCalleeSavedArea1Frame(FI);
1032 AFI->addDPRCalleeSavedAreaFrame(FI);
1037 if (Align == 8 && (GPRCS1Size & 7) != 0)
1038 // Pad CS1 to ensure proper alignment.
1042 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1043 emitSPUpdate(MBB, MBBI, -GPRCS1Size, isThumb, TII);
1044 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 1, STI);
1045 } else if (MBBI != MBB.end() && MBBI->getOpcode() == ARM::tPUSH)
1048 // Darwin ABI requires FP to point to the stack slot that contains the
1050 if (STI.isTargetDarwin() || hasFP(MF))
1051 BuildMI(MBB, MBBI, TII.get(isThumb ? ARM::tADDrSPi : ARM::ADDri), FramePtr)
1052 .addFrameIndex(FramePtrSpillFI).addImm(0);
1055 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1056 emitSPUpdate(MBB, MBBI, -GPRCS2Size, false, TII);
1058 // Build the new SUBri to adjust SP for FP callee-save spill area.
1059 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, 2, STI);
1060 emitSPUpdate(MBB, MBBI, -DPRCSSize, false, TII);
1063 // Determine starting offsets of spill areas.
1064 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1065 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1066 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1067 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1068 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1069 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1070 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1072 NumBytes = DPRCSOffset;
1074 // Insert it after all the callee-save spills.
1076 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 3, STI);
1077 emitSPUpdate(MBB, MBBI, -NumBytes, isThumb, TII);
1080 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1081 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1082 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1085 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1086 for (unsigned i = 0; CSRegs[i]; ++i)
1087 if (Reg == CSRegs[i])
1092 static bool isCSRestore(MachineInstr *MI, const unsigned *CSRegs) {
1093 return ((MI->getOpcode() == ARM::FLDD ||
1094 MI->getOpcode() == ARM::LDR ||
1095 MI->getOpcode() == ARM::tLDRspi) &&
1096 MI->getOperand(1).isFrameIndex() &&
1097 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1100 void ARMRegisterInfo::emitEpilogue(MachineFunction &MF,
1101 MachineBasicBlock &MBB) const {
1102 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1103 assert((MBBI->getOpcode() == ARM::BX_RET ||
1104 MBBI->getOpcode() == ARM::tBX_RET ||
1105 MBBI->getOpcode() == ARM::tPOP_RET) &&
1106 "Can only insert epilog into returning blocks");
1108 MachineFrameInfo *MFI = MF.getFrameInfo();
1109 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1110 bool isThumb = AFI->isThumbFunction();
1111 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1112 int NumBytes = (int)MFI->getStackSize();
1113 if (!AFI->hasStackFrame()) {
1115 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1119 // Unwind MBBI to point to first LDR / FLDD.
1120 const unsigned *CSRegs = getCalleeSavedRegs();
1121 if (MBBI != MBB.begin()) {
1124 while (MBBI != MBB.begin() && isCSRestore(MBBI, CSRegs));
1125 if (!isCSRestore(MBBI, CSRegs))
1129 // Move SP to start of FP callee save spill area.
1130 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1131 AFI->getGPRCalleeSavedArea2Size() +
1132 AFI->getDPRCalleeSavedAreaSize());
1134 if (MBBI->getOpcode() == ARM::tBX_RET &&
1135 &MBB.front() != MBBI &&
1136 prior(MBBI)->getOpcode() == ARM::tPOP) {
1137 MachineBasicBlock::iterator PMBBI = prior(MBBI);
1138 emitSPUpdate(MBB, PMBBI, NumBytes, isThumb, TII);
1140 emitSPUpdate(MBB, MBBI, NumBytes, isThumb, TII);
1142 // Darwin ABI requires FP to point to the stack slot that contains the
1144 if (STI.isTargetDarwin() || hasFP(MF)) {
1145 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1146 // Reset SP based on frame pointer only if the stack frame extends beyond
1147 // frame pointer stack slot or target is ELF and the function has FP.
1148 if (AFI->getGPRCalleeSavedArea2Size() ||
1149 AFI->getDPRCalleeSavedAreaSize() ||
1150 AFI->getDPRCalleeSavedAreaOffset()||
1153 BuildMI(MBB, MBBI, TII.get(ARM::SUBri), ARM::SP).addReg(FramePtr)
1156 BuildMI(MBB, MBBI, TII.get(ARM::MOVrr), ARM::SP).addReg(FramePtr);
1157 } else if (NumBytes) {
1158 emitSPUpdate(MBB, MBBI, NumBytes, false, TII);
1161 // Move SP to start of integer callee save spill area 2.
1162 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 3, STI);
1163 emitSPUpdate(MBB, MBBI, AFI->getDPRCalleeSavedAreaSize(), false, TII);
1165 // Move SP to start of integer callee save spill area 1.
1166 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 2, STI);
1167 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea2Size(), false, TII);
1169 // Move SP to SP upon entry to the function.
1170 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, 1, STI);
1171 emitSPUpdate(MBB, MBBI, AFI->getGPRCalleeSavedArea1Size(), false, TII);
1174 if (VARegSaveSize) {
1176 // Epilogue for vararg functions: pop LR to R3 and branch off it.
1177 // FIXME: Verify this is still ok when R3 is no longer being reserved.
1178 BuildMI(MBB, MBBI, TII.get(ARM::tPOP)).addReg(ARM::R3);
1180 emitSPUpdate(MBB, MBBI, VARegSaveSize, isThumb, TII);
1183 BuildMI(MBB, MBBI, TII.get(ARM::tBX_RET_vararg)).addReg(ARM::R3);
1189 unsigned ARMRegisterInfo::getRARegister() const {
1193 unsigned ARMRegisterInfo::getFrameRegister(MachineFunction &MF) const {
1194 return STI.useThumbBacktraces() ? ARM::R7 : ARM::R11;
1197 #include "ARMGenRegisterInfo.inc"