1 //===- ARMBaseRegisterInfo.cpp - ARM 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 base ARM implementation of TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMBaseRegisterInfo.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineLocation.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterScavenging.h"
32 #include "llvm/Support/ErrorHandling.h"
33 #include "llvm/Support/raw_ostream.h"
34 #include "llvm/Target/TargetFrameInfo.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Target/TargetOptions.h"
37 #include "llvm/ADT/BitVector.h"
38 #include "llvm/ADT/SmallVector.h"
41 unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum,
49 llvm_unreachable("Unknown ARM register!");
50 case R0: case D0: case Q0: return 0;
51 case R1: case D1: case Q1: return 1;
52 case R2: case D2: case Q2: return 2;
53 case R3: case D3: case Q3: return 3;
54 case R4: case D4: case Q4: return 4;
55 case R5: case D5: case Q5: return 5;
56 case R6: case D6: case Q6: return 6;
57 case R7: case D7: case Q7: return 7;
58 case R8: case D8: case Q8: return 8;
59 case R9: case D9: case Q9: return 9;
60 case R10: case D10: case Q10: return 10;
61 case R11: case D11: case Q11: return 11;
62 case R12: case D12: case Q12: return 12;
63 case SP: case D13: case Q13: return 13;
64 case LR: case D14: case Q14: return 14;
65 case PC: case D15: case Q15: return 15;
84 case S0: case S1: case S2: case S3:
85 case S4: case S5: case S6: case S7:
86 case S8: case S9: case S10: case S11:
87 case S12: case S13: case S14: case S15:
88 case S16: case S17: case S18: case S19:
89 case S20: case S21: case S22: case S23:
90 case S24: case S25: case S26: case S27:
91 case S28: case S29: case S30: case S31: {
95 default: return 0; // Avoid compile time warning.
133 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
134 const ARMSubtarget &sti)
135 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
137 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
141 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
142 static const unsigned CalleeSavedRegs[] = {
143 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
144 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
146 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
147 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
151 static const unsigned DarwinCalleeSavedRegs[] = {
152 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
154 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
155 ARM::R11, ARM::R10, ARM::R8,
157 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
158 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
161 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
164 const TargetRegisterClass* const *
165 ARMBaseRegisterInfo::getCalleeSavedRegClasses(const MachineFunction *MF) const {
166 static const TargetRegisterClass * const CalleeSavedRegClasses[] = {
167 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
168 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
169 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
171 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
172 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
176 static const TargetRegisterClass * const ThumbCalleeSavedRegClasses[] = {
177 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
178 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::tGPRRegClass,
179 &ARM::tGPRRegClass,&ARM::tGPRRegClass,&ARM::tGPRRegClass,
181 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
182 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
186 static const TargetRegisterClass * const DarwinCalleeSavedRegClasses[] = {
187 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
188 &ARM::GPRRegClass, &ARM::GPRRegClass, &ARM::GPRRegClass,
189 &ARM::GPRRegClass, &ARM::GPRRegClass,
191 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
192 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
196 static const TargetRegisterClass * const DarwinThumbCalleeSavedRegClasses[] ={
197 &ARM::GPRRegClass, &ARM::tGPRRegClass, &ARM::tGPRRegClass,
198 &ARM::tGPRRegClass, &ARM::tGPRRegClass, &ARM::GPRRegClass,
199 &ARM::GPRRegClass, &ARM::GPRRegClass,
201 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
202 &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass, &ARM::DPRRegClass,
206 if (STI.isThumb1Only()) {
207 return STI.isTargetDarwin()
208 ? DarwinThumbCalleeSavedRegClasses : ThumbCalleeSavedRegClasses;
210 return STI.isTargetDarwin()
211 ? DarwinCalleeSavedRegClasses : CalleeSavedRegClasses;
214 BitVector ARMBaseRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
215 // FIXME: avoid re-calculating this everytime.
216 BitVector Reserved(getNumRegs());
217 Reserved.set(ARM::SP);
218 Reserved.set(ARM::PC);
219 if (STI.isTargetDarwin() || hasFP(MF))
220 Reserved.set(FramePtr);
221 // Some targets reserve R9.
222 if (STI.isR9Reserved())
223 Reserved.set(ARM::R9);
227 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
228 unsigned Reg) const {
236 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
240 return STI.isR9Reserved();
246 const TargetRegisterClass *
247 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
248 return &ARM::GPRRegClass;
251 /// getAllocationOrder - Returns the register allocation order for a specified
252 /// register class in the form of a pair of TargetRegisterClass iterators.
253 std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
254 ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC,
255 unsigned HintType, unsigned HintReg,
256 const MachineFunction &MF) const {
257 // Alternative register allocation orders when favoring even / odd registers
258 // of register pairs.
260 // No FP, R9 is available.
261 static const unsigned GPREven1[] = {
262 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
263 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
266 static const unsigned GPROdd1[] = {
267 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
268 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
272 // FP is R7, R9 is available.
273 static const unsigned GPREven2[] = {
274 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
275 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
278 static const unsigned GPROdd2[] = {
279 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
280 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
284 // FP is R11, R9 is available.
285 static const unsigned GPREven3[] = {
286 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
287 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
290 static const unsigned GPROdd3[] = {
291 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
292 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
296 // No FP, R9 is not available.
297 static const unsigned GPREven4[] = {
298 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
299 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
302 static const unsigned GPROdd4[] = {
303 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
304 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
308 // FP is R7, R9 is not available.
309 static const unsigned GPREven5[] = {
310 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
311 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
314 static const unsigned GPROdd5[] = {
315 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
316 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
320 // FP is R11, R9 is not available.
321 static const unsigned GPREven6[] = {
322 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
323 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
325 static const unsigned GPROdd6[] = {
326 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
327 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
331 if (HintType == ARMRI::RegPairEven) {
332 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
333 // It's no longer possible to fulfill this hint. Return the default
335 return std::make_pair(RC->allocation_order_begin(MF),
336 RC->allocation_order_end(MF));
338 if (!STI.isTargetDarwin() && !hasFP(MF)) {
339 if (!STI.isR9Reserved())
340 return std::make_pair(GPREven1,
341 GPREven1 + (sizeof(GPREven1)/sizeof(unsigned)));
343 return std::make_pair(GPREven4,
344 GPREven4 + (sizeof(GPREven4)/sizeof(unsigned)));
345 } else if (FramePtr == ARM::R7) {
346 if (!STI.isR9Reserved())
347 return std::make_pair(GPREven2,
348 GPREven2 + (sizeof(GPREven2)/sizeof(unsigned)));
350 return std::make_pair(GPREven5,
351 GPREven5 + (sizeof(GPREven5)/sizeof(unsigned)));
352 } else { // FramePtr == ARM::R11
353 if (!STI.isR9Reserved())
354 return std::make_pair(GPREven3,
355 GPREven3 + (sizeof(GPREven3)/sizeof(unsigned)));
357 return std::make_pair(GPREven6,
358 GPREven6 + (sizeof(GPREven6)/sizeof(unsigned)));
360 } else if (HintType == ARMRI::RegPairOdd) {
361 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
362 // It's no longer possible to fulfill this hint. Return the default
364 return std::make_pair(RC->allocation_order_begin(MF),
365 RC->allocation_order_end(MF));
367 if (!STI.isTargetDarwin() && !hasFP(MF)) {
368 if (!STI.isR9Reserved())
369 return std::make_pair(GPROdd1,
370 GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned)));
372 return std::make_pair(GPROdd4,
373 GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned)));
374 } else if (FramePtr == ARM::R7) {
375 if (!STI.isR9Reserved())
376 return std::make_pair(GPROdd2,
377 GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned)));
379 return std::make_pair(GPROdd5,
380 GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned)));
381 } else { // FramePtr == ARM::R11
382 if (!STI.isR9Reserved())
383 return std::make_pair(GPROdd3,
384 GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned)));
386 return std::make_pair(GPROdd6,
387 GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned)));
390 return std::make_pair(RC->allocation_order_begin(MF),
391 RC->allocation_order_end(MF));
394 /// ResolveRegAllocHint - Resolves the specified register allocation hint
395 /// to a physical register. Returns the physical register if it is successful.
397 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
398 const MachineFunction &MF) const {
399 if (Reg == 0 || !isPhysicalRegister(Reg))
403 else if (Type == (unsigned)ARMRI::RegPairOdd)
405 return getRegisterPairOdd(Reg, MF);
406 else if (Type == (unsigned)ARMRI::RegPairEven)
408 return getRegisterPairEven(Reg, MF);
413 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
414 MachineFunction &MF) const {
415 MachineRegisterInfo *MRI = &MF.getRegInfo();
416 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
417 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
418 Hint.first == (unsigned)ARMRI::RegPairEven) &&
419 Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) {
420 // If 'Reg' is one of the even / odd register pair and it's now changed
421 // (e.g. coalesced) into a different register. The other register of the
422 // pair allocation hint must be updated to reflect the relationship
424 unsigned OtherReg = Hint.second;
425 Hint = MRI->getRegAllocationHint(OtherReg);
426 if (Hint.second == Reg)
427 // Make sure the pair has not already divorced.
428 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
432 /// hasFP - Return true if the specified function should have a dedicated frame
433 /// pointer register. This is true if the function has variable sized allocas
434 /// or if frame pointer elimination is disabled.
436 bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
437 const MachineFrameInfo *MFI = MF.getFrameInfo();
438 return (NoFramePointerElim ||
439 MFI->hasVarSizedObjects() ||
440 MFI->isFrameAddressTaken());
443 /// estimateStackSize - Estimate and return the size of the frame.
444 static unsigned estimateStackSize(MachineFunction &MF, MachineFrameInfo *MFI) {
445 const MachineFrameInfo *FFI = MF.getFrameInfo();
447 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
448 int FixedOff = -FFI->getObjectOffset(i);
449 if (FixedOff > Offset) Offset = FixedOff;
451 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
452 if (FFI->isDeadObjectIndex(i))
454 Offset += FFI->getObjectSize(i);
455 unsigned Align = FFI->getObjectAlignment(i);
456 // Adjust to alignment boundary
457 Offset = (Offset+Align-1)/Align*Align;
459 return (unsigned)Offset;
462 /// estimateRSStackSizeLimit - Look at each instruction that references stack
463 /// frames and return the stack size limit beyond which some of these
464 /// instructions will require scratch register during their expansion later.
465 static unsigned estimateRSStackSizeLimit(MachineFunction &MF,
466 const ARMBaseInstrInfo &TII) {
467 unsigned Limit = (1 << 12) - 1;
468 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
469 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
471 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
472 if (!I->getOperand(i).isFI()) continue;
474 const TargetInstrDesc &Desc = TII.get(I->getOpcode());
475 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
476 if (AddrMode == ARMII::AddrMode3 ||
477 AddrMode == ARMII::AddrModeT2_i8)
480 if (AddrMode == ARMII::AddrMode5 ||
481 AddrMode == ARMII::AddrModeT2_i8s4)
482 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
483 break; // At most one FI per instruction
492 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
493 RegScavenger *RS) const {
494 // This tells PEI to spill the FP as if it is any other callee-save register
495 // to take advantage the eliminateFrameIndex machinery. This also ensures it
496 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
497 // to combine multiple loads / stores.
498 bool CanEliminateFrame = true;
499 bool CS1Spilled = false;
500 bool LRSpilled = false;
501 unsigned NumGPRSpills = 0;
502 SmallVector<unsigned, 4> UnspilledCS1GPRs;
503 SmallVector<unsigned, 4> UnspilledCS2GPRs;
504 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
506 // Don't spill FP if the frame can be eliminated. This is determined
507 // by scanning the callee-save registers to see if any is used.
508 const unsigned *CSRegs = getCalleeSavedRegs();
509 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
510 for (unsigned i = 0; CSRegs[i]; ++i) {
511 unsigned Reg = CSRegs[i];
512 bool Spilled = false;
513 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
514 AFI->setCSRegisterIsSpilled(Reg);
516 CanEliminateFrame = false;
518 // Check alias registers too.
519 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
520 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
522 CanEliminateFrame = false;
527 if (CSRegClasses[i] == &ARM::GPRRegClass) {
531 if (!STI.isTargetDarwin()) {
538 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
553 if (!STI.isTargetDarwin()) {
554 UnspilledCS1GPRs.push_back(Reg);
564 UnspilledCS1GPRs.push_back(Reg);
567 UnspilledCS2GPRs.push_back(Reg);
574 bool ForceLRSpill = false;
575 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
576 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
577 // Force LR to be spilled if the Thumb function size is > 2048. This enables
578 // use of BL to implement far jump. If it turns out that it's not needed
579 // then the branch fix up path will undo it.
580 if (FnSize >= (1 << 11)) {
581 CanEliminateFrame = false;
586 bool ExtraCSSpill = false;
587 if (!CanEliminateFrame || hasFP(MF)) {
588 AFI->setHasStackFrame(true);
590 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
591 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
592 if (!LRSpilled && CS1Spilled) {
593 MF.getRegInfo().setPhysRegUsed(ARM::LR);
594 AFI->setCSRegisterIsSpilled(ARM::LR);
596 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
597 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
598 ForceLRSpill = false;
602 // Darwin ABI requires FP to point to the stack slot that contains the
604 if (STI.isTargetDarwin() || hasFP(MF)) {
605 MF.getRegInfo().setPhysRegUsed(FramePtr);
609 // If stack and double are 8-byte aligned and we are spilling an odd number
610 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
611 // the integer and double callee save areas.
612 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
613 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
614 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
615 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
616 unsigned Reg = UnspilledCS1GPRs[i];
617 // Don't spill high register if the function is thumb1
618 if (!AFI->isThumb1OnlyFunction() ||
619 isARMLowRegister(Reg) || Reg == ARM::LR) {
620 MF.getRegInfo().setPhysRegUsed(Reg);
621 AFI->setCSRegisterIsSpilled(Reg);
622 if (!isReservedReg(MF, Reg))
627 } else if (!UnspilledCS2GPRs.empty() &&
628 !AFI->isThumb1OnlyFunction()) {
629 unsigned Reg = UnspilledCS2GPRs.front();
630 MF.getRegInfo().setPhysRegUsed(Reg);
631 AFI->setCSRegisterIsSpilled(Reg);
632 if (!isReservedReg(MF, Reg))
637 // Estimate if we might need to scavenge a register at some point in order
638 // to materialize a stack offset. If so, either spill one additional
639 // callee-saved register or reserve a special spill slot to facilitate
640 // register scavenging.
641 if (RS && !ExtraCSSpill && !AFI->isThumb1OnlyFunction()) {
642 MachineFrameInfo *MFI = MF.getFrameInfo();
643 if (estimateStackSize(MF, MFI) >= estimateRSStackSizeLimit(MF, TII)) {
644 // If any non-reserved CS register isn't spilled, just spill one or two
645 // extra. That should take care of it!
646 unsigned NumExtras = TargetAlign / 4;
647 SmallVector<unsigned, 2> Extras;
648 while (NumExtras && !UnspilledCS1GPRs.empty()) {
649 unsigned Reg = UnspilledCS1GPRs.back();
650 UnspilledCS1GPRs.pop_back();
651 if (!isReservedReg(MF, Reg)) {
652 Extras.push_back(Reg);
656 while (NumExtras && !UnspilledCS2GPRs.empty()) {
657 unsigned Reg = UnspilledCS2GPRs.back();
658 UnspilledCS2GPRs.pop_back();
659 if (!isReservedReg(MF, Reg)) {
660 Extras.push_back(Reg);
664 if (Extras.size() && NumExtras == 0) {
665 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
666 MF.getRegInfo().setPhysRegUsed(Extras[i]);
667 AFI->setCSRegisterIsSpilled(Extras[i]);
670 // Reserve a slot closest to SP or frame pointer.
671 const TargetRegisterClass *RC = &ARM::GPRRegClass;
672 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
673 RC->getAlignment()));
680 MF.getRegInfo().setPhysRegUsed(ARM::LR);
681 AFI->setCSRegisterIsSpilled(ARM::LR);
682 AFI->setLRIsSpilledForFarJump(true);
686 unsigned ARMBaseRegisterInfo::getRARegister() const {
690 unsigned ARMBaseRegisterInfo::getFrameRegister(MachineFunction &MF) const {
691 if (STI.isTargetDarwin() || hasFP(MF))
696 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
697 llvm_unreachable("What is the exception register");
701 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
702 llvm_unreachable("What is the exception handler register");
706 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
707 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
710 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
711 const MachineFunction &MF) const {
714 // Return 0 if either register of the pair is a special register.
720 return STI.isThumb1Only() ? 0 : ARM::R2;
724 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
726 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
728 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
800 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
801 const MachineFunction &MF) const {
804 // Return 0 if either register of the pair is a special register.
810 return STI.isThumb1Only() ? 0 : ARM::R3;
814 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
816 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
818 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
890 /// emitLoadConstPool - Emits a load from constpool to materialize the
891 /// specified immediate.
892 void ARMBaseRegisterInfo::
893 emitLoadConstPool(MachineBasicBlock &MBB,
894 MachineBasicBlock::iterator &MBBI,
896 unsigned DestReg, unsigned SubIdx, int Val,
897 ARMCC::CondCodes Pred,
898 unsigned PredReg) const {
899 MachineFunction &MF = *MBB.getParent();
900 MachineConstantPool *ConstantPool = MF.getConstantPool();
901 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
902 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
904 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
905 .addReg(DestReg, getDefRegState(true), SubIdx)
906 .addConstantPoolIndex(Idx)
907 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
910 bool ARMBaseRegisterInfo::
911 requiresRegisterScavenging(const MachineFunction &MF) const {
915 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
916 // not required, we reserve argument space for call sites in the function
917 // immediately on entry to the current function. This eliminates the need for
918 // add/sub sp brackets around call sites. Returns true if the call frame is
919 // included as part of the stack frame.
920 bool ARMBaseRegisterInfo::
921 hasReservedCallFrame(MachineFunction &MF) const {
922 const MachineFrameInfo *FFI = MF.getFrameInfo();
923 unsigned CFSize = FFI->getMaxCallFrameSize();
924 // It's not always a good idea to include the call frame as part of the
925 // stack frame. ARM (especially Thumb) has small immediate offset to
926 // address the stack frame. So a large call frame can cause poor codegen
927 // and may even makes it impossible to scavenge a register.
928 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
931 return !MF.getFrameInfo()->hasVarSizedObjects();
935 emitSPUpdate(bool isARM,
936 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
937 DebugLoc dl, const ARMBaseInstrInfo &TII,
939 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
941 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
944 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
949 void ARMBaseRegisterInfo::
950 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
951 MachineBasicBlock::iterator I) const {
952 if (!hasReservedCallFrame(MF)) {
953 // If we have alloca, convert as follows:
954 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
955 // ADJCALLSTACKUP -> add, sp, sp, amount
956 MachineInstr *Old = I;
957 DebugLoc dl = Old->getDebugLoc();
958 unsigned Amount = Old->getOperand(0).getImm();
960 // We need to keep the stack aligned properly. To do this, we round the
961 // amount of space needed for the outgoing arguments up to the next
962 // alignment boundary.
963 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
964 Amount = (Amount+Align-1)/Align*Align;
966 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
967 assert(!AFI->isThumb1OnlyFunction() &&
968 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
969 bool isARM = !AFI->isThumbFunction();
971 // Replace the pseudo instruction with a new instruction...
972 unsigned Opc = Old->getOpcode();
973 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
974 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
975 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
976 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
977 unsigned PredReg = Old->getOperand(2).getReg();
978 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
980 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
981 unsigned PredReg = Old->getOperand(3).getReg();
982 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
983 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
990 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
991 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
992 /// register first and then a spilled callee-saved register if that fails.
994 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
995 ARMFunctionInfo *AFI) {
996 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
997 assert(!AFI->isThumb1OnlyFunction());
999 // Try a already spilled CS register.
1000 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
1006 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1007 int SPAdj, RegScavenger *RS) const {
1009 MachineInstr &MI = *II;
1010 MachineBasicBlock &MBB = *MI.getParent();
1011 MachineFunction &MF = *MBB.getParent();
1012 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1013 assert(!AFI->isThumb1OnlyFunction() &&
1014 "This eliminateFrameIndex does not suppor Thumb1!");
1016 while (!MI.getOperand(i).isFI()) {
1018 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1021 unsigned FrameReg = ARM::SP;
1022 int FrameIndex = MI.getOperand(i).getIndex();
1023 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
1024 MF.getFrameInfo()->getStackSize() + SPAdj;
1026 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
1027 Offset -= AFI->getGPRCalleeSavedArea1Offset();
1028 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
1029 Offset -= AFI->getGPRCalleeSavedArea2Offset();
1030 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
1031 Offset -= AFI->getDPRCalleeSavedAreaOffset();
1032 else if (hasFP(MF)) {
1033 assert(SPAdj == 0 && "Unexpected");
1034 // There is alloca()'s in this function, must reference off the frame
1036 FrameReg = getFrameRegister(MF);
1037 Offset -= AFI->getFramePtrSpillOffset();
1040 // modify MI as necessary to handle as much of 'Offset' as possible
1041 if (!AFI->isThumbFunction())
1042 Offset = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1044 assert(AFI->isThumb2Function());
1045 Offset = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1050 // If we get here, the immediate doesn't fit into the instruction. We folded
1051 // as much as possible above, handle the rest, providing a register that is
1053 assert(Offset && "This code isn't needed if offset already handled!");
1055 // Insert a set of r12 with the full address: r12 = sp + offset
1056 // If the offset we have is too large to fit into the instruction, we need
1057 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
1059 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
1060 if (ScratchReg == 0)
1061 // No register is "free". Scavenge a register.
1062 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II, SPAdj);
1063 int PIdx = MI.findFirstPredOperandIdx();
1064 ARMCC::CondCodes Pred = (PIdx == -1)
1065 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1066 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1067 if (!AFI->isThumbFunction())
1068 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1069 Offset, Pred, PredReg, TII);
1071 assert(AFI->isThumb2Function());
1072 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1073 Offset, Pred, PredReg, TII);
1075 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1078 /// Move iterator pass the next bunch of callee save load / store ops for
1079 /// the particular spill area (1: integer area 1, 2: integer area 2,
1080 /// 3: fp area, 0: don't care).
1081 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1082 MachineBasicBlock::iterator &MBBI,
1083 int Opc1, int Opc2, unsigned Area,
1084 const ARMSubtarget &STI) {
1085 while (MBBI != MBB.end() &&
1086 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1087 MBBI->getOperand(1).isFI()) {
1090 unsigned Category = 0;
1091 switch (MBBI->getOperand(0).getReg()) {
1092 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1096 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1097 Category = STI.isTargetDarwin() ? 2 : 1;
1099 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1100 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1107 if (Done || Category != Area)
1115 void ARMBaseRegisterInfo::
1116 emitPrologue(MachineFunction &MF) const {
1117 MachineBasicBlock &MBB = MF.front();
1118 MachineBasicBlock::iterator MBBI = MBB.begin();
1119 MachineFrameInfo *MFI = MF.getFrameInfo();
1120 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1121 assert(!AFI->isThumb1OnlyFunction() &&
1122 "This emitPrologue does not suppor Thumb1!");
1123 bool isARM = !AFI->isThumbFunction();
1124 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1125 unsigned NumBytes = MFI->getStackSize();
1126 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1127 DebugLoc dl = (MBBI != MBB.end() ?
1128 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1130 // Determine the sizes of each callee-save spill areas and record which frame
1131 // belongs to which callee-save spill areas.
1132 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1133 int FramePtrSpillFI = 0;
1136 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1138 if (!AFI->hasStackFrame()) {
1140 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1144 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1145 unsigned Reg = CSI[i].getReg();
1146 int FI = CSI[i].getFrameIdx();
1153 if (Reg == FramePtr)
1154 FramePtrSpillFI = FI;
1155 AFI->addGPRCalleeSavedArea1Frame(FI);
1162 if (Reg == FramePtr)
1163 FramePtrSpillFI = FI;
1164 if (STI.isTargetDarwin()) {
1165 AFI->addGPRCalleeSavedArea2Frame(FI);
1168 AFI->addGPRCalleeSavedArea1Frame(FI);
1173 AFI->addDPRCalleeSavedAreaFrame(FI);
1178 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1179 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1180 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1182 // Darwin ABI requires FP to point to the stack slot that contains the
1184 if (STI.isTargetDarwin() || hasFP(MF)) {
1185 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1186 MachineInstrBuilder MIB =
1187 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1188 .addFrameIndex(FramePtrSpillFI).addImm(0);
1189 AddDefaultCC(AddDefaultPred(MIB));
1192 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1193 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1195 // Build the new SUBri to adjust SP for FP callee-save spill area.
1196 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1197 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1199 // Determine starting offsets of spill areas.
1200 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1201 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1202 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1203 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1204 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1205 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1206 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1208 NumBytes = DPRCSOffset;
1210 // Insert it after all the callee-save spills.
1211 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 0, 3, STI);
1212 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1215 if (STI.isTargetELF() && hasFP(MF)) {
1216 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1217 AFI->getFramePtrSpillOffset());
1220 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1221 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1222 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1225 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1226 for (unsigned i = 0; CSRegs[i]; ++i)
1227 if (Reg == CSRegs[i])
1232 static bool isCSRestore(MachineInstr *MI,
1233 const ARMBaseInstrInfo &TII,
1234 const unsigned *CSRegs) {
1235 return ((MI->getOpcode() == (int)ARM::FLDD ||
1236 MI->getOpcode() == (int)ARM::LDR ||
1237 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1238 MI->getOperand(1).isFI() &&
1239 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1242 void ARMBaseRegisterInfo::
1243 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1244 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1245 assert(MBBI->getDesc().isReturn() &&
1246 "Can only insert epilog into returning blocks");
1247 DebugLoc dl = MBBI->getDebugLoc();
1248 MachineFrameInfo *MFI = MF.getFrameInfo();
1249 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1250 assert(!AFI->isThumb1OnlyFunction() &&
1251 "This emitEpilogue does not suppor Thumb1!");
1252 bool isARM = !AFI->isThumbFunction();
1254 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1255 int NumBytes = (int)MFI->getStackSize();
1257 if (!AFI->hasStackFrame()) {
1259 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1261 // Unwind MBBI to point to first LDR / FLDD.
1262 const unsigned *CSRegs = getCalleeSavedRegs();
1263 if (MBBI != MBB.begin()) {
1266 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1267 if (!isCSRestore(MBBI, TII, CSRegs))
1271 // Move SP to start of FP callee save spill area.
1272 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1273 AFI->getGPRCalleeSavedArea2Size() +
1274 AFI->getDPRCalleeSavedAreaSize());
1276 // Darwin ABI requires FP to point to the stack slot that contains the
1278 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1279 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1280 // Reset SP based on frame pointer only if the stack frame extends beyond
1281 // frame pointer stack slot or target is ELF and the function has FP.
1282 if (AFI->getGPRCalleeSavedArea2Size() ||
1283 AFI->getDPRCalleeSavedAreaSize() ||
1284 AFI->getDPRCalleeSavedAreaOffset()||
1287 unsigned SUBriOpc = isARM ? ARM::SUBri : ARM::t2SUBri;
1288 BuildMI(MBB, MBBI, dl, TII.get(SUBriOpc), ARM::SP)
1291 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1294 unsigned MOVrOpc = isARM ? ARM::MOVr : ARM::t2MOVr;
1295 BuildMI(MBB, MBBI, dl, TII.get(MOVrOpc), ARM::SP)
1297 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1300 } else if (NumBytes)
1301 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1303 // Move SP to start of integer callee save spill area 2.
1304 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 0, 3, STI);
1305 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1307 // Move SP to start of integer callee save spill area 1.
1308 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1309 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1311 // Move SP to SP upon entry to the function.
1312 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1313 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1317 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1320 #include "ARMGenRegisterInfo.inc"