1 //===- Thumb1FrameLowering.cpp - Thumb1 Frame Information -----------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the Thumb1 implementation of TargetFrameLowering class.
11 //===----------------------------------------------------------------------===//
13 #include "Thumb1FrameLowering.h"
14 #include "ARMBaseInstrInfo.h"
15 #include "ARMBaseRegisterInfo.h"
16 #include "ARMMachineFunctionInfo.h"
17 #include "ARMSubtarget.h"
18 #include "Thumb1InstrInfo.h"
19 #include "ThumbRegisterInfo.h"
20 #include "Utils/ARMBaseInfo.h"
21 #include "llvm/ADT/BitVector.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/CodeGen/LivePhysRegs.h"
25 #include "llvm/CodeGen/MachineBasicBlock.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineModuleInfo.h"
31 #include "llvm/CodeGen/MachineOperand.h"
32 #include "llvm/CodeGen/MachineRegisterInfo.h"
33 #include "llvm/CodeGen/TargetInstrInfo.h"
34 #include "llvm/CodeGen/TargetOpcodes.h"
35 #include "llvm/CodeGen/TargetSubtargetInfo.h"
36 #include "llvm/IR/DebugLoc.h"
37 #include "llvm/MC/MCContext.h"
38 #include "llvm/MC/MCDwarf.h"
39 #include "llvm/MC/MCRegisterInfo.h"
40 #include "llvm/Support/Compiler.h"
41 #include "llvm/Support/ErrorHandling.h"
46 #define DEBUG_TYPE "arm-frame-lowering"
50 Thumb1FrameLowering::Thumb1FrameLowering(const ARMSubtarget
&sti
)
51 : ARMFrameLowering(sti
) {}
53 bool Thumb1FrameLowering::hasReservedCallFrame(const MachineFunction
&MF
) const{
54 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
55 unsigned CFSize
= MFI
.getMaxCallFrameSize();
56 // It's not always a good idea to include the call frame as part of the
57 // stack frame. ARM (especially Thumb) has small immediate offset to
58 // address the stack frame. So a large call frame can cause poor codegen
59 // and may even makes it impossible to scavenge a register.
60 if (CFSize
>= ((1 << 8) - 1) * 4 / 2) // Half of imm8 * 4
63 return !MFI
.hasVarSizedObjects();
67 emitPrologueEpilogueSPUpdate(MachineBasicBlock
&MBB
,
68 MachineBasicBlock::iterator
&MBBI
,
69 const TargetInstrInfo
&TII
, const DebugLoc
&dl
,
70 const ThumbRegisterInfo
&MRI
, int NumBytes
,
71 unsigned ScratchReg
, unsigned MIFlags
) {
72 // If it would take more than three instructions to adjust the stack pointer
73 // using tADDspi/tSUBspi, load an immediate instead.
74 if (std::abs(NumBytes
) > 508 * 3) {
75 // We use a different codepath here from the normal
76 // emitThumbRegPlusImmediate so we don't have to deal with register
77 // scavenging. (Scavenging could try to use the emergency spill slot
78 // before we've actually finished setting up the stack.)
79 if (ScratchReg
== ARM::NoRegister
)
80 report_fatal_error("Failed to emit Thumb1 stack adjustment");
81 MachineFunction
&MF
= *MBB
.getParent();
82 const ARMSubtarget
&ST
= MF
.getSubtarget
<ARMSubtarget
>();
83 if (ST
.genExecuteOnly()) {
84 unsigned XOInstr
= ST
.useMovt() ? ARM::t2MOVi32imm
: ARM::tMOVi32imm
;
85 BuildMI(MBB
, MBBI
, dl
, TII
.get(XOInstr
), ScratchReg
)
86 .addImm(NumBytes
).setMIFlags(MIFlags
);
88 MRI
.emitLoadConstPool(MBB
, MBBI
, dl
, ScratchReg
, 0, NumBytes
, ARMCC::AL
,
91 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tADDhirr
), ARM::SP
)
93 .addReg(ScratchReg
, RegState::Kill
)
94 .add(predOps(ARMCC::AL
))
98 // FIXME: This is assuming the heuristics in emitThumbRegPlusImmediate
100 emitThumbRegPlusImmediate(MBB
, MBBI
, dl
, ARM::SP
, ARM::SP
, NumBytes
, TII
,
105 static void emitCallSPUpdate(MachineBasicBlock
&MBB
,
106 MachineBasicBlock::iterator
&MBBI
,
107 const TargetInstrInfo
&TII
, const DebugLoc
&dl
,
108 const ThumbRegisterInfo
&MRI
, int NumBytes
,
109 unsigned MIFlags
= MachineInstr::NoFlags
) {
110 emitThumbRegPlusImmediate(MBB
, MBBI
, dl
, ARM::SP
, ARM::SP
, NumBytes
, TII
,
115 MachineBasicBlock::iterator
Thumb1FrameLowering::
116 eliminateCallFramePseudoInstr(MachineFunction
&MF
, MachineBasicBlock
&MBB
,
117 MachineBasicBlock::iterator I
) const {
118 const Thumb1InstrInfo
&TII
=
119 *static_cast<const Thumb1InstrInfo
*>(STI
.getInstrInfo());
120 const ThumbRegisterInfo
*RegInfo
=
121 static_cast<const ThumbRegisterInfo
*>(STI
.getRegisterInfo());
122 if (!hasReservedCallFrame(MF
)) {
123 // If we have alloca, convert as follows:
124 // ADJCALLSTACKDOWN -> sub, sp, sp, amount
125 // ADJCALLSTACKUP -> add, sp, sp, amount
126 MachineInstr
&Old
= *I
;
127 DebugLoc dl
= Old
.getDebugLoc();
128 unsigned Amount
= TII
.getFrameSize(Old
);
130 // We need to keep the stack aligned properly. To do this, we round the
131 // amount of space needed for the outgoing arguments up to the next
132 // alignment boundary.
133 Amount
= alignTo(Amount
, getStackAlign());
135 // Replace the pseudo instruction with a new instruction...
136 unsigned Opc
= Old
.getOpcode();
137 if (Opc
== ARM::ADJCALLSTACKDOWN
|| Opc
== ARM::tADJCALLSTACKDOWN
) {
138 emitCallSPUpdate(MBB
, I
, TII
, dl
, *RegInfo
, -Amount
);
140 assert(Opc
== ARM::ADJCALLSTACKUP
|| Opc
== ARM::tADJCALLSTACKUP
);
141 emitCallSPUpdate(MBB
, I
, TII
, dl
, *RegInfo
, Amount
);
148 void Thumb1FrameLowering::emitPrologue(MachineFunction
&MF
,
149 MachineBasicBlock
&MBB
) const {
150 MachineBasicBlock::iterator MBBI
= MBB
.begin();
151 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
152 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
153 const MCRegisterInfo
*MRI
= MF
.getContext().getRegisterInfo();
154 const ThumbRegisterInfo
*RegInfo
=
155 static_cast<const ThumbRegisterInfo
*>(STI
.getRegisterInfo());
156 const Thumb1InstrInfo
&TII
=
157 *static_cast<const Thumb1InstrInfo
*>(STI
.getInstrInfo());
159 unsigned ArgRegsSaveSize
= AFI
->getArgRegsSaveSize();
160 unsigned NumBytes
= MFI
.getStackSize();
161 assert(NumBytes
>= ArgRegsSaveSize
&&
162 "ArgRegsSaveSize is included in NumBytes");
163 const std::vector
<CalleeSavedInfo
> &CSI
= MFI
.getCalleeSavedInfo();
164 assert(STI
.getPushPopSplitVariation(MF
) == ARMSubtarget::SplitR7
&&
165 "Must use R7 spilt for Thumb1");
167 // Debug location must be unknown since the first debug location is used
168 // to determine the end of the prologue.
171 Register FramePtr
= RegInfo
->getFrameRegister(MF
);
172 Register BasePtr
= RegInfo
->getBaseRegister();
175 // Thumb add/sub sp, imm8 instructions implicitly multiply the offset by 4.
176 NumBytes
= (NumBytes
+ 3) & ~3;
177 MFI
.setStackSize(NumBytes
);
179 // Determine the sizes of each callee-save spill areas and record which frame
180 // belongs to which callee-save spill areas.
181 unsigned FRSize
= 0, GPRCS1Size
= 0, GPRCS2Size
= 0, DPRCSSize
= 0;
182 int FramePtrSpillFI
= 0;
184 if (ArgRegsSaveSize
) {
185 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
, -ArgRegsSaveSize
,
186 ARM::NoRegister
, MachineInstr::FrameSetup
);
187 CFAOffset
+= ArgRegsSaveSize
;
189 MF
.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset
));
190 BuildMI(MBB
, MBBI
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
191 .addCFIIndex(CFIIndex
)
192 .setMIFlags(MachineInstr::FrameSetup
);
195 if (!AFI
->hasStackFrame()) {
196 if (NumBytes
- ArgRegsSaveSize
!= 0) {
197 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
,
198 -(NumBytes
- ArgRegsSaveSize
),
199 ARM::NoRegister
, MachineInstr::FrameSetup
);
200 CFAOffset
+= NumBytes
- ArgRegsSaveSize
;
201 unsigned CFIIndex
= MF
.addFrameInst(
202 MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset
));
203 BuildMI(MBB
, MBBI
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
204 .addCFIIndex(CFIIndex
)
205 .setMIFlags(MachineInstr::FrameSetup
);
210 bool HasFrameRecordArea
= hasFP(MF
) && ARM::hGPRRegClass
.contains(FramePtr
);
212 for (const CalleeSavedInfo
&I
: CSI
) {
213 Register Reg
= I
.getReg();
214 int FI
= I
.getFrameIdx();
216 FramePtrSpillFI
= FI
;
219 if (HasFrameRecordArea
) {
230 if (HasFrameRecordArea
) {
246 MachineBasicBlock::iterator FRPush
, GPRCS1Push
, GPRCS2Push
;
247 if (HasFrameRecordArea
) {
248 // Skip Frame Record setup:
252 std::advance(MBBI
, 2);
256 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tPUSH
) {
261 // Find last push instruction for GPRCS2 - spilling of high registers
262 // (r8-r11) could consist of multiple tPUSH and tMOVr instructions.
264 MachineBasicBlock::iterator OldMBBI
= MBBI
;
265 // Skip a run of tMOVr instructions
266 while (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tMOVr
&&
267 MBBI
->getFlag(MachineInstr::FrameSetup
))
269 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tPUSH
&&
270 MBBI
->getFlag(MachineInstr::FrameSetup
)) {
274 // We have reached an instruction which is not a push, so the previous
275 // run of tMOVr instructions (which may have been empty) was not part of
276 // the prologue. Reset MBBI back to the last PUSH of the prologue.
282 // Skip past this code sequence, which is emitted to restore the LR if it is
283 // live-in and clobbered by the frame record setup code:
286 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tLDRspi
&&
287 MBBI
->getFlag(MachineInstr::FrameSetup
)) {
289 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tMOVr
&&
290 MBBI
->getOperand(0).getReg() == ARM::LR
&&
291 MBBI
->getFlag(MachineInstr::FrameSetup
)) {
296 // Determine starting offsets of spill areas.
297 unsigned DPRCSOffset
= NumBytes
- ArgRegsSaveSize
-
298 (FRSize
+ GPRCS1Size
+ GPRCS2Size
+ DPRCSSize
);
299 unsigned GPRCS2Offset
= DPRCSOffset
+ DPRCSSize
;
300 unsigned GPRCS1Offset
= GPRCS2Offset
+ GPRCS2Size
;
301 bool HasFP
= hasFP(MF
);
303 AFI
->setFramePtrSpillOffset(MFI
.getObjectOffset(FramePtrSpillFI
) +
305 if (HasFrameRecordArea
)
306 AFI
->setFrameRecordSavedAreaSize(FRSize
);
307 AFI
->setGPRCalleeSavedArea1Offset(GPRCS1Offset
);
308 AFI
->setGPRCalleeSavedArea2Offset(GPRCS2Offset
);
309 AFI
->setDPRCalleeSavedArea1Offset(DPRCSOffset
);
310 NumBytes
= DPRCSOffset
;
312 int FramePtrOffsetInBlock
= 0;
313 unsigned adjustedGPRCS1Size
= GPRCS1Size
;
314 if (GPRCS1Size
> 0 && GPRCS2Size
== 0 &&
315 tryFoldSPUpdateIntoPushPop(STI
, MF
, &*(GPRCS1Push
), NumBytes
)) {
316 FramePtrOffsetInBlock
= NumBytes
;
317 adjustedGPRCS1Size
+= NumBytes
;
320 CFAOffset
+= adjustedGPRCS1Size
;
322 // Adjust FP so it point to the stack slot that contains the previous FP.
324 MachineBasicBlock::iterator AfterPush
=
325 HasFrameRecordArea
? std::next(FRPush
) : std::next(GPRCS1Push
);
326 if (HasFrameRecordArea
) {
327 // We have just finished pushing the previous FP into the stack,
328 // so simply capture the SP value as the new Frame Pointer.
329 BuildMI(MBB
, AfterPush
, dl
, TII
.get(ARM::tMOVr
), FramePtr
)
331 .setMIFlags(MachineInstr::FrameSetup
)
332 .add(predOps(ARMCC::AL
));
334 FramePtrOffsetInBlock
+=
335 MFI
.getObjectOffset(FramePtrSpillFI
) + GPRCS1Size
+ ArgRegsSaveSize
;
336 BuildMI(MBB
, AfterPush
, dl
, TII
.get(ARM::tADDrSPi
), FramePtr
)
338 .addImm(FramePtrOffsetInBlock
/ 4)
339 .setMIFlags(MachineInstr::FrameSetup
)
340 .add(predOps(ARMCC::AL
));
343 if(FramePtrOffsetInBlock
) {
344 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::cfiDefCfa(
345 nullptr, MRI
->getDwarfRegNum(FramePtr
, true), (CFAOffset
- FramePtrOffsetInBlock
)));
346 BuildMI(MBB
, AfterPush
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
347 .addCFIIndex(CFIIndex
)
348 .setMIFlags(MachineInstr::FrameSetup
);
351 MF
.addFrameInst(MCCFIInstruction::createDefCfaRegister(
352 nullptr, MRI
->getDwarfRegNum(FramePtr
, true)));
353 BuildMI(MBB
, AfterPush
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
354 .addCFIIndex(CFIIndex
)
355 .setMIFlags(MachineInstr::FrameSetup
);
358 // If offset is > 508 then sp cannot be adjusted in a single instruction,
359 // try restoring from fp instead.
360 AFI
->setShouldRestoreSPFromFP(true);
363 // Emit call frame information for the callee-saved low registers.
364 if (GPRCS1Size
> 0) {
365 MachineBasicBlock::iterator Pos
= std::next(GPRCS1Push
);
366 if (adjustedGPRCS1Size
) {
368 MF
.addFrameInst(MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset
));
369 BuildMI(MBB
, Pos
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
370 .addCFIIndex(CFIIndex
)
371 .setMIFlags(MachineInstr::FrameSetup
);
373 for (const CalleeSavedInfo
&I
: CSI
) {
374 Register Reg
= I
.getReg();
375 int FI
= I
.getFrameIdx();
392 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createOffset(
393 nullptr, MRI
->getDwarfRegNum(Reg
, true), MFI
.getObjectOffset(FI
)));
394 BuildMI(MBB
, Pos
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
395 .addCFIIndex(CFIIndex
)
396 .setMIFlags(MachineInstr::FrameSetup
);
402 // Emit call frame information for the callee-saved high registers.
403 if (GPRCS2Size
> 0) {
404 MachineBasicBlock::iterator Pos
= std::next(GPRCS2Push
);
405 for (auto &I
: CSI
) {
406 Register Reg
= I
.getReg();
407 int FI
= I
.getFrameIdx();
414 unsigned CFIIndex
= MF
.addFrameInst(MCCFIInstruction::createOffset(
415 nullptr, MRI
->getDwarfRegNum(Reg
, true), MFI
.getObjectOffset(FI
)));
416 BuildMI(MBB
, Pos
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
417 .addCFIIndex(CFIIndex
)
418 .setMIFlags(MachineInstr::FrameSetup
);
428 // Insert it after all the callee-save spills.
430 // For a large stack frame, we might need a scratch register to store
431 // the size of the frame. We know all callee-save registers are free
432 // at this point in the prologue, so pick one.
433 unsigned ScratchRegister
= ARM::NoRegister
;
434 for (auto &I
: CSI
) {
435 Register Reg
= I
.getReg();
436 if (isARMLowRegister(Reg
) && !(HasFP
&& Reg
== FramePtr
)) {
437 ScratchRegister
= Reg
;
441 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
, -NumBytes
,
442 ScratchRegister
, MachineInstr::FrameSetup
);
444 CFAOffset
+= NumBytes
;
445 unsigned CFIIndex
= MF
.addFrameInst(
446 MCCFIInstruction::cfiDefCfaOffset(nullptr, CFAOffset
));
447 BuildMI(MBB
, MBBI
, dl
, TII
.get(TargetOpcode::CFI_INSTRUCTION
))
448 .addCFIIndex(CFIIndex
)
449 .setMIFlags(MachineInstr::FrameSetup
);
453 if (STI
.isTargetELF() && HasFP
)
454 MFI
.setOffsetAdjustment(MFI
.getOffsetAdjustment() -
455 AFI
->getFramePtrSpillOffset());
457 AFI
->setGPRCalleeSavedArea1Size(GPRCS1Size
);
458 AFI
->setGPRCalleeSavedArea2Size(GPRCS2Size
);
459 AFI
->setDPRCalleeSavedArea1Size(DPRCSSize
);
461 if (RegInfo
->hasStackRealignment(MF
)) {
462 const unsigned NrBitsToZero
= Log2(MFI
.getMaxAlign());
463 // Emit the following sequence, using R4 as a temporary, since we cannot use
464 // SP as a source or destination register for the shifts:
466 // lsrs r4, r4, #NrBitsToZero
467 // lsls r4, r4, #NrBitsToZero
469 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), ARM::R4
)
470 .addReg(ARM::SP
, RegState::Kill
)
471 .add(predOps(ARMCC::AL
));
473 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tLSRri
), ARM::R4
)
475 .addReg(ARM::R4
, RegState::Kill
)
476 .addImm(NrBitsToZero
)
477 .add(predOps(ARMCC::AL
));
479 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tLSLri
), ARM::R4
)
481 .addReg(ARM::R4
, RegState::Kill
)
482 .addImm(NrBitsToZero
)
483 .add(predOps(ARMCC::AL
));
485 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), ARM::SP
)
486 .addReg(ARM::R4
, RegState::Kill
)
487 .add(predOps(ARMCC::AL
));
489 AFI
->setShouldRestoreSPFromFP(true);
492 // If we need a base pointer, set it up here. It's whatever the value
493 // of the stack pointer is at this point. Any variable size objects
494 // will be allocated after this, so we can still use the base pointer
495 // to reference locals.
496 if (RegInfo
->hasBasePointer(MF
))
497 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), BasePtr
)
499 .add(predOps(ARMCC::AL
));
501 // If the frame has variable sized objects then the epilogue must restore
502 // the sp from fp. We can assume there's an FP here since hasFP already
503 // checks for hasVarSizedObjects.
504 if (MFI
.hasVarSizedObjects())
505 AFI
->setShouldRestoreSPFromFP(true);
507 // In some cases, virtual registers have been introduced, e.g. by uses of
508 // emitThumbRegPlusImmInReg.
509 MF
.getProperties().reset(MachineFunctionProperties::Property::NoVRegs
);
512 void Thumb1FrameLowering::emitEpilogue(MachineFunction
&MF
,
513 MachineBasicBlock
&MBB
) const {
514 MachineBasicBlock::iterator MBBI
= MBB
.getFirstTerminator();
515 DebugLoc dl
= MBBI
!= MBB
.end() ? MBBI
->getDebugLoc() : DebugLoc();
516 MachineFrameInfo
&MFI
= MF
.getFrameInfo();
517 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
518 const ThumbRegisterInfo
*RegInfo
=
519 static_cast<const ThumbRegisterInfo
*>(STI
.getRegisterInfo());
520 const Thumb1InstrInfo
&TII
=
521 *static_cast<const Thumb1InstrInfo
*>(STI
.getInstrInfo());
523 unsigned ArgRegsSaveSize
= AFI
->getArgRegsSaveSize();
524 int NumBytes
= (int)MFI
.getStackSize();
525 assert((unsigned)NumBytes
>= ArgRegsSaveSize
&&
526 "ArgRegsSaveSize is included in NumBytes");
527 Register FramePtr
= RegInfo
->getFrameRegister(MF
);
529 if (!AFI
->hasStackFrame()) {
530 if (NumBytes
- ArgRegsSaveSize
!= 0)
531 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
,
532 NumBytes
- ArgRegsSaveSize
, ARM::NoRegister
,
533 MachineInstr::FrameDestroy
);
535 // Unwind MBBI to point to first LDR / VLDRD.
536 if (MBBI
!= MBB
.begin()) {
539 while (MBBI
!= MBB
.begin() && MBBI
->getFlag(MachineInstr::FrameDestroy
));
540 if (!MBBI
->getFlag(MachineInstr::FrameDestroy
))
544 // Move SP to start of FP callee save spill area.
546 (AFI
->getFrameRecordSavedAreaSize() +
547 AFI
->getGPRCalleeSavedArea1Size() + AFI
->getGPRCalleeSavedArea2Size() +
548 AFI
->getDPRCalleeSavedArea1Size() + ArgRegsSaveSize
);
550 // We are likely to need a scratch register and we know all callee-save
551 // registers are free at this point in the epilogue, so pick one.
552 unsigned ScratchRegister
= ARM::NoRegister
;
553 bool HasFP
= hasFP(MF
);
554 for (auto &I
: MFI
.getCalleeSavedInfo()) {
555 Register Reg
= I
.getReg();
556 if (isARMLowRegister(Reg
) && !(HasFP
&& Reg
== FramePtr
)) {
557 ScratchRegister
= Reg
;
562 if (AFI
->shouldRestoreSPFromFP()) {
563 NumBytes
= AFI
->getFramePtrSpillOffset() - NumBytes
;
564 // Reset SP based on frame pointer only if the stack frame extends beyond
565 // frame pointer stack slot, the target is ELF and the function has FP, or
566 // the target uses var sized objects.
568 assert(ScratchRegister
!= ARM::NoRegister
&&
569 "No scratch register to restore SP from FP!");
570 emitThumbRegPlusImmediate(MBB
, MBBI
, dl
, ScratchRegister
, FramePtr
, -NumBytes
,
571 TII
, *RegInfo
, MachineInstr::FrameDestroy
);
572 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), ARM::SP
)
573 .addReg(ScratchRegister
)
574 .add(predOps(ARMCC::AL
))
575 .setMIFlag(MachineInstr::FrameDestroy
);
577 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
), ARM::SP
)
579 .add(predOps(ARMCC::AL
))
580 .setMIFlag(MachineInstr::FrameDestroy
);
582 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tBX_RET
&&
583 &MBB
.front() != &*MBBI
&& std::prev(MBBI
)->getOpcode() == ARM::tPOP
) {
584 MachineBasicBlock::iterator PMBBI
= std::prev(MBBI
);
585 if (!tryFoldSPUpdateIntoPushPop(STI
, MF
, &*PMBBI
, NumBytes
))
586 emitPrologueEpilogueSPUpdate(MBB
, PMBBI
, TII
, dl
, *RegInfo
, NumBytes
,
587 ScratchRegister
, MachineInstr::FrameDestroy
);
588 } else if (!tryFoldSPUpdateIntoPushPop(STI
, MF
, &*MBBI
, NumBytes
))
589 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
, NumBytes
,
590 ScratchRegister
, MachineInstr::FrameDestroy
);
594 if (needPopSpecialFixUp(MF
)) {
595 bool Done
= emitPopSpecialFixUp(MBB
, /* DoIt */ true);
597 assert(Done
&& "Emission of the special fixup failed!?");
601 bool Thumb1FrameLowering::canUseAsEpilogue(const MachineBasicBlock
&MBB
) const {
602 if (!needPopSpecialFixUp(*MBB
.getParent()))
605 MachineBasicBlock
*TmpMBB
= const_cast<MachineBasicBlock
*>(&MBB
);
606 return emitPopSpecialFixUp(*TmpMBB
, /* DoIt */ false);
609 bool Thumb1FrameLowering::needPopSpecialFixUp(const MachineFunction
&MF
) const {
610 ARMFunctionInfo
*AFI
=
611 const_cast<MachineFunction
*>(&MF
)->getInfo
<ARMFunctionInfo
>();
612 if (AFI
->getArgRegsSaveSize())
615 // LR cannot be encoded with Thumb1, i.e., it requires a special fix-up.
616 for (const CalleeSavedInfo
&CSI
: MF
.getFrameInfo().getCalleeSavedInfo())
617 if (CSI
.getReg() == ARM::LR
)
623 static void findTemporariesForLR(const BitVector
&GPRsNoLRSP
,
624 const BitVector
&PopFriendly
,
625 const LiveRegUnits
&UsedRegs
, unsigned &PopReg
,
626 unsigned &TmpReg
, MachineRegisterInfo
&MRI
) {
628 for (auto Reg
: GPRsNoLRSP
.set_bits()) {
629 if (UsedRegs
.available(Reg
)) {
630 // Remember the first pop-friendly register and exit.
631 if (PopFriendly
.test(Reg
)) {
636 // Otherwise, remember that the register will be available to
637 // save a pop-friendly register.
643 bool Thumb1FrameLowering::emitPopSpecialFixUp(MachineBasicBlock
&MBB
,
645 MachineFunction
&MF
= *MBB
.getParent();
646 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
647 unsigned ArgRegsSaveSize
= AFI
->getArgRegsSaveSize();
648 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
649 const ThumbRegisterInfo
*RegInfo
=
650 static_cast<const ThumbRegisterInfo
*>(STI
.getRegisterInfo());
652 // If MBBI is a return instruction, or is a tPOP followed by a return
653 // instruction in the successor BB, we may be able to directly restore
655 // This is only possible with v5T ops (v4T can't change the Thumb bit via
656 // a POP PC instruction), and only if we do not need to emit any SP update.
657 // Otherwise, we need a temporary register to pop the value
658 // and copy that value into LR.
659 auto MBBI
= MBB
.getFirstTerminator();
660 bool CanRestoreDirectly
= STI
.hasV5TOps() && !ArgRegsSaveSize
;
661 if (CanRestoreDirectly
) {
662 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() != ARM::tB
)
663 CanRestoreDirectly
= (MBBI
->getOpcode() == ARM::tBX_RET
||
664 MBBI
->getOpcode() == ARM::tPOP_RET
);
666 auto MBBI_prev
= MBBI
;
668 assert(MBBI_prev
->getOpcode() == ARM::tPOP
);
669 assert(MBB
.succ_size() == 1);
670 if ((*MBB
.succ_begin())->begin()->getOpcode() == ARM::tBX_RET
)
671 MBBI
= MBBI_prev
; // Replace the final tPOP with a tPOP_RET.
673 CanRestoreDirectly
= false;
677 if (CanRestoreDirectly
) {
678 if (!DoIt
|| MBBI
->getOpcode() == ARM::tPOP_RET
)
680 MachineInstrBuilder MIB
=
681 BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
.get(ARM::tPOP_RET
))
682 .add(predOps(ARMCC::AL
))
683 .setMIFlag(MachineInstr::FrameDestroy
);
684 // Copy implicit ops and popped registers, if any.
685 for (auto MO
: MBBI
->operands())
686 if (MO
.isReg() && (MO
.isImplicit() || MO
.isDef()))
688 MIB
.addReg(ARM::PC
, RegState::Define
);
689 // Erase the old instruction (tBX_RET or tPOP).
694 // Look for a temporary register to use.
695 // First, compute the liveness information.
696 const TargetRegisterInfo
&TRI
= *STI
.getRegisterInfo();
697 LiveRegUnits
UsedRegs(TRI
);
698 UsedRegs
.addLiveOuts(MBB
);
699 // The semantic of pristines changed recently and now,
700 // the callee-saved registers that are touched in the function
701 // are not part of the pristines set anymore.
702 // Add those callee-saved now.
703 const MCPhysReg
*CSRegs
= TRI
.getCalleeSavedRegs(&MF
);
704 for (unsigned i
= 0; CSRegs
[i
]; ++i
)
705 UsedRegs
.addReg(CSRegs
[i
]);
707 DebugLoc dl
= DebugLoc();
708 if (MBBI
!= MBB
.end()) {
709 dl
= MBBI
->getDebugLoc();
710 auto InstUpToMBBI
= MBB
.end();
711 while (InstUpToMBBI
!= MBBI
)
712 // The pre-decrement is on purpose here.
713 // We want to have the liveness right before MBBI.
714 UsedRegs
.stepBackward(*--InstUpToMBBI
);
717 // Look for a register that can be directly use in the POP.
719 // And some temporary register, just in case.
720 unsigned TemporaryReg
= 0;
721 BitVector PopFriendly
=
722 TRI
.getAllocatableSet(MF
, TRI
.getRegClass(ARM::tGPRRegClassID
));
724 assert(PopFriendly
.any() && "No allocatable pop-friendly register?!");
725 // Rebuild the GPRs from the high registers because they are removed
726 // form the GPR reg class for thumb1.
727 BitVector GPRsNoLRSP
=
728 TRI
.getAllocatableSet(MF
, TRI
.getRegClass(ARM::hGPRRegClassID
));
729 GPRsNoLRSP
|= PopFriendly
;
730 GPRsNoLRSP
.reset(ARM::LR
);
731 GPRsNoLRSP
.reset(ARM::SP
);
732 GPRsNoLRSP
.reset(ARM::PC
);
733 findTemporariesForLR(GPRsNoLRSP
, PopFriendly
, UsedRegs
, PopReg
, TemporaryReg
,
736 // If we couldn't find a pop-friendly register, try restoring LR before
737 // popping the other callee-saved registers, so we could use one of them as a
739 bool UseLDRSP
= false;
740 if (!PopReg
&& MBBI
!= MBB
.begin()) {
741 auto PrevMBBI
= MBBI
;
743 if (PrevMBBI
->getOpcode() == ARM::tPOP
) {
744 UsedRegs
.stepBackward(*PrevMBBI
);
745 findTemporariesForLR(GPRsNoLRSP
, PopFriendly
, UsedRegs
, PopReg
,
746 TemporaryReg
, MF
.getRegInfo());
754 if (!DoIt
&& !PopReg
&& !TemporaryReg
)
757 assert((PopReg
|| TemporaryReg
) && "Cannot get LR");
760 assert(PopReg
&& "Do not know how to get LR");
761 // Load the LR via LDR tmp, [SP, #off]
762 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tLDRspi
))
763 .addReg(PopReg
, RegState::Define
)
765 .addImm(MBBI
->getNumExplicitOperands() - 2)
766 .add(predOps(ARMCC::AL
))
767 .setMIFlag(MachineInstr::FrameDestroy
);
768 // Move from the temporary register to the LR.
769 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
))
770 .addReg(ARM::LR
, RegState::Define
)
771 .addReg(PopReg
, RegState::Kill
)
772 .add(predOps(ARMCC::AL
))
773 .setMIFlag(MachineInstr::FrameDestroy
);
774 // Advance past the pop instruction.
777 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
,
778 ArgRegsSaveSize
+ 4, ARM::NoRegister
,
779 MachineInstr::FrameDestroy
);
784 assert(!PopReg
&& "Unnecessary MOV is about to be inserted");
785 PopReg
= PopFriendly
.find_first();
786 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
))
787 .addReg(TemporaryReg
, RegState::Define
)
788 .addReg(PopReg
, RegState::Kill
)
789 .add(predOps(ARMCC::AL
))
790 .setMIFlag(MachineInstr::FrameDestroy
);
793 if (MBBI
!= MBB
.end() && MBBI
->getOpcode() == ARM::tPOP_RET
) {
794 // We couldn't use the direct restoration above, so
795 // perform the opposite conversion: tPOP_RET to tPOP.
796 MachineInstrBuilder MIB
=
797 BuildMI(MBB
, MBBI
, MBBI
->getDebugLoc(), TII
.get(ARM::tPOP
))
798 .add(predOps(ARMCC::AL
))
799 .setMIFlag(MachineInstr::FrameDestroy
);
801 for (auto MO
: MBBI
->operands())
802 if (MO
.isReg() && (MO
.isImplicit() || MO
.isDef()) &&
803 MO
.getReg() != ARM::PC
) {
805 if (!MO
.isImplicit())
808 // Is there anything left to pop?
810 MBB
.erase(MIB
.getInstr());
811 // Erase the old instruction.
813 MBBI
= BuildMI(MBB
, MBB
.end(), dl
, TII
.get(ARM::tBX_RET
))
814 .add(predOps(ARMCC::AL
))
815 .setMIFlag(MachineInstr::FrameDestroy
);
818 assert(PopReg
&& "Do not know how to get LR");
819 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tPOP
))
820 .add(predOps(ARMCC::AL
))
821 .addReg(PopReg
, RegState::Define
)
822 .setMIFlag(MachineInstr::FrameDestroy
);
824 emitPrologueEpilogueSPUpdate(MBB
, MBBI
, TII
, dl
, *RegInfo
, ArgRegsSaveSize
,
825 ARM::NoRegister
, MachineInstr::FrameDestroy
);
827 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
))
828 .addReg(ARM::LR
, RegState::Define
)
829 .addReg(PopReg
, RegState::Kill
)
830 .add(predOps(ARMCC::AL
))
831 .setMIFlag(MachineInstr::FrameDestroy
);
834 BuildMI(MBB
, MBBI
, dl
, TII
.get(ARM::tMOVr
))
835 .addReg(PopReg
, RegState::Define
)
836 .addReg(TemporaryReg
, RegState::Kill
)
837 .add(predOps(ARMCC::AL
))
838 .setMIFlag(MachineInstr::FrameDestroy
);
843 static const SmallVector
<Register
> OrderedLowRegs
= {ARM::R4
, ARM::R5
, ARM::R6
,
845 static const SmallVector
<Register
> OrderedHighRegs
= {ARM::R8
, ARM::R9
,
847 static const SmallVector
<Register
> OrderedCopyRegs
= {
848 ARM::R0
, ARM::R1
, ARM::R2
, ARM::R3
, ARM::R4
,
849 ARM::R5
, ARM::R6
, ARM::R7
, ARM::LR
};
851 static void splitLowAndHighRegs(const std::set
<Register
> &Regs
,
852 std::set
<Register
> &LowRegs
,
853 std::set
<Register
> &HighRegs
) {
854 for (Register Reg
: Regs
) {
855 if (ARM::tGPRRegClass
.contains(Reg
) || Reg
== ARM::LR
) {
857 } else if (ARM::hGPRRegClass
.contains(Reg
) && Reg
!= ARM::LR
) {
858 HighRegs
.insert(Reg
);
860 llvm_unreachable("callee-saved register of unexpected class");
865 template <typename It
>
866 It
getNextOrderedReg(It OrderedStartIt
, It OrderedEndIt
,
867 const std::set
<Register
> &RegSet
) {
868 return std::find_if(OrderedStartIt
, OrderedEndIt
,
869 [&](Register Reg
) { return RegSet
.count(Reg
); });
872 static void pushRegsToStack(MachineBasicBlock
&MBB
,
873 MachineBasicBlock::iterator MI
,
874 const TargetInstrInfo
&TII
,
875 const std::set
<Register
> &RegsToSave
,
876 const std::set
<Register
> &CopyRegs
,
877 bool &UsedLRAsTemp
) {
878 MachineFunction
&MF
= *MBB
.getParent();
879 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
882 std::set
<Register
> LowRegs
, HighRegs
;
883 splitLowAndHighRegs(RegsToSave
, LowRegs
, HighRegs
);
885 // Push low regs first
886 if (!LowRegs
.empty()) {
887 MachineInstrBuilder MIB
=
888 BuildMI(MBB
, MI
, DL
, TII
.get(ARM::tPUSH
)).add(predOps(ARMCC::AL
));
889 for (unsigned Reg
: OrderedLowRegs
) {
890 if (LowRegs
.count(Reg
)) {
891 bool isKill
= !MRI
.isLiveIn(Reg
);
892 if (isKill
&& !MRI
.isReserved(Reg
))
895 MIB
.addReg(Reg
, getKillRegState(isKill
));
898 MIB
.setMIFlags(MachineInstr::FrameSetup
);
901 // Now push the high registers
902 // There are no store instructions that can access high registers directly,
903 // so we have to move them to low registers, and push them.
904 // This might take multiple pushes, as it is possible for there to
905 // be fewer low registers available than high registers which need saving.
907 // Find the first register to save.
908 // Registers must be processed in reverse order so that in case we need to use
909 // multiple PUSH instructions, the order of the registers on the stack still
910 // matches the unwind info. They need to be swicthed back to ascending order
911 // before adding to the PUSH instruction.
912 auto HiRegToSave
= getNextOrderedReg(OrderedHighRegs
.rbegin(),
913 OrderedHighRegs
.rend(),
916 while (HiRegToSave
!= OrderedHighRegs
.rend()) {
917 // Find the first low register to use.
918 auto CopyRegIt
= getNextOrderedReg(OrderedCopyRegs
.rbegin(),
919 OrderedCopyRegs
.rend(),
922 // Create the PUSH, but don't insert it yet (the MOVs need to come first).
923 MachineInstrBuilder PushMIB
= BuildMI(MF
, DL
, TII
.get(ARM::tPUSH
))
924 .add(predOps(ARMCC::AL
))
925 .setMIFlags(MachineInstr::FrameSetup
);
927 SmallVector
<unsigned, 4> RegsToPush
;
928 while (HiRegToSave
!= OrderedHighRegs
.rend() &&
929 CopyRegIt
!= OrderedCopyRegs
.rend()) {
930 if (HighRegs
.count(*HiRegToSave
)) {
931 bool isKill
= !MRI
.isLiveIn(*HiRegToSave
);
932 if (isKill
&& !MRI
.isReserved(*HiRegToSave
))
933 MBB
.addLiveIn(*HiRegToSave
);
934 if (*CopyRegIt
== ARM::LR
)
937 // Emit a MOV from the high reg to the low reg.
938 BuildMI(MBB
, MI
, DL
, TII
.get(ARM::tMOVr
))
939 .addReg(*CopyRegIt
, RegState::Define
)
940 .addReg(*HiRegToSave
, getKillRegState(isKill
))
941 .add(predOps(ARMCC::AL
))
942 .setMIFlags(MachineInstr::FrameSetup
);
944 // Record the register that must be added to the PUSH.
945 RegsToPush
.push_back(*CopyRegIt
);
947 CopyRegIt
= getNextOrderedReg(std::next(CopyRegIt
),
948 OrderedCopyRegs
.rend(),
950 HiRegToSave
= getNextOrderedReg(std::next(HiRegToSave
),
951 OrderedHighRegs
.rend(),
956 // Add the low registers to the PUSH, in ascending order.
957 for (unsigned Reg
: llvm::reverse(RegsToPush
))
958 PushMIB
.addReg(Reg
, RegState::Kill
);
960 // Insert the PUSH instruction after the MOVs.
961 MBB
.insert(MI
, PushMIB
);
965 static void popRegsFromStack(MachineBasicBlock
&MBB
,
966 MachineBasicBlock::iterator
&MI
,
967 const TargetInstrInfo
&TII
,
968 const std::set
<Register
> &RegsToRestore
,
969 const std::set
<Register
> &AvailableCopyRegs
,
970 bool IsVarArg
, bool HasV5Ops
) {
971 if (RegsToRestore
.empty())
974 MachineFunction
&MF
= *MBB
.getParent();
975 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
976 DebugLoc DL
= MI
!= MBB
.end() ? MI
->getDebugLoc() : DebugLoc();
978 std::set
<Register
> LowRegs
, HighRegs
;
979 splitLowAndHighRegs(RegsToRestore
, LowRegs
, HighRegs
);
981 // Pop the high registers first
982 // There are no store instructions that can access high registers directly,
983 // so we have to pop into low registers and them move to the high registers.
984 // This might take multiple pops, as it is possible for there to
985 // be fewer low registers available than high registers which need restoring.
987 // Find the first register to restore.
988 auto HiRegToRestore
= getNextOrderedReg(OrderedHighRegs
.begin(),
989 OrderedHighRegs
.end(),
992 std::set
<Register
> CopyRegs
= AvailableCopyRegs
;
993 Register LowScratchReg
;
994 if (!HighRegs
.empty() && CopyRegs
.empty()) {
995 // No copy regs are available to pop high regs. Let's make use of a return
996 // register and the scratch register (IP/R12) to copy things around.
997 LowScratchReg
= ARM::R0
;
998 BuildMI(MBB
, MI
, DL
, TII
.get(ARM::tMOVr
))
999 .addReg(ARM::R12
, RegState::Define
)
1000 .addReg(LowScratchReg
, RegState::Kill
)
1001 .add(predOps(ARMCC::AL
))
1002 .setMIFlag(MachineInstr::FrameDestroy
);
1003 CopyRegs
.insert(LowScratchReg
);
1006 while (HiRegToRestore
!= OrderedHighRegs
.end()) {
1007 assert(!CopyRegs
.empty());
1008 // Find the first low register to use.
1009 auto CopyReg
= getNextOrderedReg(OrderedCopyRegs
.begin(),
1010 OrderedCopyRegs
.end(),
1013 // Create the POP instruction.
1014 MachineInstrBuilder PopMIB
= BuildMI(MBB
, MI
, DL
, TII
.get(ARM::tPOP
))
1015 .add(predOps(ARMCC::AL
))
1016 .setMIFlag(MachineInstr::FrameDestroy
);
1018 while (HiRegToRestore
!= OrderedHighRegs
.end() &&
1019 CopyReg
!= OrderedCopyRegs
.end()) {
1020 // Add the low register to the POP.
1021 PopMIB
.addReg(*CopyReg
, RegState::Define
);
1023 // Create the MOV from low to high register.
1024 BuildMI(MBB
, MI
, DL
, TII
.get(ARM::tMOVr
))
1025 .addReg(*HiRegToRestore
, RegState::Define
)
1026 .addReg(*CopyReg
, RegState::Kill
)
1027 .add(predOps(ARMCC::AL
))
1028 .setMIFlag(MachineInstr::FrameDestroy
);
1030 CopyReg
= getNextOrderedReg(std::next(CopyReg
),
1031 OrderedCopyRegs
.end(),
1033 HiRegToRestore
= getNextOrderedReg(std::next(HiRegToRestore
),
1034 OrderedHighRegs
.end(),
1039 // Restore low register used as scratch if necessary
1040 if (LowScratchReg
.isValid()) {
1041 BuildMI(MBB
, MI
, DL
, TII
.get(ARM::tMOVr
))
1042 .addReg(LowScratchReg
, RegState::Define
)
1043 .addReg(ARM::R12
, RegState::Kill
)
1044 .add(predOps(ARMCC::AL
))
1045 .setMIFlag(MachineInstr::FrameDestroy
);
1048 // Now pop the low registers
1049 if (!LowRegs
.empty()) {
1050 MachineInstrBuilder MIB
= BuildMI(MF
, DL
, TII
.get(ARM::tPOP
))
1051 .add(predOps(ARMCC::AL
))
1052 .setMIFlag(MachineInstr::FrameDestroy
);
1054 bool NeedsPop
= false;
1055 for (Register Reg
: OrderedLowRegs
) {
1056 if (!LowRegs
.count(Reg
))
1059 if (Reg
== ARM::LR
) {
1060 if (!MBB
.succ_empty() || MI
->getOpcode() == ARM::TCRETURNdi
||
1061 MI
->getOpcode() == ARM::TCRETURNri
||
1062 MI
->getOpcode() == ARM::TCRETURNrinotr12
)
1063 // LR may only be popped into PC, as part of return sequence.
1064 // If this isn't the return sequence, we'll need emitPopSpecialFixUp
1065 // to restore LR the hard way.
1066 // FIXME: if we don't pass any stack arguments it would be actually
1067 // advantageous *and* correct to do the conversion to an ordinary call
1068 // instruction here.
1070 // Special epilogue for vararg functions. See emitEpilogue
1073 // ARMv4T requires BX, see emitEpilogue
1077 // CMSE entry functions must return via BXNS, see emitEpilogue.
1078 if (AFI
->isCmseNSEntryFunction())
1083 (*MIB
).setDesc(TII
.get(ARM::tPOP_RET
));
1084 if (MI
!= MBB
.end())
1085 MIB
.copyImplicitOps(*MI
);
1088 MIB
.addReg(Reg
, getDefRegState(true));
1092 // It's illegal to emit pop instruction without operands.
1094 MBB
.insert(MI
, &*MIB
);
1096 MF
.deleteMachineInstr(MIB
);
1100 bool Thumb1FrameLowering::spillCalleeSavedRegisters(
1101 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
1102 ArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
1106 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
1107 MachineFunction
&MF
= *MBB
.getParent();
1108 const ARMBaseRegisterInfo
*RegInfo
= static_cast<const ARMBaseRegisterInfo
*>(
1109 MF
.getSubtarget().getRegisterInfo());
1110 Register FPReg
= RegInfo
->getFrameRegister(MF
);
1112 // In case FP is a high reg, we need a separate push sequence to generate
1113 // a correct Frame Record
1114 bool NeedsFrameRecordPush
= hasFP(MF
) && ARM::hGPRRegClass
.contains(FPReg
);
1115 bool LRLiveIn
= MF
.getRegInfo().isLiveIn(ARM::LR
);
1116 bool UsedLRAsTemp
= false;
1118 std::set
<Register
> FrameRecord
;
1119 std::set
<Register
> SpilledGPRs
;
1120 for (const CalleeSavedInfo
&I
: CSI
) {
1121 Register Reg
= I
.getReg();
1122 if (NeedsFrameRecordPush
&& (Reg
== FPReg
|| Reg
== ARM::LR
))
1123 FrameRecord
.insert(Reg
);
1125 SpilledGPRs
.insert(Reg
);
1128 // Determine intermediate registers which can be used for pushing the frame
1130 // - Unused argument registers
1131 // - LR: This is possible because the first PUSH will save it on the stack,
1132 // so it is free to be used as a temporary for the second. However, it
1133 // is possible for LR to be live-in to the function, in which case we
1134 // will need to restore it later in the prologue, so we only use this
1135 // if there are no free argument registers.
1136 std::set
<Register
> FrameRecordCopyRegs
;
1137 for (unsigned ArgReg
: {ARM::R0
, ARM::R1
, ARM::R2
, ARM::R3
})
1138 if (!MF
.getRegInfo().isLiveIn(ArgReg
))
1139 FrameRecordCopyRegs
.insert(ArgReg
);
1140 if (FrameRecordCopyRegs
.empty())
1141 FrameRecordCopyRegs
.insert(ARM::LR
);
1143 pushRegsToStack(MBB
, MI
, TII
, FrameRecord
, FrameRecordCopyRegs
, UsedLRAsTemp
);
1145 // Determine intermediate registers which can be used for pushing high regs:
1146 // - Spilled low regs
1147 // - Unused argument registers
1148 std::set
<Register
> CopyRegs
;
1149 for (Register Reg
: SpilledGPRs
)
1150 if ((ARM::tGPRRegClass
.contains(Reg
) || Reg
== ARM::LR
) &&
1151 !MF
.getRegInfo().isLiveIn(Reg
) && !(hasFP(MF
) && Reg
== FPReg
))
1152 CopyRegs
.insert(Reg
);
1153 for (unsigned ArgReg
: {ARM::R0
, ARM::R1
, ARM::R2
, ARM::R3
})
1154 if (!MF
.getRegInfo().isLiveIn(ArgReg
))
1155 CopyRegs
.insert(ArgReg
);
1157 pushRegsToStack(MBB
, MI
, TII
, SpilledGPRs
, CopyRegs
, UsedLRAsTemp
);
1159 // If the push sequence used LR as a temporary, and LR is live-in (for
1160 // example because it is used by the llvm.returnaddress intrinsic), then we
1161 // need to reload it from the stack. Thumb1 does not have a load instruction
1162 // which can use LR, so we need to load into a temporary low register and
1164 if (LRLiveIn
&& UsedLRAsTemp
) {
1165 auto CopyRegIt
= getNextOrderedReg(OrderedCopyRegs
.rbegin(),
1166 OrderedCopyRegs
.rend(), CopyRegs
);
1167 assert(CopyRegIt
!= OrderedCopyRegs
.rend());
1168 unsigned NumRegsPushed
= FrameRecord
.size() + SpilledGPRs
.size();
1170 dbgs() << "LR is live-in but clobbered in prologue, restoring via "
1171 << RegInfo
->getName(*CopyRegIt
) << "\n");
1173 BuildMI(MBB
, MI
, DebugLoc(), TII
.get(ARM::tLDRspi
), *CopyRegIt
)
1175 .addImm(NumRegsPushed
- 1)
1176 .add(predOps(ARMCC::AL
))
1177 .setMIFlags(MachineInstr::FrameSetup
);
1179 BuildMI(MBB
, MI
, DebugLoc(), TII
.get(ARM::tMOVr
), ARM::LR
)
1181 .add(predOps(ARMCC::AL
))
1182 .setMIFlags(MachineInstr::FrameSetup
);
1188 bool Thumb1FrameLowering::restoreCalleeSavedRegisters(
1189 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
1190 MutableArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
1194 MachineFunction
&MF
= *MBB
.getParent();
1195 ARMFunctionInfo
*AFI
= MF
.getInfo
<ARMFunctionInfo
>();
1196 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
1197 const ARMBaseRegisterInfo
*RegInfo
= static_cast<const ARMBaseRegisterInfo
*>(
1198 MF
.getSubtarget().getRegisterInfo());
1199 bool IsVarArg
= AFI
->getArgRegsSaveSize() > 0;
1200 Register FPReg
= RegInfo
->getFrameRegister(MF
);
1202 // In case FP is a high reg, we need a separate pop sequence to generate
1203 // a correct Frame Record
1204 bool NeedsFrameRecordPop
= hasFP(MF
) && ARM::hGPRRegClass
.contains(FPReg
);
1206 std::set
<Register
> FrameRecord
;
1207 std::set
<Register
> SpilledGPRs
;
1208 for (CalleeSavedInfo
&I
: CSI
) {
1209 Register Reg
= I
.getReg();
1210 if (NeedsFrameRecordPop
&& (Reg
== FPReg
|| Reg
== ARM::LR
))
1211 FrameRecord
.insert(Reg
);
1213 SpilledGPRs
.insert(Reg
);
1216 I
.setRestored(false);
1219 // Determine intermidiate registers which can be used for popping high regs:
1220 // - Spilled low regs
1221 // - Unused return registers
1222 std::set
<Register
> CopyRegs
;
1223 std::set
<Register
> UnusedReturnRegs
;
1224 for (Register Reg
: SpilledGPRs
)
1225 if ((ARM::tGPRRegClass
.contains(Reg
)) && !(hasFP(MF
) && Reg
== FPReg
))
1226 CopyRegs
.insert(Reg
);
1227 auto Terminator
= MBB
.getFirstTerminator();
1228 if (Terminator
!= MBB
.end() && Terminator
->getOpcode() == ARM::tBX_RET
) {
1229 UnusedReturnRegs
.insert(ARM::R0
);
1230 UnusedReturnRegs
.insert(ARM::R1
);
1231 UnusedReturnRegs
.insert(ARM::R2
);
1232 UnusedReturnRegs
.insert(ARM::R3
);
1233 for (auto Op
: Terminator
->implicit_operands()) {
1235 UnusedReturnRegs
.erase(Op
.getReg());
1238 CopyRegs
.insert(UnusedReturnRegs
.begin(), UnusedReturnRegs
.end());
1240 // First pop regular spilled regs.
1241 popRegsFromStack(MBB
, MI
, TII
, SpilledGPRs
, CopyRegs
, IsVarArg
,
1244 // LR may only be popped into pc, as part of a return sequence.
1245 // Check that no other pop instructions are inserted after that.
1246 assert((!SpilledGPRs
.count(ARM::LR
) || FrameRecord
.empty()) &&
1247 "Can't insert pop after return sequence");
1249 // Now pop Frame Record regs.
1250 // Only unused return registers can be used as copy regs at this point.
1251 popRegsFromStack(MBB
, MI
, TII
, FrameRecord
, UnusedReturnRegs
, IsVarArg
,