1 //===- ARMBaseRegisterInfo.cpp - ARM Register Information -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the base ARM implementation of TargetRegisterInfo class.
12 //===----------------------------------------------------------------------===//
15 #include "ARMAddressingModes.h"
16 #include "ARMBaseInstrInfo.h"
17 #include "ARMBaseRegisterInfo.h"
18 #include "ARMInstrInfo.h"
19 #include "ARMMachineFunctionInfo.h"
20 #include "ARMSubtarget.h"
21 #include "llvm/Constants.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/Function.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstrBuilder.h"
29 #include "llvm/CodeGen/MachineLocation.h"
30 #include "llvm/CodeGen/MachineRegisterInfo.h"
31 #include "llvm/CodeGen/RegisterScavenging.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Support/raw_ostream.h"
35 #include "llvm/Target/TargetFrameInfo.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"
44 ReuseFrameIndexVals("arm-reuse-frame-index-vals", cl::Hidden, cl::init(true),
45 cl::desc("Reuse repeated frame index values"));
50 unsigned ARMBaseRegisterInfo::getRegisterNumbering(unsigned RegEnum,
58 llvm_unreachable("Unknown ARM register!");
59 case R0: case D0: case Q0: return 0;
60 case R1: case D1: case Q1: return 1;
61 case R2: case D2: case Q2: return 2;
62 case R3: case D3: case Q3: return 3;
63 case R4: case D4: case Q4: return 4;
64 case R5: case D5: case Q5: return 5;
65 case R6: case D6: case Q6: return 6;
66 case R7: case D7: case Q7: return 7;
67 case R8: case D8: case Q8: return 8;
68 case R9: case D9: case Q9: return 9;
69 case R10: case D10: case Q10: return 10;
70 case R11: case D11: case Q11: return 11;
71 case R12: case D12: case Q12: return 12;
72 case SP: case D13: case Q13: return 13;
73 case LR: case D14: case Q14: return 14;
74 case PC: case D15: case Q15: return 15;
93 case S0: case S1: case S2: case S3:
94 case S4: case S5: case S6: case S7:
95 case S8: case S9: case S10: case S11:
96 case S12: case S13: case S14: case S15:
97 case S16: case S17: case S18: case S19:
98 case S20: case S21: case S22: case S23:
99 case S24: case S25: case S26: case S27:
100 case S28: case S29: case S30: case S31: {
104 default: return 0; // Avoid compile time warning.
142 ARMBaseRegisterInfo::ARMBaseRegisterInfo(const ARMBaseInstrInfo &tii,
143 const ARMSubtarget &sti)
144 : ARMGenRegisterInfo(ARM::ADJCALLSTACKDOWN, ARM::ADJCALLSTACKUP),
146 FramePtr((STI.isTargetDarwin() || STI.isThumb()) ? ARM::R7 : ARM::R11) {
150 ARMBaseRegisterInfo::getCalleeSavedRegs(const MachineFunction *MF) const {
151 static const unsigned CalleeSavedRegs[] = {
152 ARM::LR, ARM::R11, ARM::R10, ARM::R9, ARM::R8,
153 ARM::R7, ARM::R6, ARM::R5, ARM::R4,
155 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
156 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
160 static const unsigned DarwinCalleeSavedRegs[] = {
161 // Darwin ABI deviates from ARM standard ABI. R9 is not a callee-saved
163 ARM::LR, ARM::R7, ARM::R6, ARM::R5, ARM::R4,
164 ARM::R11, ARM::R10, ARM::R8,
166 ARM::D15, ARM::D14, ARM::D13, ARM::D12,
167 ARM::D11, ARM::D10, ARM::D9, ARM::D8,
170 return STI.isTargetDarwin() ? DarwinCalleeSavedRegs : CalleeSavedRegs;
173 BitVector ARMBaseRegisterInfo::
174 getReservedRegs(const MachineFunction &MF) const {
175 // FIXME: avoid re-calculating this everytime.
176 BitVector Reserved(getNumRegs());
177 Reserved.set(ARM::SP);
178 Reserved.set(ARM::PC);
179 if (STI.isTargetDarwin() || hasFP(MF))
180 Reserved.set(FramePtr);
181 // Some targets reserve R9.
182 if (STI.isR9Reserved())
183 Reserved.set(ARM::R9);
187 bool ARMBaseRegisterInfo::isReservedReg(const MachineFunction &MF,
188 unsigned Reg) const {
196 if (FramePtr == Reg && (STI.isTargetDarwin() || hasFP(MF)))
200 return STI.isR9Reserved();
206 const TargetRegisterClass *
207 ARMBaseRegisterInfo::getMatchingSuperRegClass(const TargetRegisterClass *A,
208 const TargetRegisterClass *B,
209 unsigned SubIdx) const {
217 if (A->getSize() == 8) {
218 if (B == &ARM::SPR_8RegClass)
219 return &ARM::DPR_8RegClass;
220 assert(B == &ARM::SPRRegClass && "Expecting SPR register class!");
221 if (A == &ARM::DPR_8RegClass)
223 return &ARM::DPR_VFP2RegClass;
226 if (A->getSize() == 16) {
227 if (B == &ARM::SPR_8RegClass)
228 return &ARM::QPR_8RegClass;
229 return &ARM::QPR_VFP2RegClass;
232 if (A->getSize() == 32) {
233 if (B == &ARM::SPR_8RegClass)
234 return 0; // Do not allow coalescing!
235 return &ARM::QQPR_VFP2RegClass;
238 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
239 return 0; // Do not allow coalescing!
246 if (A->getSize() == 16) {
247 if (B == &ARM::DPR_VFP2RegClass)
248 return &ARM::QPR_VFP2RegClass;
249 if (B == &ARM::DPR_8RegClass)
250 return 0; // Do not allow coalescing!
254 if (A->getSize() == 32) {
255 if (B == &ARM::DPR_VFP2RegClass)
256 return &ARM::QQPR_VFP2RegClass;
257 if (B == &ARM::DPR_8RegClass)
258 return 0; // Do not allow coalescing!
262 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
263 if (B != &ARM::DPRRegClass)
264 return 0; // Do not allow coalescing!
271 // D sub-registers of QQQQ registers.
272 if (A->getSize() == 64 && B == &ARM::DPRRegClass)
274 return 0; // Do not allow coalescing!
280 if (A->getSize() == 32) {
281 if (B == &ARM::QPR_VFP2RegClass)
282 return &ARM::QQPR_VFP2RegClass;
283 if (B == &ARM::QPR_8RegClass)
284 return 0; // Do not allow coalescing!
288 assert(A->getSize() == 64 && "Expecting a QQQQ register class!");
289 if (B == &ARM::QPRRegClass)
291 return 0; // Do not allow coalescing!
295 // Q sub-registers of QQQQ registers.
296 if (A->getSize() == 64 && B == &ARM::QPRRegClass)
298 return 0; // Do not allow coalescing!
305 ARMBaseRegisterInfo::canCombineSubRegIndices(const TargetRegisterClass *RC,
306 SmallVectorImpl<unsigned> &SubIndices,
307 unsigned &NewSubIdx) const {
309 unsigned Size = RC->getSize() * 8;
313 NewSubIdx = 0; // Whole register.
314 unsigned NumRegs = SubIndices.size();
316 // 8 D registers -> 1 QQQQ register.
317 return (Size == 512 &&
318 SubIndices[0] == ARM::dsub_0 &&
319 SubIndices[1] == ARM::dsub_1 &&
320 SubIndices[2] == ARM::dsub_2 &&
321 SubIndices[3] == ARM::dsub_3 &&
322 SubIndices[4] == ARM::dsub_4 &&
323 SubIndices[5] == ARM::dsub_5 &&
324 SubIndices[6] == ARM::dsub_6 &&
325 SubIndices[7] == ARM::dsub_7);
326 } else if (NumRegs == 4) {
327 if (SubIndices[0] == ARM::qsub_0) {
328 // 4 Q registers -> 1 QQQQ register.
329 return (Size == 512 &&
330 SubIndices[1] == ARM::qsub_1 &&
331 SubIndices[2] == ARM::qsub_2 &&
332 SubIndices[3] == ARM::qsub_3);
333 } else if (SubIndices[0] == ARM::dsub_0) {
334 // 4 D registers -> 1 QQ register.
336 SubIndices[1] == ARM::dsub_1 &&
337 SubIndices[2] == ARM::dsub_2 &&
338 SubIndices[3] == ARM::dsub_3) {
340 NewSubIdx = ARM::qqsub_0;
343 } else if (SubIndices[0] == ARM::dsub_4) {
344 // 4 D registers -> 1 QQ register (2nd).
346 SubIndices[1] == ARM::dsub_5 &&
347 SubIndices[2] == ARM::dsub_6 &&
348 SubIndices[3] == ARM::dsub_7) {
349 NewSubIdx = ARM::qqsub_1;
352 } else if (SubIndices[0] == ARM::ssub_0) {
353 // 4 S registers -> 1 Q register.
355 SubIndices[1] == ARM::ssub_1 &&
356 SubIndices[2] == ARM::ssub_2 &&
357 SubIndices[3] == ARM::ssub_3) {
359 NewSubIdx = ARM::qsub_0;
363 } else if (NumRegs == 2) {
364 if (SubIndices[0] == ARM::qsub_0) {
365 // 2 Q registers -> 1 QQ register.
366 if (Size >= 256 && SubIndices[1] == ARM::qsub_1) {
368 NewSubIdx = ARM::qqsub_0;
371 } else if (SubIndices[0] == ARM::qsub_2) {
372 // 2 Q registers -> 1 QQ register (2nd).
373 if (Size == 512 && SubIndices[1] == ARM::qsub_3) {
374 NewSubIdx = ARM::qqsub_1;
377 } else if (SubIndices[0] == ARM::dsub_0) {
378 // 2 D registers -> 1 Q register.
379 if (Size >= 128 && SubIndices[1] == ARM::dsub_1) {
381 NewSubIdx = ARM::qsub_0;
384 } else if (SubIndices[0] == ARM::dsub_2) {
385 // 2 D registers -> 1 Q register (2nd).
386 if (Size >= 256 && SubIndices[1] == ARM::dsub_3) {
387 NewSubIdx = ARM::qsub_1;
390 } else if (SubIndices[0] == ARM::dsub_4) {
391 // 2 D registers -> 1 Q register (3rd).
392 if (Size == 512 && SubIndices[1] == ARM::dsub_5) {
393 NewSubIdx = ARM::qsub_2;
396 } else if (SubIndices[0] == ARM::dsub_6) {
397 // 2 D registers -> 1 Q register (3rd).
398 if (Size == 512 && SubIndices[1] == ARM::dsub_7) {
399 NewSubIdx = ARM::qsub_3;
402 } else if (SubIndices[0] == ARM::ssub_0) {
403 // 2 S registers -> 1 D register.
404 if (SubIndices[1] == ARM::ssub_1) {
406 NewSubIdx = ARM::dsub_0;
409 } else if (SubIndices[0] == ARM::ssub_2) {
410 // 2 S registers -> 1 D register (2nd).
411 if (Size >= 128 && SubIndices[1] == ARM::ssub_3) {
412 NewSubIdx = ARM::dsub_1;
421 const TargetRegisterClass *
422 ARMBaseRegisterInfo::getPointerRegClass(unsigned Kind) const {
423 return ARM::GPRRegisterClass;
426 /// getAllocationOrder - Returns the register allocation order for a specified
427 /// register class in the form of a pair of TargetRegisterClass iterators.
428 std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
429 ARMBaseRegisterInfo::getAllocationOrder(const TargetRegisterClass *RC,
430 unsigned HintType, unsigned HintReg,
431 const MachineFunction &MF) const {
432 // Alternative register allocation orders when favoring even / odd registers
433 // of register pairs.
435 // No FP, R9 is available.
436 static const unsigned GPREven1[] = {
437 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8, ARM::R10,
438 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7,
441 static const unsigned GPROdd1[] = {
442 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R9, ARM::R11,
443 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
447 // FP is R7, R9 is available.
448 static const unsigned GPREven2[] = {
449 ARM::R0, ARM::R2, ARM::R4, ARM::R8, ARM::R10,
450 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6,
453 static const unsigned GPROdd2[] = {
454 ARM::R1, ARM::R3, ARM::R5, ARM::R9, ARM::R11,
455 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6,
459 // FP is R11, R9 is available.
460 static const unsigned GPREven3[] = {
461 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R8,
462 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7,
465 static const unsigned GPROdd3[] = {
466 ARM::R1, ARM::R3, ARM::R5, ARM::R6, ARM::R9,
467 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R7,
471 // No FP, R9 is not available.
472 static const unsigned GPREven4[] = {
473 ARM::R0, ARM::R2, ARM::R4, ARM::R6, ARM::R10,
474 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8,
477 static const unsigned GPROdd4[] = {
478 ARM::R1, ARM::R3, ARM::R5, ARM::R7, ARM::R11,
479 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
483 // FP is R7, R9 is not available.
484 static const unsigned GPREven5[] = {
485 ARM::R0, ARM::R2, ARM::R4, ARM::R10,
486 ARM::R1, ARM::R3, ARM::R12,ARM::LR, ARM::R5, ARM::R6, ARM::R8,
489 static const unsigned GPROdd5[] = {
490 ARM::R1, ARM::R3, ARM::R5, ARM::R11,
491 ARM::R0, ARM::R2, ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8,
495 // FP is R11, R9 is not available.
496 static const unsigned GPREven6[] = {
497 ARM::R0, ARM::R2, ARM::R4, ARM::R6,
498 ARM::R1, ARM::R3, ARM::R10,ARM::R12,ARM::LR, ARM::R5, ARM::R7, ARM::R8
500 static const unsigned GPROdd6[] = {
501 ARM::R1, ARM::R3, ARM::R5, ARM::R7,
502 ARM::R0, ARM::R2, ARM::R10,ARM::R12,ARM::LR, ARM::R4, ARM::R6, ARM::R8
506 if (HintType == ARMRI::RegPairEven) {
507 if (isPhysicalRegister(HintReg) && getRegisterPairEven(HintReg, MF) == 0)
508 // It's no longer possible to fulfill this hint. Return the default
510 return std::make_pair(RC->allocation_order_begin(MF),
511 RC->allocation_order_end(MF));
513 if (!STI.isTargetDarwin() && !hasFP(MF)) {
514 if (!STI.isR9Reserved())
515 return std::make_pair(GPREven1,
516 GPREven1 + (sizeof(GPREven1)/sizeof(unsigned)));
518 return std::make_pair(GPREven4,
519 GPREven4 + (sizeof(GPREven4)/sizeof(unsigned)));
520 } else if (FramePtr == ARM::R7) {
521 if (!STI.isR9Reserved())
522 return std::make_pair(GPREven2,
523 GPREven2 + (sizeof(GPREven2)/sizeof(unsigned)));
525 return std::make_pair(GPREven5,
526 GPREven5 + (sizeof(GPREven5)/sizeof(unsigned)));
527 } else { // FramePtr == ARM::R11
528 if (!STI.isR9Reserved())
529 return std::make_pair(GPREven3,
530 GPREven3 + (sizeof(GPREven3)/sizeof(unsigned)));
532 return std::make_pair(GPREven6,
533 GPREven6 + (sizeof(GPREven6)/sizeof(unsigned)));
535 } else if (HintType == ARMRI::RegPairOdd) {
536 if (isPhysicalRegister(HintReg) && getRegisterPairOdd(HintReg, MF) == 0)
537 // It's no longer possible to fulfill this hint. Return the default
539 return std::make_pair(RC->allocation_order_begin(MF),
540 RC->allocation_order_end(MF));
542 if (!STI.isTargetDarwin() && !hasFP(MF)) {
543 if (!STI.isR9Reserved())
544 return std::make_pair(GPROdd1,
545 GPROdd1 + (sizeof(GPROdd1)/sizeof(unsigned)));
547 return std::make_pair(GPROdd4,
548 GPROdd4 + (sizeof(GPROdd4)/sizeof(unsigned)));
549 } else if (FramePtr == ARM::R7) {
550 if (!STI.isR9Reserved())
551 return std::make_pair(GPROdd2,
552 GPROdd2 + (sizeof(GPROdd2)/sizeof(unsigned)));
554 return std::make_pair(GPROdd5,
555 GPROdd5 + (sizeof(GPROdd5)/sizeof(unsigned)));
556 } else { // FramePtr == ARM::R11
557 if (!STI.isR9Reserved())
558 return std::make_pair(GPROdd3,
559 GPROdd3 + (sizeof(GPROdd3)/sizeof(unsigned)));
561 return std::make_pair(GPROdd6,
562 GPROdd6 + (sizeof(GPROdd6)/sizeof(unsigned)));
565 return std::make_pair(RC->allocation_order_begin(MF),
566 RC->allocation_order_end(MF));
569 /// ResolveRegAllocHint - Resolves the specified register allocation hint
570 /// to a physical register. Returns the physical register if it is successful.
572 ARMBaseRegisterInfo::ResolveRegAllocHint(unsigned Type, unsigned Reg,
573 const MachineFunction &MF) const {
574 if (Reg == 0 || !isPhysicalRegister(Reg))
578 else if (Type == (unsigned)ARMRI::RegPairOdd)
580 return getRegisterPairOdd(Reg, MF);
581 else if (Type == (unsigned)ARMRI::RegPairEven)
583 return getRegisterPairEven(Reg, MF);
588 ARMBaseRegisterInfo::UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
589 MachineFunction &MF) const {
590 MachineRegisterInfo *MRI = &MF.getRegInfo();
591 std::pair<unsigned, unsigned> Hint = MRI->getRegAllocationHint(Reg);
592 if ((Hint.first == (unsigned)ARMRI::RegPairOdd ||
593 Hint.first == (unsigned)ARMRI::RegPairEven) &&
594 Hint.second && TargetRegisterInfo::isVirtualRegister(Hint.second)) {
595 // If 'Reg' is one of the even / odd register pair and it's now changed
596 // (e.g. coalesced) into a different register. The other register of the
597 // pair allocation hint must be updated to reflect the relationship
599 unsigned OtherReg = Hint.second;
600 Hint = MRI->getRegAllocationHint(OtherReg);
601 if (Hint.second == Reg)
602 // Make sure the pair has not already divorced.
603 MRI->setRegAllocationHint(OtherReg, Hint.first, NewReg);
607 /// hasFP - Return true if the specified function should have a dedicated frame
608 /// pointer register. This is true if the function has variable sized allocas
609 /// or if frame pointer elimination is disabled.
611 bool ARMBaseRegisterInfo::hasFP(const MachineFunction &MF) const {
612 const MachineFrameInfo *MFI = MF.getFrameInfo();
613 return ((DisableFramePointerElim(MF) && MFI->adjustsStack())||
614 needsStackRealignment(MF) ||
615 MFI->hasVarSizedObjects() ||
616 MFI->isFrameAddressTaken());
619 bool ARMBaseRegisterInfo::canRealignStack(const MachineFunction &MF) const {
620 const MachineFrameInfo *MFI = MF.getFrameInfo();
621 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
622 return (RealignStack &&
623 !AFI->isThumb1OnlyFunction() &&
624 !MFI->hasVarSizedObjects());
627 bool ARMBaseRegisterInfo::
628 needsStackRealignment(const MachineFunction &MF) const {
629 const MachineFrameInfo *MFI = MF.getFrameInfo();
630 const Function *F = MF.getFunction();
631 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
632 unsigned StackAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
633 bool requiresRealignment =
634 RealignStack && ((MFI->getMaxAlignment() > StackAlign) ||
635 F->hasFnAttr(Attribute::StackAlignment));
637 // FIXME: Currently we don't support stack realignment for functions with
638 // variable-sized allocas.
639 // FIXME: It's more complicated than this...
640 if (0 && requiresRealignment && MFI->hasVarSizedObjects())
642 "Stack realignment in presense of dynamic allocas is not supported");
644 // FIXME: This probably isn't the right place for this.
645 if (0 && requiresRealignment && AFI->isThumb1OnlyFunction())
647 "Stack realignment in thumb1 functions is not supported");
649 return requiresRealignment && canRealignStack(MF);
652 bool ARMBaseRegisterInfo::
653 cannotEliminateFrame(const MachineFunction &MF) const {
654 const MachineFrameInfo *MFI = MF.getFrameInfo();
655 if (DisableFramePointerElim(MF) && MFI->adjustsStack())
657 return MFI->hasVarSizedObjects() || MFI->isFrameAddressTaken()
658 || needsStackRealignment(MF);
661 /// estimateStackSize - Estimate and return the size of the frame.
662 static unsigned estimateStackSize(MachineFunction &MF) {
663 const MachineFrameInfo *FFI = MF.getFrameInfo();
665 for (int i = FFI->getObjectIndexBegin(); i != 0; ++i) {
666 int FixedOff = -FFI->getObjectOffset(i);
667 if (FixedOff > Offset) Offset = FixedOff;
669 for (unsigned i = 0, e = FFI->getObjectIndexEnd(); i != e; ++i) {
670 if (FFI->isDeadObjectIndex(i))
672 Offset += FFI->getObjectSize(i);
673 unsigned Align = FFI->getObjectAlignment(i);
674 // Adjust to alignment boundary
675 Offset = (Offset+Align-1)/Align*Align;
677 return (unsigned)Offset;
680 /// estimateRSStackSizeLimit - Look at each instruction that references stack
681 /// frames and return the stack size limit beyond which some of these
682 /// instructions will require a scratch register during their expansion later.
684 ARMBaseRegisterInfo::estimateRSStackSizeLimit(MachineFunction &MF) const {
685 unsigned Limit = (1 << 12) - 1;
686 for (MachineFunction::iterator BB = MF.begin(),E = MF.end(); BB != E; ++BB) {
687 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end();
689 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) {
690 if (!I->getOperand(i).isFI()) continue;
692 // When using ADDri to get the address of a stack object, 255 is the
693 // largest offset guaranteed to fit in the immediate offset.
694 if (I->getOpcode() == ARM::ADDri) {
695 Limit = std::min(Limit, (1U << 8) - 1);
699 // Otherwise check the addressing mode.
700 switch (I->getDesc().TSFlags & ARMII::AddrModeMask) {
701 case ARMII::AddrMode3:
702 case ARMII::AddrModeT2_i8:
703 Limit = std::min(Limit, (1U << 8) - 1);
705 case ARMII::AddrMode5:
706 case ARMII::AddrModeT2_i8s4:
707 Limit = std::min(Limit, ((1U << 8) - 1) * 4);
709 case ARMII::AddrModeT2_i12:
710 if (hasFP(MF)) Limit = std::min(Limit, (1U << 8) - 1);
712 case ARMII::AddrMode6:
713 // Addressing mode 6 (load/store) instructions can't encode an
714 // immediate offset for stack references.
719 break; // At most one FI per instruction
728 ARMBaseRegisterInfo::processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
729 RegScavenger *RS) const {
730 // This tells PEI to spill the FP as if it is any other callee-save register
731 // to take advantage the eliminateFrameIndex machinery. This also ensures it
732 // is spilled in the order specified by getCalleeSavedRegs() to make it easier
733 // to combine multiple loads / stores.
734 bool CanEliminateFrame = true;
735 bool CS1Spilled = false;
736 bool LRSpilled = false;
737 unsigned NumGPRSpills = 0;
738 SmallVector<unsigned, 4> UnspilledCS1GPRs;
739 SmallVector<unsigned, 4> UnspilledCS2GPRs;
740 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
741 MachineFrameInfo *MFI = MF.getFrameInfo();
743 // Spill R4 if Thumb2 function requires stack realignment - it will be used as
745 // FIXME: It will be better just to find spare register here.
746 if (needsStackRealignment(MF) &&
747 AFI->isThumb2Function())
748 MF.getRegInfo().setPhysRegUsed(ARM::R4);
750 // Spill LR if Thumb1 function uses variable length argument lists.
751 if (AFI->isThumb1OnlyFunction() && AFI->getVarArgsRegSaveSize() > 0)
752 MF.getRegInfo().setPhysRegUsed(ARM::LR);
754 // Don't spill FP if the frame can be eliminated. This is determined
755 // by scanning the callee-save registers to see if any is used.
756 const unsigned *CSRegs = getCalleeSavedRegs();
757 for (unsigned i = 0; CSRegs[i]; ++i) {
758 unsigned Reg = CSRegs[i];
759 bool Spilled = false;
760 if (MF.getRegInfo().isPhysRegUsed(Reg)) {
761 AFI->setCSRegisterIsSpilled(Reg);
763 CanEliminateFrame = false;
765 // Check alias registers too.
766 for (const unsigned *Aliases = getAliasSet(Reg); *Aliases; ++Aliases) {
767 if (MF.getRegInfo().isPhysRegUsed(*Aliases)) {
769 CanEliminateFrame = false;
774 if (!ARM::GPRRegisterClass->contains(Reg))
780 if (!STI.isTargetDarwin()) {
787 // Keep track if LR and any of R4, R5, R6, and R7 is spilled.
802 if (!STI.isTargetDarwin()) {
803 UnspilledCS1GPRs.push_back(Reg);
813 UnspilledCS1GPRs.push_back(Reg);
816 UnspilledCS2GPRs.push_back(Reg);
822 bool ForceLRSpill = false;
823 if (!LRSpilled && AFI->isThumb1OnlyFunction()) {
824 unsigned FnSize = TII.GetFunctionSizeInBytes(MF);
825 // Force LR to be spilled if the Thumb function size is > 2048. This enables
826 // use of BL to implement far jump. If it turns out that it's not needed
827 // then the branch fix up path will undo it.
828 if (FnSize >= (1 << 11)) {
829 CanEliminateFrame = false;
834 // If any of the stack slot references may be out of range of an immediate
835 // offset, make sure a register (or a spill slot) is available for the
836 // register scavenger. Note that if we're indexing off the frame pointer, the
837 // effective stack size is 4 bytes larger since the FP points to the stack
838 // slot of the previous FP. Also, if we have variable sized objects in the
839 // function, stack slot references will often be negative, and some of
840 // our instructions are positive-offset only, so conservatively consider
841 // that case to want a spill slot (or register) as well.
842 // FIXME: We could add logic to be more precise about negative offsets
843 // and which instructions will need a scratch register for them. Is it
844 // worth the effort and added fragility?
846 (RS && (estimateStackSize(MF) + (hasFP(MF) ? 4:0) >=
847 estimateRSStackSizeLimit(MF))) || MFI->hasVarSizedObjects();
849 bool ExtraCSSpill = false;
850 if (BigStack || !CanEliminateFrame || cannotEliminateFrame(MF)) {
851 AFI->setHasStackFrame(true);
853 // If LR is not spilled, but at least one of R4, R5, R6, and R7 is spilled.
854 // Spill LR as well so we can fold BX_RET to the registers restore (LDM).
855 if (!LRSpilled && CS1Spilled) {
856 MF.getRegInfo().setPhysRegUsed(ARM::LR);
857 AFI->setCSRegisterIsSpilled(ARM::LR);
859 UnspilledCS1GPRs.erase(std::find(UnspilledCS1GPRs.begin(),
860 UnspilledCS1GPRs.end(), (unsigned)ARM::LR));
861 ForceLRSpill = false;
865 // Darwin ABI requires FP to point to the stack slot that contains the
867 if (STI.isTargetDarwin() || hasFP(MF)) {
868 MF.getRegInfo().setPhysRegUsed(FramePtr);
872 // If stack and double are 8-byte aligned and we are spilling an odd number
873 // of GPRs. Spill one extra callee save GPR so we won't have to pad between
874 // the integer and double callee save areas.
875 unsigned TargetAlign = MF.getTarget().getFrameInfo()->getStackAlignment();
876 if (TargetAlign == 8 && (NumGPRSpills & 1)) {
877 if (CS1Spilled && !UnspilledCS1GPRs.empty()) {
878 for (unsigned i = 0, e = UnspilledCS1GPRs.size(); i != e; ++i) {
879 unsigned Reg = UnspilledCS1GPRs[i];
880 // Don't spill high register if the function is thumb1
881 if (!AFI->isThumb1OnlyFunction() ||
882 isARMLowRegister(Reg) || Reg == ARM::LR) {
883 MF.getRegInfo().setPhysRegUsed(Reg);
884 AFI->setCSRegisterIsSpilled(Reg);
885 if (!isReservedReg(MF, Reg))
890 } else if (!UnspilledCS2GPRs.empty() &&
891 !AFI->isThumb1OnlyFunction()) {
892 unsigned Reg = UnspilledCS2GPRs.front();
893 MF.getRegInfo().setPhysRegUsed(Reg);
894 AFI->setCSRegisterIsSpilled(Reg);
895 if (!isReservedReg(MF, Reg))
900 // Estimate if we might need to scavenge a register at some point in order
901 // to materialize a stack offset. If so, either spill one additional
902 // callee-saved register or reserve a special spill slot to facilitate
903 // register scavenging. Thumb1 needs a spill slot for stack pointer
904 // adjustments also, even when the frame itself is small.
905 if (BigStack && !ExtraCSSpill) {
906 // If any non-reserved CS register isn't spilled, just spill one or two
907 // extra. That should take care of it!
908 unsigned NumExtras = TargetAlign / 4;
909 SmallVector<unsigned, 2> Extras;
910 while (NumExtras && !UnspilledCS1GPRs.empty()) {
911 unsigned Reg = UnspilledCS1GPRs.back();
912 UnspilledCS1GPRs.pop_back();
913 if (!isReservedReg(MF, Reg) &&
914 (!AFI->isThumb1OnlyFunction() || isARMLowRegister(Reg) ||
916 Extras.push_back(Reg);
920 // For non-Thumb1 functions, also check for hi-reg CS registers
921 if (!AFI->isThumb1OnlyFunction()) {
922 while (NumExtras && !UnspilledCS2GPRs.empty()) {
923 unsigned Reg = UnspilledCS2GPRs.back();
924 UnspilledCS2GPRs.pop_back();
925 if (!isReservedReg(MF, Reg)) {
926 Extras.push_back(Reg);
931 if (Extras.size() && NumExtras == 0) {
932 for (unsigned i = 0, e = Extras.size(); i != e; ++i) {
933 MF.getRegInfo().setPhysRegUsed(Extras[i]);
934 AFI->setCSRegisterIsSpilled(Extras[i]);
936 } else if (!AFI->isThumb1OnlyFunction()) {
937 // note: Thumb1 functions spill to R12, not the stack. Reserve a slot
938 // closest to SP or frame pointer.
939 const TargetRegisterClass *RC = ARM::GPRRegisterClass;
940 RS->setScavengingFrameIndex(MFI->CreateStackObject(RC->getSize(),
948 MF.getRegInfo().setPhysRegUsed(ARM::LR);
949 AFI->setCSRegisterIsSpilled(ARM::LR);
950 AFI->setLRIsSpilledForFarJump(true);
954 unsigned ARMBaseRegisterInfo::getRARegister() const {
959 ARMBaseRegisterInfo::getFrameRegister(const MachineFunction &MF) const {
960 if (STI.isTargetDarwin() || hasFP(MF))
966 ARMBaseRegisterInfo::getFrameIndexReference(const MachineFunction &MF, int FI,
967 unsigned &FrameReg) const {
968 const MachineFrameInfo *MFI = MF.getFrameInfo();
969 const ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
970 int Offset = MFI->getObjectOffset(FI) + MFI->getStackSize();
971 bool isFixed = MFI->isFixedObjectIndex(FI);
974 if (AFI->isGPRCalleeSavedArea1Frame(FI))
975 Offset -= AFI->getGPRCalleeSavedArea1Offset();
976 else if (AFI->isGPRCalleeSavedArea2Frame(FI))
977 Offset -= AFI->getGPRCalleeSavedArea2Offset();
978 else if (AFI->isDPRCalleeSavedAreaFrame(FI))
979 Offset -= AFI->getDPRCalleeSavedAreaOffset();
980 else if (needsStackRealignment(MF)) {
981 // When dynamically realigning the stack, use the frame pointer for
982 // parameters, and the stack pointer for locals.
983 assert (hasFP(MF) && "dynamic stack realignment without a FP!");
985 FrameReg = getFrameRegister(MF);
986 Offset -= AFI->getFramePtrSpillOffset();
988 } else if (hasFP(MF) && AFI->hasStackFrame()) {
989 if (isFixed || MFI->hasVarSizedObjects()) {
990 // Use frame pointer to reference fixed objects unless this is a
991 // frameless function.
992 FrameReg = getFrameRegister(MF);
993 Offset -= AFI->getFramePtrSpillOffset();
994 } else if (AFI->isThumb2Function()) {
995 // In Thumb2 mode, the negative offset is very limited.
996 int FPOffset = Offset - AFI->getFramePtrSpillOffset();
997 if (FPOffset >= -255 && FPOffset < 0) {
998 FrameReg = getFrameRegister(MF);
1008 ARMBaseRegisterInfo::getFrameIndexOffset(const MachineFunction &MF,
1011 return getFrameIndexReference(MF, FI, FrameReg);
1014 unsigned ARMBaseRegisterInfo::getEHExceptionRegister() const {
1015 llvm_unreachable("What is the exception register");
1019 unsigned ARMBaseRegisterInfo::getEHHandlerRegister() const {
1020 llvm_unreachable("What is the exception handler register");
1024 int ARMBaseRegisterInfo::getDwarfRegNum(unsigned RegNum, bool isEH) const {
1025 return ARMGenRegisterInfo::getDwarfRegNumFull(RegNum, 0);
1028 unsigned ARMBaseRegisterInfo::getRegisterPairEven(unsigned Reg,
1029 const MachineFunction &MF) const {
1032 // Return 0 if either register of the pair is a special register.
1041 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R6;
1043 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R8;
1045 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R10;
1117 unsigned ARMBaseRegisterInfo::getRegisterPairOdd(unsigned Reg,
1118 const MachineFunction &MF) const {
1121 // Return 0 if either register of the pair is a special register.
1130 return isReservedReg(MF, ARM::R7) ? 0 : ARM::R7;
1132 return isReservedReg(MF, ARM::R9) ? 0 :ARM::R9;
1134 return isReservedReg(MF, ARM::R11) ? 0 : ARM::R11;
1206 /// emitLoadConstPool - Emits a load from constpool to materialize the
1207 /// specified immediate.
1208 void ARMBaseRegisterInfo::
1209 emitLoadConstPool(MachineBasicBlock &MBB,
1210 MachineBasicBlock::iterator &MBBI,
1212 unsigned DestReg, unsigned SubIdx, int Val,
1213 ARMCC::CondCodes Pred,
1214 unsigned PredReg) const {
1215 MachineFunction &MF = *MBB.getParent();
1216 MachineConstantPool *ConstantPool = MF.getConstantPool();
1218 ConstantInt::get(Type::getInt32Ty(MF.getFunction()->getContext()), Val);
1219 unsigned Idx = ConstantPool->getConstantPoolIndex(C, 4);
1221 BuildMI(MBB, MBBI, dl, TII.get(ARM::LDRcp))
1222 .addReg(DestReg, getDefRegState(true), SubIdx)
1223 .addConstantPoolIndex(Idx)
1224 .addReg(0).addImm(0).addImm(Pred).addReg(PredReg);
1227 bool ARMBaseRegisterInfo::
1228 requiresRegisterScavenging(const MachineFunction &MF) const {
1232 bool ARMBaseRegisterInfo::
1233 requiresFrameIndexScavenging(const MachineFunction &MF) const {
1237 // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
1238 // not required, we reserve argument space for call sites in the function
1239 // immediately on entry to the current function. This eliminates the need for
1240 // add/sub sp brackets around call sites. Returns true if the call frame is
1241 // included as part of the stack frame.
1242 bool ARMBaseRegisterInfo::
1243 hasReservedCallFrame(MachineFunction &MF) const {
1244 const MachineFrameInfo *FFI = MF.getFrameInfo();
1245 unsigned CFSize = FFI->getMaxCallFrameSize();
1246 // It's not always a good idea to include the call frame as part of the
1247 // stack frame. ARM (especially Thumb) has small immediate offset to
1248 // address the stack frame. So a large call frame can cause poor codegen
1249 // and may even makes it impossible to scavenge a register.
1250 if (CFSize >= ((1 << 12) - 1) / 2) // Half of imm12
1253 return !MF.getFrameInfo()->hasVarSizedObjects();
1256 // canSimplifyCallFramePseudos - If there is a reserved call frame, the
1257 // call frame pseudos can be simplified. Unlike most targets, having a FP
1258 // is not sufficient here since we still may reference some objects via SP
1259 // even when FP is available in Thumb2 mode.
1260 bool ARMBaseRegisterInfo::
1261 canSimplifyCallFramePseudos(MachineFunction &MF) const {
1262 return hasReservedCallFrame(MF) || MF.getFrameInfo()->hasVarSizedObjects();
1266 emitSPUpdate(bool isARM,
1267 MachineBasicBlock &MBB, MachineBasicBlock::iterator &MBBI,
1268 DebugLoc dl, const ARMBaseInstrInfo &TII,
1270 ARMCC::CondCodes Pred = ARMCC::AL, unsigned PredReg = 0) {
1272 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1273 Pred, PredReg, TII);
1275 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, ARM::SP, NumBytes,
1276 Pred, PredReg, TII);
1280 void ARMBaseRegisterInfo::
1281 eliminateCallFramePseudoInstr(MachineFunction &MF, MachineBasicBlock &MBB,
1282 MachineBasicBlock::iterator I) const {
1283 if (!hasReservedCallFrame(MF)) {
1284 // If we have alloca, convert as follows:
1285 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
1286 // ADJCALLSTACKUP -> add, sp, sp, amount
1287 MachineInstr *Old = I;
1288 DebugLoc dl = Old->getDebugLoc();
1289 unsigned Amount = Old->getOperand(0).getImm();
1291 // We need to keep the stack aligned properly. To do this, we round the
1292 // amount of space needed for the outgoing arguments up to the next
1293 // alignment boundary.
1294 unsigned Align = MF.getTarget().getFrameInfo()->getStackAlignment();
1295 Amount = (Amount+Align-1)/Align*Align;
1297 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1298 assert(!AFI->isThumb1OnlyFunction() &&
1299 "This eliminateCallFramePseudoInstr does not support Thumb1!");
1300 bool isARM = !AFI->isThumbFunction();
1302 // Replace the pseudo instruction with a new instruction...
1303 unsigned Opc = Old->getOpcode();
1304 int PIdx = Old->findFirstPredOperandIdx();
1305 ARMCC::CondCodes Pred = (PIdx == -1)
1306 ? ARMCC::AL : (ARMCC::CondCodes)Old->getOperand(PIdx).getImm();
1307 if (Opc == ARM::ADJCALLSTACKDOWN || Opc == ARM::tADJCALLSTACKDOWN) {
1308 // Note: PredReg is operand 2 for ADJCALLSTACKDOWN.
1309 unsigned PredReg = Old->getOperand(2).getReg();
1310 emitSPUpdate(isARM, MBB, I, dl, TII, -Amount, Pred, PredReg);
1312 // Note: PredReg is operand 3 for ADJCALLSTACKUP.
1313 unsigned PredReg = Old->getOperand(3).getReg();
1314 assert(Opc == ARM::ADJCALLSTACKUP || Opc == ARM::tADJCALLSTACKUP);
1315 emitSPUpdate(isARM, MBB, I, dl, TII, Amount, Pred, PredReg);
1323 ARMBaseRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
1324 int SPAdj, FrameIndexValue *Value,
1325 RegScavenger *RS) const {
1327 MachineInstr &MI = *II;
1328 MachineBasicBlock &MBB = *MI.getParent();
1329 MachineFunction &MF = *MBB.getParent();
1330 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1331 assert(!AFI->isThumb1OnlyFunction() &&
1332 "This eliminateFrameIndex does not support Thumb1!");
1334 while (!MI.getOperand(i).isFI()) {
1336 assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
1339 int FrameIndex = MI.getOperand(i).getIndex();
1342 int Offset = getFrameIndexReference(MF, FrameIndex, FrameReg);
1343 if (FrameReg != ARM::SP)
1347 // Special handling of dbg_value instructions.
1348 if (MI.isDebugValue()) {
1349 MI.getOperand(i). ChangeToRegister(FrameReg, false /*isDef*/);
1350 MI.getOperand(i+1).ChangeToImmediate(Offset);
1354 // Modify MI as necessary to handle as much of 'Offset' as possible
1356 if (!AFI->isThumbFunction())
1357 Done = rewriteARMFrameIndex(MI, i, FrameReg, Offset, TII);
1359 assert(AFI->isThumb2Function());
1360 Done = rewriteT2FrameIndex(MI, i, FrameReg, Offset, TII);
1365 // If we get here, the immediate doesn't fit into the instruction. We folded
1366 // as much as possible above, handle the rest, providing a register that is
1369 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode4 ||
1370 (MI.getDesc().TSFlags & ARMII::AddrModeMask) == ARMII::AddrMode6) &&
1371 "This code isn't needed if offset already handled!");
1373 unsigned ScratchReg = 0;
1374 int PIdx = MI.findFirstPredOperandIdx();
1375 ARMCC::CondCodes Pred = (PIdx == -1)
1376 ? ARMCC::AL : (ARMCC::CondCodes)MI.getOperand(PIdx).getImm();
1377 unsigned PredReg = (PIdx == -1) ? 0 : MI.getOperand(PIdx+1).getReg();
1379 // Must be addrmode4/6.
1380 MI.getOperand(i).ChangeToRegister(FrameReg, false, false, false);
1382 ScratchReg = MF.getRegInfo().createVirtualRegister(ARM::GPRRegisterClass);
1384 Value->first = FrameReg; // use the frame register as a kind indicator
1385 Value->second = Offset;
1387 if (!AFI->isThumbFunction())
1388 emitARMRegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1389 Offset, Pred, PredReg, TII);
1391 assert(AFI->isThumb2Function());
1392 emitT2RegPlusImmediate(MBB, II, MI.getDebugLoc(), ScratchReg, FrameReg,
1393 Offset, Pred, PredReg, TII);
1395 MI.getOperand(i).ChangeToRegister(ScratchReg, false, false, true);
1396 if (!ReuseFrameIndexVals)
1402 /// Move iterator past the next bunch of callee save load / store ops for
1403 /// the particular spill area (1: integer area 1, 2: integer area 2,
1404 /// 3: fp area, 0: don't care).
1405 static void movePastCSLoadStoreOps(MachineBasicBlock &MBB,
1406 MachineBasicBlock::iterator &MBBI,
1407 int Opc1, int Opc2, unsigned Area,
1408 const ARMSubtarget &STI) {
1409 while (MBBI != MBB.end() &&
1410 ((MBBI->getOpcode() == Opc1) || (MBBI->getOpcode() == Opc2)) &&
1411 MBBI->getOperand(1).isFI()) {
1414 unsigned Category = 0;
1415 switch (MBBI->getOperand(0).getReg()) {
1416 case ARM::R4: case ARM::R5: case ARM::R6: case ARM::R7:
1420 case ARM::R8: case ARM::R9: case ARM::R10: case ARM::R11:
1421 Category = STI.isTargetDarwin() ? 2 : 1;
1423 case ARM::D8: case ARM::D9: case ARM::D10: case ARM::D11:
1424 case ARM::D12: case ARM::D13: case ARM::D14: case ARM::D15:
1431 if (Done || Category != Area)
1439 void ARMBaseRegisterInfo::
1440 emitPrologue(MachineFunction &MF) const {
1441 MachineBasicBlock &MBB = MF.front();
1442 MachineBasicBlock::iterator MBBI = MBB.begin();
1443 MachineFrameInfo *MFI = MF.getFrameInfo();
1444 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1445 assert(!AFI->isThumb1OnlyFunction() &&
1446 "This emitPrologue does not support Thumb1!");
1447 bool isARM = !AFI->isThumbFunction();
1448 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1449 unsigned NumBytes = MFI->getStackSize();
1450 const std::vector<CalleeSavedInfo> &CSI = MFI->getCalleeSavedInfo();
1451 DebugLoc dl = MBBI != MBB.end() ? MBBI->getDebugLoc() : DebugLoc();
1453 // Determine the sizes of each callee-save spill areas and record which frame
1454 // belongs to which callee-save spill areas.
1455 unsigned GPRCS1Size = 0, GPRCS2Size = 0, DPRCSSize = 0;
1456 int FramePtrSpillFI = 0;
1458 // Allocate the vararg register save area. This is not counted in NumBytes.
1460 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -VARegSaveSize);
1462 if (!AFI->hasStackFrame()) {
1464 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1468 for (unsigned i = 0, e = CSI.size(); i != e; ++i) {
1469 unsigned Reg = CSI[i].getReg();
1470 int FI = CSI[i].getFrameIdx();
1477 if (Reg == FramePtr)
1478 FramePtrSpillFI = FI;
1479 AFI->addGPRCalleeSavedArea1Frame(FI);
1486 if (Reg == FramePtr)
1487 FramePtrSpillFI = FI;
1488 if (STI.isTargetDarwin()) {
1489 AFI->addGPRCalleeSavedArea2Frame(FI);
1492 AFI->addGPRCalleeSavedArea1Frame(FI);
1497 AFI->addDPRCalleeSavedAreaFrame(FI);
1502 // Build the new SUBri to adjust SP for integer callee-save spill area 1.
1503 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS1Size);
1504 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 1, STI);
1506 // Set FP to point to the stack slot that contains the previous FP.
1507 // For Darwin, FP is R7, which has now been stored in spill area 1.
1508 // Otherwise, if this is not Darwin, all the callee-saved registers go
1509 // into spill area 1, including the FP in R11. In either case, it is
1510 // now safe to emit this assignment.
1511 if (STI.isTargetDarwin() || hasFP(MF)) {
1512 unsigned ADDriOpc = !AFI->isThumbFunction() ? ARM::ADDri : ARM::t2ADDri;
1513 MachineInstrBuilder MIB =
1514 BuildMI(MBB, MBBI, dl, TII.get(ADDriOpc), FramePtr)
1515 .addFrameIndex(FramePtrSpillFI).addImm(0);
1516 AddDefaultCC(AddDefaultPred(MIB));
1519 // Build the new SUBri to adjust SP for integer callee-save spill area 2.
1520 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -GPRCS2Size);
1522 // Build the new SUBri to adjust SP for FP callee-save spill area.
1523 movePastCSLoadStoreOps(MBB, MBBI, ARM::STR, ARM::t2STRi12, 2, STI);
1524 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -DPRCSSize);
1526 // Determine starting offsets of spill areas.
1527 unsigned DPRCSOffset = NumBytes - (GPRCS1Size + GPRCS2Size + DPRCSSize);
1528 unsigned GPRCS2Offset = DPRCSOffset + DPRCSSize;
1529 unsigned GPRCS1Offset = GPRCS2Offset + GPRCS2Size;
1530 if (STI.isTargetDarwin() || hasFP(MF))
1531 AFI->setFramePtrSpillOffset(MFI->getObjectOffset(FramePtrSpillFI) +
1533 AFI->setGPRCalleeSavedArea1Offset(GPRCS1Offset);
1534 AFI->setGPRCalleeSavedArea2Offset(GPRCS2Offset);
1535 AFI->setDPRCalleeSavedAreaOffset(DPRCSOffset);
1537 movePastCSLoadStoreOps(MBB, MBBI, ARM::VSTRD, 0, 3, STI);
1538 NumBytes = DPRCSOffset;
1540 // Adjust SP after all the callee-save spills.
1541 emitSPUpdate(isARM, MBB, MBBI, dl, TII, -NumBytes);
1544 if (STI.isTargetELF() && hasFP(MF)) {
1545 MFI->setOffsetAdjustment(MFI->getOffsetAdjustment() -
1546 AFI->getFramePtrSpillOffset());
1549 AFI->setGPRCalleeSavedArea1Size(GPRCS1Size);
1550 AFI->setGPRCalleeSavedArea2Size(GPRCS2Size);
1551 AFI->setDPRCalleeSavedAreaSize(DPRCSSize);
1553 // If we need dynamic stack realignment, do it here.
1554 if (needsStackRealignment(MF)) {
1555 unsigned MaxAlign = MFI->getMaxAlignment();
1556 assert (!AFI->isThumb1OnlyFunction());
1557 if (!AFI->isThumbFunction()) {
1558 // Emit bic sp, sp, MaxAlign
1559 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
1560 TII.get(ARM::BICri), ARM::SP)
1561 .addReg(ARM::SP, RegState::Kill)
1562 .addImm(MaxAlign-1)));
1564 // We cannot use sp as source/dest register here, thus we're emitting the
1565 // following sequence:
1567 // bic r4, r4, MaxAlign
1569 // FIXME: It will be better just to find spare register here.
1570 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2tgpr), ARM::R4)
1571 .addReg(ARM::SP, RegState::Kill);
1572 AddDefaultCC(AddDefaultPred(BuildMI(MBB, MBBI, dl,
1573 TII.get(ARM::t2BICri), ARM::R4)
1574 .addReg(ARM::R4, RegState::Kill)
1575 .addImm(MaxAlign-1)));
1576 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVtgpr2gpr), ARM::SP)
1577 .addReg(ARM::R4, RegState::Kill);
1582 static bool isCalleeSavedRegister(unsigned Reg, const unsigned *CSRegs) {
1583 for (unsigned i = 0; CSRegs[i]; ++i)
1584 if (Reg == CSRegs[i])
1589 static bool isCSRestore(MachineInstr *MI,
1590 const ARMBaseInstrInfo &TII,
1591 const unsigned *CSRegs) {
1592 return ((MI->getOpcode() == (int)ARM::VLDRD ||
1593 MI->getOpcode() == (int)ARM::LDR ||
1594 MI->getOpcode() == (int)ARM::t2LDRi12) &&
1595 MI->getOperand(1).isFI() &&
1596 isCalleeSavedRegister(MI->getOperand(0).getReg(), CSRegs));
1599 void ARMBaseRegisterInfo::
1600 emitEpilogue(MachineFunction &MF, MachineBasicBlock &MBB) const {
1601 MachineBasicBlock::iterator MBBI = prior(MBB.end());
1602 assert(MBBI->getDesc().isReturn() &&
1603 "Can only insert epilog into returning blocks");
1604 unsigned RetOpcode = MBBI->getOpcode();
1605 DebugLoc dl = MBBI->getDebugLoc();
1606 MachineFrameInfo *MFI = MF.getFrameInfo();
1607 ARMFunctionInfo *AFI = MF.getInfo<ARMFunctionInfo>();
1608 assert(!AFI->isThumb1OnlyFunction() &&
1609 "This emitEpilogue does not support Thumb1!");
1610 bool isARM = !AFI->isThumbFunction();
1612 unsigned VARegSaveSize = AFI->getVarArgsRegSaveSize();
1613 int NumBytes = (int)MFI->getStackSize();
1615 if (!AFI->hasStackFrame()) {
1617 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1619 // Unwind MBBI to point to first LDR / VLDRD.
1620 const unsigned *CSRegs = getCalleeSavedRegs();
1621 if (MBBI != MBB.begin()) {
1624 while (MBBI != MBB.begin() && isCSRestore(MBBI, TII, CSRegs));
1625 if (!isCSRestore(MBBI, TII, CSRegs))
1629 // Move SP to start of FP callee save spill area.
1630 NumBytes -= (AFI->getGPRCalleeSavedArea1Size() +
1631 AFI->getGPRCalleeSavedArea2Size() +
1632 AFI->getDPRCalleeSavedAreaSize());
1634 // Darwin ABI requires FP to point to the stack slot that contains the
1636 bool HasFP = hasFP(MF);
1637 if ((STI.isTargetDarwin() && NumBytes) || HasFP) {
1638 NumBytes = AFI->getFramePtrSpillOffset() - NumBytes;
1639 // Reset SP based on frame pointer only if the stack frame extends beyond
1640 // frame pointer stack slot or target is ELF and the function has FP.
1642 AFI->getGPRCalleeSavedArea2Size() ||
1643 AFI->getDPRCalleeSavedAreaSize() ||
1644 AFI->getDPRCalleeSavedAreaOffset()) {
1647 emitARMRegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1650 emitT2RegPlusImmediate(MBB, MBBI, dl, ARM::SP, FramePtr, -NumBytes,
1655 BuildMI(MBB, MBBI, dl, TII.get(ARM::MOVr), ARM::SP)
1657 .addImm((unsigned)ARMCC::AL).addReg(0).addReg(0);
1659 BuildMI(MBB, MBBI, dl, TII.get(ARM::tMOVgpr2gpr), ARM::SP)
1663 } else if (NumBytes)
1664 emitSPUpdate(isARM, MBB, MBBI, dl, TII, NumBytes);
1666 // Move SP to start of integer callee save spill area 2.
1667 movePastCSLoadStoreOps(MBB, MBBI, ARM::VLDRD, 0, 3, STI);
1668 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getDPRCalleeSavedAreaSize());
1670 // Move SP to start of integer callee save spill area 1.
1671 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 2, STI);
1672 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea2Size());
1674 // Move SP to SP upon entry to the function.
1675 movePastCSLoadStoreOps(MBB, MBBI, ARM::LDR, ARM::t2LDRi12, 1, STI);
1676 emitSPUpdate(isARM, MBB, MBBI, dl, TII, AFI->getGPRCalleeSavedArea1Size());
1679 if (RetOpcode == ARM::TCRETURNdi || RetOpcode == ARM::TCRETURNdiND ||
1680 RetOpcode == ARM::TCRETURNri || RetOpcode == ARM::TCRETURNriND) {
1681 // Tail call return: adjust the stack pointer and jump to callee.
1682 MBBI = prior(MBB.end());
1683 MachineOperand &JumpTarget = MBBI->getOperand(0);
1685 // Jump to label or value in register.
1686 if (RetOpcode == ARM::TCRETURNdi) {
1687 BuildMI(MBB, MBBI, dl,
1688 TII.get(STI.isThumb() ? ARM::TAILJMPdt : ARM::TAILJMPd)).
1689 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
1690 JumpTarget.getTargetFlags());
1691 } else if (RetOpcode == ARM::TCRETURNdiND) {
1692 BuildMI(MBB, MBBI, dl,
1693 TII.get(STI.isThumb() ? ARM::TAILJMPdNDt : ARM::TAILJMPdND)).
1694 addGlobalAddress(JumpTarget.getGlobal(), JumpTarget.getOffset(),
1695 JumpTarget.getTargetFlags());
1696 } else if (RetOpcode == ARM::TCRETURNri) {
1697 BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPr)).
1698 addReg(JumpTarget.getReg(), RegState::Kill);
1699 } else if (RetOpcode == ARM::TCRETURNriND) {
1700 BuildMI(MBB, MBBI, dl, TII.get(ARM::TAILJMPrND)).
1701 addReg(JumpTarget.getReg(), RegState::Kill);
1704 MachineInstr *NewMI = prior(MBBI);
1705 for (unsigned i = 1, e = MBBI->getNumOperands(); i != e; ++i)
1706 NewMI->addOperand(MBBI->getOperand(i));
1708 // Delete the pseudo instruction TCRETURN.
1713 emitSPUpdate(isARM, MBB, MBBI, dl, TII, VARegSaveSize);
1716 #include "ARMGenRegisterInfo.inc"