blob: 25232d2e47e13705b08998113dd779e1b7423d16 [file] [log] [blame]
Dylan McKay192405a2016-10-05 11:48:56 +00001//===-- AVRFrameLowering.cpp - AVR Frame Information ----------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the AVR implementation of TargetFrameLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AVRFrameLowering.h"
15
16#include "AVR.h"
17#include "AVRInstrInfo.h"
18#include "AVRMachineFunctionInfo.h"
19#include "AVRTargetMachine.h"
20#include "MCTargetDesc/AVRMCTargetDesc.h"
21
22#include "llvm/CodeGen/MachineFrameInfo.h"
23#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineFunctionPass.h"
25#include "llvm/CodeGen/MachineInstrBuilder.h"
26#include "llvm/CodeGen/MachineRegisterInfo.h"
27#include "llvm/IR/Function.h"
28
Dylan McKay7c2d41a2016-10-08 01:09:06 +000029#include <vector>
30
Dylan McKay192405a2016-10-05 11:48:56 +000031namespace llvm {
32
33AVRFrameLowering::AVRFrameLowering()
34 : TargetFrameLowering(TargetFrameLowering::StackGrowsDown, 1, -2) {}
35
36bool AVRFrameLowering::canSimplifyCallFramePseudos(
37 const MachineFunction &MF) const {
38 // Always simplify call frame pseudo instructions, even when
39 // hasReservedCallFrame is false.
40 return true;
41}
42
43bool AVRFrameLowering::hasReservedCallFrame(const MachineFunction &MF) const {
44 // Reserve call frame memory in function prologue under the following
45 // conditions:
46 // - Y pointer is reserved to be the frame pointer.
47 // - The function does not contain variable sized objects.
Dylan McKayb16b6d52016-10-08 01:10:31 +000048
Dylan McKay192405a2016-10-05 11:48:56 +000049 const MachineFrameInfo &MFI = MF.getFrameInfo();
Dylan McKayb16b6d52016-10-08 01:10:31 +000050 return hasFP(MF) && !MFI.hasVarSizedObjects();
Dylan McKay192405a2016-10-05 11:48:56 +000051}
52
53void AVRFrameLowering::emitPrologue(MachineFunction &MF,
54 MachineBasicBlock &MBB) const {
55 MachineBasicBlock::iterator MBBI = MBB.begin();
56 CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
57 DebugLoc DL = (MBBI != MBB.end()) ? MBBI->getDebugLoc() : DebugLoc();
58 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
59 const AVRInstrInfo &TII = *STI.getInstrInfo();
Dylan McKay634339a2017-05-02 00:11:34 +000060 bool HasFP = hasFP(MF);
Dylan McKay192405a2016-10-05 11:48:56 +000061
62 // Interrupt handlers re-enable interrupts in function entry.
63 if (CallConv == CallingConv::AVR_INTR) {
64 BuildMI(MBB, MBBI, DL, TII.get(AVR::BSETs))
65 .addImm(0x07)
66 .setMIFlag(MachineInstr::FrameSetup);
67 }
68
Dylan McKay28355ef2017-05-02 01:57:48 +000069 // Save the frame pointer if we have one.
70 if (HasFP) {
71 BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHWRr))
72 .addReg(AVR::R29R28, RegState::Kill)
73 .setMIFlag(MachineInstr::FrameSetup);
74 }
75
Dylan McKay192405a2016-10-05 11:48:56 +000076 // Emit special prologue code to save R1, R0 and SREG in interrupt/signal
77 // handlers before saving any other registers.
78 if (CallConv == CallingConv::AVR_INTR ||
79 CallConv == CallingConv::AVR_SIGNAL) {
80 BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHWRr))
81 .addReg(AVR::R1R0, RegState::Kill)
82 .setMIFlag(MachineInstr::FrameSetup);
Dylan McKay634339a2017-05-02 00:11:34 +000083
Dylan McKay192405a2016-10-05 11:48:56 +000084 BuildMI(MBB, MBBI, DL, TII.get(AVR::INRdA), AVR::R0)
85 .addImm(0x3f)
86 .setMIFlag(MachineInstr::FrameSetup);
87 BuildMI(MBB, MBBI, DL, TII.get(AVR::PUSHRr))
88 .addReg(AVR::R0, RegState::Kill)
89 .setMIFlag(MachineInstr::FrameSetup);
90 BuildMI(MBB, MBBI, DL, TII.get(AVR::EORRdRr))
91 .addReg(AVR::R0, RegState::Define)
92 .addReg(AVR::R0, RegState::Kill)
93 .addReg(AVR::R0, RegState::Kill)
94 .setMIFlag(MachineInstr::FrameSetup);
95 }
96
97 // Early exit if the frame pointer is not needed in this function.
Dylan McKay634339a2017-05-02 00:11:34 +000098 if (!HasFP) {
Dylan McKay192405a2016-10-05 11:48:56 +000099 return;
100 }
101
102 const MachineFrameInfo &MFI = MF.getFrameInfo();
103 const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
104 unsigned FrameSize = MFI.getStackSize() - AFI->getCalleeSavedFrameSize();
105
106 // Skip the callee-saved push instructions.
107 while (
108 (MBBI != MBB.end()) && MBBI->getFlag(MachineInstr::FrameSetup) &&
109 (MBBI->getOpcode() == AVR::PUSHRr || MBBI->getOpcode() == AVR::PUSHWRr)) {
110 ++MBBI;
111 }
112
113 // Update Y with the new base value.
114 BuildMI(MBB, MBBI, DL, TII.get(AVR::SPREAD), AVR::R29R28)
115 .addReg(AVR::SP)
116 .setMIFlag(MachineInstr::FrameSetup);
117
118 // Mark the FramePtr as live-in in every block except the entry.
119 for (MachineFunction::iterator I = std::next(MF.begin()), E = MF.end();
120 I != E; ++I) {
121 I->addLiveIn(AVR::R29R28);
122 }
123
124 if (!FrameSize) {
125 return;
126 }
127
128 // Reserve the necessary frame memory by doing FP -= <size>.
129 unsigned Opcode = (isUInt<6>(FrameSize)) ? AVR::SBIWRdK : AVR::SUBIWRdK;
130
131 MachineInstr *MI = BuildMI(MBB, MBBI, DL, TII.get(Opcode), AVR::R29R28)
132 .addReg(AVR::R29R28, RegState::Kill)
133 .addImm(FrameSize)
134 .setMIFlag(MachineInstr::FrameSetup);
135 // The SREG implicit def is dead.
136 MI->getOperand(3).setIsDead();
137
138 // Write back R29R28 to SP and temporarily disable interrupts.
139 BuildMI(MBB, MBBI, DL, TII.get(AVR::SPWRITE), AVR::SP)
140 .addReg(AVR::R29R28)
141 .setMIFlag(MachineInstr::FrameSetup);
142}
143
144void AVRFrameLowering::emitEpilogue(MachineFunction &MF,
145 MachineBasicBlock &MBB) const {
146 CallingConv::ID CallConv = MF.getFunction()->getCallingConv();
147 bool isHandler = (CallConv == CallingConv::AVR_INTR ||
148 CallConv == CallingConv::AVR_SIGNAL);
149
150 // Early exit if the frame pointer is not needed in this function except for
151 // signal/interrupt handlers where special code generation is required.
152 if (!hasFP(MF) && !isHandler) {
153 return;
154 }
155
156 MachineBasicBlock::iterator MBBI = MBB.getLastNonDebugInstr();
Dylan McKay552b7852016-10-08 01:10:36 +0000157 assert(MBBI->getDesc().isReturn() &&
Dylan McKay192405a2016-10-05 11:48:56 +0000158 "Can only insert epilog into returning blocks");
Dylan McKay552b7852016-10-08 01:10:36 +0000159
Dylan McKay192405a2016-10-05 11:48:56 +0000160 DebugLoc DL = MBBI->getDebugLoc();
161 const MachineFrameInfo &MFI = MF.getFrameInfo();
162 const AVRMachineFunctionInfo *AFI = MF.getInfo<AVRMachineFunctionInfo>();
163 unsigned FrameSize = MFI.getStackSize() - AFI->getCalleeSavedFrameSize();
164 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
165 const AVRInstrInfo &TII = *STI.getInstrInfo();
166
167 // Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal
168 // handlers at the very end of the function, just before reti.
169 if (isHandler) {
170 BuildMI(MBB, MBBI, DL, TII.get(AVR::POPRd), AVR::R0);
171 BuildMI(MBB, MBBI, DL, TII.get(AVR::OUTARr))
172 .addImm(0x3f)
173 .addReg(AVR::R0, RegState::Kill);
174 BuildMI(MBB, MBBI, DL, TII.get(AVR::POPWRd), AVR::R1R0);
175 }
176
Dylan McKay28355ef2017-05-02 01:57:48 +0000177 if (hasFP(MF))
178 BuildMI(MBB, MBBI, DL, TII.get(AVR::POPWRd), AVR::R29R28);
179
Dylan McKay192405a2016-10-05 11:48:56 +0000180 // Early exit if there is no need to restore the frame pointer.
181 if (!FrameSize) {
182 return;
183 }
184
185 // Skip the callee-saved pop instructions.
186 while (MBBI != MBB.begin()) {
187 MachineBasicBlock::iterator PI = std::prev(MBBI);
188 int Opc = PI->getOpcode();
189
190 if (Opc != AVR::POPRd && Opc != AVR::POPWRd && !PI->isTerminator()) {
191 break;
192 }
193
194 --MBBI;
195 }
196
197 unsigned Opcode;
198
199 // Select the optimal opcode depending on how big it is.
200 if (isUInt<6>(FrameSize)) {
201 Opcode = AVR::ADIWRdK;
202 } else {
203 Opcode = AVR::SUBIWRdK;
204 FrameSize = -FrameSize;
205 }
206
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)
210 .addImm(FrameSize);
211 // The SREG implicit def is dead.
212 MI->getOperand(3).setIsDead();
213
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);
217}
218
219// Return true if the specified function should have a dedicated frame
220// pointer register. This is true if the function meets any of the following
221// conditions:
222// - a register has been spilled
223// - has allocas
224// - input arguments are passed using the stack
225//
226// Notice that strictly this is not a frame pointer because it contains SP after
227// frame allocation instead of having the original SP in function entry.
228bool AVRFrameLowering::hasFP(const MachineFunction &MF) const {
229 const AVRMachineFunctionInfo *FuncInfo = MF.getInfo<AVRMachineFunctionInfo>();
230
Dylan McKay3bb6eb22017-05-01 23:16:59 +0000231 // TODO: We do not always need a frame pointer.
232 // This can be optimised.
233 return true;
Dylan McKay192405a2016-10-05 11:48:56 +0000234}
235
236bool AVRFrameLowering::spillCalleeSavedRegisters(
237 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
238 const std::vector<CalleeSavedInfo> &CSI,
239 const TargetRegisterInfo *TRI) const {
240 if (CSI.empty()) {
241 return false;
242 }
243
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>();
250
251 for (unsigned i = CSI.size(); i != 0; --i) {
252 unsigned Reg = CSI[i - 1].getReg();
253 bool IsNotLiveIn = !MBB.isLiveIn(Reg);
254
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000255 assert(TRI->getRegSizeInBits(*TRI->getMinimalPhysRegClass(Reg)) == 8 &&
Dylan McKay192405a2016-10-05 11:48:56 +0000256 "Invalid register size");
257
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.
261 if (IsNotLiveIn) {
262 MBB.addLiveIn(Reg);
263 }
264
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);
269 ++CalleeFrameSize;
270 }
271
272 AVRFI->setCalleeSavedFrameSize(CalleeFrameSize);
273
274 return true;
275}
276
277bool AVRFrameLowering::restoreCalleeSavedRegisters(
278 MachineBasicBlock &MBB, MachineBasicBlock::iterator MI,
279 const std::vector<CalleeSavedInfo> &CSI,
280 const TargetRegisterInfo *TRI) const {
281 if (CSI.empty()) {
282 return false;
283 }
284
285 DebugLoc DL = MBB.findDebugLoc(MI);
286 const MachineFunction &MF = *MBB.getParent();
287 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
288 const TargetInstrInfo &TII = *STI.getInstrInfo();
289
Dylan McKay7c2d41a2016-10-08 01:09:06 +0000290 for (const CalleeSavedInfo &CCSI : CSI) {
291 unsigned Reg = CCSI.getReg();
Dylan McKay192405a2016-10-05 11:48:56 +0000292
Krzysztof Parzyszek44e25f32017-04-24 18:55:33 +0000293 assert(TRI->getRegSizeInBits(*TRI->getMinimalPhysRegClass(Reg)) == 8 &&
Dylan McKay192405a2016-10-05 11:48:56 +0000294 "Invalid register size");
295
296 BuildMI(MBB, MI, DL, TII.get(AVR::POPRd), Reg);
297 }
298
299 return true;
300}
301
302/// Replace pseudo store instructions that pass arguments through the stack with
303/// real instructions. If insertPushes is true then all instructions are
304/// replaced with push instructions, otherwise regular std instructions are
305/// inserted.
306static void fixStackStores(MachineBasicBlock &MBB,
307 MachineBasicBlock::iterator MI,
308 const TargetInstrInfo &TII, bool insertPushes) {
309 const AVRSubtarget &STI = MBB.getParent()->getSubtarget<AVRSubtarget>();
310 const TargetRegisterInfo &TRI = *STI.getRegisterInfo();
311
312 // Iterate through the BB until we hit a call instruction or we reach the end.
313 for (auto I = MI, E = MBB.end(); I != E && !I->isCall();) {
314 MachineBasicBlock::iterator NextMI = std::next(I);
315 MachineInstr &MI = *I;
316 unsigned Opcode = I->getOpcode();
317
318 // Only care of pseudo store instructions where SP is the base pointer.
319 if (Opcode != AVR::STDSPQRr && Opcode != AVR::STDWSPQRr) {
320 I = NextMI;
321 continue;
322 }
323
324 assert(MI.getOperand(0).getReg() == AVR::SP &&
325 "Invalid register, should be SP!");
326 if (insertPushes) {
327 // Replace this instruction with a push.
328 unsigned SrcReg = MI.getOperand(2).getReg();
329 bool SrcIsKill = MI.getOperand(2).isKill();
330
331 // We can't use PUSHWRr here because when expanded the order of the new
332 // instructions are reversed from what we need. Perform the expansion now.
333 if (Opcode == AVR::STDWSPQRr) {
334 BuildMI(MBB, I, MI.getDebugLoc(), TII.get(AVR::PUSHRr))
335 .addReg(TRI.getSubReg(SrcReg, AVR::sub_hi),
336 getKillRegState(SrcIsKill));
337 BuildMI(MBB, I, MI.getDebugLoc(), TII.get(AVR::PUSHRr))
338 .addReg(TRI.getSubReg(SrcReg, AVR::sub_lo),
339 getKillRegState(SrcIsKill));
340 } else {
341 BuildMI(MBB, I, MI.getDebugLoc(), TII.get(AVR::PUSHRr))
342 .addReg(SrcReg, getKillRegState(SrcIsKill));
343 }
344
345 MI.eraseFromParent();
346 I = NextMI;
347 continue;
348 }
349
350 // Replace this instruction with a regular store. Use Y as the base
351 // pointer since it is guaranteed to contain a copy of SP.
352 unsigned STOpc =
353 (Opcode == AVR::STDWSPQRr) ? AVR::STDWPtrQRr : AVR::STDPtrQRr;
Dylan McKay192405a2016-10-05 11:48:56 +0000354
355 MI.setDesc(TII.get(STOpc));
356 MI.getOperand(0).setReg(AVR::R29R28);
357
358 I = NextMI;
359 }
360}
361
362MachineBasicBlock::iterator AVRFrameLowering::eliminateCallFramePseudoInstr(
363 MachineFunction &MF, MachineBasicBlock &MBB,
364 MachineBasicBlock::iterator MI) const {
365 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
366 const TargetFrameLowering &TFI = *STI.getFrameLowering();
367 const AVRInstrInfo &TII = *STI.getInstrInfo();
368
369 // There is nothing to insert when the call frame memory is allocated during
370 // function entry. Delete the call frame pseudo and replace all pseudo stores
371 // with real store instructions.
372 if (TFI.hasReservedCallFrame(MF)) {
373 fixStackStores(MBB, MI, TII, false);
374 return MBB.erase(MI);
375 }
376
377 DebugLoc DL = MI->getDebugLoc();
378 unsigned int Opcode = MI->getOpcode();
379 int Amount = MI->getOperand(0).getImm();
380
381 // Adjcallstackup does not need to allocate stack space for the call, instead
382 // we insert push instructions that will allocate the necessary stack.
383 // For adjcallstackdown we convert it into an 'adiw reg, <amt>' handling
384 // the read and write of SP in I/O space.
385 if (Amount != 0) {
386 assert(TFI.getStackAlignment() == 1 && "Unsupported stack alignment");
387
388 if (Opcode == TII.getCallFrameSetupOpcode()) {
389 fixStackStores(MBB, MI, TII, true);
390 } else {
391 assert(Opcode == TII.getCallFrameDestroyOpcode());
392
393 // Select the best opcode to adjust SP based on the offset size.
394 unsigned addOpcode;
395 if (isUInt<6>(Amount)) {
396 addOpcode = AVR::ADIWRdK;
397 } else {
398 addOpcode = AVR::SUBIWRdK;
399 Amount = -Amount;
400 }
401
402 // Build the instruction sequence.
403 BuildMI(MBB, MI, DL, TII.get(AVR::SPREAD), AVR::R31R30).addReg(AVR::SP);
404
405 MachineInstr *New = BuildMI(MBB, MI, DL, TII.get(addOpcode), AVR::R31R30)
406 .addReg(AVR::R31R30, RegState::Kill)
407 .addImm(Amount);
408 New->getOperand(3).setIsDead();
409
410 BuildMI(MBB, MI, DL, TII.get(AVR::SPWRITE), AVR::SP)
411 .addReg(AVR::R31R30, RegState::Kill);
412 }
413 }
414
415 return MBB.erase(MI);
416}
417
418void AVRFrameLowering::determineCalleeSaves(MachineFunction &MF,
419 BitVector &SavedRegs,
420 RegScavenger *RS) const {
421 TargetFrameLowering::determineCalleeSaves(MF, SavedRegs, RS);
422
Dylan McKay634339a2017-05-02 00:11:34 +0000423 // If we have a frame pointer, the Y register needs to be saved as well.
424 // We don't do that here however - the prologue and epilogue generation
425 // code will handle it specially.
Dylan McKay192405a2016-10-05 11:48:56 +0000426}
427/// The frame analyzer pass.
428///
429/// Scans the function for allocas and used arguments
430/// that are passed through the stack.
431struct AVRFrameAnalyzer : public MachineFunctionPass {
432 static char ID;
433 AVRFrameAnalyzer() : MachineFunctionPass(ID) {}
434
435 bool runOnMachineFunction(MachineFunction &MF) {
436 const MachineFrameInfo &MFI = MF.getFrameInfo();
437 AVRMachineFunctionInfo *FuncInfo = MF.getInfo<AVRMachineFunctionInfo>();
438
439 // If there are no fixed frame indexes during this stage it means there
440 // are allocas present in the function.
441 if (MFI.getNumObjects() != MFI.getNumFixedObjects()) {
442 // Check for the type of allocas present in the function. We only care
443 // about fixed size allocas so do not give false positives if only
444 // variable sized allocas are present.
445 for (unsigned i = 0, e = MFI.getObjectIndexEnd(); i != e; ++i) {
446 // Variable sized objects have size 0.
447 if (MFI.getObjectSize(i)) {
448 FuncInfo->setHasAllocas(true);
449 break;
450 }
451 }
452 }
453
454 // If there are fixed frame indexes present, scan the function to see if
455 // they are really being used.
456 if (MFI.getNumFixedObjects() == 0) {
457 return false;
458 }
459
460 // Ok fixed frame indexes present, now scan the function to see if they
461 // are really being used, otherwise we can ignore them.
462 for (const MachineBasicBlock &BB : MF) {
463 for (const MachineInstr &MI : BB) {
464 int Opcode = MI.getOpcode();
465
466 if ((Opcode != AVR::LDDRdPtrQ) && (Opcode != AVR::LDDWRdPtrQ) &&
467 (Opcode != AVR::STDPtrQRr) && (Opcode != AVR::STDWPtrQRr)) {
468 continue;
469 }
470
471 for (const MachineOperand &MO : MI.operands()) {
472 if (!MO.isFI()) {
473 continue;
474 }
475
476 if (MFI.isFixedObjectIndex(MO.getIndex())) {
477 FuncInfo->setHasStackArgs(true);
478 return false;
479 }
480 }
481 }
482 }
483
484 return false;
485 }
486
Dylan McKayea555542016-10-05 12:32:24 +0000487 StringRef getPassName() const { return "AVR Frame Analyzer"; }
Dylan McKay192405a2016-10-05 11:48:56 +0000488};
489
490char AVRFrameAnalyzer::ID = 0;
491
492/// Creates instance of the frame analyzer pass.
493FunctionPass *createAVRFrameAnalyzerPass() { return new AVRFrameAnalyzer(); }
494
495/// Create the Dynalloca Stack Pointer Save/Restore pass.
496/// Insert a copy of SP before allocating the dynamic stack memory and restore
497/// it in function exit to restore the original SP state. This avoids the need
498/// of reserving a register pair for a frame pointer.
499struct AVRDynAllocaSR : public MachineFunctionPass {
500 static char ID;
501 AVRDynAllocaSR() : MachineFunctionPass(ID) {}
502
503 bool runOnMachineFunction(MachineFunction &MF) {
504 // Early exit when there are no variable sized objects in the function.
505 if (!MF.getFrameInfo().hasVarSizedObjects()) {
506 return false;
507 }
508
509 const AVRSubtarget &STI = MF.getSubtarget<AVRSubtarget>();
510 const TargetInstrInfo &TII = *STI.getInstrInfo();
511 MachineBasicBlock &EntryMBB = MF.front();
512 MachineBasicBlock::iterator MBBI = EntryMBB.begin();
513 DebugLoc DL = EntryMBB.findDebugLoc(MBBI);
514
515 unsigned SPCopy =
516 MF.getRegInfo().createVirtualRegister(&AVR::DREGSRegClass);
517
518 // Create a copy of SP in function entry before any dynallocas are
519 // inserted.
520 BuildMI(EntryMBB, MBBI, DL, TII.get(AVR::COPY), SPCopy).addReg(AVR::SP);
521
522 // Restore SP in all exit basic blocks.
523 for (MachineBasicBlock &MBB : MF) {
524 // If last instruction is a return instruction, add a restore copy.
525 if (!MBB.empty() && MBB.back().isReturn()) {
526 MBBI = MBB.getLastNonDebugInstr();
527 DL = MBBI->getDebugLoc();
528 BuildMI(MBB, MBBI, DL, TII.get(AVR::COPY), AVR::SP)
529 .addReg(SPCopy, RegState::Kill);
530 }
531 }
532
533 return true;
534 }
535
Dylan McKayea555542016-10-05 12:32:24 +0000536 StringRef getPassName() const {
Dylan McKay192405a2016-10-05 11:48:56 +0000537 return "AVR dynalloca stack pointer save/restore";
538 }
539};
540
541char AVRDynAllocaSR::ID = 0;
542
543/// createAVRDynAllocaSRPass - returns an instance of the dynalloca stack
544/// pointer save/restore pass.
545FunctionPass *createAVRDynAllocaSRPass() { return new AVRDynAllocaSR(); }
546
547} // end of namespace llvm
548