1 //===-- AVRFrameLowering.cpp - AVR 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 AVR implementation of TargetFrameLowering class.
11 //===----------------------------------------------------------------------===//
13 #include "AVRFrameLowering.h"
16 #include "AVRInstrInfo.h"
17 #include "AVRMachineFunctionInfo.h"
18 #include "AVRTargetMachine.h"
19 #include "MCTargetDesc/AVRMCTargetDesc.h"
21 #include "llvm/CodeGen/MachineFrameInfo.h"
22 #include "llvm/CodeGen/MachineFunction.h"
23 #include "llvm/CodeGen/MachineFunctionPass.h"
24 #include "llvm/CodeGen/MachineInstrBuilder.h"
25 #include "llvm/CodeGen/MachineRegisterInfo.h"
26 #include "llvm/IR/Function.h"
32 AVRFrameLowering::AVRFrameLowering()
33 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown
, Align(1), -2) {}
35 bool AVRFrameLowering::canSimplifyCallFramePseudos(
36 const MachineFunction
&MF
) const {
37 // Always simplify call frame pseudo instructions, even when
38 // hasReservedCallFrame is false.
42 bool AVRFrameLowering::hasReservedCallFrame(const MachineFunction
&MF
) const {
43 // Reserve call frame memory in function prologue under the following
45 // - Y pointer is reserved to be the frame pointer.
46 // - The function does not contain variable sized objects.
48 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
49 return hasFP(MF
) && !MFI
.hasVarSizedObjects();
52 void AVRFrameLowering::emitPrologue(MachineFunction
&MF
,
53 MachineBasicBlock
&MBB
) const {
54 MachineBasicBlock::iterator MBBI
= MBB
.begin();
55 DebugLoc DL
= (MBBI
!= MBB
.end()) ? MBBI
->getDebugLoc() : DebugLoc();
56 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
57 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
58 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
59 bool HasFP
= hasFP(MF
);
61 // Interrupt handlers re-enable interrupts in function entry.
62 if (AFI
->isInterruptHandler()) {
63 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::BSETs
))
65 .setMIFlag(MachineInstr::FrameSetup
);
68 // Emit special prologue code to save R1, R0 and SREG in interrupt/signal
69 // handlers before saving any other registers.
70 if (AFI
->isInterruptOrSignalHandler()) {
71 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHWRr
))
72 .addReg(AVR::R1R0
, RegState::Kill
)
73 .setMIFlag(MachineInstr::FrameSetup
);
75 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::INRdA
), AVR::R0
)
77 .setMIFlag(MachineInstr::FrameSetup
);
78 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
79 .addReg(AVR::R0
, RegState::Kill
)
80 .setMIFlag(MachineInstr::FrameSetup
);
81 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::EORRdRr
))
82 .addReg(AVR::R0
, RegState::Define
)
83 .addReg(AVR::R0
, RegState::Kill
)
84 .addReg(AVR::R0
, RegState::Kill
)
85 .setMIFlag(MachineInstr::FrameSetup
);
86 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::EORRdRr
))
87 .addReg(AVR::R1
, RegState::Define
)
88 .addReg(AVR::R1
, RegState::Kill
)
89 .addReg(AVR::R1
, RegState::Kill
)
90 .setMIFlag(MachineInstr::FrameSetup
);
93 // Early exit if the frame pointer is not needed in this function.
98 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
99 unsigned FrameSize
= MFI
.getStackSize() - AFI
->getCalleeSavedFrameSize();
101 // Skip the callee-saved push instructions.
103 (MBBI
!= MBB
.end()) && MBBI
->getFlag(MachineInstr::FrameSetup
) &&
104 (MBBI
->getOpcode() == AVR::PUSHRr
|| MBBI
->getOpcode() == AVR::PUSHWRr
)) {
108 // Update Y with the new base value.
109 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPREAD
), AVR::R29R28
)
111 .setMIFlag(MachineInstr::FrameSetup
);
113 // Mark the FramePtr as live-in in every block except the entry.
114 for (MachineFunction::iterator I
= std::next(MF
.begin()), E
= MF
.end();
116 I
->addLiveIn(AVR::R29R28
);
123 // Reserve the necessary frame memory by doing FP -= <size>.
124 unsigned Opcode
= (isUInt
<6>(FrameSize
)) ? AVR::SBIWRdK
: AVR::SUBIWRdK
;
126 MachineInstr
*MI
= BuildMI(MBB
, MBBI
, DL
, TII
.get(Opcode
), AVR::R29R28
)
127 .addReg(AVR::R29R28
, RegState::Kill
)
129 .setMIFlag(MachineInstr::FrameSetup
);
130 // The SREG implicit def is dead.
131 MI
->getOperand(3).setIsDead();
133 // Write back R29R28 to SP and temporarily disable interrupts.
134 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
136 .setMIFlag(MachineInstr::FrameSetup
);
139 static void restoreStatusRegister(MachineFunction
&MF
, MachineBasicBlock
&MBB
) {
140 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
142 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
144 DebugLoc DL
= MBBI
->getDebugLoc();
145 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
146 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
148 // Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal
149 // handlers at the very end of the function, just before reti.
150 if (AFI
->isInterruptOrSignalHandler()) {
151 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), AVR::R0
);
152 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::OUTARr
))
154 .addReg(AVR::R0
, RegState::Kill
);
155 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPWRd
), AVR::R1R0
);
159 void AVRFrameLowering::emitEpilogue(MachineFunction
&MF
,
160 MachineBasicBlock
&MBB
) const {
161 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
163 // Early exit if the frame pointer is not needed in this function except for
164 // signal/interrupt handlers where special code generation is required.
165 if (!hasFP(MF
) && !AFI
->isInterruptOrSignalHandler()) {
169 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
170 assert(MBBI
->getDesc().isReturn() &&
171 "Can only insert epilog into returning blocks");
173 DebugLoc DL
= MBBI
->getDebugLoc();
174 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
175 unsigned FrameSize
= MFI
.getStackSize() - AFI
->getCalleeSavedFrameSize();
176 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
177 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
179 // Early exit if there is no need to restore the frame pointer.
181 restoreStatusRegister(MF
, MBB
);
185 // Skip the callee-saved pop instructions.
186 while (MBBI
!= MBB
.begin()) {
187 MachineBasicBlock::iterator PI
= std::prev(MBBI
);
188 int Opc
= PI
->getOpcode();
190 if (Opc
!= AVR::POPRd
&& Opc
!= AVR::POPWRd
&& !PI
->isTerminator()) {
199 // Select the optimal opcode depending on how big it is.
200 if (isUInt
<6>(FrameSize
)) {
201 Opcode
= AVR::ADIWRdK
;
203 Opcode
= AVR::SUBIWRdK
;
204 FrameSize
= -FrameSize
;
207 // Restore the frame pointer by doing FP += <size>.
208 MachineInstr
*MI
= BuildMI(MBB
, MBBI
, DL
, TII
.get(Opcode
), AVR::R29R28
)
209 .addReg(AVR::R29R28
, RegState::Kill
)
211 // The SREG implicit def is dead.
212 MI
->getOperand(3).setIsDead();
214 // Write back R29R28 to SP and temporarily disable interrupts.
215 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
216 .addReg(AVR::R29R28
, RegState::Kill
);
218 restoreStatusRegister(MF
, MBB
);
221 // Return true if the specified function should have a dedicated frame
222 // pointer register. This is true if the function meets any of the following
224 // - a register has been spilled
226 // - input arguments are passed using the stack
228 // Notice that strictly this is not a frame pointer because it contains SP after
229 // frame allocation instead of having the original SP in function entry.
230 bool AVRFrameLowering::hasFP(const MachineFunction
&MF
) const {
231 const AVRMachineFunctionInfo
*FuncInfo
= MF
.getInfo
<AVRMachineFunctionInfo
>();
233 return (FuncInfo
->getHasSpills() || FuncInfo
->getHasAllocas() ||
234 FuncInfo
->getHasStackArgs());
237 bool AVRFrameLowering::spillCalleeSavedRegisters(
238 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
239 ArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
244 unsigned CalleeFrameSize
= 0;
245 DebugLoc DL
= MBB
.findDebugLoc(MI
);
246 MachineFunction
&MF
= *MBB
.getParent();
247 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
248 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
249 AVRMachineFunctionInfo
*AVRFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
251 for (unsigned i
= CSI
.size(); i
!= 0; --i
) {
252 unsigned Reg
= CSI
[i
- 1].getReg();
253 bool IsNotLiveIn
= !MBB
.isLiveIn(Reg
);
255 assert(TRI
->getRegSizeInBits(*TRI
->getMinimalPhysRegClass(Reg
)) == 8 &&
256 "Invalid register size");
258 // Add the callee-saved register as live-in only if it is not already a
259 // live-in register, this usually happens with arguments that are passed
260 // through callee-saved registers.
265 // Do not kill the register when it is an input argument.
266 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::PUSHRr
))
267 .addReg(Reg
, getKillRegState(IsNotLiveIn
))
268 .setMIFlag(MachineInstr::FrameSetup
);
272 AVRFI
->setCalleeSavedFrameSize(CalleeFrameSize
);
277 bool AVRFrameLowering::restoreCalleeSavedRegisters(
278 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
279 MutableArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
284 DebugLoc DL
= MBB
.findDebugLoc(MI
);
285 const MachineFunction
&MF
= *MBB
.getParent();
286 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
287 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
289 for (const CalleeSavedInfo
&CCSI
: CSI
) {
290 unsigned Reg
= CCSI
.getReg();
292 assert(TRI
->getRegSizeInBits(*TRI
->getMinimalPhysRegClass(Reg
)) == 8 &&
293 "Invalid register size");
295 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::POPRd
), Reg
);
301 /// Replace pseudo store instructions that pass arguments through the stack with
302 /// real instructions.
303 static void fixStackStores(MachineBasicBlock
&MBB
,
304 MachineBasicBlock::iterator MI
,
305 const TargetInstrInfo
&TII
, Register FP
) {
306 // Iterate through the BB until we hit a call instruction or we reach the end.
307 for (auto I
= MI
, E
= MBB
.end(); I
!= E
&& !I
->isCall();) {
308 MachineBasicBlock::iterator NextMI
= std::next(I
);
309 MachineInstr
&MI
= *I
;
310 unsigned Opcode
= I
->getOpcode();
312 // Only care of pseudo store instructions where SP is the base pointer.
313 if (Opcode
!= AVR::STDSPQRr
&& Opcode
!= AVR::STDWSPQRr
) {
318 assert(MI
.getOperand(0).getReg() == AVR::SP
&&
319 "Invalid register, should be SP!");
321 // Replace this instruction with a regular store. Use Y as the base
322 // pointer since it is guaranteed to contain a copy of SP.
324 (Opcode
== AVR::STDWSPQRr
) ? AVR::STDWPtrQRr
: AVR::STDPtrQRr
;
326 MI
.setDesc(TII
.get(STOpc
));
327 MI
.getOperand(0).setReg(FP
);
333 MachineBasicBlock::iterator
AVRFrameLowering::eliminateCallFramePseudoInstr(
334 MachineFunction
&MF
, MachineBasicBlock
&MBB
,
335 MachineBasicBlock::iterator MI
) const {
336 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
337 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
339 // There is nothing to insert when the call frame memory is allocated during
340 // function entry. Delete the call frame pseudo and replace all pseudo stores
341 // with real store instructions.
342 if (hasReservedCallFrame(MF
)) {
343 fixStackStores(MBB
, MI
, TII
, AVR::R29R28
);
344 return MBB
.erase(MI
);
347 DebugLoc DL
= MI
->getDebugLoc();
348 unsigned int Opcode
= MI
->getOpcode();
349 int Amount
= TII
.getFrameSize(*MI
);
351 // ADJCALLSTACKUP and ADJCALLSTACKDOWN are converted to adiw/subi
352 // instructions to read and write the stack pointer in I/O space.
354 assert(getStackAlign() == Align(1) && "Unsupported stack alignment");
356 if (Opcode
== TII
.getCallFrameSetupOpcode()) {
357 // Update the stack pointer.
358 // In many cases this can be done far more efficiently by pushing the
359 // relevant values directly to the stack. However, doing that correctly
360 // (in the right order, possibly skipping some empty space for undef
361 // values, etc) is tricky and thus left to be optimized in the future.
362 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPREAD
), AVR::R31R30
).addReg(AVR::SP
);
364 MachineInstr
*New
= BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SUBIWRdK
), AVR::R31R30
)
365 .addReg(AVR::R31R30
, RegState::Kill
)
367 New
->getOperand(3).setIsDead();
369 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
370 .addReg(AVR::R31R30
);
372 // Make sure the remaining stack stores are converted to real store
374 fixStackStores(MBB
, MI
, TII
, AVR::R31R30
);
376 assert(Opcode
== TII
.getCallFrameDestroyOpcode());
378 // Note that small stack changes could be implemented more efficiently
379 // with a few pop instructions instead of the 8-9 instructions now
382 // Select the best opcode to adjust SP based on the offset size.
384 if (isUInt
<6>(Amount
)) {
385 addOpcode
= AVR::ADIWRdK
;
387 addOpcode
= AVR::SUBIWRdK
;
391 // Build the instruction sequence.
392 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPREAD
), AVR::R31R30
).addReg(AVR::SP
);
394 MachineInstr
*New
= BuildMI(MBB
, MI
, DL
, TII
.get(addOpcode
), AVR::R31R30
)
395 .addReg(AVR::R31R30
, RegState::Kill
)
397 New
->getOperand(3).setIsDead();
399 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
400 .addReg(AVR::R31R30
, RegState::Kill
);
404 return MBB
.erase(MI
);
407 void AVRFrameLowering::determineCalleeSaves(MachineFunction
&MF
,
408 BitVector
&SavedRegs
,
409 RegScavenger
*RS
) const {
410 TargetFrameLowering::determineCalleeSaves(MF
, SavedRegs
, RS
);
412 // If we have a frame pointer, the Y register needs to be saved as well.
414 SavedRegs
.set(AVR::R29
);
415 SavedRegs
.set(AVR::R28
);
418 /// The frame analyzer pass.
420 /// Scans the function for allocas and used arguments
421 /// that are passed through the stack.
422 struct AVRFrameAnalyzer
: public MachineFunctionPass
{
424 AVRFrameAnalyzer() : MachineFunctionPass(ID
) {}
426 bool runOnMachineFunction(MachineFunction
&MF
) override
{
427 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
428 AVRMachineFunctionInfo
*FuncInfo
= MF
.getInfo
<AVRMachineFunctionInfo
>();
430 // If there are no fixed frame indexes during this stage it means there
431 // are allocas present in the function.
432 if (MFI
.getNumObjects() != MFI
.getNumFixedObjects()) {
433 // Check for the type of allocas present in the function. We only care
434 // about fixed size allocas so do not give false positives if only
435 // variable sized allocas are present.
436 for (unsigned i
= 0, e
= MFI
.getObjectIndexEnd(); i
!= e
; ++i
) {
437 // Variable sized objects have size 0.
438 if (MFI
.getObjectSize(i
)) {
439 FuncInfo
->setHasAllocas(true);
445 // If there are fixed frame indexes present, scan the function to see if
446 // they are really being used.
447 if (MFI
.getNumFixedObjects() == 0) {
451 // Ok fixed frame indexes present, now scan the function to see if they
452 // are really being used, otherwise we can ignore them.
453 for (const MachineBasicBlock
&BB
: MF
) {
454 for (const MachineInstr
&MI
: BB
) {
455 int Opcode
= MI
.getOpcode();
457 if ((Opcode
!= AVR::LDDRdPtrQ
) && (Opcode
!= AVR::LDDWRdPtrQ
) &&
458 (Opcode
!= AVR::STDPtrQRr
) && (Opcode
!= AVR::STDWPtrQRr
)) {
462 for (const MachineOperand
&MO
: MI
.operands()) {
467 if (MFI
.isFixedObjectIndex(MO
.getIndex())) {
468 FuncInfo
->setHasStackArgs(true);
478 StringRef
getPassName() const override
{ return "AVR Frame Analyzer"; }
481 char AVRFrameAnalyzer::ID
= 0;
483 /// Creates instance of the frame analyzer pass.
484 FunctionPass
*createAVRFrameAnalyzerPass() { return new AVRFrameAnalyzer(); }
486 /// Create the Dynalloca Stack Pointer Save/Restore pass.
487 /// Insert a copy of SP before allocating the dynamic stack memory and restore
488 /// it in function exit to restore the original SP state. This avoids the need
489 /// of reserving a register pair for a frame pointer.
490 struct AVRDynAllocaSR
: public MachineFunctionPass
{
492 AVRDynAllocaSR() : MachineFunctionPass(ID
) {}
494 bool runOnMachineFunction(MachineFunction
&MF
) override
{
495 // Early exit when there are no variable sized objects in the function.
496 if (!MF
.getFrameInfo().hasVarSizedObjects()) {
500 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
501 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
502 MachineBasicBlock
&EntryMBB
= MF
.front();
503 MachineBasicBlock::iterator MBBI
= EntryMBB
.begin();
504 DebugLoc DL
= EntryMBB
.findDebugLoc(MBBI
);
507 MF
.getRegInfo().createVirtualRegister(&AVR::DREGSRegClass
);
509 // Create a copy of SP in function entry before any dynallocas are
511 BuildMI(EntryMBB
, MBBI
, DL
, TII
.get(AVR::COPY
), SPCopy
).addReg(AVR::SP
);
513 // Restore SP in all exit basic blocks.
514 for (MachineBasicBlock
&MBB
: MF
) {
515 // If last instruction is a return instruction, add a restore copy.
516 if (!MBB
.empty() && MBB
.back().isReturn()) {
517 MBBI
= MBB
.getLastNonDebugInstr();
518 DL
= MBBI
->getDebugLoc();
519 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::COPY
), AVR::SP
)
520 .addReg(SPCopy
, RegState::Kill
);
527 StringRef
getPassName() const override
{
528 return "AVR dynalloca stack pointer save/restore";
532 char AVRDynAllocaSR::ID
= 0;
534 /// createAVRDynAllocaSRPass - returns an instance of the dynalloca stack
535 /// pointer save/restore pass.
536 FunctionPass
*createAVRDynAllocaSRPass() { return new AVRDynAllocaSR(); }
538 } // end of namespace llvm