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"
46 ForceAllBaseRegAlloc("arm-force-base-reg-alloc", cl::Hidden, cl::init(false),
47 cl::desc("Force use of virtual base registers for stack load/store"));
49 EnableLocalStackAlloc("enable-local-stack-alloc", cl::init(true), cl::Hidden,
50 cl::desc("Enable pre-regalloc stack frame index allocation"));
52 EnableBasePointer("arm-use-base-pointer", cl::Hidden, cl::init(true),
53 cl::desc("Enable use of a base pointer for complex stack frames"));
55 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
56 const ARMSubtarget &sti)
57 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
59 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
64 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
65 static const unsigned CalleeSavedRegs[] = {
66 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
67 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
69 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
70 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
74 static const unsigned DarwinCalleeSavedRegs[] = {
75 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
77 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
78 ARM::R11, ARM::R10, ARM::R8,
80 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
81 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
84 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
87 BitVector ARMBaseRegisterInfo::
88 getReservedRegs(const MachineFunction &MF) const {
89 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
91 // FIXME: avoid re-calculating this every time.
92 BitVector Reserved(getNumRegs());
93 Reserved.set(ARM::SP);
94 Reserved.set(ARM::PC);
95 Reserved.set(ARM::FPSCR);
97 Reserved.set(FramePtr);
98 if (hasBasePointer(MF))
99 Reserved.set(BasePtr);
100 // Some targets reserve R9.
101 if (STI.isR9Reserved())
102 Reserved.set(ARM::R9);
103 // Reserve D16-D31 if the subtarget doesn't support them.
104 if (!STI.hasVFP3() || STI.hasD16()) {
105 assert(ARM::D31 == ARM::D16 + 15);
106 for (unsigned i = 0; i != 16; ++i)
107 Reserved.set(ARM::D16 + i);
112 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
113 unsigned Reg) const {
114 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
122 if (hasBasePointer(MF))
127 if (FramePtr == Reg && TFI->hasFP(MF))
131 return STI.isR9Reserved();
137 const TargetRegisterClass *
138 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
139 const TargetRegisterClass *B,
140 unsigned SubIdx) const {
148 if (A->getSize() == 8) {
149 if (B == &ARM::SPR_8RegClass)
150 return &ARM::DPR_8RegClass;
151 assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
152 if (A == &ARM::DPR_8RegClass)
154 return &ARM::DPR_VFP2RegClass;
157 if (A->getSize() == 16) {
158 if (B == &ARM::SPR_8RegClass)
159 return &ARM::QPR_8RegClass;
160 return &ARM::QPR_VFP2RegClass;
163 if (A->getSize() == 32) {
164 if (B == &ARM::SPR_8RegClass)
165 return 0; // Do not allow coalescing!
166 return &ARM::QQPR_VFP2RegClass;
169 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
170 return 0; // Do not allow coalescing!
177 if (A->getSize() == 16) {
178 if (B == &ARM::DPR_VFP2RegClass)
179 return &ARM::QPR_VFP2RegClass;
180 if (B == &ARM::DPR_8RegClass)
181 return 0; // Do not allow coalescing!
185 if (A->getSize() == 32) {
186 if (B == &ARM::DPR_VFP2RegClass)
187 return &ARM::QQPR_VFP2RegClass;
188 if (B == &ARM::DPR_8RegClass)
189 return 0; // Do not allow coalescing!
193 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
194 if (B != &ARM::DPRRegClass)
195 return 0; // Do not allow coalescing!
202 // D sub-registers of QQQQ registers.
203 if (A->getSize() == 64 && B == &ARM::DPRRegClass)
205 return 0; // Do not allow coalescing!
211 if (A->getSize() == 32) {
212 if (B == &ARM::QPR_VFP2RegClass)
213 return &ARM::QQPR_VFP2RegClass;
214 if (B == &ARM::QPR_8RegClass)
215 return 0; // Do not allow coalescing!
219 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
220 if (B == &ARM::QPRRegClass)
222 return 0; // Do not allow coalescing!
226 // Q sub-registers of QQQQ registers.
227 if (A->getSize() == 64 && B == &ARM::QPRRegClass)
229 return 0; // Do not allow coalescing!
236 ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
237 SmallVectorImpl<unsigned> &SubIndices,
238 unsigned &NewSubIdx) const {
240 unsigned Size = RC->getSize() * 8;
244 NewSubIdx = 0; // Whole register.
245 unsigned NumRegs = SubIndices.size();
247 // 8 D registers -> 1 QQQQ register.
248 return (Size == 512 &&
249 SubIndices[0] == ARM::dsub_0 &&
250 SubIndices[1] == ARM::dsub_1 &&
251 SubIndices[2] == ARM::dsub_2 &&
252 SubIndices[3] == ARM::dsub_3 &&
253 SubIndices[4] == ARM::dsub_4 &&
254 SubIndices[5] == ARM::dsub_5 &&
255 SubIndices[6] == ARM::dsub_6 &&
256 SubIndices[7] == ARM::dsub_7);
257 } else if (NumRegs == 4) {
258 if (SubIndices[0] == ARM::qsub_0) {
259 // 4 Q registers -> 1 QQQQ register.
260 return (Size == 512 &&
261 SubIndices[1] == ARM::qsub_1 &&
262 SubIndices[2] == ARM::qsub_2 &&
263 SubIndices[3] == ARM::qsub_3);
264 } else if (SubIndices[0] == ARM::dsub_0) {
265 // 4 D registers -> 1 QQ register.
267 SubIndices[1] == ARM::dsub_1 &&
268 SubIndices[2] == ARM::dsub_2 &&
269 SubIndices[3] == ARM::dsub_3) {
271 NewSubIdx = ARM::qqsub_0;
274 } else if (SubIndices[0] == ARM::dsub_4) {
275 // 4 D registers -> 1 QQ register (2nd).
277 SubIndices[1] == ARM::dsub_5 &&
278 SubIndices[2] == ARM::dsub_6 &&
279 SubIndices[3] == ARM::dsub_7) {
280 NewSubIdx = ARM::qqsub_1;
283 } else if (SubIndices[0] == ARM::ssub_0) {
284 // 4 S registers -> 1 Q register.
286 SubIndices[1] == ARM::ssub_1 &&
287 SubIndices[2] == ARM::ssub_2 &&
288 SubIndices[3] == ARM::ssub_3) {
290 NewSubIdx = ARM::qsub_0;
294 } else if (NumRegs == 2) {
295 if (SubIndices[0] == ARM::qsub_0) {
296 // 2 Q registers -> 1 QQ register.
297 if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
299 NewSubIdx = ARM::qqsub_0;
302 } else if (SubIndices[0] == ARM::qsub_2) {
303 // 2 Q registers -> 1 QQ register (2nd).
304 if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
305 NewSubIdx = ARM::qqsub_1;
308 } else if (SubIndices[0] == ARM::dsub_0) {
309 // 2 D registers -> 1 Q register.
310 if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
312 NewSubIdx = ARM::qsub_0;
315 } else if (SubIndices[0] == ARM::dsub_2) {
316 // 2 D registers -> 1 Q register (2nd).
317 if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
318 NewSubIdx = ARM::qsub_1;
321 } else if (SubIndices[0] == ARM::dsub_4) {
322 // 2 D registers -> 1 Q register (3rd).
323 if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
324 NewSubIdx = ARM::qsub_2;
327 } else if (SubIndices[0] == ARM::dsub_6) {
328 // 2 D registers -> 1 Q register (3rd).
329 if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
330 NewSubIdx = ARM::qsub_3;
333 } else if (SubIndices[0] == ARM::ssub_0) {
334 // 2 S registers -> 1 D register.
335 if (SubIndices[1] == ARM::ssub_1) {
337 NewSubIdx = ARM::dsub_0;
340 } else if (SubIndices[0] == ARM::ssub_2) {
341 // 2 S registers -> 1 D register (2nd).
342 if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
343 NewSubIdx = ARM::dsub_1;
351 const TargetRegisterClass*
352 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
354 const TargetRegisterClass *Super = RC;
355 TargetRegisterClass::sc_iterator I = RC->superclasses_begin();
357 switch (Super->getID()) {
358 case ARM::GPRRegClassID:
359 case ARM::SPRRegClassID:
360 case ARM::DPRRegClassID:
361 case ARM::QPRRegClassID:
362 case ARM::QQPRRegClassID:
363 case ARM::QQQQPRRegClassID:
371 const TargetRegisterClass *
372 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
373 return ARM::GPRRegisterClass;
377 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
378 MachineFunction &MF) const {
379 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
381 switch (RC->getID()) {
384 case ARM::tGPRRegClassID:
385 return TFI->hasFP(MF) ? 4 : 5;
386 case ARM::GPRRegClassID: {
387 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
388 return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
390 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
391 case ARM::DPRRegClassID:
396 /// getRawAllocationOrder - Returns the register allocation order for a
397 /// specified register class with a target-dependent hint.
399 ARMBaseRegisterInfo::getRawAllocationOrder(const TargetRegisterClass *RC,
400 unsigned HintType, unsigned HintReg,
401 const MachineFunction &MF) const {
402 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
403 // Alternative register allocation orders when favoring even / odd registers
404 // of register pairs.
406 // No FP, R9 is available.
407 static const unsigned GPREven1[] = {
408 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
409 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
412 static const unsigned GPROdd1[] = {
413 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
414 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
418 // FP is R7, R9 is available.
419 static const unsigned GPREven2[] = {
420 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
421 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
424 static const unsigned GPROdd2[] = {
425 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
426 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
430 // FP is R11, R9 is available.
431 static const unsigned GPREven3[] = {
432 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
433 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
436 static const unsigned GPROdd3[] = {
437 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
438 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
442 // No FP, R9 is not available.
443 static const unsigned GPREven4[] = {
444 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
445 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
448 static const unsigned GPROdd4[] = {
449 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
450 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
454 // FP is R7, R9 is not available.
455 static const unsigned GPREven5[] = {
456 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
457 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
460 static const unsigned GPROdd5[] = {
461 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
462 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
466 // FP is R11, R9 is not available.
467 static const unsigned GPREven6[] = {
468 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
469 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
471 static const unsigned GPROdd6[] = {
472 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
473 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
476 // We only support even/odd hints for GPR and rGPR.
477 if (RC != ARM::GPRRegisterClass && RC != ARM::rGPRRegisterClass)
478 return RC->getRawAllocationOrder(MF);
480 if (HintType == ARMRI::RegPairEven) {
481 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
482 // It's no longer possible to fulfill this hint. Return the default
484 return RC->getRawAllocationOrder(MF);
486 if (!TFI->hasFP(MF)) {
487 if (!STI.isR9Reserved())
488 return ArrayRef<unsigned>(GPREven1);
490 return ArrayRef<unsigned>(GPREven4);
491 } else if (FramePtr == ARM::R7) {
492 if (!STI.isR9Reserved())
493 return ArrayRef<unsigned>(GPREven2);
495 return ArrayRef<unsigned>(GPREven5);
496 } else { // FramePtr == ARM::R11
497 if (!STI.isR9Reserved())
498 return ArrayRef<unsigned>(GPREven3);
500 return ArrayRef<unsigned>(GPREven6);
502 } else if (HintType == ARMRI::RegPairOdd) {
503 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
504 // It's no longer possible to fulfill this hint. Return the default
506 return RC->getRawAllocationOrder(MF);
508 if (!TFI->hasFP(MF)) {
509 if (!STI.isR9Reserved())
510 return ArrayRef<unsigned>(GPROdd1);
512 return ArrayRef<unsigned>(GPROdd4);
513 } else if (FramePtr == ARM::R7) {
514 if (!STI.isR9Reserved())
515 return ArrayRef<unsigned>(GPROdd2);
517 return ArrayRef<unsigned>(GPROdd5);
518 } else { // FramePtr == ARM::R11
519 if (!STI.isR9Reserved())
520 return ArrayRef<unsigned>(GPROdd3);
522 return ArrayRef<unsigned>(GPROdd6);
525 return RC->getRawAllocationOrder(MF);
528 /// ResolveRegAllocHint - Resolves the specified register allocation hint
529 /// to a physical register. Returns the physical register if it is successful.
531 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
532 const MachineFunction &MF) const {
533 if (Reg == 0 || !isPhysicalRegister(Reg))
537 else if (Type == (unsigned)ARMRI::RegPairOdd)
539 return getRegisterPairOdd(Reg, MF);
540 else if (Type == (unsigned)ARMRI::RegPairEven)
542 return getRegisterPairEven(Reg, MF);
547 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
548 MachineFunction &MF) const {
549 MachineRegisterInfo *MRI = &MF.getRegInfo();
550 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
551 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
552 Hint.first == (unsigned)ARMRI::RegPairEven) &&
553 TargetRegisterInfo::isVirtualRegister(Hint.second)) {
554 // If 'Reg' is one of the even / odd register pair and it's now changed
555 // (e.g. coalesced) into a different register. The other register of the
556 // pair allocation hint must be updated to reflect the relationship
558 unsigned OtherReg = Hint.second;
559 Hint = MRI->getRegAllocationHint(OtherReg);
560 if (Hint.second == Reg)
561 // Make sure the pair has not already divorced.
562 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
567 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
568 // CortexA9 has a Write-after-write hazard for NEON registers.
569 if (!STI.isCortexA9())
572 switch (RC->getID()) {
573 case ARM::DPRRegClassID:
574 case ARM::DPR_8RegClassID:
575 case ARM::DPR_VFP2RegClassID:
576 case ARM::QPRRegClassID:
577 case ARM::QPR_8RegClassID:
578 case ARM::QPR_VFP2RegClassID:
579 case ARM::SPRRegClassID:
580 case ARM::SPR_8RegClassID:
581 // Avoid reusing S, D, and Q registers.
582 // Don't increase register pressure for QQ and QQQQ.
589 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
590 const MachineFrameInfo *MFI = MF.getFrameInfo();
591 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
593 if (!EnableBasePointer)
596 if (needsStackRealignment(MF) && MFI->hasVarSizedObjects())
599 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
600 // negative range for ldr/str (255), and thumb1 is positive offsets only.
601 // It's going to be better to use the SP or Base Pointer instead. When there
602 // are variable sized objects, we can't reference off of the SP, so we
603 // reserve a Base Pointer.
604 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
605 // Conservatively estimate whether the negative offset from the frame
606 // pointer will be sufficient to reach. If a function has a smallish
607 // frame, it's less likely to have lots of spills and callee saved
608 // space, so it's all more likely to be within range of the frame pointer.
609 // If it's wrong, the scavenger will still enable access to work, it just
611 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
619 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
620 const MachineFrameInfo *MFI = MF.getFrameInfo();
621 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
622 // We can't realign the stack if:
623 // 1. Dynamic stack realignment is explicitly disabled,
624 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
625 // 3. There are VLAs in the function and the base pointer is disabled.
626 return (RealignStack && !AFI->isThumb1OnlyFunction() &&
627 (!MFI->hasVarSizedObjects() || EnableBasePointer));
630 bool ARMBaseRegisterInfo::
631 needsStackRealignment(const MachineFunction &MF) const {
632 const MachineFrameInfo *MFI = MF.getFrameInfo();
633 const Function *F = MF.getFunction();
634 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
635 bool requiresRealignment = ((MFI->getLocalFrameMaxAlign() > StackAlign) ||
636 F->hasFnAttr(Attribute::StackAlignment));
638 return requiresRealignment && canRealignStack(MF);
641 bool ARMBaseRegisterInfo::
642 cannotEliminateFrame(const MachineFunction &MF) const {
643 const MachineFrameInfo *MFI = MF.getFrameInfo();
644 if (DisableFramePointerElim(MF) && MFI->adjustsStack())
646 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
647 || needsStackRealignment(MF);
650 unsigned ARMBaseRegisterInfo::getRARegister() const {
655 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
656 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
663 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
664 llvm_unreachable("What is the exception register");
668 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
669 llvm_unreachable("What is the exception handler register");
673 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
674 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
677 int ARMBaseRegisterInfo::getLLVMRegNum(unsigned DwarfRegNo, bool isEH) const {
678 return ARMGenRegisterInfo::getLLVMRegNumFull(DwarfRegNo,0);
681 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
682 const MachineFunction &MF) const {
685 // Return 0 if either register of the pair is a special register.
694 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
697 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
699 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
771 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
772 const MachineFunction &MF) const {
775 // Return 0 if either register of the pair is a special register.
784 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
787 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
789 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
861 /// emitLoadConstPool - Emits a load from constpool to materialize the
862 /// specified immediate.
863 void ARMBaseRegisterInfo::
864 emitLoadConstPool(MachineBasicBlock &MBB,
865 MachineBasicBlock::iterator &MBBI,
867 unsigned DestReg, unsigned SubIdx, int Val,
868 ARMCC::CondCodes Pred,
869 unsigned PredReg, unsigned MIFlags) const {
870 MachineFunction &MF = *MBB.getParent();
871 MachineConstantPool *ConstantPool = MF.getConstantPool();
873 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
874 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
876 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
877 .addReg(DestReg, getDefRegState(true), SubIdx)
878 .addConstantPoolIndex(Idx)
879 .addImm(0).addImm(Pred).addReg(PredReg)
880 .setMIFlags(MIFlags);
883 bool ARMBaseRegisterInfo::
884 requiresRegisterScavenging(const MachineFunction &MF) const {
888 bool ARMBaseRegisterInfo::
889 requiresFrameIndexScavenging(const MachineFunction &MF) const {
893 bool ARMBaseRegisterInfo::
894 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
895 return EnableLocalStackAlloc;
899 emitSPUpdate(bool isARM,
900 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
901 DebugLoc dl, const ARMBaseInstrInfo &TII,
903 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
905 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
908 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
913 void ARMBaseRegisterInfo::
914 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
915 MachineBasicBlock::iterator I) const {
916 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
917 if (!TFI->hasReservedCallFrame(MF)) {
918 // If we have alloca, convert as follows:
919 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
920 // ADJCALLSTACKUP -> add, sp, sp, amount
921 MachineInstr *Old = I;
922 DebugLoc dl = Old->getDebugLoc();
923 unsigned Amount = Old->getOperand(0).getImm();
925 // We need to keep the stack aligned properly. To do this, we round the
926 // amount of space needed for the outgoing arguments up to the next
927 // alignment boundary.
928 unsigned Align = TFI->getStackAlignment();
929 Amount = (Amount+Align-1)/Align*Align;
931 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
932 assert(!AFI->isThumb1OnlyFunction() &&
933 "This eliminateCallFramePseudoInstr does not support Thumb1!");
934 bool isARM = !AFI->isThumbFunction();
936 // Replace the pseudo instruction with a new instruction...
937 unsigned Opc = Old->getOpcode();
938 int PIdx = Old->findFirstPredOperandIdx();
939 ARMCC::CondCodes Pred = (PIdx == -1)
940 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
941 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
942 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
943 unsigned PredReg = Old->getOperand(2).getReg();
944 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
946 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
947 unsigned PredReg = Old->getOperand(3).getReg();
948 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
949 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
956 int64_t ARMBaseRegisterInfo::
957 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
958 const TargetInstrDesc &Desc = MI->getDesc();
959 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
960 int64_t InstrOffs = 0;;
964 case ARMII::AddrModeT2_i8:
965 case ARMII::AddrModeT2_i12:
966 case ARMII::AddrMode_i12:
967 InstrOffs = MI->getOperand(Idx+1).getImm();
970 case ARMII::AddrMode5: {
972 const MachineOperand &OffOp = MI->getOperand(Idx+1);
973 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
974 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
975 InstrOffs = -InstrOffs;
979 case ARMII::AddrMode2: {
981 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
982 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
983 InstrOffs = -InstrOffs;
986 case ARMII::AddrMode3: {
988 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
989 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
990 InstrOffs = -InstrOffs;
993 case ARMII::AddrModeT1_s: {
995 InstrOffs = MI->getOperand(ImmIdx).getImm();
1000 llvm_unreachable("Unsupported addressing mode!");
1004 return InstrOffs * Scale;
1007 /// needsFrameBaseReg - Returns true if the instruction's frame index
1008 /// reference would be better served by a base register other than FP
1009 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
1010 /// references it should create new base registers for.
1011 bool ARMBaseRegisterInfo::
1012 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
1013 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
1014 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1017 // It's the load/store FI references that cause issues, as it can be difficult
1018 // to materialize the offset if it won't fit in the literal field. Estimate
1019 // based on the size of the local frame and some conservative assumptions
1020 // about the rest of the stack frame (note, this is pre-regalloc, so
1021 // we don't know everything for certain yet) whether this offset is likely
1022 // to be out of range of the immediate. Return true if so.
1024 // We only generate virtual base registers for loads and stores, so
1025 // return false for everything else.
1026 unsigned Opc = MI->getOpcode();
1028 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
1029 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
1030 case ARM::t2LDRi12: case ARM::t2LDRi8:
1031 case ARM::t2STRi12: case ARM::t2STRi8:
1032 case ARM::VLDRS: case ARM::VLDRD:
1033 case ARM::VSTRS: case ARM::VSTRD:
1034 case ARM::tSTRspi: case ARM::tLDRspi:
1035 if (ForceAllBaseRegAlloc)
1042 // Without a virtual base register, if the function has variable sized
1043 // objects, all fixed-size local references will be via the frame pointer,
1044 // Approximate the offset and see if it's legal for the instruction.
1045 // Note that the incoming offset is based on the SP value at function entry,
1046 // so it'll be negative.
1047 MachineFunction &MF = *MI->getParent()->getParent();
1048 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
1049 MachineFrameInfo *MFI = MF.getFrameInfo();
1050 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1052 // Estimate an offset from the frame pointer.
1053 // Conservatively assume all callee-saved registers get pushed. R4-R6
1054 // will be earlier than the FP, so we ignore those.
1056 int64_t FPOffset = Offset - 8;
1057 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
1058 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
1060 // Estimate an offset from the stack pointer.
1061 // The incoming offset is relating to the SP at the start of the function,
1062 // but when we access the local it'll be relative to the SP after local
1063 // allocation, so adjust our SP-relative offset by that allocation size.
1065 Offset += MFI->getLocalFrameSize();
1066 // Assume that we'll have at least some spill slots allocated.
1067 // FIXME: This is a total SWAG number. We should run some statistics
1068 // and pick a real one.
1069 Offset += 128; // 128 bytes of spill slots
1071 // If there is a frame pointer, try using it.
1072 // The FP is only available if there is no dynamic realignment. We
1073 // don't know for sure yet whether we'll need that, so we guess based
1074 // on whether there are any local variables that would trigger it.
1075 unsigned StackAlign = TFI->getStackAlignment();
1076 if (TFI->hasFP(MF) &&
1077 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
1078 if (isFrameOffsetLegal(MI, FPOffset))
1081 // If we can reference via the stack pointer, try that.
1082 // FIXME: This (and the code that resolves the references) can be improved
1083 // to only disallow SP relative references in the live range of
1084 // the VLA(s). In practice, it's unclear how much difference that
1085 // would make, but it may be worth doing.
1086 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
1089 // The offset likely isn't legal, we want to allocate a virtual base register.
1093 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
1094 /// be a pointer to FrameIdx at the beginning of the basic block.
1095 void ARMBaseRegisterInfo::
1096 materializeFrameBaseRegister(MachineBasicBlock *MBB,
1097 unsigned BaseReg, int FrameIdx,
1098 int64_t Offset) const {
1099 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
1100 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
1101 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
1103 MachineBasicBlock::iterator Ins = MBB->begin();
1104 DebugLoc DL; // Defaults to "unknown"
1105 if (Ins != MBB->end())
1106 DL = Ins->getDebugLoc();
1108 const TargetInstrDesc &TID = TII.get(ADDriOpc);
1109 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
1110 MRI.constrainRegClass(BaseReg, TID.OpInfo[0].getRegClass(this));
1112 MachineInstrBuilder MIB = BuildMI(*MBB, Ins, DL, TID, BaseReg)
1113 .addFrameIndex(FrameIdx).addImm(Offset);
1115 if (!AFI->isThumb1OnlyFunction())
1116 AddDefaultCC(AddDefaultPred(MIB));
1120 ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
1121 unsigned BaseReg, int64_t Offset) const {
1122 MachineInstr &MI = *I;
1123 MachineBasicBlock &MBB = *MI.getParent();
1124 MachineFunction &MF = *MBB.getParent();
1125 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1126 int Off = Offset; // ARM doesn't need the general 64-bit offsets
1129 assert(!AFI->isThumb1OnlyFunction() &&
1130 "This resolveFrameIndex does not support Thumb1!");
1132 while (!MI.getOperand(i).isFI()) {
1134 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1137 if (!AFI->isThumbFunction())
1138 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
1140 assert(AFI->isThumb2Function());
1141 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
1143 assert (Done && "Unable to resolve frame index!");
1146 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
1147 int64_t Offset) const {
1148 const TargetInstrDesc &Desc = MI->getDesc();
1149 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
1152 while (!MI->getOperand(i).isFI()) {
1154 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
1157 // AddrMode4 and AddrMode6 cannot handle any offset.
1158 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
1161 unsigned NumBits = 0;
1163 bool isSigned = true;
1165 case ARMII::AddrModeT2_i8:
1166 case ARMII::AddrModeT2_i12:
1167 // i8 supports only negative, and i12 supports only positive, so
1168 // based on Offset sign, consider the appropriate instruction
1177 case ARMII::AddrMode5:
1178 // VFP address mode.
1182 case ARMII::AddrMode_i12:
1183 case ARMII::AddrMode2:
1186 case ARMII::AddrMode3:
1189 case ARMII::AddrModeT1_s:
1195 llvm_unreachable("Unsupported addressing mode!");
1199 Offset += getFrameIndexInstrOffset(MI, i);
1200 // Make sure the offset is encodable for instructions that scale the
1202 if ((Offset & (Scale-1)) != 0)
1205 if (isSigned && Offset < 0)
1208 unsigned Mask = (1 << NumBits) - 1;
1209 if ((unsigned)Offset <= Mask * Scale)
1216 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1217 int SPAdj, RegScavenger *RS) const {
1219 MachineInstr &MI = *II;
1220 MachineBasicBlock &MBB = *MI.getParent();
1221 MachineFunction &MF = *MBB.getParent();
1222 const ARMFrameLowering *TFI =
1223 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
1224 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1225 assert(!AFI->isThumb1OnlyFunction() &&
1226 "This eliminateFrameIndex does not support Thumb1!");
1228 while (!MI.getOperand(i).isFI()) {
1230 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1233 int FrameIndex = MI.getOperand(i).getIndex();
1236 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
1238 // Special handling of dbg_value instructions.
1239 if (MI.isDebugValue()) {
1240 MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/);
1241 MI.getOperand(i+1).ChangeToImmediate(Offset);
1245 // Modify MI as necessary to handle as much of 'Offset' as possible
1247 if (!AFI->isThumbFunction())
1248 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1250 assert(AFI->isThumb2Function());
1251 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1256 // If we get here, the immediate doesn't fit into the instruction. We folded
1257 // as much as possible above, handle the rest, providing a register that is
1260 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1261 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1262 "This code isn't needed if offset already handled!");
1264 unsigned ScratchReg = 0;
1265 int PIdx = MI.findFirstPredOperandIdx();
1266 ARMCC::CondCodes Pred = (PIdx == -1)
1267 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1268 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1270 // Must be addrmode4/6.
1271 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1273 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1274 if (!AFI->isThumbFunction())
1275 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1276 Offset, Pred, PredReg, TII);
1278 assert(AFI->isThumb2Function());
1279 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1280 Offset, Pred, PredReg, TII);
1282 // Update the original instruction to use the scratch register.
1283 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1284 if (MI.getOpcode() == ARM::t2ADDrSPi)
1285 MI.setDesc(TII.get(ARM::t2ADDri));
1286 else if (MI.getOpcode() == ARM::t2SUBrSPi)
1287 MI.setDesc(TII.get(ARM::t2SUBri));
1291 #include "ARMGenRegisterInfo.inc"