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 "ARMFrameLowering.h"
19 #include "ARMInstrInfo.h"
20 #include "ARMMachineFunctionInfo.h"
21 #include "ARMSubtarget.h"
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Function.h"
25 #include "llvm/LLVMContext.h"
26 #include "llvm/CodeGen/MachineConstantPool.h"
27 #include "llvm/CodeGen/MachineFrameInfo.h"
28 #include "llvm/CodeGen/MachineFunction.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineLocation.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/CodeGen/RegisterScavenging.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetFrameLowering.h"
37 #include "llvm/Target/TargetMachine.h"
38 #include "llvm/Target/TargetOptions.h"
39 #include "llvm/ADT/BitVector.h"
40 #include "llvm/ADT/SmallVector.h"
41 #include "llvm/Support/CommandLine.h"
43 #define GET_REGINFO_TARGET_DESC
44 #include "ARMGenRegisterInfo.inc"
49 ForceAllBaseRegAlloc("arm-force-base-reg-alloc", cl::Hidden, cl::init(false),
50 cl::desc("Force use of virtual base registers for stack load/store"));
52 EnableLocalStackAlloc("enable-local-stack-alloc", cl::init(true), cl::Hidden,
53 cl::desc("Enable pre-regalloc stack frame index allocation"));
55 EnableBasePointer("arm-use-base-pointer", cl::Hidden, cl::init(true),
56 cl::desc("Enable use of a base pointer for complex stack frames"));
58 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
59 const ARMSubtarget &sti)
60 : ARMGenRegisterInfo(), TII(tii), STI(sti),
61 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
66 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
67 static const unsigned CalleeSavedRegs[] = {
68 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
69 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
71 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
72 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
76 static const unsigned DarwinCalleeSavedRegs[] = {
77 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
79 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
80 ARM::R11, ARM::R10, ARM::R8,
82 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
83 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
86 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
89 BitVector ARMBaseRegisterInfo::
90 getReservedRegs(const MachineFunction &MF) const {
91 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
93 // FIXME: avoid re-calculating this every time.
94 BitVector Reserved(getNumRegs());
95 Reserved.set(ARM::SP);
96 Reserved.set(ARM::PC);
97 Reserved.set(ARM::FPSCR);
99 Reserved.set(FramePtr);
100 if (hasBasePointer(MF))
101 Reserved.set(BasePtr);
102 // Some targets reserve R9.
103 if (STI.isR9Reserved())
104 Reserved.set(ARM::R9);
105 // Reserve D16-D31 if the subtarget doesn't support them.
106 if (!STI.hasVFP3() || STI.hasD16()) {
107 assert(ARM::D31 == ARM::D16 + 15);
108 for (unsigned i = 0; i != 16; ++i)
109 Reserved.set(ARM::D16 + i);
114 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
115 unsigned Reg) const {
116 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
124 if (hasBasePointer(MF))
129 if (FramePtr == Reg && TFI->hasFP(MF))
133 return STI.isR9Reserved();
139 const TargetRegisterClass *
140 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
141 const TargetRegisterClass *B,
142 unsigned SubIdx) const {
150 if (A->getSize() == 8) {
151 if (B == &ARM::SPR_8RegClass)
152 return &ARM::DPR_8RegClass;
153 assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
154 if (A == &ARM::DPR_8RegClass)
156 return &ARM::DPR_VFP2RegClass;
159 if (A->getSize() == 16) {
160 if (B == &ARM::SPR_8RegClass)
161 return &ARM::QPR_8RegClass;
162 return &ARM::QPR_VFP2RegClass;
165 if (A->getSize() == 32) {
166 if (B == &ARM::SPR_8RegClass)
167 return 0; // Do not allow coalescing!
168 return &ARM::QQPR_VFP2RegClass;
171 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
172 return 0; // Do not allow coalescing!
179 if (A->getSize() == 16) {
180 if (B == &ARM::DPR_VFP2RegClass)
181 return &ARM::QPR_VFP2RegClass;
182 if (B == &ARM::DPR_8RegClass)
183 return 0; // Do not allow coalescing!
187 if (A->getSize() == 32) {
188 if (B == &ARM::DPR_VFP2RegClass)
189 return &ARM::QQPR_VFP2RegClass;
190 if (B == &ARM::DPR_8RegClass)
191 return 0; // Do not allow coalescing!
195 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
196 if (B != &ARM::DPRRegClass)
197 return 0; // Do not allow coalescing!
204 // D sub-registers of QQQQ registers.
205 if (A->getSize() == 64 && B == &ARM::DPRRegClass)
207 return 0; // Do not allow coalescing!
213 if (A->getSize() == 32) {
214 if (B == &ARM::QPR_VFP2RegClass)
215 return &ARM::QQPR_VFP2RegClass;
216 if (B == &ARM::QPR_8RegClass)
217 return 0; // Do not allow coalescing!
221 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
222 if (B == &ARM::QPRRegClass)
224 return 0; // Do not allow coalescing!
228 // Q sub-registers of QQQQ registers.
229 if (A->getSize() == 64 && B == &ARM::QPRRegClass)
231 return 0; // Do not allow coalescing!
238 ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
239 SmallVectorImpl<unsigned> &SubIndices,
240 unsigned &NewSubIdx) const {
242 unsigned Size = RC->getSize() * 8;
246 NewSubIdx = 0; // Whole register.
247 unsigned NumRegs = SubIndices.size();
249 // 8 D registers -> 1 QQQQ register.
250 return (Size == 512 &&
251 SubIndices[0] == ARM::dsub_0 &&
252 SubIndices[1] == ARM::dsub_1 &&
253 SubIndices[2] == ARM::dsub_2 &&
254 SubIndices[3] == ARM::dsub_3 &&
255 SubIndices[4] == ARM::dsub_4 &&
256 SubIndices[5] == ARM::dsub_5 &&
257 SubIndices[6] == ARM::dsub_6 &&
258 SubIndices[7] == ARM::dsub_7);
259 } else if (NumRegs == 4) {
260 if (SubIndices[0] == ARM::qsub_0) {
261 // 4 Q registers -> 1 QQQQ register.
262 return (Size == 512 &&
263 SubIndices[1] == ARM::qsub_1 &&
264 SubIndices[2] == ARM::qsub_2 &&
265 SubIndices[3] == ARM::qsub_3);
266 } else if (SubIndices[0] == ARM::dsub_0) {
267 // 4 D registers -> 1 QQ register.
269 SubIndices[1] == ARM::dsub_1 &&
270 SubIndices[2] == ARM::dsub_2 &&
271 SubIndices[3] == ARM::dsub_3) {
273 NewSubIdx = ARM::qqsub_0;
276 } else if (SubIndices[0] == ARM::dsub_4) {
277 // 4 D registers -> 1 QQ register (2nd).
279 SubIndices[1] == ARM::dsub_5 &&
280 SubIndices[2] == ARM::dsub_6 &&
281 SubIndices[3] == ARM::dsub_7) {
282 NewSubIdx = ARM::qqsub_1;
285 } else if (SubIndices[0] == ARM::ssub_0) {
286 // 4 S registers -> 1 Q register.
288 SubIndices[1] == ARM::ssub_1 &&
289 SubIndices[2] == ARM::ssub_2 &&
290 SubIndices[3] == ARM::ssub_3) {
292 NewSubIdx = ARM::qsub_0;
296 } else if (NumRegs == 2) {
297 if (SubIndices[0] == ARM::qsub_0) {
298 // 2 Q registers -> 1 QQ register.
299 if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
301 NewSubIdx = ARM::qqsub_0;
304 } else if (SubIndices[0] == ARM::qsub_2) {
305 // 2 Q registers -> 1 QQ register (2nd).
306 if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
307 NewSubIdx = ARM::qqsub_1;
310 } else if (SubIndices[0] == ARM::dsub_0) {
311 // 2 D registers -> 1 Q register.
312 if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
314 NewSubIdx = ARM::qsub_0;
317 } else if (SubIndices[0] == ARM::dsub_2) {
318 // 2 D registers -> 1 Q register (2nd).
319 if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
320 NewSubIdx = ARM::qsub_1;
323 } else if (SubIndices[0] == ARM::dsub_4) {
324 // 2 D registers -> 1 Q register (3rd).
325 if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
326 NewSubIdx = ARM::qsub_2;
329 } else if (SubIndices[0] == ARM::dsub_6) {
330 // 2 D registers -> 1 Q register (3rd).
331 if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
332 NewSubIdx = ARM::qsub_3;
335 } else if (SubIndices[0] == ARM::ssub_0) {
336 // 2 S registers -> 1 D register.
337 if (SubIndices[1] == ARM::ssub_1) {
339 NewSubIdx = ARM::dsub_0;
342 } else if (SubIndices[0] == ARM::ssub_2) {
343 // 2 S registers -> 1 D register (2nd).
344 if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
345 NewSubIdx = ARM::dsub_1;
353 const TargetRegisterClass*
354 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
356 const TargetRegisterClass *Super = RC;
357 TargetRegisterClass::sc_iterator I = RC->superclasses_begin();
359 switch (Super->getID()) {
360 case ARM::GPRRegClassID:
361 case ARM::SPRRegClassID:
362 case ARM::DPRRegClassID:
363 case ARM::QPRRegClassID:
364 case ARM::QQPRRegClassID:
365 case ARM::QQQQPRRegClassID:
373 const TargetRegisterClass *
374 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
375 return ARM::GPRRegisterClass;
379 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
380 MachineFunction &MF) const {
381 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
383 switch (RC->getID()) {
386 case ARM::tGPRRegClassID:
387 return TFI->hasFP(MF) ? 4 : 5;
388 case ARM::GPRRegClassID: {
389 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
390 return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
392 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
393 case ARM::DPRRegClassID:
398 /// getRawAllocationOrder - Returns the register allocation order for a
399 /// specified register class with a target-dependent hint.
401 ARMBaseRegisterInfo::getRawAllocationOrder(const TargetRegisterClass *RC,
402 unsigned HintType, unsigned HintReg,
403 const MachineFunction &MF) const {
404 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
405 // Alternative register allocation orders when favoring even / odd registers
406 // of register pairs.
408 // No FP, R9 is available.
409 static const unsigned GPREven1[] = {
410 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
411 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
414 static const unsigned GPROdd1[] = {
415 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
416 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
420 // FP is R7, R9 is available.
421 static const unsigned GPREven2[] = {
422 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
423 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
426 static const unsigned GPROdd2[] = {
427 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
428 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
432 // FP is R11, R9 is available.
433 static const unsigned GPREven3[] = {
434 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
435 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
438 static const unsigned GPROdd3[] = {
439 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
440 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
444 // No FP, R9 is not available.
445 static const unsigned GPREven4[] = {
446 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
447 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
450 static const unsigned GPROdd4[] = {
451 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
452 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
456 // FP is R7, R9 is not available.
457 static const unsigned GPREven5[] = {
458 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
459 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
462 static const unsigned GPROdd5[] = {
463 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
464 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
468 // FP is R11, R9 is not available.
469 static const unsigned GPREven6[] = {
470 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
471 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
473 static const unsigned GPROdd6[] = {
474 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
475 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
478 // We only support even/odd hints for GPR and rGPR.
479 if (RC != ARM::GPRRegisterClass && RC != ARM::rGPRRegisterClass)
480 return RC->getRawAllocationOrder(MF);
482 if (HintType == ARMRI::RegPairEven) {
483 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
484 // It's no longer possible to fulfill this hint. Return the default
486 return RC->getRawAllocationOrder(MF);
488 if (!TFI->hasFP(MF)) {
489 if (!STI.isR9Reserved())
490 return ArrayRef<unsigned>(GPREven1);
492 return ArrayRef<unsigned>(GPREven4);
493 } else if (FramePtr == ARM::R7) {
494 if (!STI.isR9Reserved())
495 return ArrayRef<unsigned>(GPREven2);
497 return ArrayRef<unsigned>(GPREven5);
498 } else { // FramePtr == ARM::R11
499 if (!STI.isR9Reserved())
500 return ArrayRef<unsigned>(GPREven3);
502 return ArrayRef<unsigned>(GPREven6);
504 } else if (HintType == ARMRI::RegPairOdd) {
505 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
506 // It's no longer possible to fulfill this hint. Return the default
508 return RC->getRawAllocationOrder(MF);
510 if (!TFI->hasFP(MF)) {
511 if (!STI.isR9Reserved())
512 return ArrayRef<unsigned>(GPROdd1);
514 return ArrayRef<unsigned>(GPROdd4);
515 } else if (FramePtr == ARM::R7) {
516 if (!STI.isR9Reserved())
517 return ArrayRef<unsigned>(GPROdd2);
519 return ArrayRef<unsigned>(GPROdd5);
520 } else { // FramePtr == ARM::R11
521 if (!STI.isR9Reserved())
522 return ArrayRef<unsigned>(GPROdd3);
524 return ArrayRef<unsigned>(GPROdd6);
527 return RC->getRawAllocationOrder(MF);
530 /// ResolveRegAllocHint - Resolves the specified register allocation hint
531 /// to a physical register. Returns the physical register if it is successful.
533 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
534 const MachineFunction &MF) const {
535 if (Reg == 0 || !isPhysicalRegister(Reg))
539 else if (Type == (unsigned)ARMRI::RegPairOdd)
541 return getRegisterPairOdd(Reg, MF);
542 else if (Type == (unsigned)ARMRI::RegPairEven)
544 return getRegisterPairEven(Reg, MF);
549 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
550 MachineFunction &MF) const {
551 MachineRegisterInfo *MRI = &MF.getRegInfo();
552 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
553 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
554 Hint.first == (unsigned)ARMRI::RegPairEven) &&
555 TargetRegisterInfo::isVirtualRegister(Hint.second)) {
556 // If 'Reg' is one of the even / odd register pair and it's now changed
557 // (e.g. coalesced) into a different register. The other register of the
558 // pair allocation hint must be updated to reflect the relationship
560 unsigned OtherReg = Hint.second;
561 Hint = MRI->getRegAllocationHint(OtherReg);
562 if (Hint.second == Reg)
563 // Make sure the pair has not already divorced.
564 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
569 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
570 // CortexA9 has a Write-after-write hazard for NEON registers.
571 if (!STI.isCortexA9())
574 switch (RC->getID()) {
575 case ARM::DPRRegClassID:
576 case ARM::DPR_8RegClassID:
577 case ARM::DPR_VFP2RegClassID:
578 case ARM::QPRRegClassID:
579 case ARM::QPR_8RegClassID:
580 case ARM::QPR_VFP2RegClassID:
581 case ARM::SPRRegClassID:
582 case ARM::SPR_8RegClassID:
583 // Avoid reusing S, D, and Q registers.
584 // Don't increase register pressure for QQ and QQQQ.
591 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
592 const MachineFrameInfo *MFI = MF.getFrameInfo();
593 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
595 if (!EnableBasePointer)
598 if (needsStackRealignment(MF) && MFI->hasVarSizedObjects())
601 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
602 // negative range for ldr/str (255), and thumb1 is positive offsets only.
603 // It's going to be better to use the SP or Base Pointer instead. When there
604 // are variable sized objects, we can't reference off of the SP, so we
605 // reserve a Base Pointer.
606 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
607 // Conservatively estimate whether the negative offset from the frame
608 // pointer will be sufficient to reach. If a function has a smallish
609 // frame, it's less likely to have lots of spills and callee saved
610 // space, so it's all more likely to be within range of the frame pointer.
611 // If it's wrong, the scavenger will still enable access to work, it just
613 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
621 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
622 const MachineFrameInfo *MFI = MF.getFrameInfo();
623 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
624 // We can't realign the stack if:
625 // 1. Dynamic stack realignment is explicitly disabled,
626 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
627 // 3. There are VLAs in the function and the base pointer is disabled.
628 return (RealignStack && !AFI->isThumb1OnlyFunction() &&
629 (!MFI->hasVarSizedObjects() || EnableBasePointer));
632 bool ARMBaseRegisterInfo::
633 needsStackRealignment(const MachineFunction &MF) const {
634 const MachineFrameInfo *MFI = MF.getFrameInfo();
635 const Function *F = MF.getFunction();
636 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
637 bool requiresRealignment = ((MFI->getLocalFrameMaxAlign() > StackAlign) ||
638 F->hasFnAttr(Attribute::StackAlignment));
640 return requiresRealignment && canRealignStack(MF);
643 bool ARMBaseRegisterInfo::
644 cannotEliminateFrame(const MachineFunction &MF) const {
645 const MachineFrameInfo *MFI = MF.getFrameInfo();
646 if (DisableFramePointerElim(MF) && MFI->adjustsStack())
648 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
649 || needsStackRealignment(MF);
652 unsigned ARMBaseRegisterInfo::getRARegister() const {
657 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
658 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
665 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
666 llvm_unreachable("What is the exception register");
670 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
671 llvm_unreachable("What is the exception handler register");
675 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
676 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
679 int ARMBaseRegisterInfo::getLLVMRegNum(unsigned DwarfRegNo, bool isEH) const {
680 return ARMGenRegisterInfo::getLLVMRegNumFull(DwarfRegNo,0);
683 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
684 const MachineFunction &MF) const {
687 // Return 0 if either register of the pair is a special register.
696 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
699 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
701 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
773 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
774 const MachineFunction &MF) const {
777 // Return 0 if either register of the pair is a special register.
786 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
789 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
791 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
863 /// emitLoadConstPool - Emits a load from constpool to materialize the
864 /// specified immediate.
865 void ARMBaseRegisterInfo::
866 emitLoadConstPool(MachineBasicBlock &MBB,
867 MachineBasicBlock::iterator &MBBI,
869 unsigned DestReg, unsigned SubIdx, int Val,
870 ARMCC::CondCodes Pred,
871 unsigned PredReg, unsigned MIFlags) const {
872 MachineFunction &MF = *MBB.getParent();
873 MachineConstantPool *ConstantPool = MF.getConstantPool();
875 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
876 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
878 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
879 .addReg(DestReg, getDefRegState(true), SubIdx)
880 .addConstantPoolIndex(Idx)
881 .addImm(0).addImm(Pred).addReg(PredReg)
882 .setMIFlags(MIFlags);
885 bool ARMBaseRegisterInfo::
886 requiresRegisterScavenging(const MachineFunction &MF) const {
890 bool ARMBaseRegisterInfo::
891 requiresFrameIndexScavenging(const MachineFunction &MF) const {
895 bool ARMBaseRegisterInfo::
896 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
897 return EnableLocalStackAlloc;
901 emitSPUpdate(bool isARM,
902 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
903 DebugLoc dl, const ARMBaseInstrInfo &TII,
905 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
907 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
910 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
915 void ARMBaseRegisterInfo::
916 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
917 MachineBasicBlock::iterator I) const {
918 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
919 if (!TFI->hasReservedCallFrame(MF)) {
920 // If we have alloca, convert as follows:
921 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
922 // ADJCALLSTACKUP -> add, sp, sp, amount
923 MachineInstr *Old = I;
924 DebugLoc dl = Old->getDebugLoc();
925 unsigned Amount = Old->getOperand(0).getImm();
927 // We need to keep the stack aligned properly. To do this, we round the
928 // amount of space needed for the outgoing arguments up to the next
929 // alignment boundary.
930 unsigned Align = TFI->getStackAlignment();
931 Amount = (Amount+Align-1)/Align*Align;
933 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
934 assert(!AFI->isThumb1OnlyFunction() &&
935 "This eliminateCallFramePseudoInstr does not support Thumb1!");
936 bool isARM = !AFI->isThumbFunction();
938 // Replace the pseudo instruction with a new instruction...
939 unsigned Opc = Old->getOpcode();
940 int PIdx = Old->findFirstPredOperandIdx();
941 ARMCC::CondCodes Pred = (PIdx == -1)
942 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
943 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
944 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
945 unsigned PredReg = Old->getOperand(2).getReg();
946 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
948 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
949 unsigned PredReg = Old->getOperand(3).getReg();
950 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
951 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
958 int64_t ARMBaseRegisterInfo::
959 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
960 const MCInstrDesc &Desc = MI->getDesc();
961 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
962 int64_t InstrOffs = 0;;
966 case ARMII::AddrModeT2_i8:
967 case ARMII::AddrModeT2_i12:
968 case ARMII::AddrMode_i12:
969 InstrOffs = MI->getOperand(Idx+1).getImm();
972 case ARMII::AddrMode5: {
974 const MachineOperand &OffOp = MI->getOperand(Idx+1);
975 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
976 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
977 InstrOffs = -InstrOffs;
981 case ARMII::AddrMode2: {
983 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
984 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
985 InstrOffs = -InstrOffs;
988 case ARMII::AddrMode3: {
990 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
991 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
992 InstrOffs = -InstrOffs;
995 case ARMII::AddrModeT1_s: {
997 InstrOffs = MI->getOperand(ImmIdx).getImm();
1002 llvm_unreachable("Unsupported addressing mode!");
1006 return InstrOffs * Scale;
1009 /// needsFrameBaseReg - Returns true if the instruction's frame index
1010 /// reference would be better served by a base register other than FP
1011 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1012 /// references it should create new base registers for.
1013 bool ARMBaseRegisterInfo::
1014 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1015 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
1016 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1019 // It's the load/store FI references that cause issues, as it can be difficult
1020 // to materialize the offset if it won't fit in the literal field. Estimate
1021 // based on the size of the local frame and some conservative assumptions
1022 // about the rest of the stack frame (note, this is pre-regalloc, so
1023 // we don't know everything for certain yet) whether this offset is likely
1024 // to be out of range of the immediate. Return true if so.
1026 // We only generate virtual base registers for loads and stores, so
1027 // return false for everything else.
1028 unsigned Opc = MI->getOpcode();
1030 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
1031 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
1032 case ARM::t2LDRi12: case ARM::t2LDRi8:
1033 case ARM::t2STRi12: case ARM::t2STRi8:
1034 case ARM::VLDRS: case ARM::VLDRD:
1035 case ARM::VSTRS: case ARM::VSTRD:
1036 case ARM::tSTRspi: case ARM::tLDRspi:
1037 if (ForceAllBaseRegAlloc)
1044 // Without a virtual base register, if the function has variable sized
1045 // objects, all fixed-size local references will be via the frame pointer,
1046 // Approximate the offset and see if it's legal for the instruction.
1047 // Note that the incoming offset is based on the SP value at function entry,
1048 // so it'll be negative.
1049 MachineFunction &MF = *MI->getParent()->getParent();
1050 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
1051 MachineFrameInfo *MFI = MF.getFrameInfo();
1052 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1054 // Estimate an offset from the frame pointer.
1055 // Conservatively assume all callee-saved registers get pushed. R4-R6
1056 // will be earlier than the FP, so we ignore those.
1058 int64_t FPOffset = Offset - 8;
1059 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
1060 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
1062 // Estimate an offset from the stack pointer.
1063 // The incoming offset is relating to the SP at the start of the function,
1064 // but when we access the local it'll be relative to the SP after local
1065 // allocation, so adjust our SP-relative offset by that allocation size.
1067 Offset += MFI->getLocalFrameSize();
1068 // Assume that we'll have at least some spill slots allocated.
1069 // FIXME: This is a total SWAG number. We should run some statistics
1070 // and pick a real one.
1071 Offset += 128; // 128 bytes of spill slots
1073 // If there is a frame pointer, try using it.
1074 // The FP is only available if there is no dynamic realignment. We
1075 // don't know for sure yet whether we'll need that, so we guess based
1076 // on whether there are any local variables that would trigger it.
1077 unsigned StackAlign = TFI->getStackAlignment();
1078 if (TFI->hasFP(MF) &&
1079 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
1080 if (isFrameOffsetLegal(MI, FPOffset))
1083 // If we can reference via the stack pointer, try that.
1084 // FIXME: This (and the code that resolves the references) can be improved
1085 // to only disallow SP relative references in the live range of
1086 // the VLA(s). In practice, it's unclear how much difference that
1087 // would make, but it may be worth doing.
1088 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
1091 // The offset likely isn't legal, we want to allocate a virtual base register.
1095 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
1096 /// be a pointer to FrameIdx at the beginning of the basic block.
1097 void ARMBaseRegisterInfo::
1098 materializeFrameBaseRegister(MachineBasicBlock *MBB,
1099 unsigned BaseReg, int FrameIdx,
1100 int64_t Offset) const {
1101 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
1102 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
1103 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
1105 MachineBasicBlock::iterator Ins = MBB->begin();
1106 DebugLoc DL; // Defaults to "unknown"
1107 if (Ins != MBB->end())
1108 DL = Ins->getDebugLoc();
1110 const MCInstrDesc &MCID = TII.get(ADDriOpc);
1111 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1112 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this));
1114 MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, MCID, BaseReg)
1115 .addFrameIndex(FrameIdx).addImm(Offset);
1117 if (!AFI->isThumb1OnlyFunction())
1118 AddDefaultCC(AddDefaultPred(MIB));
1122 ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
1123 unsigned BaseReg, int64_t Offset) const {
1124 MachineInstr &MI = *I;
1125 MachineBasicBlock &MBB = *MI.getParent();
1126 MachineFunction &MF = *MBB.getParent();
1127 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1128 int Off = Offset; // ARM doesn't need the general 64-bit offsets
1131 assert(!AFI->isThumb1OnlyFunction() &&
1132 "This resolveFrameIndex does not support Thumb1!");
1134 while (!MI.getOperand(i).isFI()) {
1136 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1139 if (!AFI->isThumbFunction())
1140 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
1142 assert(AFI->isThumb2Function());
1143 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
1145 assert (Done && "Unable to resolve frame index!");
1148 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1149 int64_t Offset) const {
1150 const MCInstrDesc &Desc = MI->getDesc();
1151 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1154 while (!MI->getOperand(i).isFI()) {
1156 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1159 // AddrMode4 and AddrMode6 cannot handle any offset.
1160 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
1163 unsigned NumBits = 0;
1165 bool isSigned = true;
1167 case ARMII::AddrModeT2_i8:
1168 case ARMII::AddrModeT2_i12:
1169 // i8 supports only negative, and i12 supports only positive, so
1170 // based on Offset sign, consider the appropriate instruction
1179 case ARMII::AddrMode5:
1180 // VFP address mode.
1184 case ARMII::AddrMode_i12:
1185 case ARMII::AddrMode2:
1188 case ARMII::AddrMode3:
1191 case ARMII::AddrModeT1_s:
1197 llvm_unreachable("Unsupported addressing mode!");
1201 Offset += getFrameIndexInstrOffset(MI, i);
1202 // Make sure the offset is encodable for instructions that scale the
1204 if ((Offset & (Scale-1)) != 0)
1207 if (isSigned && Offset < 0)
1210 unsigned Mask = (1 << NumBits) - 1;
1211 if ((unsigned)Offset <= Mask * Scale)
1218 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1219 int SPAdj, RegScavenger *RS) const {
1221 MachineInstr &MI = *II;
1222 MachineBasicBlock &MBB = *MI.getParent();
1223 MachineFunction &MF = *MBB.getParent();
1224 const ARMFrameLowering *TFI =
1225 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
1226 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1227 assert(!AFI->isThumb1OnlyFunction() &&
1228 "This eliminateFrameIndex does not support Thumb1!");
1230 while (!MI.getOperand(i).isFI()) {
1232 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1235 int FrameIndex = MI.getOperand(i).getIndex();
1238 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
1240 // Special handling of dbg_value instructions.
1241 if (MI.isDebugValue()) {
1242 MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/);
1243 MI.getOperand(i+1).ChangeToImmediate(Offset);
1247 // Modify MI as necessary to handle as much of 'Offset' as possible
1249 if (!AFI->isThumbFunction())
1250 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1252 assert(AFI->isThumb2Function());
1253 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1258 // If we get here, the immediate doesn't fit into the instruction. We folded
1259 // as much as possible above, handle the rest, providing a register that is
1262 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1263 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1264 "This code isn't needed if offset already handled!");
1266 unsigned ScratchReg = 0;
1267 int PIdx = MI.findFirstPredOperandIdx();
1268 ARMCC::CondCodes Pred = (PIdx == -1)
1269 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1270 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1272 // Must be addrmode4/6.
1273 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1275 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1276 if (!AFI->isThumbFunction())
1277 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1278 Offset, Pred, PredReg, TII);
1280 assert(AFI->isThumb2Function());
1281 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1282 Offset, Pred, PredReg, TII);
1284 // Update the original instruction to use the scratch register.
1285 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);