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();
906 Constant *C = ConstantInt::get(Type::Int32Ty, Val);
907 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
909 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
910 .addReg(DestReg, getDefRegState(true), SubIdx)
911 .addConstantPoolIndex(Idx)
912 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
915 bool ARMBaseRegisterInfo::
916 requiresRegisterScavenging(const MachineFunction &MF) const {
920 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
921 // not required, we reserve argument space for call sites in the function
922 // immediately on entry to the current function. This eliminates the need for
923 // add/sub sp brackets around call sites. Returns true if the call frame is
924 // included as part of the stack frame.
925 bool ARMBaseRegisterInfo::
926 hasReservedCallFrame(MachineFunction &MF) const {
927 const MachineFrameInfo *FFI = MF.getFrameInfo();
928 unsigned CFSize = FFI->getMaxCallFrameSize();
929 // It's not always a good idea to include the call frame as part of the
930 // stack frame. ARM (especially Thumb) has small immediate offset to
931 // address the stack frame. So a large call frame can cause poor codegen
932 // and may even makes it impossible to scavenge a register.
933 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
936 return !MF.getFrameInfo()->hasVarSizedObjects();
940 emitSPUpdate(bool isARM,
941 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
942 DebugLoc dl, const ARMBaseInstrInfo &TII,
944 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
946 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
949 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
954 void ARMBaseRegisterInfo::
955 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
956 MachineBasicBlock::iterator I) const {
957 if (!hasReservedCallFrame(MF)) {
958 // If we have alloca, convert as follows:
959 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
960 // ADJCALLSTACKUP -> add, sp, sp, amount
961 MachineInstr *Old = I;
962 DebugLoc dl = Old->getDebugLoc();
963 unsigned Amount = Old->getOperand(0).getImm();
965 // We need to keep the stack aligned properly. To do this, we round the
966 // amount of space needed for the outgoing arguments up to the next
967 // alignment boundary.
968 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
969 Amount = (Amount+Align-1)/Align*Align;
971 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
972 assert(!AFI->isThumb1OnlyFunction() &&
973 "This eliminateCallFramePseudoInstr does not suppor Thumb1!");
974 bool isARM = !AFI->isThumbFunction();
976 // Replace the pseudo instruction with a new instruction...
977 unsigned Opc = Old->getOpcode();
978 ARMCC::CondCodes Pred = (ARMCC::CondCodes)Old->getOperand(1).getImm();
979 // FIXME: Thumb2 version of ADJCALLSTACKUP and ADJCALLSTACKDOWN?
980 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
981 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
982 unsigned PredReg = Old->getOperand(2).getReg();
983 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
985 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
986 unsigned PredReg = Old->getOperand(3).getReg();
987 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
988 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
995 /// findScratchRegister - Find a 'free' ARM register. If register scavenger
996 /// is not being used, R12 is available. Otherwise, try for a call-clobbered
997 /// register first and then a spilled callee-saved register if that fails.
999 unsigned findScratchRegister(RegScavenger *RS, const TargetRegisterClass *RC,
1000 ARMFunctionInfo *AFI) {
1001 unsigned Reg = RS ? RS->FindUnusedReg(RC, true) : (unsigned) ARM::R12;
1002 assert(!AFI->isThumb1OnlyFunction());
1004 // Try a already spilled CS register.
1005 Reg = RS->FindUnusedReg(RC, AFI->getSpilledCSRegisters());
1011 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1012 int SPAdj, RegScavenger *RS) const {
1014 MachineInstr &MI = *II;
1015 MachineBasicBlock &MBB = *MI.getParent();
1016 MachineFunction &MF = *MBB.getParent();
1017 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1018 assert(!AFI->isThumb1OnlyFunction() &&
1019 "This eliminateFrameIndex does not suppor Thumb1!");
1021 while (!MI.getOperand(i).isFI()) {
1023 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1026 unsigned FrameReg = ARM::SP;
1027 int FrameIndex = MI.getOperand(i).getIndex();
1028 int Offset = MF.getFrameInfo()->getObjectOffset(FrameIndex) +
1029 MF.getFrameInfo()->getStackSize() + SPAdj;
1031 if (AFI->isGPRCalleeSavedArea1Frame(FrameIndex))
1032 Offset -= AFI->getGPRCalleeSavedArea1Offset();
1033 else if (AFI->isGPRCalleeSavedArea2Frame(FrameIndex))
1034 Offset -= AFI->getGPRCalleeSavedArea2Offset();
1035 else if (AFI->isDPRCalleeSavedAreaFrame(FrameIndex))
1036 Offset -= AFI->getDPRCalleeSavedAreaOffset();
1037 else if (hasFP(MF)) {
1038 assert(SPAdj == 0 && "Unexpected");
1039 // There is alloca()'s in this function, must reference off the frame
1041 FrameReg = getFrameRegister(MF);
1042 Offset -= AFI->getFramePtrSpillOffset();
1045 // modify MI as necessary to handle as much of 'Offset' as possible
1046 if (!AFI->isThumbFunction())
1047 Offset = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1049 assert(AFI->isThumb2Function());
1050 Offset = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1055 // If we get here, the immediate doesn't fit into the instruction. We folded
1056 // as much as possible above, handle the rest, providing a register that is
1058 assert(Offset && "This code isn't needed if offset already handled!");
1060 // Insert a set of r12 with the full address: r12 = sp + offset
1061 // If the offset we have is too large to fit into the instruction, we need
1062 // to form it with a series of ADDri's. Do this by taking 8-bit chunks
1064 unsigned ScratchReg = findScratchRegister(RS, &ARM::GPRRegClass, AFI);
1065 if (ScratchReg == 0)
1066 // No register is "free". Scavenge a register.
1067 ScratchReg = RS->scavengeRegister(&ARM::GPRRegClass, II, SPAdj);
1068 int PIdx = MI.findFirstPredOperandIdx();
1069 ARMCC::CondCodes Pred = (PIdx == -1)
1070 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1071 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1072 if (!AFI->isThumbFunction())
1073 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1074 Offset, Pred, PredReg, TII);
1076 assert(AFI->isThumb2Function());
1077 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1078 Offset, Pred, PredReg, TII);
1080 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1083 /// Move iterator pass the next bunch of callee save load / store ops for
1084 /// the particular spill area (1: integer area 1, 2: integer area 2,
1085 /// 3: fp area, 0: don't care).
1086 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1087 MachineBasicBlock::iterator &MBBI,
1088 int Opc1, int Opc2, unsigned Area,
1089 const ARMSubtarget &STI) {
1090 while (MBBI != MBB.end() &&
1091 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1092 MBBI->getOperand(1).isFI()) {
1095 unsigned Category = 0;
1096 switch (MBBI->getOperand(0).getReg()) {
1097 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1101 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1102 Category = STI.isTargetDarwin() ? 2 : 1;
1104 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1105 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1112 if (Done || Category != Area)
1120 void ARMBaseRegisterInfo::
1121 emitPrologue(MachineFunction &MF) const {
1122 MachineBasicBlock &MBB = MF.front();
1123 MachineBasicBlock::iterator MBBI = MBB.begin();
1124 MachineFrameInfo *MFI = MF.getFrameInfo();
1125 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1126 assert(!AFI->isThumb1OnlyFunction() &&
1127 "This emitPrologue does not suppor Thumb1!");
1128 bool isARM = !AFI->isThumbFunction();
1129 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1130 unsigned NumBytes = MFI->getStackSize();
1131 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1132 DebugLoc dl = (MBBI != MBB.end() ?
1133 MBBI->getDebugLoc() : DebugLoc::getUnknownLoc());
1135 // Determine the sizes of each callee-save spill areas and record which frame
1136 // belongs to which callee-save spill areas.
1137 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1138 int FramePtrSpillFI = 0;
1141 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1143 if (!AFI->hasStackFrame()) {
1145 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1149 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1150 unsigned Reg = CSI[i].getReg();
1151 int FI = CSI[i].getFrameIdx();
1158 if (Reg == FramePtr)
1159 FramePtrSpillFI = FI;
1160 AFI->addGPRCalleeSavedArea1Frame(FI);
1167 if (Reg == FramePtr)
1168 FramePtrSpillFI = FI;
1169 if (STI.isTargetDarwin()) {
1170 AFI->addGPRCalleeSavedArea2Frame(FI);
1173 AFI->addGPRCalleeSavedArea1Frame(FI);
1178 AFI->addDPRCalleeSavedAreaFrame(FI);
1183 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1184 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1185 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1187 // Darwin ABI requires FP to point to the stack slot that contains the
1189 if (STI.isTargetDarwin() || hasFP(MF)) {
1190 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1191 MachineInstrBuilder MIB =
1192 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1193 .addFrameIndex(FramePtrSpillFI).addImm(0);
1194 AddDefaultCC(AddDefaultPred(MIB));
1197 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1198 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1200 // Build the new SUBri to adjust SP for FP callee-save spill area.
1201 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1202 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1204 // Determine starting offsets of spill areas.
1205 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1206 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1207 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1208 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) + NumBytes);
1209 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1210 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1211 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1213 NumBytes = DPRCSOffset;
1215 // Insert it after all the callee-save spills.
1216 movePastCSLoadStoreOps(MBB, MBBI, ARM::FSTD, 0, 3, STI);
1217 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1220 if (STI.isTargetELF() && hasFP(MF)) {
1221 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1222 AFI->getFramePtrSpillOffset());
1225 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1226 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1227 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1230 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1231 for (unsigned i = 0; CSRegs[i]; ++i)
1232 if (Reg == CSRegs[i])
1237 static bool isCSRestore(MachineInstr *MI,
1238 const ARMBaseInstrInfo &TII,
1239 const unsigned *CSRegs) {
1240 return ((MI->getOpcode() == (int)ARM::FLDD ||
1241 MI->getOpcode() == (int)ARM::LDR ||
1242 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1243 MI->getOperand(1).isFI() &&
1244 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1247 void ARMBaseRegisterInfo::
1248 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1249 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1250 assert(MBBI->getDesc().isReturn() &&
1251 "Can only insert epilog into returning blocks");
1252 DebugLoc dl = MBBI->getDebugLoc();
1253 MachineFrameInfo *MFI = MF.getFrameInfo();
1254 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1255 assert(!AFI->isThumb1OnlyFunction() &&
1256 "This emitEpilogue does not suppor Thumb1!");
1257 bool isARM = !AFI->isThumbFunction();
1259 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1260 int NumBytes = (int)MFI->getStackSize();
1262 if (!AFI->hasStackFrame()) {
1264 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1266 // Unwind MBBI to point to first LDR / FLDD.
1267 const unsigned *CSRegs = getCalleeSavedRegs();
1268 if (MBBI != MBB.begin()) {
1271 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1272 if (!isCSRestore(MBBI, TII, CSRegs))
1276 // Move SP to start of FP callee save spill area.
1277 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1278 AFI->getGPRCalleeSavedArea2Size() +
1279 AFI->getDPRCalleeSavedAreaSize());
1281 // Darwin ABI requires FP to point to the stack slot that contains the
1283 if ((STI.isTargetDarwin() && NumBytes) || hasFP(MF)) {
1284 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1285 // Reset SP based on frame pointer only if the stack frame extends beyond
1286 // frame pointer stack slot or target is ELF and the function has FP.
1287 if (AFI->getGPRCalleeSavedArea2Size() ||
1288 AFI->getDPRCalleeSavedAreaSize() ||
1289 AFI->getDPRCalleeSavedAreaOffset()||
1292 unsigned SUBriOpc = isARM ? ARM::SUBri : ARM::t2SUBri;
1293 BuildMI(MBB, MBBI, dl, TII.get(SUBriOpc), ARM::SP)
1296 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1299 unsigned MOVrOpc = isARM ? ARM::MOVr : ARM::t2MOVr;
1300 BuildMI(MBB, MBBI, dl, TII.get(MOVrOpc), ARM::SP)
1302 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1305 } else if (NumBytes)
1306 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1308 // Move SP to start of integer callee save spill area 2.
1309 movePastCSLoadStoreOps(MBB, MBBI, ARM::FLDD, 0, 3, STI);
1310 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1312 // Move SP to start of integer callee save spill area 1.
1313 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1314 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1316 // Move SP to SP upon entry to the function.
1317 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1318 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1322 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1325 #include "ARMGenRegisterInfo.inc"