Skip to content

Commit 4fd186d

Browse files
committed
[PEI] Simplify iterator handling in replaceFrameIndicesBackward. NFCI.
Differential Revision: https://reviews.llvm.org/D154346
1 parent b4a62b1 commit 4fd186d

File tree

1 file changed

+8
-40
lines changed

1 file changed

+8
-40
lines changed

llvm/lib/CodeGen/PrologEpilogInserter.cpp

Lines changed: 8 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,59 +1464,27 @@ void PEI::replaceFrameIndicesBackward(MachineBasicBlock *BB,
14641464
RS->enterBasicBlockEnd(*BB);
14651465

14661466
for (MachineInstr &MI : make_early_inc_range(reverse(*BB))) {
1467+
// Step backwards to get the liveness state at (immedately after) MI.
1468+
RS->backward(MI);
14671469

1468-
// Register scavenger backward step
1469-
MachineBasicBlock::iterator Step(MI);
14701470
for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
14711471
if (!MI.getOperand(i).isFI())
14721472
continue;
14731473

14741474
if (replaceFrameIndexDebugInstr(MF, MI, i, SPAdj))
14751475
continue;
14761476

1477-
// If this instruction has a FrameIndex operand, we need to
1478-
// use that target machine register info object to eliminate
1479-
// it.
1480-
1481-
// TRI.eliminateFrameIndex may lower the frame index to a sequence of
1482-
// instructions. It also can remove/change instructions passed by the
1483-
// iterator and invalidate the iterator. We have to take care of this. For
1484-
// that we support two iterators: *Step* - points to the position up to
1485-
// which the scavenger should scan by the next iteration to have liveness
1486-
// information up to date. *Curr* - keeps track of the correct RS->MBBI -
1487-
// the scan start point. It points to the currently processed instruction
1488-
// right before the frame lowering.
1477+
// Eliminate this FrameIndex operand.
14891478
//
1490-
// ITERATORS WORK AS FOLLOWS:
1491-
// *Step* is shifted one step back right before the frame lowering and
1492-
// one step forward right after it. No matter how many instructions were
1493-
// inserted, *Step* will be right after the position which is going to be
1494-
// processed in the next iteration, thus, in the correct position for the
1495-
// scavenger to go up to.
1496-
// *Curr* is shifted one step forward right before calling
1497-
// TRI.eliminateFrameIndex and one step backward after. Thus, we make sure
1498-
// it points right to the position that is the correct starting point for
1499-
// the scavenger to scan.
1500-
MachineBasicBlock::iterator Curr = ++RS->getCurrentPosition();
1501-
1502-
// Shift back
1503-
--Step;
1504-
1479+
// Save and restore the scavenger's position around the call to
1480+
// eliminateFrameIndex in case it erases MI and invalidates the iterator.
1481+
MachineBasicBlock::iterator Save = std::next(RS->getCurrentPosition());
15051482
bool Removed = TRI.eliminateFrameIndex(MI, SPAdj, i, RS);
1506-
// Restore to unify logic with a shift back that happens in the end of
1507-
// the outer loop.
1508-
++Step;
1509-
RS->skipTo(--Curr);
1483+
RS->skipTo(std::prev(Save));
1484+
15101485
if (Removed)
15111486
break;
15121487
}
1513-
1514-
// Shift it to make RS collect reg info up to the current instruction.
1515-
if (Step != BB->begin())
1516-
Step--;
1517-
1518-
// Update register states.
1519-
RS->backward(Step);
15201488
}
15211489
}
15221490

0 commit comments

Comments
 (0)