[PATCH] D156690: [PEI][PowerPC] Switch to backwards frame index elimination

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 31 06:54:35 PDT 2023


foad created this revision.
foad added reviewers: nemanjai, hfinkel, arsenm, qcolombet, sepavloff, kparzysz.
Herald added subscribers: StephenFan, shchenz, arphaman, kbarton, hiraditya.
Herald added a project: All.
foad requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

This adds support for reprocessing new instructions that were generated
by the target's eliminateFrameIndex.

Backwards frame index elimination uses backwards register scavenging,
which is preferred because it does not rely on accurate kill flags.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D156690

Files:
  llvm/lib/CodeGen/PrologEpilogInserter.cpp
  llvm/lib/Target/PowerPC/PPCRegisterInfo.h


Index: llvm/lib/Target/PowerPC/PPCRegisterInfo.h
===================================================================
--- llvm/lib/Target/PowerPC/PPCRegisterInfo.h
+++ llvm/lib/Target/PowerPC/PPCRegisterInfo.h
@@ -113,6 +113,8 @@
 
   bool requiresFrameIndexScavenging(const MachineFunction &MF) const override;
 
+  bool supportsBackwardScavenger() const override { return true; }
+
   bool requiresVirtualBaseRegisters(const MachineFunction &MF) const override;
 
   void lowerDynamicAlloc(MachineBasicBlock::iterator II) const;
Index: llvm/lib/CodeGen/PrologEpilogInserter.cpp
===================================================================
--- llvm/lib/CodeGen/PrologEpilogInserter.cpp
+++ llvm/lib/CodeGen/PrologEpilogInserter.cpp
@@ -1486,7 +1486,9 @@
   if (LocalRS)
     LocalRS->enterBasicBlockEnd(*BB);
 
-  for (MachineInstr &MI : make_early_inc_range(reverse(*BB))) {
+  for (MachineBasicBlock::iterator I = BB->end(); I != BB->begin();) {
+    MachineInstr &MI = *std::prev(I);
+
     if (TII.isFrameInstr(MI)) {
       SPAdj -= TII.getSPAdjust(MI);
       TFI.eliminateCallFramePseudoInstr(MF, *BB, &MI);
@@ -1497,27 +1499,27 @@
     if (LocalRS)
       LocalRS->backward(MI);
 
-    for (unsigned i = 0; i != MI.getNumOperands(); ++i) {
-      if (!MI.getOperand(i).isFI())
+    bool RemovedMI = false;
+    for (const auto &[Idx, Op] : enumerate(MI.operands())) {
+      if (!Op.isFI())
         continue;
 
-      if (replaceFrameIndexDebugInstr(MF, MI, i, SPAdj))
+      if (replaceFrameIndexDebugInstr(MF, MI, Idx, SPAdj))
         continue;
 
       // Eliminate this FrameIndex operand.
-      //
-      // Save and restore the scavenger's position around the call to
-      // eliminateFrameIndex in case it erases MI and invalidates the iterator.
-      MachineBasicBlock::iterator Save;
-      if (LocalRS)
-	Save = std::next(LocalRS->getCurrentPosition());
-      bool Removed = TRI.eliminateFrameIndex(MI, SPAdj, i, LocalRS);
-      if (LocalRS)
-	LocalRS->skipTo(std::prev(Save));
-
-      if (Removed)
+      RemovedMI = TRI.eliminateFrameIndex(MI, SPAdj, Idx, LocalRS);
+      if (RemovedMI)
         break;
     }
+
+    // Refresh the scavenger's internal iterator in case MI was removed or more
+    // instructions were inserted after it.
+    if (LocalRS)
+      LocalRS->skipTo(std::prev(I));
+
+    if (!RemovedMI)
+      --I;
   }
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D156690.545635.patch
Type: text/x-patch
Size: 2376 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230731/51ef3c01/attachment.bin>


More information about the llvm-commits mailing list