@@ -133,8 +133,8 @@ class PEI : public MachineFunctionPass {
133
133
bool replaceFrameIndexDebugInstr (MachineFunction &MF, MachineInstr &MI,
134
134
unsigned OpIdx, int SPAdj = 0 );
135
135
// Does same as replaceFrameIndices but using the backward MIR walk and
136
- // backward register scavenger walk. Does not yet support call sequence
137
- // processing.
136
+ // backward register scavenger walk.
137
+ void replaceFrameIndicesBackward (MachineFunction &MF);
138
138
void replaceFrameIndicesBackward (MachineBasicBlock *BB, MachineFunction &MF,
139
139
int &SPAdj);
140
140
@@ -271,8 +271,17 @@ bool PEI::runOnMachineFunction(MachineFunction &MF) {
271
271
272
272
// Replace all MO_FrameIndex operands with physical register references
273
273
// and actual offsets.
274
- //
275
- replaceFrameIndices (MF);
274
+ if (TFI->needsFrameIndexResolution (MF)) {
275
+ // Allow the target to determine this after knowing the frame size.
276
+ FrameIndexEliminationScavenging =
277
+ (RS && !FrameIndexVirtualScavenging) ||
278
+ TRI->requiresFrameIndexReplacementScavenging (MF);
279
+
280
+ if (TRI->supportsBackwardScavenger ())
281
+ replaceFrameIndicesBackward (MF);
282
+ else
283
+ replaceFrameIndices (MF);
284
+ }
276
285
277
286
// If register scavenging is needed, as we've enabled doing it as a
278
287
// post-pass, scavenge the virtual registers that frame index elimination
@@ -1331,19 +1340,38 @@ void PEI::insertZeroCallUsedRegs(MachineFunction &MF) {
1331
1340
TFI.emitZeroCallUsedRegs (RegsToZero, MBB);
1332
1341
}
1333
1342
1343
+ // / Replace all FrameIndex operands with physical register references and actual
1344
+ // / offsets.
1345
+ void PEI::replaceFrameIndicesBackward (MachineFunction &MF) {
1346
+ const TargetFrameLowering &TFI = *MF.getSubtarget ().getFrameLowering ();
1347
+
1348
+ for (auto &MBB : MF) {
1349
+ int SPAdj = 0 ;
1350
+ if (!MBB.succ_empty ()) {
1351
+ // Get the SP adjustment for the end of MBB from the start of any of its
1352
+ // successors. They should all be the same.
1353
+ assert (all_of (MBB.successors (), [&MBB](const MachineBasicBlock *Succ) {
1354
+ return Succ->getCallFrameSize () ==
1355
+ (*MBB.succ_begin ())->getCallFrameSize ();
1356
+ }));
1357
+ const MachineBasicBlock &FirstSucc = **MBB.succ_begin ();
1358
+ SPAdj = TFI.alignSPAdjust (FirstSucc.getCallFrameSize ());
1359
+ if (TFI.getStackGrowthDirection () == TargetFrameLowering::StackGrowsUp)
1360
+ SPAdj = -SPAdj;
1361
+ }
1362
+
1363
+ replaceFrameIndicesBackward (&MBB, MF, SPAdj);
1364
+
1365
+ // We can't track the call frame size after call frame pseudos have been
1366
+ // eliminated. Set it to zero everywhere to keep MachineVerifier happy.
1367
+ MBB.setCallFrameSize (0 );
1368
+ }
1369
+ }
1370
+
1334
1371
// / replaceFrameIndices - Replace all MO_FrameIndex operands with physical
1335
1372
// / register references and actual offsets.
1336
1373
void PEI::replaceFrameIndices (MachineFunction &MF) {
1337
- const auto &ST = MF.getSubtarget ();
1338
- const TargetFrameLowering &TFI = *ST.getFrameLowering ();
1339
- if (!TFI.needsFrameIndexResolution (MF))
1340
- return ;
1341
-
1342
- const TargetRegisterInfo *TRI = ST.getRegisterInfo ();
1343
-
1344
- // Allow the target to determine this after knowing the frame size.
1345
- FrameIndexEliminationScavenging = (RS && !FrameIndexVirtualScavenging) ||
1346
- TRI->requiresFrameIndexReplacementScavenging (MF);
1374
+ const TargetFrameLowering &TFI = *MF.getSubtarget ().getFrameLowering ();
1347
1375
1348
1376
for (auto &MBB : MF) {
1349
1377
int SPAdj = TFI.alignSPAdjust (MBB.getCallFrameSize ());
@@ -1455,6 +1483,7 @@ void PEI::replaceFrameIndicesBackward(MachineBasicBlock *BB,
1455
1483
1456
1484
for (MachineInstr &MI : make_early_inc_range (reverse (*BB))) {
1457
1485
if (TII.isFrameInstr (MI)) {
1486
+ SPAdj -= TII.getSPAdjust (MI);
1458
1487
TFI.eliminateCallFramePseudoInstr (MF, *BB, &MI);
1459
1488
continue ;
1460
1489
}
@@ -1477,7 +1506,7 @@ void PEI::replaceFrameIndicesBackward(MachineBasicBlock *BB,
1477
1506
MachineBasicBlock::iterator Save;
1478
1507
if (LocalRS)
1479
1508
Save = std::next (LocalRS->getCurrentPosition ());
1480
- bool Removed = TRI.eliminateFrameIndex (MI, SPAdj, i, RS );
1509
+ bool Removed = TRI.eliminateFrameIndex (MI, SPAdj, i, LocalRS );
1481
1510
if (LocalRS)
1482
1511
LocalRS->skipTo (std::prev (Save));
1483
1512
@@ -1495,9 +1524,6 @@ void PEI::replaceFrameIndices(MachineBasicBlock *BB, MachineFunction &MF,
1495
1524
const TargetRegisterInfo &TRI = *MF.getSubtarget ().getRegisterInfo ();
1496
1525
const TargetFrameLowering *TFI = MF.getSubtarget ().getFrameLowering ();
1497
1526
1498
- if (TRI.supportsBackwardScavenger ())
1499
- return replaceFrameIndicesBackward (BB, MF, SPAdj);
1500
-
1501
1527
if (RS && FrameIndexEliminationScavenging)
1502
1528
RS->enterBasicBlock (*BB);
1503
1529
0 commit comments