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"
29 AVRFrameLowering::AVRFrameLowering()
30 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown
, Align(1), -2) {}
32 bool AVRFrameLowering::canSimplifyCallFramePseudos(
33 const MachineFunction
&MF
) const {
34 // Always simplify call frame pseudo instructions, even when
35 // hasReservedCallFrame is false.
39 bool AVRFrameLowering::hasReservedCallFrame(const MachineFunction
&MF
) const {
40 // Reserve call frame memory in function prologue under the following
42 // - Y pointer is reserved to be the frame pointer.
43 // - The function does not contain variable sized objects.
45 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
46 return hasFP(MF
) && !MFI
.hasVarSizedObjects();
49 void AVRFrameLowering::emitPrologue(MachineFunction
&MF
,
50 MachineBasicBlock
&MBB
) const {
51 MachineBasicBlock::iterator MBBI
= MBB
.begin();
52 DebugLoc DL
= (MBBI
!= MBB
.end()) ? MBBI
->getDebugLoc() : DebugLoc();
53 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
54 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
55 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
56 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
57 bool HasFP
= hasFP(MF
);
59 // Interrupt handlers re-enable interrupts in function entry.
60 if (AFI
->isInterruptHandler()) {
61 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::BSETs
))
63 .setMIFlag(MachineInstr::FrameSetup
);
66 // Emit special prologue code to save R1, R0 and SREG in interrupt/signal
67 // handlers before saving any other registers.
68 if (AFI
->isInterruptOrSignalHandler()) {
69 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
70 .addReg(STI
.getTmpRegister(), RegState::Kill
)
71 .setMIFlag(MachineInstr::FrameSetup
);
73 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::INRdA
), STI
.getTmpRegister())
74 .addImm(STI
.getIORegSREG())
75 .setMIFlag(MachineInstr::FrameSetup
);
76 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
77 .addReg(STI
.getTmpRegister(), RegState::Kill
)
78 .setMIFlag(MachineInstr::FrameSetup
);
79 if (!MRI
.reg_empty(STI
.getZeroRegister())) {
80 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
81 .addReg(STI
.getZeroRegister(), RegState::Kill
)
82 .setMIFlag(MachineInstr::FrameSetup
);
83 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::EORRdRr
))
84 .addReg(STI
.getZeroRegister(), RegState::Define
)
85 .addReg(STI
.getZeroRegister(), RegState::Kill
)
86 .addReg(STI
.getZeroRegister(), RegState::Kill
)
87 .setMIFlag(MachineInstr::FrameSetup
);
91 // Early exit if the frame pointer is not needed in this function.
96 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
97 unsigned FrameSize
= MFI
.getStackSize() - AFI
->getCalleeSavedFrameSize();
99 // Skip the callee-saved push instructions.
101 (MBBI
!= MBB
.end()) && MBBI
->getFlag(MachineInstr::FrameSetup
) &&
102 (MBBI
->getOpcode() == AVR::PUSHRr
|| MBBI
->getOpcode() == AVR::PUSHWRr
)) {
106 // Update Y with the new base value.
107 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPREAD
), AVR::R29R28
)
109 .setMIFlag(MachineInstr::FrameSetup
);
111 // Mark the FramePtr as live-in in every block except the entry.
112 for (MachineBasicBlock
&MBBJ
: llvm::drop_begin(MF
)) {
113 MBBJ
.addLiveIn(AVR::R29R28
);
120 // Reserve the necessary frame memory by doing FP -= <size>.
121 unsigned Opcode
= (isUInt
<6>(FrameSize
) && STI
.hasADDSUBIW()) ? AVR::SBIWRdK
124 MachineInstr
*MI
= BuildMI(MBB
, MBBI
, DL
, TII
.get(Opcode
), AVR::R29R28
)
125 .addReg(AVR::R29R28
, RegState::Kill
)
127 .setMIFlag(MachineInstr::FrameSetup
);
128 // The SREG implicit def is dead.
129 MI
->getOperand(3).setIsDead();
131 // Write back R29R28 to SP and temporarily disable interrupts.
132 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
134 .setMIFlag(MachineInstr::FrameSetup
);
137 static void restoreStatusRegister(MachineFunction
&MF
, MachineBasicBlock
&MBB
) {
138 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
139 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
141 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
143 DebugLoc DL
= MBBI
->getDebugLoc();
144 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
145 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
147 // Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal
148 // handlers at the very end of the function, just before reti.
149 if (AFI
->isInterruptOrSignalHandler()) {
150 if (!MRI
.reg_empty(STI
.getZeroRegister())) {
151 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), STI
.getZeroRegister());
153 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), STI
.getTmpRegister());
154 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::OUTARr
))
155 .addImm(STI
.getIORegSREG())
156 .addReg(STI
.getTmpRegister(), RegState::Kill
);
157 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), STI
.getTmpRegister());
161 void AVRFrameLowering::emitEpilogue(MachineFunction
&MF
,
162 MachineBasicBlock
&MBB
) const {
163 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
165 // Early exit if the frame pointer is not needed in this function except for
166 // signal/interrupt handlers where special code generation is required.
167 if (!hasFP(MF
) && !AFI
->isInterruptOrSignalHandler()) {
171 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
172 assert(MBBI
->getDesc().isReturn() &&
173 "Can only insert epilog into returning blocks");
175 DebugLoc DL
= MBBI
->getDebugLoc();
176 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
177 unsigned FrameSize
= MFI
.getStackSize() - AFI
->getCalleeSavedFrameSize();
178 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
179 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
181 // Early exit if there is no need to restore the frame pointer.
182 if (!FrameSize
&& !MF
.getFrameInfo().hasVarSizedObjects()) {
183 restoreStatusRegister(MF
, MBB
);
187 // Skip the callee-saved pop instructions.
188 while (MBBI
!= MBB
.begin()) {
189 MachineBasicBlock::iterator PI
= std::prev(MBBI
);
190 int Opc
= PI
->getOpcode();
192 if (Opc
!= AVR::POPRd
&& Opc
!= AVR::POPWRd
&& !PI
->isTerminator()) {
202 // Select the optimal opcode depending on how big it is.
203 if (isUInt
<6>(FrameSize
) && STI
.hasADDSUBIW()) {
204 Opcode
= AVR::ADIWRdK
;
206 Opcode
= AVR::SUBIWRdK
;
207 FrameSize
= -FrameSize
;
210 // Restore the frame pointer by doing FP += <size>.
211 MachineInstr
*MI
= BuildMI(MBB
, MBBI
, DL
, TII
.get(Opcode
), AVR::R29R28
)
212 .addReg(AVR::R29R28
, RegState::Kill
)
214 // The SREG implicit def is dead.
215 MI
->getOperand(3).setIsDead();
218 // Write back R29R28 to SP and temporarily disable interrupts.
219 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
220 .addReg(AVR::R29R28
, RegState::Kill
);
222 restoreStatusRegister(MF
, MBB
);
225 // Return true if the specified function should have a dedicated frame
226 // pointer register. This is true if the function meets any of the following
228 // - a register has been spilled
230 // - input arguments are passed using the stack
232 // Notice that strictly this is not a frame pointer because it contains SP after
233 // frame allocation instead of having the original SP in function entry.
234 bool AVRFrameLowering::hasFPImpl(const MachineFunction
&MF
) const {
235 const AVRMachineFunctionInfo
*FuncInfo
= MF
.getInfo
<AVRMachineFunctionInfo
>();
237 return (FuncInfo
->getHasSpills() || FuncInfo
->getHasAllocas() ||
238 FuncInfo
->getHasStackArgs() ||
239 MF
.getFrameInfo().hasVarSizedObjects());
242 bool AVRFrameLowering::spillCalleeSavedRegisters(
243 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
244 ArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
249 unsigned CalleeFrameSize
= 0;
250 DebugLoc DL
= MBB
.findDebugLoc(MI
);
251 MachineFunction
&MF
= *MBB
.getParent();
252 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
253 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
254 AVRMachineFunctionInfo
*AVRFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
256 for (const CalleeSavedInfo
&I
: llvm::reverse(CSI
)) {
257 Register Reg
= I
.getReg();
258 bool IsNotLiveIn
= !MBB
.isLiveIn(Reg
);
260 // Check if Reg is a sub register of a 16-bit livein register, and then
261 // add it to the livein list.
263 for (const auto &LiveIn
: MBB
.liveins())
264 if (STI
.getRegisterInfo()->isSubRegister(LiveIn
.PhysReg
, Reg
)) {
270 assert(TRI
->getRegSizeInBits(*TRI
->getMinimalPhysRegClass(Reg
)) == 8 &&
271 "Invalid register size");
273 // Add the callee-saved register as live-in only if it is not already a
274 // live-in register, this usually happens with arguments that are passed
275 // through callee-saved registers.
280 // Do not kill the register when it is an input argument.
281 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::PUSHRr
))
282 .addReg(Reg
, getKillRegState(IsNotLiveIn
))
283 .setMIFlag(MachineInstr::FrameSetup
);
287 AVRFI
->setCalleeSavedFrameSize(CalleeFrameSize
);
292 bool AVRFrameLowering::restoreCalleeSavedRegisters(
293 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
294 MutableArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
299 DebugLoc DL
= MBB
.findDebugLoc(MI
);
300 const MachineFunction
&MF
= *MBB
.getParent();
301 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
302 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
304 for (const CalleeSavedInfo
&CCSI
: CSI
) {
305 Register Reg
= CCSI
.getReg();
307 assert(TRI
->getRegSizeInBits(*TRI
->getMinimalPhysRegClass(Reg
)) == 8 &&
308 "Invalid register size");
310 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::POPRd
), Reg
);
316 /// Replace pseudo store instructions that pass arguments through the stack with
317 /// real instructions.
318 static void fixStackStores(MachineBasicBlock
&MBB
,
319 MachineBasicBlock::iterator StartMI
,
320 const TargetInstrInfo
&TII
) {
321 // Iterate through the BB until we hit a call instruction or we reach the end.
322 for (MachineInstr
&MI
:
323 llvm::make_early_inc_range(llvm::make_range(StartMI
, MBB
.end()))) {
327 unsigned Opcode
= MI
.getOpcode();
329 // Only care of pseudo store instructions where SP is the base pointer.
330 if (Opcode
!= AVR::STDSPQRr
&& Opcode
!= AVR::STDWSPQRr
)
333 assert(MI
.getOperand(0).getReg() == AVR::SP
&&
334 "SP is expected as base pointer");
336 // Replace this instruction with a regular store. Use Y as the base
337 // pointer since it is guaranteed to contain a copy of SP.
339 (Opcode
== AVR::STDWSPQRr
) ? AVR::STDWPtrQRr
: AVR::STDPtrQRr
;
341 MI
.setDesc(TII
.get(STOpc
));
342 MI
.getOperand(0).setReg(AVR::R31R30
);
346 MachineBasicBlock::iterator
AVRFrameLowering::eliminateCallFramePseudoInstr(
347 MachineFunction
&MF
, MachineBasicBlock
&MBB
,
348 MachineBasicBlock::iterator MI
) const {
349 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
350 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
352 if (hasReservedCallFrame(MF
)) {
353 return MBB
.erase(MI
);
356 DebugLoc DL
= MI
->getDebugLoc();
357 unsigned int Opcode
= MI
->getOpcode();
358 int Amount
= TII
.getFrameSize(*MI
);
361 return MBB
.erase(MI
);
364 assert(getStackAlign() == Align(1) && "Unsupported stack alignment");
366 if (Opcode
== TII
.getCallFrameSetupOpcode()) {
367 // Update the stack pointer.
368 // In many cases this can be done far more efficiently by pushing the
369 // relevant values directly to the stack. However, doing that correctly
370 // (in the right order, possibly skipping some empty space for undef
371 // values, etc) is tricky and thus left to be optimized in the future.
372 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPREAD
), AVR::R31R30
).addReg(AVR::SP
);
375 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SUBIWRdK
), AVR::R31R30
)
376 .addReg(AVR::R31R30
, RegState::Kill
)
378 New
->getOperand(3).setIsDead();
380 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
).addReg(AVR::R31R30
);
382 // Make sure the remaining stack stores are converted to real store
384 fixStackStores(MBB
, MI
, TII
);
386 assert(Opcode
== TII
.getCallFrameDestroyOpcode());
388 // Note that small stack changes could be implemented more efficiently
389 // with a few pop instructions instead of the 8-9 instructions now
392 // Select the best opcode to adjust SP based on the offset size.
395 if (isUInt
<6>(Amount
) && STI
.hasADDSUBIW()) {
396 AddOpcode
= AVR::ADIWRdK
;
398 AddOpcode
= AVR::SUBIWRdK
;
402 // Build the instruction sequence.
403 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPREAD
), AVR::R31R30
).addReg(AVR::SP
);
405 MachineInstr
*New
= BuildMI(MBB
, MI
, DL
, TII
.get(AddOpcode
), AVR::R31R30
)
406 .addReg(AVR::R31R30
, RegState::Kill
)
408 New
->getOperand(3).setIsDead();
410 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
411 .addReg(AVR::R31R30
, RegState::Kill
);
414 return MBB
.erase(MI
);
417 void AVRFrameLowering::determineCalleeSaves(MachineFunction
&MF
,
418 BitVector
&SavedRegs
,
419 RegScavenger
*RS
) const {
420 TargetFrameLowering::determineCalleeSaves(MF
, SavedRegs
, RS
);
422 // If we have a frame pointer, the Y register needs to be saved as well.
424 SavedRegs
.set(AVR::R29
);
425 SavedRegs
.set(AVR::R28
);
428 /// The frame analyzer pass.
430 /// Scans the function for allocas and used arguments
431 /// that are passed through the stack.
432 struct AVRFrameAnalyzer
: public MachineFunctionPass
{
434 AVRFrameAnalyzer() : MachineFunctionPass(ID
) {}
436 bool runOnMachineFunction(MachineFunction
&MF
) override
{
437 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
438 AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
440 // If there are no fixed frame indexes during this stage it means there
441 // are allocas present in the function.
442 if (MFI
.getNumObjects() != MFI
.getNumFixedObjects()) {
443 // Check for the type of allocas present in the function. We only care
444 // about fixed size allocas so do not give false positives if only
445 // variable sized allocas are present.
446 for (unsigned i
= 0, e
= MFI
.getObjectIndexEnd(); i
!= e
; ++i
) {
447 // Variable sized objects have size 0.
448 if (MFI
.getObjectSize(i
)) {
449 AFI
->setHasAllocas(true);
455 // If there are fixed frame indexes present, scan the function to see if
456 // they are really being used.
457 if (MFI
.getNumFixedObjects() == 0) {
461 // Ok fixed frame indexes present, now scan the function to see if they
462 // are really being used, otherwise we can ignore them.
463 for (const MachineBasicBlock
&BB
: MF
) {
464 for (const MachineInstr
&MI
: BB
) {
465 int Opcode
= MI
.getOpcode();
467 if ((Opcode
!= AVR::LDDRdPtrQ
) && (Opcode
!= AVR::LDDWRdPtrQ
) &&
468 (Opcode
!= AVR::STDPtrQRr
) && (Opcode
!= AVR::STDWPtrQRr
) &&
469 (Opcode
!= AVR::FRMIDX
)) {
473 for (const MachineOperand
&MO
: MI
.operands()) {
478 if (MFI
.isFixedObjectIndex(MO
.getIndex())) {
479 AFI
->setHasStackArgs(true);
489 StringRef
getPassName() const override
{ return "AVR Frame Analyzer"; }
492 char AVRFrameAnalyzer::ID
= 0;
494 /// Creates instance of the frame analyzer pass.
495 FunctionPass
*createAVRFrameAnalyzerPass() { return new AVRFrameAnalyzer(); }
497 } // end of namespace llvm