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.
466 ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
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);
484 if (AddrMode == ARMII::AddrModeT2_i12 && hasFP(MF))
485 // When the stack offset is negative, we will end up using
486 // the i8 instructions instead.
488 break; // At most one FI per instruction
497 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
498 RegScavenger *RS) const {
499 // This tells PEI to spill the FP as if it is any other callee-save register
500 // to take advantage the eliminateFrameIndex machinery. This also ensures it
501 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
502 // to combine multiple loads / stores.
503 bool CanEliminateFrame = true;
504 bool CS1Spilled = false;
505 bool LRSpilled = false;
506 unsigned NumGPRSpills = 0;
507 SmallVector<unsigned, 4> UnspilledCS1GPRs;
508 SmallVector<unsigned, 4> UnspilledCS2GPRs;
509 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
511 // Don't spill FP if the frame can be eliminated. This is determined
512 // by scanning the callee-save registers to see if any is used.
513 const unsigned *CSRegs = getCalleeSavedRegs();
514 const TargetRegisterClass* const *CSRegClasses = getCalleeSavedRegClasses();
515 for (unsigned i = 0; CSRegs[i]; ++i) {
516 unsigned Reg = CSRegs[i];
517 bool Spilled = false;
518 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
519 AFI->setCSRegisterIsSpilled(Reg);
521 CanEliminateFrame = false;
523 // Check alias registers too.
524 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
525 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
527 CanEliminateFrame = false;
532 if (CSRegClasses[i] == &ARM::GPRRegClass) {
536 if (!STI.isTargetDarwin()) {
543 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
558 if (!STI.isTargetDarwin()) {
559 UnspilledCS1GPRs.push_back(Reg);
569 UnspilledCS1GPRs.push_back(Reg);
572 UnspilledCS2GPRs.push_back(Reg);
579 bool ForceLRSpill = false;
580 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
581 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
582 // Force LR to be spilled if the Thumb function size is > 2048. This enables
583 // use of BL to implement far jump. If it turns out that it's not needed
584 // then the branch fix up path will undo it.
585 if (FnSize >= (1 << 11)) {
586 CanEliminateFrame = false;
591 bool ExtraCSSpill = false;
592 if (!CanEliminateFrame || hasFP(MF)) {
593 AFI->setHasStackFrame(true);
595 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
596 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
597 if (!LRSpilled && CS1Spilled) {
598 MF.getRegInfo().setPhysRegUsed(ARM::LR);
599 AFI->setCSRegisterIsSpilled(ARM::LR);
601 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
602 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
603 ForceLRSpill = false;
607 // Darwin ABI requires FP to point to the stack slot that contains the
609 if (STI.isTargetDarwin() || hasFP(MF)) {
610 MF.getRegInfo().setPhysRegUsed(FramePtr);
614 // If stack and double are 8-byte aligned and we are spilling an odd number
615 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
616 // the integer and double callee save areas.
617 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
618 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
619 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
620 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
621 unsigned Reg = UnspilledCS1GPRs[i];
622 // Don't spill high register if the function is thumb1
623 if (!AFI->isThumb1OnlyFunction() ||
624 isARMLowRegister(Reg) || Reg == ARM::LR) {
625 MF.getRegInfo().setPhysRegUsed(Reg);
626 AFI->setCSRegisterIsSpilled(Reg);
627 if (!isReservedReg(MF, Reg))
632 } else if (!UnspilledCS2GPRs.empty() &&
633 !AFI->isThumb1OnlyFunction()) {
634 unsigned Reg = UnspilledCS2GPRs.front();
635 MF.getRegInfo().setPhysRegUsed(Reg);
636 AFI->setCSRegisterIsSpilled(Reg);
637 if (!isReservedReg(MF, Reg))
642 // Estimate if we might need to scavenge a register at some point in order
643 // to materialize a stack offset. If so, either spill one additional
644 // callee-saved register or reserve a special spill slot to facilitate
645 // register scavenging.
646 if (RS && !ExtraCSSpill && !AFI->isThumb1OnlyFunction()) {
647 MachineFrameInfo *MFI = MF.getFrameInfo();
648 if (estimateStackSize(MF, MFI) >= estimateRSStackSizeLimit(MF)) {
649 // If any non-reserved CS register isn't spilled, just spill one or two
650 // extra. That should take care of it!
651 unsigned NumExtras = TargetAlign / 4;
652 SmallVector<unsigned, 2> Extras;
653 while (NumExtras && !UnspilledCS1GPRs.empty()) {
654 unsigned Reg = UnspilledCS1GPRs.back();
655 UnspilledCS1GPRs.pop_back();
656 if (!isReservedReg(MF, Reg)) {
657 Extras.push_back(Reg);
661 while (NumExtras && !UnspilledCS2GPRs.empty()) {
662 unsigned Reg = UnspilledCS2GPRs.back();
663 UnspilledCS2GPRs.pop_back();
664 if (!isReservedReg(MF, Reg)) {
665 Extras.push_back(Reg);
669 if (Extras.size() && NumExtras == 0) {
670 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
671 MF.getRegInfo().setPhysRegUsed(Extras[i]);
672 AFI->setCSRegisterIsSpilled(Extras[i]);
675 // Reserve a slot closest to SP or frame pointer.
676 const TargetRegisterClass *RC = &ARM::GPRRegClass;
677 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
678 RC->getAlignment()));
685 MF.getRegInfo().setPhysRegUsed(ARM::LR);
686 AFI->setCSRegisterIsSpilled(ARM::LR);
687 AFI->setLRIsSpilledForFarJump(true);
691 unsigned ARMBaseRegisterInfo::getRARegister() const {
695 unsigned ARMBaseRegisterInfo::getFrameRegister(MachineFunction &MF) const {
696 if (STI.isTargetDarwin() || hasFP(MF))
701 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
702 llvm_unreachable("What is the exception register");
706 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
707 llvm_unreachable("What is the exception handler register");
711 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
712 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
715 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
716 const MachineFunction &MF) const {
719 // Return 0 if either register of the pair is a special register.
725 return STI.isThumb1Only() ? 0 : ARM::R2;
729 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
731 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
733 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
805 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
806 const MachineFunction &MF) const {
809 // Return 0 if either register of the pair is a special register.
815 return STI.isThumb1Only() ? 0 : ARM::R3;
819 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
821 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
823 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
895 /// emitLoadConstPool - Emits a load from constpool to materialize the
896 /// specified immediate.
897 void ARMBaseRegisterInfo::
898 emitLoadConstPool(MachineBasicBlock &MBB,
899 MachineBasicBlock::iterator &MBBI,
901 unsigned DestReg, unsigned SubIdx, int Val,
902 ARMCC::CondCodes Pred,
903 unsigned PredReg) const {
904 MachineFunction &MF = *MBB.getParent();
905 MachineConstantPool *ConstantPool = MF.getConstantPool();
907 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
908 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
910 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
911 .addReg(DestReg, getDefRegState(true), SubIdx)
912 .addConstantPoolIndex(Idx)
913 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
916 bool ARMBaseRegisterInfo::
917 requiresRegisterScavenging(const MachineFunction &MF) const {
921 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
922 // not required, we reserve argument space for call sites in the function
923 // immediately on entry to the current function. This eliminates the need for
924 // add/sub sp brackets around call sites. Returns true if the call frame is
925 // included as part of the stack frame.
926 bool ARMBaseRegisterInfo::
927 hasReservedCallFrame(MachineFunction &MF) const {
928 const MachineFrameInfo *FFI = MF.getFrameInfo();
929 unsigned CFSize = FFI->getMaxCallFrameSize();
930 // It's not always a good idea to include the call frame as part of the
931 // stack frame. ARM (especially Thumb) has small immediate offset to
932 // address the stack frame. So a large call frame can cause poor codegen
933 // and may even makes it impossible to scavenge a register.
934 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
937 return !MF.getFrameInfo()->hasVarSizedObjects();
941 emitSPUpdate(bool isARM,
942 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
943 DebugLoc dl, const ARMBaseInstrInfo &TII,
945 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
947 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
950 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
955 void ARMBaseRegisterInfo::
956 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
957 MachineBasicBlock::iterator I) const {
958 if (!hasReservedCallFrame(MF)) {
959 // If we have alloca, convert as follows:
960 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
961 // ADJCALLSTACKUP -> add, sp, sp, amount
962 MachineInstr *Old = I;
963 DebugLoc dl = Old->getDebugLoc();
964 unsigned Amount = Old->getOperand(0).getImm();
966 // We need to keep the stack aligned properly. To do this, we round the
967 // amount of space needed for the outgoing arguments up to the next
968 // alignment boundary.
969 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
970 Amount = (Amount+Align-1)/Align*Align;
972 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
973 assert(!AFI->isThumb1OnlyFunction() &&
974 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
975 bool isARM = !AFI->isThumbFunction();
977 // Replace the pseudo instruction with a new instruction...
978 unsigned Opc = Old->getOpcode();
979 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
980 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
981 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
982 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
983 unsigned PredReg = Old->getOperand(2).getReg();
984 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
986 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
987 unsigned PredReg = Old->getOperand(3).getReg();
988 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
989 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
996 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
997 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
998 /// register first and then a spilled callee-saved register if that fails.
1000 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
1001 ARMFunctionInfo *AFI) {
1002 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
1003 assert(!AFI->isThumb1OnlyFunction());
1005 // Try a already spilled CS register.
1006 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
1012 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1013 int SPAdj, RegScavenger *RS) const {
1015 MachineInstr &MI = *II;
1016 MachineBasicBlock &MBB = *MI.getParent();
1017 MachineFunction &MF = *MBB.getParent();
1018 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1019 assert(!AFI->isThumb1OnlyFunction() &&
1020 "This eliminateFrameIndex does not suppor Thumb1!");
1022 while (!MI.getOperand(i).isFI()) {
1024 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1027 unsigned FrameReg = ARM::SP;
1028 int FrameIndex = MI.getOperand(i).getIndex();
1029 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
1030 MF.getFrameInfo()->getStackSize() + SPAdj;
1032 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
1033 Offset -= AFI->getGPRCalleeSavedArea1Offset();
1034 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
1035 Offset -= AFI->getGPRCalleeSavedArea2Offset();
1036 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
1037 Offset -= AFI->getDPRCalleeSavedAreaOffset();
1038 else if (hasFP(MF)) {
1039 assert(SPAdj == 0 && "Unexpected");
1040 // There is alloca()'s in this function, must reference off the frame
1042 FrameReg = getFrameRegister(MF);
1043 Offset -= AFI->getFramePtrSpillOffset();
1046 // modify MI as necessary to handle as much of 'Offset' as possible
1047 if (!AFI->isThumbFunction())
1048 Offset = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1050 assert(AFI->isThumb2Function());
1051 Offset = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1056 // If we get here, the immediate doesn't fit into the instruction. We folded
1057 // as much as possible above, handle the rest, providing a register that is
1059 assert(Offset && "This code isn't needed if offset already handled!");
1061 // Insert a set of r12 with the full address: r12 = sp + offset
1062 // If the offset we have is too large to fit into the instruction, we need
1063 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
1065 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
1066 if (ScratchReg == 0)
1067 // No register is "free". Scavenge a register.
1068 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II, SPAdj);
1069 int PIdx = MI.findFirstPredOperandIdx();
1070 ARMCC::CondCodes Pred = (PIdx == -1)
1071 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1072 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1073 if (!AFI->isThumbFunction())
1074 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1075 Offset, Pred, PredReg, TII);
1077 assert(AFI->isThumb2Function());
1078 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1079 Offset, Pred, PredReg, TII);
1081 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1084 /// Move iterator pass the next bunch of callee save load / store ops for
1085 /// the particular spill area (1: integer area 1, 2: integer area 2,
1086 /// 3: fp area, 0: don't care).
1087 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1088 MachineBasicBlock::iterator &MBBI,
1089 int Opc1, int Opc2, unsigned Area,
1090 const ARMSubtarget &STI) {
1091 while (MBBI != MBB.end() &&
1092 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1093 MBBI->getOperand(1).isFI()) {
1096 unsigned Category = 0;
1097 switch (MBBI->getOperand(0).getReg()) {
1098 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1102 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1103 Category = STI.isTargetDarwin() ? 2 : 1;
1105 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1106 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1113 if (Done || Category != Area)
1121 void ARMBaseRegisterInfo::
1122 emitPrologue(MachineFunction &MF) const {
1123 MachineBasicBlock &MBB = MF.front();
1124 MachineBasicBlock::iterator MBBI = MBB.begin();
1125 MachineFrameInfo *MFI = MF.getFrameInfo();
1126 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1127 assert(!AFI->isThumb1OnlyFunction() &&
1128 "This emitPrologue does not suppor Thumb1!");
1129 bool isARM = !AFI->isThumbFunction();
1130 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1131 unsigned NumBytes = MFI->getStackSize();
1132 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1133 DebugLoc dl = (MBBI != MBB.end() ?
1134 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1136 // Determine the sizes of each callee-save spill areas and record which frame
1137 // belongs to which callee-save spill areas.
1138 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1139 int FramePtrSpillFI = 0;
1142 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1144 if (!AFI->hasStackFrame()) {
1146 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1150 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1151 unsigned Reg = CSI[i].getReg();
1152 int FI = CSI[i].getFrameIdx();
1159 if (Reg == FramePtr)
1160 FramePtrSpillFI = FI;
1161 AFI->addGPRCalleeSavedArea1Frame(FI);
1168 if (Reg == FramePtr)
1169 FramePtrSpillFI = FI;
1170 if (STI.isTargetDarwin()) {
1171 AFI->addGPRCalleeSavedArea2Frame(FI);
1174 AFI->addGPRCalleeSavedArea1Frame(FI);
1179 AFI->addDPRCalleeSavedAreaFrame(FI);
1184 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1185 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1186 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1188 // Darwin ABI requires FP to point to the stack slot that contains the
1190 if (STI.isTargetDarwin() || hasFP(MF)) {
1191 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1192 MachineInstrBuilder MIB =
1193 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1194 .addFrameIndex(FramePtrSpillFI).addImm(0);
1195 AddDefaultCC(AddDefaultPred(MIB));
1198 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1199 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1201 // Build the new SUBri to adjust SP for FP callee-save spill area.
1202 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1203 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1205 // Determine starting offsets of spill areas.
1206 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1207 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1208 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1209 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1210 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1211 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1212 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1214 NumBytes = DPRCSOffset;
1216 // Insert it after all the callee-save spills.
1217 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 0, 3, STI);
1218 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1221 if (STI.isTargetELF() && hasFP(MF)) {
1222 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1223 AFI->getFramePtrSpillOffset());
1226 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1227 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1228 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1231 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1232 for (unsigned i = 0; CSRegs[i]; ++i)
1233 if (Reg == CSRegs[i])
1238 static bool isCSRestore(MachineInstr *MI,
1239 const ARMBaseInstrInfo &TII,
1240 const unsigned *CSRegs) {
1241 return ((MI->getOpcode() == (int)ARM::FLDD ||
1242 MI->getOpcode() == (int)ARM::LDR ||
1243 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1244 MI->getOperand(1).isFI() &&
1245 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1248 void ARMBaseRegisterInfo::
1249 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1250 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1251 assert(MBBI->getDesc().isReturn() &&
1252 "Can only insert epilog into returning blocks");
1253 DebugLoc dl = MBBI->getDebugLoc();
1254 MachineFrameInfo *MFI = MF.getFrameInfo();
1255 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1256 assert(!AFI->isThumb1OnlyFunction() &&
1257 "This emitEpilogue does not suppor Thumb1!");
1258 bool isARM = !AFI->isThumbFunction();
1260 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1261 int NumBytes = (int)MFI->getStackSize();
1263 if (!AFI->hasStackFrame()) {
1265 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1267 // Unwind MBBI to point to first LDR / FLDD.
1268 const unsigned *CSRegs = getCalleeSavedRegs();
1269 if (MBBI != MBB.begin()) {
1272 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1273 if (!isCSRestore(MBBI, TII, CSRegs))
1277 // Move SP to start of FP callee save spill area.
1278 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1279 AFI->getGPRCalleeSavedArea2Size() +
1280 AFI->getDPRCalleeSavedAreaSize());
1282 // Darwin ABI requires FP to point to the stack slot that contains the
1284 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1285 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1286 // Reset SP based on frame pointer only if the stack frame extends beyond
1287 // frame pointer stack slot or target is ELF and the function has FP.
1288 if (AFI->getGPRCalleeSavedArea2Size() ||
1289 AFI->getDPRCalleeSavedAreaSize() ||
1290 AFI->getDPRCalleeSavedAreaOffset()||
1294 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1297 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1302 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1304 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1306 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1310 } else if (NumBytes)
1311 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1313 // Move SP to start of integer callee save spill area 2.
1314 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 0, 3, STI);
1315 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1317 // Move SP to start of integer callee save spill area 1.
1318 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1319 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1321 // Move SP to SP upon entry to the function.
1322 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1323 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1327 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1330 #include "ARMGenRegisterInfo.inc"