1 //===-- ARMBaseRegisterInfo.cpp - ARM Register Information ----------------===//
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 "ARMBaseInstrInfo.h"
16 #include "ARMBaseRegisterInfo.h"
17 #include "ARMFrameLowering.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "MCTargetDesc/ARMAddressingModes.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/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterScavenging.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetFrameLowering.h"
36 #include "llvm/Target/TargetMachine.h"
37 #include "llvm/Target/TargetOptions.h"
38 #include "llvm/ADT/BitVector.h"
39 #include "llvm/ADT/SmallVector.h"
40 #include "llvm/Support/CommandLine.h"
42 #define GET_REGINFO_TARGET_DESC
43 #include "ARMGenRegisterInfo.inc"
48 ForceAllBaseRegAlloc("arm-force-base-reg-alloc", cl::Hidden, cl::init(false),
49 cl::desc("Force use of virtual base registers for stack load/store"));
51 EnableLocalStackAlloc("enable-local-stack-alloc", cl::init(true), cl::Hidden,
52 cl::desc("Enable pre-regalloc stack frame index allocation"));
54 EnableBasePointer("arm-use-base-pointer", cl::Hidden, cl::init(true),
55 cl::desc("Enable use of a base pointer for complex stack frames"));
57 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
58 const ARMSubtarget &sti)
59 : ARMGenRegisterInfo(ARM::LR), TII(tii), STI(sti),
60 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11),
65 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
66 return (STI.isTargetIOS()) ? CSR_iOS_SaveList : CSR_AAPCS_SaveList;
70 ARMBaseRegisterInfo::getCallPreservedMask(CallingConv::ID) const {
71 return (STI.isTargetIOS()) ? CSR_iOS_RegMask : CSR_AAPCS_RegMask;
74 BitVector ARMBaseRegisterInfo::
75 getReservedRegs(const MachineFunction &MF) const {
76 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
78 // FIXME: avoid re-calculating this every time.
79 BitVector Reserved(getNumRegs());
80 Reserved.set(ARM::SP);
81 Reserved.set(ARM::PC);
83 Reserved.set(FramePtr);
84 if (hasBasePointer(MF))
85 Reserved.set(BasePtr);
86 // Some targets reserve R9.
87 if (STI.isR9Reserved())
88 Reserved.set(ARM::R9);
89 // Reserve D16-D31 if the subtarget doesn't support them.
90 if (!STI.hasVFP3() || STI.hasD16()) {
91 assert(ARM::D31 == ARM::D16 + 15);
92 for (unsigned i = 0; i != 16; ++i)
93 Reserved.set(ARM::D16 + i);
98 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
100 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
108 if (hasBasePointer(MF))
113 if (FramePtr == Reg && TFI->hasFP(MF))
117 return STI.isR9Reserved();
124 ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
125 SmallVectorImpl<unsigned> &SubIndices,
126 unsigned &NewSubIdx) const {
128 unsigned Size = RC->getSize() * 8;
132 NewSubIdx = 0; // Whole register.
133 unsigned NumRegs = SubIndices.size();
135 // 8 D registers -> 1 QQQQ register.
136 return (Size == 512 &&
137 SubIndices[0] == ARM::dsub_0 &&
138 SubIndices[1] == ARM::dsub_1 &&
139 SubIndices[2] == ARM::dsub_2 &&
140 SubIndices[3] == ARM::dsub_3 &&
141 SubIndices[4] == ARM::dsub_4 &&
142 SubIndices[5] == ARM::dsub_5 &&
143 SubIndices[6] == ARM::dsub_6 &&
144 SubIndices[7] == ARM::dsub_7);
145 } else if (NumRegs == 4) {
146 if (SubIndices[0] == ARM::qsub_0) {
147 // 4 Q registers -> 1 QQQQ register.
148 return (Size == 512 &&
149 SubIndices[1] == ARM::qsub_1 &&
150 SubIndices[2] == ARM::qsub_2 &&
151 SubIndices[3] == ARM::qsub_3);
152 } else if (SubIndices[0] == ARM::dsub_0) {
153 // 4 D registers -> 1 QQ register.
155 SubIndices[1] == ARM::dsub_1 &&
156 SubIndices[2] == ARM::dsub_2 &&
157 SubIndices[3] == ARM::dsub_3) {
159 NewSubIdx = ARM::qqsub_0;
162 } else if (SubIndices[0] == ARM::dsub_4) {
163 // 4 D registers -> 1 QQ register (2nd).
165 SubIndices[1] == ARM::dsub_5 &&
166 SubIndices[2] == ARM::dsub_6 &&
167 SubIndices[3] == ARM::dsub_7) {
168 NewSubIdx = ARM::qqsub_1;
171 } else if (SubIndices[0] == ARM::ssub_0) {
172 // 4 S registers -> 1 Q register.
174 SubIndices[1] == ARM::ssub_1 &&
175 SubIndices[2] == ARM::ssub_2 &&
176 SubIndices[3] == ARM::ssub_3) {
178 NewSubIdx = ARM::qsub_0;
182 } else if (NumRegs == 2) {
183 if (SubIndices[0] == ARM::qsub_0) {
184 // 2 Q registers -> 1 QQ register.
185 if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
187 NewSubIdx = ARM::qqsub_0;
190 } else if (SubIndices[0] == ARM::qsub_2) {
191 // 2 Q registers -> 1 QQ register (2nd).
192 if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
193 NewSubIdx = ARM::qqsub_1;
196 } else if (SubIndices[0] == ARM::dsub_0) {
197 // 2 D registers -> 1 Q register.
198 if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
200 NewSubIdx = ARM::qsub_0;
203 } else if (SubIndices[0] == ARM::dsub_2) {
204 // 2 D registers -> 1 Q register (2nd).
205 if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
206 NewSubIdx = ARM::qsub_1;
209 } else if (SubIndices[0] == ARM::dsub_4) {
210 // 2 D registers -> 1 Q register (3rd).
211 if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
212 NewSubIdx = ARM::qsub_2;
215 } else if (SubIndices[0] == ARM::dsub_6) {
216 // 2 D registers -> 1 Q register (3rd).
217 if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
218 NewSubIdx = ARM::qsub_3;
221 } else if (SubIndices[0] == ARM::ssub_0) {
222 // 2 S registers -> 1 D register.
223 if (SubIndices[1] == ARM::ssub_1) {
225 NewSubIdx = ARM::dsub_0;
228 } else if (SubIndices[0] == ARM::ssub_2) {
229 // 2 S registers -> 1 D register (2nd).
230 if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
231 NewSubIdx = ARM::dsub_1;
239 const TargetRegisterClass*
240 ARMBaseRegisterInfo::getLargestLegalSuperClass(const TargetRegisterClass *RC)
242 const TargetRegisterClass *Super = RC;
243 TargetRegisterClass::sc_iterator I = RC->getSuperClasses();
245 switch (Super->getID()) {
246 case ARM::GPRRegClassID:
247 case ARM::SPRRegClassID:
248 case ARM::DPRRegClassID:
249 case ARM::QPRRegClassID:
250 case ARM::QQPRRegClassID:
251 case ARM::QQQQPRRegClassID:
259 const TargetRegisterClass *
260 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
261 return ARM::GPRRegisterClass;
264 const TargetRegisterClass *
265 ARMBaseRegisterInfo::getCrossCopyRegClass(const TargetRegisterClass *RC) const {
266 if (RC == &ARM::CCRRegClass)
267 return 0; // Can't copy CCR registers.
272 ARMBaseRegisterInfo::getRegPressureLimit(const TargetRegisterClass *RC,
273 MachineFunction &MF) const {
274 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
276 switch (RC->getID()) {
279 case ARM::tGPRRegClassID:
280 return TFI->hasFP(MF) ? 4 : 5;
281 case ARM::GPRRegClassID: {
282 unsigned FP = TFI->hasFP(MF) ? 1 : 0;
283 return 10 - FP - (STI.isR9Reserved() ? 1 : 0);
285 case ARM::SPRRegClassID: // Currently not used as 'rep' register class.
286 case ARM::DPRRegClassID:
291 /// getRawAllocationOrder - Returns the register allocation order for a
292 /// specified register class with a target-dependent hint.
294 ARMBaseRegisterInfo::getRawAllocationOrder(const TargetRegisterClass *RC,
295 unsigned HintType, unsigned HintReg,
296 const MachineFunction &MF) const {
297 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
298 // Alternative register allocation orders when favoring even / odd registers
299 // of register pairs.
301 // No FP, R9 is available.
302 static const unsigned GPREven1[] = {
303 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
304 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
307 static const unsigned GPROdd1[] = {
308 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
309 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
313 // FP is R7, R9 is available.
314 static const unsigned GPREven2[] = {
315 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
316 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
319 static const unsigned GPROdd2[] = {
320 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
321 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
325 // FP is R11, R9 is available.
326 static const unsigned GPREven3[] = {
327 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
328 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
331 static const unsigned GPROdd3[] = {
332 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
333 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
337 // No FP, R9 is not available.
338 static const unsigned GPREven4[] = {
339 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
340 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
343 static const unsigned GPROdd4[] = {
344 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
345 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
349 // FP is R7, R9 is not available.
350 static const unsigned GPREven5[] = {
351 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
352 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
355 static const unsigned GPROdd5[] = {
356 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
357 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
361 // FP is R11, R9 is not available.
362 static const unsigned GPREven6[] = {
363 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
364 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
366 static const unsigned GPROdd6[] = {
367 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
368 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
371 // We only support even/odd hints for GPR and rGPR.
372 if (RC != ARM::GPRRegisterClass && RC != ARM::rGPRRegisterClass)
373 return RC->getRawAllocationOrder(MF);
375 if (HintType == ARMRI::RegPairEven) {
376 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
377 // It's no longer possible to fulfill this hint. Return the default
379 return RC->getRawAllocationOrder(MF);
381 if (!TFI->hasFP(MF)) {
382 if (!STI.isR9Reserved())
383 return makeArrayRef(GPREven1);
385 return makeArrayRef(GPREven4);
386 } else if (FramePtr == ARM::R7) {
387 if (!STI.isR9Reserved())
388 return makeArrayRef(GPREven2);
390 return makeArrayRef(GPREven5);
391 } else { // FramePtr == ARM::R11
392 if (!STI.isR9Reserved())
393 return makeArrayRef(GPREven3);
395 return makeArrayRef(GPREven6);
397 } else if (HintType == ARMRI::RegPairOdd) {
398 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
399 // It's no longer possible to fulfill this hint. Return the default
401 return RC->getRawAllocationOrder(MF);
403 if (!TFI->hasFP(MF)) {
404 if (!STI.isR9Reserved())
405 return makeArrayRef(GPROdd1);
407 return makeArrayRef(GPROdd4);
408 } else if (FramePtr == ARM::R7) {
409 if (!STI.isR9Reserved())
410 return makeArrayRef(GPROdd2);
412 return makeArrayRef(GPROdd5);
413 } else { // FramePtr == ARM::R11
414 if (!STI.isR9Reserved())
415 return makeArrayRef(GPROdd3);
417 return makeArrayRef(GPROdd6);
420 return RC->getRawAllocationOrder(MF);
423 /// ResolveRegAllocHint - Resolves the specified register allocation hint
424 /// to a physical register. Returns the physical register if it is successful.
426 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
427 const MachineFunction &MF) const {
428 if (Reg == 0 || !isPhysicalRegister(Reg))
432 else if (Type == (unsigned)ARMRI::RegPairOdd)
434 return getRegisterPairOdd(Reg, MF);
435 else if (Type == (unsigned)ARMRI::RegPairEven)
437 return getRegisterPairEven(Reg, MF);
442 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
443 MachineFunction &MF) const {
444 MachineRegisterInfo *MRI = &MF.getRegInfo();
445 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
446 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
447 Hint.first == (unsigned)ARMRI::RegPairEven) &&
448 TargetRegisterInfo::isVirtualRegister(Hint.second)) {
449 // If 'Reg' is one of the even / odd register pair and it's now changed
450 // (e.g. coalesced) into a different register. The other register of the
451 // pair allocation hint must be updated to reflect the relationship
453 unsigned OtherReg = Hint.second;
454 Hint = MRI->getRegAllocationHint(OtherReg);
455 if (Hint.second == Reg)
456 // Make sure the pair has not already divorced.
457 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
462 ARMBaseRegisterInfo::avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
463 // CortexA9 has a Write-after-write hazard for NEON registers.
464 if (!STI.isCortexA9())
467 switch (RC->getID()) {
468 case ARM::DPRRegClassID:
469 case ARM::DPR_8RegClassID:
470 case ARM::DPR_VFP2RegClassID:
471 case ARM::QPRRegClassID:
472 case ARM::QPR_8RegClassID:
473 case ARM::QPR_VFP2RegClassID:
474 case ARM::SPRRegClassID:
475 case ARM::SPR_8RegClassID:
476 // Avoid reusing S, D, and Q registers.
477 // Don't increase register pressure for QQ and QQQQ.
484 bool ARMBaseRegisterInfo::hasBasePointer(const MachineFunction &MF) const {
485 const MachineFrameInfo *MFI = MF.getFrameInfo();
486 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
487 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
489 if (!EnableBasePointer)
492 // When outgoing call frames are so large that we adjust the stack pointer
493 // around the call, we can no longer use the stack pointer to reach the
494 // emergency spill slot.
495 if (needsStackRealignment(MF) && (MFI->hasVarSizedObjects() ||
496 !TFI->hasReservedCallFrame(MF)))
499 // Thumb has trouble with negative offsets from the FP. Thumb2 has a limited
500 // negative range for ldr/str (255), and thumb1 is positive offsets only.
501 // It's going to be better to use the SP or Base Pointer instead. When there
502 // are variable sized objects, we can't reference off of the SP, so we
503 // reserve a Base Pointer.
504 if (AFI->isThumbFunction() && MFI->hasVarSizedObjects()) {
505 // Conservatively estimate whether the negative offset from the frame
506 // pointer will be sufficient to reach. If a function has a smallish
507 // frame, it's less likely to have lots of spills and callee saved
508 // space, so it's all more likely to be within range of the frame pointer.
509 // If it's wrong, the scavenger will still enable access to work, it just
511 if (AFI->isThumb2Function() && MFI->getLocalFrameSize() < 128)
519 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
520 const MachineFrameInfo *MFI = MF.getFrameInfo();
521 const MachineRegisterInfo *MRI = &MF.getRegInfo();
522 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
523 // We can't realign the stack if:
524 // 1. Dynamic stack realignment is explicitly disabled,
525 // 2. This is a Thumb1 function (it's not useful, so we don't bother), or
526 // 3. There are VLAs in the function and the base pointer is disabled.
527 if (!MF.getTarget().Options.RealignStack)
529 if (AFI->isThumb1OnlyFunction())
531 // Stack realignment requires a frame pointer. If we already started
532 // register allocation with frame pointer elimination, it is too late now.
533 if (!MRI->canReserveReg(FramePtr))
535 // We may also need a base pointer if there are dynamic allocas.
536 if (!MFI->hasVarSizedObjects())
538 if (!EnableBasePointer)
540 // A base pointer is required and allowed. Check that it isn't too late to
542 return MRI->canReserveReg(BasePtr);
545 bool ARMBaseRegisterInfo::
546 needsStackRealignment(const MachineFunction &MF) const {
547 const MachineFrameInfo *MFI = MF.getFrameInfo();
548 const Function *F = MF.getFunction();
549 unsigned StackAlign = MF.getTarget().getFrameLowering()->getStackAlignment();
550 bool requiresRealignment = ((MFI->getMaxAlignment() > StackAlign) ||
551 F->hasFnAttr(Attribute::StackAlignment));
553 return requiresRealignment && canRealignStack(MF);
556 bool ARMBaseRegisterInfo::
557 cannotEliminateFrame(const MachineFunction &MF) const {
558 const MachineFrameInfo *MFI = MF.getFrameInfo();
559 if (MF.getTarget().Options.DisableFramePointerElim(MF) && MFI->adjustsStack())
561 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
562 || needsStackRealignment(MF);
566 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
567 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
574 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
575 llvm_unreachable("What is the exception register");
578 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
579 llvm_unreachable("What is the exception handler register");
582 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
583 const MachineFunction &MF) const {
586 // Return 0 if either register of the pair is a special register.
588 case ARM::R1: return ARM::R0;
589 case ARM::R3: return ARM::R2;
590 case ARM::R5: return ARM::R4;
592 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
594 case ARM::R9: return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
595 case ARM::R11: return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
597 case ARM::S1: return ARM::S0;
598 case ARM::S3: return ARM::S2;
599 case ARM::S5: return ARM::S4;
600 case ARM::S7: return ARM::S6;
601 case ARM::S9: return ARM::S8;
602 case ARM::S11: return ARM::S10;
603 case ARM::S13: return ARM::S12;
604 case ARM::S15: return ARM::S14;
605 case ARM::S17: return ARM::S16;
606 case ARM::S19: return ARM::S18;
607 case ARM::S21: return ARM::S20;
608 case ARM::S23: return ARM::S22;
609 case ARM::S25: return ARM::S24;
610 case ARM::S27: return ARM::S26;
611 case ARM::S29: return ARM::S28;
612 case ARM::S31: return ARM::S30;
614 case ARM::D1: return ARM::D0;
615 case ARM::D3: return ARM::D2;
616 case ARM::D5: return ARM::D4;
617 case ARM::D7: return ARM::D6;
618 case ARM::D9: return ARM::D8;
619 case ARM::D11: return ARM::D10;
620 case ARM::D13: return ARM::D12;
621 case ARM::D15: return ARM::D14;
622 case ARM::D17: return ARM::D16;
623 case ARM::D19: return ARM::D18;
624 case ARM::D21: return ARM::D20;
625 case ARM::D23: return ARM::D22;
626 case ARM::D25: return ARM::D24;
627 case ARM::D27: return ARM::D26;
628 case ARM::D29: return ARM::D28;
629 case ARM::D31: return ARM::D30;
635 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
636 const MachineFunction &MF) const {
639 // Return 0 if either register of the pair is a special register.
641 case ARM::R0: return ARM::R1;
642 case ARM::R2: return ARM::R3;
643 case ARM::R4: return ARM::R5;
645 return (isReservedReg(MF, ARM::R7) || isReservedReg(MF, ARM::R6))
647 case ARM::R8: return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
648 case ARM::R10: return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
650 case ARM::S0: return ARM::S1;
651 case ARM::S2: return ARM::S3;
652 case ARM::S4: return ARM::S5;
653 case ARM::S6: return ARM::S7;
654 case ARM::S8: return ARM::S9;
655 case ARM::S10: return ARM::S11;
656 case ARM::S12: return ARM::S13;
657 case ARM::S14: return ARM::S15;
658 case ARM::S16: return ARM::S17;
659 case ARM::S18: return ARM::S19;
660 case ARM::S20: return ARM::S21;
661 case ARM::S22: return ARM::S23;
662 case ARM::S24: return ARM::S25;
663 case ARM::S26: return ARM::S27;
664 case ARM::S28: return ARM::S29;
665 case ARM::S30: return ARM::S31;
667 case ARM::D0: return ARM::D1;
668 case ARM::D2: return ARM::D3;
669 case ARM::D4: return ARM::D5;
670 case ARM::D6: return ARM::D7;
671 case ARM::D8: return ARM::D9;
672 case ARM::D10: return ARM::D11;
673 case ARM::D12: return ARM::D13;
674 case ARM::D14: return ARM::D15;
675 case ARM::D16: return ARM::D17;
676 case ARM::D18: return ARM::D19;
677 case ARM::D20: return ARM::D21;
678 case ARM::D22: return ARM::D23;
679 case ARM::D24: return ARM::D25;
680 case ARM::D26: return ARM::D27;
681 case ARM::D28: return ARM::D29;
682 case ARM::D30: return ARM::D31;
688 /// emitLoadConstPool - Emits a load from constpool to materialize the
689 /// specified immediate.
690 void ARMBaseRegisterInfo::
691 emitLoadConstPool(MachineBasicBlock &MBB,
692 MachineBasicBlock::iterator &MBBI,
694 unsigned DestReg, unsigned SubIdx, int Val,
695 ARMCC::CondCodes Pred,
696 unsigned PredReg, unsigned MIFlags) const {
697 MachineFunction &MF = *MBB.getParent();
698 MachineConstantPool *ConstantPool = MF.getConstantPool();
700 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
701 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
703 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
704 .addReg(DestReg, getDefRegState(true), SubIdx)
705 .addConstantPoolIndex(Idx)
706 .addImm(0).addImm(Pred).addReg(PredReg)
707 .setMIFlags(MIFlags);
710 bool ARMBaseRegisterInfo::
711 requiresRegisterScavenging(const MachineFunction &MF) const {
715 bool ARMBaseRegisterInfo::
716 requiresFrameIndexScavenging(const MachineFunction &MF) const {
720 bool ARMBaseRegisterInfo::
721 requiresVirtualBaseRegisters(const MachineFunction &MF) const {
722 return EnableLocalStackAlloc;
726 emitSPUpdate(bool isARM,
727 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
728 DebugLoc dl, const ARMBaseInstrInfo &TII,
730 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
732 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
735 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
740 void ARMBaseRegisterInfo::
741 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
742 MachineBasicBlock::iterator I) const {
743 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
744 if (!TFI->hasReservedCallFrame(MF)) {
745 // If we have alloca, convert as follows:
746 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
747 // ADJCALLSTACKUP -> add, sp, sp, amount
748 MachineInstr *Old = I;
749 DebugLoc dl = Old->getDebugLoc();
750 unsigned Amount = Old->getOperand(0).getImm();
752 // We need to keep the stack aligned properly. To do this, we round the
753 // amount of space needed for the outgoing arguments up to the next
754 // alignment boundary.
755 unsigned Align = TFI->getStackAlignment();
756 Amount = (Amount+Align-1)/Align*Align;
758 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
759 assert(!AFI->isThumb1OnlyFunction() &&
760 "This eliminateCallFramePseudoInstr does not support Thumb1!");
761 bool isARM = !AFI->isThumbFunction();
763 // Replace the pseudo instruction with a new instruction...
764 unsigned Opc = Old->getOpcode();
765 int PIdx = Old->findFirstPredOperandIdx();
766 ARMCC::CondCodes Pred = (PIdx == -1)
767 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
768 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
769 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
770 unsigned PredReg = Old->getOperand(2).getReg();
771 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
773 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
774 unsigned PredReg = Old->getOperand(3).getReg();
775 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
776 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
783 int64_t ARMBaseRegisterInfo::
784 getFrameIndexInstrOffset(const MachineInstr *MI, int Idx) const {
785 const MCInstrDesc &Desc = MI->getDesc();
786 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
787 int64_t InstrOffs = 0;
791 case ARMII::AddrModeT2_i8:
792 case ARMII::AddrModeT2_i12:
793 case ARMII::AddrMode_i12:
794 InstrOffs = MI->getOperand(Idx+1).getImm();
797 case ARMII::AddrMode5: {
799 const MachineOperand &OffOp = MI->getOperand(Idx+1);
800 InstrOffs = ARM_AM::getAM5Offset(OffOp.getImm());
801 if (ARM_AM::getAM5Op(OffOp.getImm()) == ARM_AM::sub)
802 InstrOffs = -InstrOffs;
806 case ARMII::AddrMode2: {
808 InstrOffs = ARM_AM::getAM2Offset(MI->getOperand(ImmIdx).getImm());
809 if (ARM_AM::getAM2Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
810 InstrOffs = -InstrOffs;
813 case ARMII::AddrMode3: {
815 InstrOffs = ARM_AM::getAM3Offset(MI->getOperand(ImmIdx).getImm());
816 if (ARM_AM::getAM3Op(MI->getOperand(ImmIdx).getImm()) == ARM_AM::sub)
817 InstrOffs = -InstrOffs;
820 case ARMII::AddrModeT1_s: {
822 InstrOffs = MI->getOperand(ImmIdx).getImm();
827 llvm_unreachable("Unsupported addressing mode!");
830 return InstrOffs * Scale;
833 /// needsFrameBaseReg - Returns true if the instruction's frame index
834 /// reference would be better served by a base register other than FP
835 /// or SP. Used by LocalStackFrameAllocation to determine which frame index
836 /// references it should create new base registers for.
837 bool ARMBaseRegisterInfo::
838 needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
839 for (unsigned i = 0; !MI->getOperand(i).isFI(); ++i) {
840 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
843 // It's the load/store FI references that cause issues, as it can be difficult
844 // to materialize the offset if it won't fit in the literal field. Estimate
845 // based on the size of the local frame and some conservative assumptions
846 // about the rest of the stack frame (note, this is pre-regalloc, so
847 // we don't know everything for certain yet) whether this offset is likely
848 // to be out of range of the immediate. Return true if so.
850 // We only generate virtual base registers for loads and stores, so
851 // return false for everything else.
852 unsigned Opc = MI->getOpcode();
854 case ARM::LDRi12: case ARM::LDRH: case ARM::LDRBi12:
855 case ARM::STRi12: case ARM::STRH: case ARM::STRBi12:
856 case ARM::t2LDRi12: case ARM::t2LDRi8:
857 case ARM::t2STRi12: case ARM::t2STRi8:
858 case ARM::VLDRS: case ARM::VLDRD:
859 case ARM::VSTRS: case ARM::VSTRD:
860 case ARM::tSTRspi: case ARM::tLDRspi:
861 if (ForceAllBaseRegAlloc)
868 // Without a virtual base register, if the function has variable sized
869 // objects, all fixed-size local references will be via the frame pointer,
870 // Approximate the offset and see if it's legal for the instruction.
871 // Note that the incoming offset is based on the SP value at function entry,
872 // so it'll be negative.
873 MachineFunction &MF = *MI->getParent()->getParent();
874 const TargetFrameLowering *TFI = MF.getTarget().getFrameLowering();
875 MachineFrameInfo *MFI = MF.getFrameInfo();
876 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
878 // Estimate an offset from the frame pointer.
879 // Conservatively assume all callee-saved registers get pushed. R4-R6
880 // will be earlier than the FP, so we ignore those.
882 int64_t FPOffset = Offset - 8;
883 // ARM and Thumb2 functions also need to consider R8-R11 and D8-D15
884 if (!AFI->isThumbFunction() || !AFI->isThumb1OnlyFunction())
886 // Estimate an offset from the stack pointer.
887 // The incoming offset is relating to the SP at the start of the function,
888 // but when we access the local it'll be relative to the SP after local
889 // allocation, so adjust our SP-relative offset by that allocation size.
891 Offset += MFI->getLocalFrameSize();
892 // Assume that we'll have at least some spill slots allocated.
893 // FIXME: This is a total SWAG number. We should run some statistics
894 // and pick a real one.
895 Offset += 128; // 128 bytes of spill slots
897 // If there is a frame pointer, try using it.
898 // The FP is only available if there is no dynamic realignment. We
899 // don't know for sure yet whether we'll need that, so we guess based
900 // on whether there are any local variables that would trigger it.
901 unsigned StackAlign = TFI->getStackAlignment();
902 if (TFI->hasFP(MF) &&
903 !((MFI->getLocalFrameMaxAlign() > StackAlign) && canRealignStack(MF))) {
904 if (isFrameOffsetLegal(MI, FPOffset))
907 // If we can reference via the stack pointer, try that.
908 // FIXME: This (and the code that resolves the references) can be improved
909 // to only disallow SP relative references in the live range of
910 // the VLA(s). In practice, it's unclear how much difference that
911 // would make, but it may be worth doing.
912 if (!MFI->hasVarSizedObjects() && isFrameOffsetLegal(MI, Offset))
915 // The offset likely isn't legal, we want to allocate a virtual base register.
919 /// materializeFrameBaseRegister - Insert defining instruction(s) for BaseReg to
920 /// be a pointer to FrameIdx at the beginning of the basic block.
921 void ARMBaseRegisterInfo::
922 materializeFrameBaseRegister(MachineBasicBlock *MBB,
923 unsigned BaseReg, int FrameIdx,
924 int64_t Offset) const {
925 ARMFunctionInfo *AFI = MBB->getParent()->getInfo<ARMFunctionInfo>();
926 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri :
927 (AFI->isThumb1OnlyFunction() ? ARM::tADDrSPi : ARM::t2ADDri);
929 MachineBasicBlock::iterator Ins = MBB->begin();
930 DebugLoc DL; // Defaults to "unknown"
931 if (Ins != MBB->end())
932 DL = Ins->getDebugLoc();
934 const MCInstrDesc &MCID = TII.get(ADDriOpc);
935 MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
936 MRI.constrainRegClass(BaseReg, TII.getRegClass(MCID, 0, this));
938 MachineInstrBuilder MIB = AddDefaultPred(BuildMI(*MBB, Ins, DL, MCID, BaseReg)
939 .addFrameIndex(FrameIdx).addImm(Offset));
941 if (!AFI->isThumb1OnlyFunction())
946 ARMBaseRegisterInfo::resolveFrameIndex(MachineBasicBlock::iterator I,
947 unsigned BaseReg, int64_t Offset) const {
948 MachineInstr &MI = *I;
949 MachineBasicBlock &MBB = *MI.getParent();
950 MachineFunction &MF = *MBB.getParent();
951 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
952 int Off = Offset; // ARM doesn't need the general 64-bit offsets
955 assert(!AFI->isThumb1OnlyFunction() &&
956 "This resolveFrameIndex does not support Thumb1!");
958 while (!MI.getOperand(i).isFI()) {
960 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
963 if (!AFI->isThumbFunction())
964 Done = rewriteARMFrameIndex(MI, i, BaseReg, Off, TII);
966 assert(AFI->isThumb2Function());
967 Done = rewriteT2FrameIndex(MI, i, BaseReg, Off, TII);
969 assert (Done && "Unable to resolve frame index!");
973 bool ARMBaseRegisterInfo::isFrameOffsetLegal(const MachineInstr *MI,
974 int64_t Offset) const {
975 const MCInstrDesc &Desc = MI->getDesc();
976 unsigned AddrMode = (Desc.TSFlags & ARMII::AddrModeMask);
979 while (!MI->getOperand(i).isFI()) {
981 assert(i < MI->getNumOperands() &&"Instr doesn't have FrameIndex operand!");
984 // AddrMode4 and AddrMode6 cannot handle any offset.
985 if (AddrMode == ARMII::AddrMode4 || AddrMode == ARMII::AddrMode6)
988 unsigned NumBits = 0;
990 bool isSigned = true;
992 case ARMII::AddrModeT2_i8:
993 case ARMII::AddrModeT2_i12:
994 // i8 supports only negative, and i12 supports only positive, so
995 // based on Offset sign, consider the appropriate instruction
1004 case ARMII::AddrMode5:
1005 // VFP address mode.
1009 case ARMII::AddrMode_i12:
1010 case ARMII::AddrMode2:
1013 case ARMII::AddrMode3:
1016 case ARMII::AddrModeT1_s:
1022 llvm_unreachable("Unsupported addressing mode!");
1025 Offset += getFrameIndexInstrOffset(MI, i);
1026 // Make sure the offset is encodable for instructions that scale the
1028 if ((Offset & (Scale-1)) != 0)
1031 if (isSigned && Offset < 0)
1034 unsigned Mask = (1 << NumBits) - 1;
1035 if ((unsigned)Offset <= Mask * Scale)
1042 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1043 int SPAdj, RegScavenger *RS) const {
1045 MachineInstr &MI = *II;
1046 MachineBasicBlock &MBB = *MI.getParent();
1047 MachineFunction &MF = *MBB.getParent();
1048 const ARMFrameLowering *TFI =
1049 static_cast<const ARMFrameLowering*>(MF.getTarget().getFrameLowering());
1050 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1051 assert(!AFI->isThumb1OnlyFunction() &&
1052 "This eliminateFrameIndex does not support Thumb1!");
1054 while (!MI.getOperand(i).isFI()) {
1056 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1059 int FrameIndex = MI.getOperand(i).getIndex();
1062 int Offset = TFI->ResolveFrameIndexReference(MF, FrameIndex, FrameReg, SPAdj);
1064 // PEI::scavengeFrameVirtualRegs() cannot accurately track SPAdj because the
1065 // call frame setup/destroy instructions have already been eliminated. That
1066 // means the stack pointer cannot be used to access the emergency spill slot
1067 // when !hasReservedCallFrame().
1069 if (RS && FrameReg == ARM::SP && FrameIndex == RS->getScavengingFrameIndex()){
1070 assert(TFI->hasReservedCallFrame(MF) &&
1071 "Cannot use SP to access the emergency spill slot in "
1072 "functions without a reserved call frame");
1073 assert(!MF.getFrameInfo()->hasVarSizedObjects() &&
1074 "Cannot use SP to access the emergency spill slot in "
1075 "functions with variable sized frame objects");
1079 // Special handling of dbg_value instructions.
1080 if (MI.isDebugValue()) {
1081 MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/);
1082 MI.getOperand(i+1).ChangeToImmediate(Offset);
1086 // Modify MI as necessary to handle as much of 'Offset' as possible
1088 if (!AFI->isThumbFunction())
1089 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1091 assert(AFI->isThumb2Function());
1092 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1097 // If we get here, the immediate doesn't fit into the instruction. We folded
1098 // as much as possible above, handle the rest, providing a register that is
1101 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1102 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1103 "This code isn't needed if offset already handled!");
1105 unsigned ScratchReg = 0;
1106 int PIdx = MI.findFirstPredOperandIdx();
1107 ARMCC::CondCodes Pred = (PIdx == -1)
1108 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1109 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1111 // Must be addrmode4/6.
1112 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1114 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1115 if (!AFI->isThumbFunction())
1116 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1117 Offset, Pred, PredReg, TII);
1119 assert(AFI->isThumb2Function());
1120 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1121 Offset, Pred, PredReg, TII);
1123 // Update the original instruction to use the scratch register.
1124 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);