@@ -57,26 +57,19 @@ void AVRFrameLowering::emitPrologue(MachineFunction &MF,
57
57
DebugLoc DL = (MBBI != MBB.end ()) ? MBBI->getDebugLoc () : DebugLoc ();
58
58
const AVRSubtarget &STI = MF.getSubtarget <AVRSubtarget>();
59
59
const AVRInstrInfo &TII = *STI.getInstrInfo ();
60
+ const AVRMachineFunctionInfo *AFI = MF.getInfo <AVRMachineFunctionInfo>();
60
61
bool HasFP = hasFP (MF);
61
62
62
63
// Interrupt handlers re-enable interrupts in function entry.
63
- if (CallConv == CallingConv::AVR_INTR ) {
64
+ if (AFI-> isInterruptHandler () ) {
64
65
BuildMI (MBB, MBBI, DL, TII.get (AVR::BSETs))
65
66
.addImm (0x07 )
66
67
.setMIFlag (MachineInstr::FrameSetup);
67
68
}
68
69
69
- // 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
-
76
70
// Emit special prologue code to save R1, R0 and SREG in interrupt/signal
77
71
// handlers before saving any other registers.
78
- if (CallConv == CallingConv::AVR_INTR ||
79
- CallConv == CallingConv::AVR_SIGNAL) {
72
+ if (AFI->isInterruptOrSignalHandler ()) {
80
73
BuildMI (MBB, MBBI, DL, TII.get (AVR::PUSHWRr))
81
74
.addReg (AVR::R1R0, RegState::Kill)
82
75
.setMIFlag (MachineInstr::FrameSetup);
@@ -100,7 +93,6 @@ void AVRFrameLowering::emitPrologue(MachineFunction &MF,
100
93
}
101
94
102
95
const MachineFrameInfo &MFI = MF.getFrameInfo ();
103
- const AVRMachineFunctionInfo *AFI = MF.getInfo <AVRMachineFunctionInfo>();
104
96
unsigned FrameSize = MFI.getStackSize () - AFI->getCalleeSavedFrameSize ();
105
97
106
98
// Skip the callee-saved push instructions.
@@ -143,13 +135,11 @@ void AVRFrameLowering::emitPrologue(MachineFunction &MF,
143
135
144
136
void AVRFrameLowering::emitEpilogue (MachineFunction &MF,
145
137
MachineBasicBlock &MBB) const {
146
- CallingConv::ID CallConv = MF.getFunction ().getCallingConv ();
147
- bool isHandler = (CallConv == CallingConv::AVR_INTR ||
148
- CallConv == CallingConv::AVR_SIGNAL);
138
+ const AVRMachineFunctionInfo *AFI = MF.getInfo <AVRMachineFunctionInfo>();
149
139
150
140
// Early exit if the frame pointer is not needed in this function except for
151
141
// signal/interrupt handlers where special code generation is required.
152
- if (!hasFP (MF) && !isHandler ) {
142
+ if (!hasFP (MF) && !AFI-> isInterruptOrSignalHandler () ) {
153
143
return ;
154
144
}
155
145
@@ -159,24 +149,20 @@ void AVRFrameLowering::emitEpilogue(MachineFunction &MF,
159
149
160
150
DebugLoc DL = MBBI->getDebugLoc ();
161
151
const MachineFrameInfo &MFI = MF.getFrameInfo ();
162
- const AVRMachineFunctionInfo *AFI = MF.getInfo <AVRMachineFunctionInfo>();
163
152
unsigned FrameSize = MFI.getStackSize () - AFI->getCalleeSavedFrameSize ();
164
153
const AVRSubtarget &STI = MF.getSubtarget <AVRSubtarget>();
165
154
const AVRInstrInfo &TII = *STI.getInstrInfo ();
166
155
167
156
// Emit special epilogue code to restore R1, R0 and SREG in interrupt/signal
168
157
// handlers at the very end of the function, just before reti.
169
- if (isHandler ) {
158
+ if (AFI-> isInterruptOrSignalHandler () ) {
170
159
BuildMI (MBB, MBBI, DL, TII.get (AVR::POPRd), AVR::R0);
171
160
BuildMI (MBB, MBBI, DL, TII.get (AVR::OUTARr))
172
161
.addImm (0x3f )
173
162
.addReg (AVR::R0, RegState::Kill);
174
163
BuildMI (MBB, MBBI, DL, TII.get (AVR::POPWRd), AVR::R1R0);
175
164
}
176
165
177
- if (hasFP (MF))
178
- BuildMI (MBB, MBBI, DL, TII.get (AVR::POPWRd), AVR::R29R28);
179
-
180
166
// Early exit if there is no need to restore the frame pointer.
181
167
if (!FrameSize) {
182
168
return ;
@@ -299,15 +285,10 @@ bool AVRFrameLowering::restoreCalleeSavedRegisters(
299
285
}
300
286
301
287
// / Replace pseudo store instructions that pass arguments through the stack with
302
- // / real instructions. If insertPushes is true then all instructions are
303
- // / replaced with push instructions, otherwise regular std instructions are
304
- // / inserted.
288
+ // / real instructions.
305
289
static void fixStackStores (MachineBasicBlock &MBB,
306
290
MachineBasicBlock::iterator MI,
307
- const TargetInstrInfo &TII, bool insertPushes) {
308
- const AVRSubtarget &STI = MBB.getParent ()->getSubtarget <AVRSubtarget>();
309
- const TargetRegisterInfo &TRI = *STI.getRegisterInfo ();
310
-
291
+ const TargetInstrInfo &TII, Register FP) {
311
292
// Iterate through the BB until we hit a call instruction or we reach the end.
312
293
for (auto I = MI, E = MBB.end (); I != E && !I->isCall ();) {
313
294
MachineBasicBlock::iterator NextMI = std::next (I);
@@ -322,37 +303,14 @@ static void fixStackStores(MachineBasicBlock &MBB,
322
303
323
304
assert (MI.getOperand (0 ).getReg () == AVR::SP &&
324
305
" Invalid register, should be SP!" );
325
- if (insertPushes) {
326
- // Replace this instruction with a push.
327
- Register SrcReg = MI.getOperand (2 ).getReg ();
328
- bool SrcIsKill = MI.getOperand (2 ).isKill ();
329
-
330
- // We can't use PUSHWRr here because when expanded the order of the new
331
- // instructions are reversed from what we need. Perform the expansion now.
332
- if (Opcode == AVR::STDWSPQRr) {
333
- BuildMI (MBB, I, MI.getDebugLoc (), TII.get (AVR::PUSHRr))
334
- .addReg (TRI.getSubReg (SrcReg, AVR::sub_hi),
335
- getKillRegState (SrcIsKill));
336
- BuildMI (MBB, I, MI.getDebugLoc (), TII.get (AVR::PUSHRr))
337
- .addReg (TRI.getSubReg (SrcReg, AVR::sub_lo),
338
- getKillRegState (SrcIsKill));
339
- } else {
340
- BuildMI (MBB, I, MI.getDebugLoc (), TII.get (AVR::PUSHRr))
341
- .addReg (SrcReg, getKillRegState (SrcIsKill));
342
- }
343
-
344
- MI.eraseFromParent ();
345
- I = NextMI;
346
- continue ;
347
- }
348
306
349
307
// Replace this instruction with a regular store. Use Y as the base
350
308
// pointer since it is guaranteed to contain a copy of SP.
351
309
unsigned STOpc =
352
310
(Opcode == AVR::STDWSPQRr) ? AVR::STDWPtrQRr : AVR::STDPtrQRr;
353
311
354
312
MI.setDesc (TII.get (STOpc));
355
- MI.getOperand (0 ).setReg (AVR::R29R28 );
313
+ MI.getOperand (0 ).setReg (FP );
356
314
357
315
I = NextMI;
358
316
}
@@ -368,26 +326,45 @@ MachineBasicBlock::iterator AVRFrameLowering::eliminateCallFramePseudoInstr(
368
326
// function entry. Delete the call frame pseudo and replace all pseudo stores
369
327
// with real store instructions.
370
328
if (hasReservedCallFrame (MF)) {
371
- fixStackStores (MBB, MI, TII, false );
329
+ fixStackStores (MBB, MI, TII, AVR::R29R28 );
372
330
return MBB.erase (MI);
373
331
}
374
332
375
333
DebugLoc DL = MI->getDebugLoc ();
376
334
unsigned int Opcode = MI->getOpcode ();
377
335
int Amount = TII.getFrameSize (*MI);
378
336
379
- // Adjcallstackup does not need to allocate stack space for the call, instead
380
- // we insert push instructions that will allocate the necessary stack.
381
- // For adjcallstackdown we convert it into an 'adiw reg, <amt>' handling
382
- // the read and write of SP in I/O space.
337
+ // ADJCALLSTACKUP and ADJCALLSTACKDOWN are converted to adiw/subi
338
+ // instructions to read and write the stack pointer in I/O space.
383
339
if (Amount != 0 ) {
384
340
assert (getStackAlignment () == 1 && " Unsupported stack alignment" );
385
341
386
342
if (Opcode == TII.getCallFrameSetupOpcode ()) {
387
- fixStackStores (MBB, MI, TII, true );
343
+ // Update the stack pointer.
344
+ // In many cases this can be done far more efficiently by pushing the
345
+ // relevant values directly to the stack. However, doing that correctly
346
+ // (in the right order, possibly skipping some empty space for undef
347
+ // values, etc) is tricky and thus left to be optimized in the future.
348
+ BuildMI (MBB, MI, DL, TII.get (AVR::SPREAD), AVR::R31R30).addReg (AVR::SP);
349
+
350
+ MachineInstr *New = BuildMI (MBB, MI, DL, TII.get (AVR::SUBIWRdK), AVR::R31R30)
351
+ .addReg (AVR::R31R30, RegState::Kill)
352
+ .addImm (Amount);
353
+ New->getOperand (3 ).setIsDead ();
354
+
355
+ BuildMI (MBB, MI, DL, TII.get (AVR::SPWRITE), AVR::SP)
356
+ .addReg (AVR::R31R30, RegState::Kill);
357
+
358
+ // Make sure the remaining stack stores are converted to real store
359
+ // instructions.
360
+ fixStackStores (MBB, MI, TII, AVR::R31R30);
388
361
} else {
389
362
assert (Opcode == TII.getCallFrameDestroyOpcode ());
390
363
364
+ // Note that small stack changes could be implemented more efficiently
365
+ // with a few pop instructions instead of the 8-9 instructions now
366
+ // required.
367
+
391
368
// Select the best opcode to adjust SP based on the offset size.
392
369
unsigned addOpcode;
393
370
if (isUInt<6 >(Amount)) {
@@ -419,8 +396,10 @@ void AVRFrameLowering::determineCalleeSaves(MachineFunction &MF,
419
396
TargetFrameLowering::determineCalleeSaves (MF, SavedRegs, RS);
420
397
421
398
// If we have a frame pointer, the Y register needs to be saved as well.
422
- // We don't do that here however - the prologue and epilogue generation
423
- // code will handle it specially.
399
+ if (hasFP (MF)) {
400
+ SavedRegs.set (AVR::R29);
401
+ SavedRegs.set (AVR::R28);
402
+ }
424
403
}
425
404
// / The frame analyzer pass.
426
405
// /
0 commit comments