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 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
60 bool HasFP
= hasFP(MF
);
62 // Interrupt handlers re-enable interrupts in function entry.
63 if (AFI
->isInterruptHandler()) {
64 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::BSETs
))
66 .setMIFlag(MachineInstr::FrameSetup
);
69 // Emit special prologue code to save R1, R0 and SREG in interrupt/signal
70 // handlers before saving any other registers.
71 if (AFI
->isInterruptOrSignalHandler()) {
72 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
73 .addReg(STI
.getTmpRegister(), RegState::Kill
)
74 .setMIFlag(MachineInstr::FrameSetup
);
76 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::INRdA
), STI
.getTmpRegister())
77 .addImm(STI
.getIORegSREG())
78 .setMIFlag(MachineInstr::FrameSetup
);
79 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
80 .addReg(STI
.getTmpRegister(), RegState::Kill
)
81 .setMIFlag(MachineInstr::FrameSetup
);
82 if (!MRI
.reg_empty(STI
.getZeroRegister())) {
83 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::PUSHRr
))
84 .addReg(STI
.getZeroRegister(), RegState::Kill
)
85 .setMIFlag(MachineInstr::FrameSetup
);
86 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::EORRdRr
))
87 .addReg(STI
.getZeroRegister(), RegState::Define
)
88 .addReg(STI
.getZeroRegister(), RegState::Kill
)
89 .addReg(STI
.getZeroRegister(), RegState::Kill
)
90 .setMIFlag(MachineInstr::FrameSetup
);
94 // Early exit if the frame pointer is not needed in this function.
99 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
100 unsigned FrameSize
= MFI
.getStackSize() - AFI
->getCalleeSavedFrameSize();
102 // Skip the callee-saved push instructions.
104 (MBBI
!= MBB
.end()) && MBBI
->getFlag(MachineInstr::FrameSetup
) &&
105 (MBBI
->getOpcode() == AVR::PUSHRr
|| MBBI
->getOpcode() == AVR::PUSHWRr
)) {
109 // Update Y with the new base value.
110 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPREAD
), AVR::R29R28
)
112 .setMIFlag(MachineInstr::FrameSetup
);
114 // Mark the FramePtr as live-in in every block except the entry.
115 for (MachineBasicBlock
&MBBJ
: llvm::drop_begin(MF
)) {
116 MBBJ
.addLiveIn(AVR::R29R28
);
123 // Reserve the necessary frame memory by doing FP -= <size>.
124 unsigned Opcode
= (isUInt
<6>(FrameSize
) && STI
.hasADDSUBIW()) ? AVR::SBIWRdK
127 MachineInstr
*MI
= BuildMI(MBB
, MBBI
, DL
, TII
.get(Opcode
), AVR::R29R28
)
128 .addReg(AVR::R29R28
, RegState::Kill
)
130 .setMIFlag(MachineInstr::FrameSetup
);
131 // The SREG implicit def is dead.
132 MI
->getOperand(3).setIsDead();
134 // Write back R29R28 to SP and temporarily disable interrupts.
135 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
137 .setMIFlag(MachineInstr::FrameSetup
);
140 static void restoreStatusRegister(MachineFunction
&MF
, MachineBasicBlock
&MBB
) {
141 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
142 const MachineRegisterInfo
&MRI
= MF
.getRegInfo();
144 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
146 DebugLoc DL
= MBBI
->getDebugLoc();
147 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
148 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
150 // Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal
151 // handlers at the very end of the function, just before reti.
152 if (AFI
->isInterruptOrSignalHandler()) {
153 if (!MRI
.reg_empty(STI
.getZeroRegister())) {
154 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), STI
.getZeroRegister());
156 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), STI
.getTmpRegister());
157 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::OUTARr
))
158 .addImm(STI
.getIORegSREG())
159 .addReg(STI
.getTmpRegister(), RegState::Kill
);
160 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::POPRd
), STI
.getTmpRegister());
164 void AVRFrameLowering::emitEpilogue(MachineFunction
&MF
,
165 MachineBasicBlock
&MBB
) const {
166 const AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
168 // Early exit if the frame pointer is not needed in this function except for
169 // signal/interrupt handlers where special code generation is required.
170 if (!hasFP(MF
) && !AFI
->isInterruptOrSignalHandler()) {
174 MachineBasicBlock::iterator MBBI
= MBB
.getLastNonDebugInstr();
175 assert(MBBI
->getDesc().isReturn() &&
176 "Can only insert epilog into returning blocks");
178 DebugLoc DL
= MBBI
->getDebugLoc();
179 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
180 unsigned FrameSize
= MFI
.getStackSize() - AFI
->getCalleeSavedFrameSize();
181 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
182 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
184 // Early exit if there is no need to restore the frame pointer.
185 if (!FrameSize
&& !MF
.getFrameInfo().hasVarSizedObjects()) {
186 restoreStatusRegister(MF
, MBB
);
190 // Skip the callee-saved pop instructions.
191 while (MBBI
!= MBB
.begin()) {
192 MachineBasicBlock::iterator PI
= std::prev(MBBI
);
193 int Opc
= PI
->getOpcode();
195 if (Opc
!= AVR::POPRd
&& Opc
!= AVR::POPWRd
&& !PI
->isTerminator()) {
205 // Select the optimal opcode depending on how big it is.
206 if (isUInt
<6>(FrameSize
) && STI
.hasADDSUBIW()) {
207 Opcode
= AVR::ADIWRdK
;
209 Opcode
= AVR::SUBIWRdK
;
210 FrameSize
= -FrameSize
;
213 // Restore the frame pointer by doing FP += <size>.
214 MachineInstr
*MI
= BuildMI(MBB
, MBBI
, DL
, TII
.get(Opcode
), AVR::R29R28
)
215 .addReg(AVR::R29R28
, RegState::Kill
)
217 // The SREG implicit def is dead.
218 MI
->getOperand(3).setIsDead();
221 // Write back R29R28 to SP and temporarily disable interrupts.
222 BuildMI(MBB
, MBBI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
223 .addReg(AVR::R29R28
, RegState::Kill
);
225 restoreStatusRegister(MF
, MBB
);
228 // Return true if the specified function should have a dedicated frame
229 // pointer register. This is true if the function meets any of the following
231 // - a register has been spilled
233 // - input arguments are passed using the stack
235 // Notice that strictly this is not a frame pointer because it contains SP after
236 // frame allocation instead of having the original SP in function entry.
237 bool AVRFrameLowering::hasFP(const MachineFunction
&MF
) const {
238 const AVRMachineFunctionInfo
*FuncInfo
= MF
.getInfo
<AVRMachineFunctionInfo
>();
240 return (FuncInfo
->getHasSpills() || FuncInfo
->getHasAllocas() ||
241 FuncInfo
->getHasStackArgs() ||
242 MF
.getFrameInfo().hasVarSizedObjects());
245 bool AVRFrameLowering::spillCalleeSavedRegisters(
246 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
247 ArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
252 unsigned CalleeFrameSize
= 0;
253 DebugLoc DL
= MBB
.findDebugLoc(MI
);
254 MachineFunction
&MF
= *MBB
.getParent();
255 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
256 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
257 AVRMachineFunctionInfo
*AVRFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
259 for (const CalleeSavedInfo
&I
: llvm::reverse(CSI
)) {
260 Register Reg
= I
.getReg();
261 bool IsNotLiveIn
= !MBB
.isLiveIn(Reg
);
263 // Check if Reg is a sub register of a 16-bit livein register, and then
264 // add it to the livein list.
266 for (const auto &LiveIn
: MBB
.liveins())
267 if (STI
.getRegisterInfo()->isSubRegister(LiveIn
.PhysReg
, Reg
)) {
273 assert(TRI
->getRegSizeInBits(*TRI
->getMinimalPhysRegClass(Reg
)) == 8 &&
274 "Invalid register size");
276 // Add the callee-saved register as live-in only if it is not already a
277 // live-in register, this usually happens with arguments that are passed
278 // through callee-saved registers.
283 // Do not kill the register when it is an input argument.
284 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::PUSHRr
))
285 .addReg(Reg
, getKillRegState(IsNotLiveIn
))
286 .setMIFlag(MachineInstr::FrameSetup
);
290 AVRFI
->setCalleeSavedFrameSize(CalleeFrameSize
);
295 bool AVRFrameLowering::restoreCalleeSavedRegisters(
296 MachineBasicBlock
&MBB
, MachineBasicBlock::iterator MI
,
297 MutableArrayRef
<CalleeSavedInfo
> CSI
, const TargetRegisterInfo
*TRI
) const {
302 DebugLoc DL
= MBB
.findDebugLoc(MI
);
303 const MachineFunction
&MF
= *MBB
.getParent();
304 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
305 const TargetInstrInfo
&TII
= *STI
.getInstrInfo();
307 for (const CalleeSavedInfo
&CCSI
: CSI
) {
308 Register Reg
= CCSI
.getReg();
310 assert(TRI
->getRegSizeInBits(*TRI
->getMinimalPhysRegClass(Reg
)) == 8 &&
311 "Invalid register size");
313 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::POPRd
), Reg
);
319 /// Replace pseudo store instructions that pass arguments through the stack with
320 /// real instructions.
321 static void fixStackStores(MachineBasicBlock
&MBB
,
322 MachineBasicBlock::iterator StartMI
,
323 const TargetInstrInfo
&TII
) {
324 // Iterate through the BB until we hit a call instruction or we reach the end.
325 for (MachineInstr
&MI
:
326 llvm::make_early_inc_range(llvm::make_range(StartMI
, MBB
.end()))) {
330 unsigned Opcode
= MI
.getOpcode();
332 // Only care of pseudo store instructions where SP is the base pointer.
333 if (Opcode
!= AVR::STDSPQRr
&& Opcode
!= AVR::STDWSPQRr
)
336 assert(MI
.getOperand(0).getReg() == AVR::SP
&&
337 "SP is expected as base pointer");
339 // Replace this instruction with a regular store. Use Y as the base
340 // pointer since it is guaranteed to contain a copy of SP.
342 (Opcode
== AVR::STDWSPQRr
) ? AVR::STDWPtrQRr
: AVR::STDPtrQRr
;
344 MI
.setDesc(TII
.get(STOpc
));
345 MI
.getOperand(0).setReg(AVR::R31R30
);
349 MachineBasicBlock::iterator
AVRFrameLowering::eliminateCallFramePseudoInstr(
350 MachineFunction
&MF
, MachineBasicBlock
&MBB
,
351 MachineBasicBlock::iterator MI
) const {
352 const AVRSubtarget
&STI
= MF
.getSubtarget
<AVRSubtarget
>();
353 const AVRInstrInfo
&TII
= *STI
.getInstrInfo();
355 if (hasReservedCallFrame(MF
)) {
356 return MBB
.erase(MI
);
359 DebugLoc DL
= MI
->getDebugLoc();
360 unsigned int Opcode
= MI
->getOpcode();
361 int Amount
= TII
.getFrameSize(*MI
);
364 return MBB
.erase(MI
);
367 assert(getStackAlign() == Align(1) && "Unsupported stack alignment");
369 if (Opcode
== TII
.getCallFrameSetupOpcode()) {
370 // Update the stack pointer.
371 // In many cases this can be done far more efficiently by pushing the
372 // relevant values directly to the stack. However, doing that correctly
373 // (in the right order, possibly skipping some empty space for undef
374 // values, etc) is tricky and thus left to be optimized in the future.
375 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPREAD
), AVR::R31R30
).addReg(AVR::SP
);
378 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SUBIWRdK
), AVR::R31R30
)
379 .addReg(AVR::R31R30
, RegState::Kill
)
381 New
->getOperand(3).setIsDead();
383 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
).addReg(AVR::R31R30
);
385 // Make sure the remaining stack stores are converted to real store
387 fixStackStores(MBB
, MI
, TII
);
389 assert(Opcode
== TII
.getCallFrameDestroyOpcode());
391 // Note that small stack changes could be implemented more efficiently
392 // with a few pop instructions instead of the 8-9 instructions now
395 // Select the best opcode to adjust SP based on the offset size.
398 if (isUInt
<6>(Amount
) && STI
.hasADDSUBIW()) {
399 AddOpcode
= AVR::ADIWRdK
;
401 AddOpcode
= AVR::SUBIWRdK
;
405 // Build the instruction sequence.
406 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPREAD
), AVR::R31R30
).addReg(AVR::SP
);
408 MachineInstr
*New
= BuildMI(MBB
, MI
, DL
, TII
.get(AddOpcode
), AVR::R31R30
)
409 .addReg(AVR::R31R30
, RegState::Kill
)
411 New
->getOperand(3).setIsDead();
413 BuildMI(MBB
, MI
, DL
, TII
.get(AVR::SPWRITE
), AVR::SP
)
414 .addReg(AVR::R31R30
, RegState::Kill
);
417 return MBB
.erase(MI
);
420 void AVRFrameLowering::determineCalleeSaves(MachineFunction
&MF
,
421 BitVector
&SavedRegs
,
422 RegScavenger
*RS
) const {
423 TargetFrameLowering::determineCalleeSaves(MF
, SavedRegs
, RS
);
425 // If we have a frame pointer, the Y register needs to be saved as well.
427 SavedRegs
.set(AVR::R29
);
428 SavedRegs
.set(AVR::R28
);
431 /// The frame analyzer pass.
433 /// Scans the function for allocas and used arguments
434 /// that are passed through the stack.
435 struct AVRFrameAnalyzer
: public MachineFunctionPass
{
437 AVRFrameAnalyzer() : MachineFunctionPass(ID
) {}
439 bool runOnMachineFunction(MachineFunction
&MF
) override
{
440 const MachineFrameInfo
&MFI
= MF
.getFrameInfo();
441 AVRMachineFunctionInfo
*AFI
= MF
.getInfo
<AVRMachineFunctionInfo
>();
443 // If there are no fixed frame indexes during this stage it means there
444 // are allocas present in the function.
445 if (MFI
.getNumObjects() != MFI
.getNumFixedObjects()) {
446 // Check for the type of allocas present in the function. We only care
447 // about fixed size allocas so do not give false positives if only
448 // variable sized allocas are present.
449 for (unsigned i
= 0, e
= MFI
.getObjectIndexEnd(); i
!= e
; ++i
) {
450 // Variable sized objects have size 0.
451 if (MFI
.getObjectSize(i
)) {
452 AFI
->setHasAllocas(true);
458 // If there are fixed frame indexes present, scan the function to see if
459 // they are really being used.
460 if (MFI
.getNumFixedObjects() == 0) {
464 // Ok fixed frame indexes present, now scan the function to see if they
465 // are really being used, otherwise we can ignore them.
466 for (const MachineBasicBlock
&BB
: MF
) {
467 for (const MachineInstr
&MI
: BB
) {
468 int Opcode
= MI
.getOpcode();
470 if ((Opcode
!= AVR::LDDRdPtrQ
) && (Opcode
!= AVR::LDDWRdPtrQ
) &&
471 (Opcode
!= AVR::STDPtrQRr
) && (Opcode
!= AVR::STDWPtrQRr
) &&
472 (Opcode
!= AVR::FRMIDX
)) {
476 for (const MachineOperand
&MO
: MI
.operands()) {
481 if (MFI
.isFixedObjectIndex(MO
.getIndex())) {
482 AFI
->setHasStackArgs(true);
492 StringRef
getPassName() const override
{ return "AVR Frame Analyzer"; }
495 char AVRFrameAnalyzer::ID
= 0;
497 /// Creates instance of the frame analyzer pass.
498 FunctionPass
*createAVRFrameAnalyzerPass() { return new AVRFrameAnalyzer(); }
500 } // end of namespace llvm