[PATCH] D100188: [AMDGPU] SIFoldOperands: eagerly erase dead REG_SEQUENCEs

Jay Foad via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 9 06:11:19 PDT 2021


foad created this revision.
foad added reviewers: arsenm, rampitec.
Herald added subscribers: kerbowa, hiraditya, t-tye, tpr, dstuttard, yaxunl, nhaehnle, jvesely, kzhuravl.
foad requested review of this revision.
Herald added subscribers: llvm-commits, wdng.
Herald added a project: LLVM.

This is fairly cheap to implement and means less work for future
passes like MachineDCE.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100188

Files:
  llvm/lib/Target/AMDGPU/SIFoldOperands.cpp


Index: llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
===================================================================
--- llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
+++ llvm/lib/Target/AMDGPU/SIFoldOperands.cpp
@@ -1596,6 +1596,10 @@
 
   LLVM_DEBUG(dbgs() << "Folded " << *RS << " into " << *UseMI);
 
+  // Erase the REG_SEQUENCE eagerly, unless we followed a chain of COPY users,
+  // in which case we can erase them all later in runOnMachineFunction.
+  if (MRI->use_nodbg_empty(MI.getOperand(0).getReg()))
+    MI.eraseFromParentAndMarkDBGValuesForRemoval();
   return true;
 }
 
@@ -1786,8 +1790,23 @@
 
       // If we managed to fold all uses of this copy then we might as well
       // delete it now.
-      if (MRI->use_nodbg_empty(MI.getOperand(0).getReg()))
-        MI.eraseFromParentAndMarkDBGValuesForRemoval();
+      // The only reason we need to follow chains of copies here is that
+      // tryFoldRegSequence looks forward through copies before folding a
+      // REG_SEQUENCE into its eventual users.
+      auto *InstToErase = &MI;
+      while (MRI->use_nodbg_empty(InstToErase->getOperand(0).getReg())) {
+        auto &SrcOp = InstToErase->getOperand(1);
+        auto SrcReg = SrcOp.isReg() ? SrcOp.getReg() : Register();
+        InstToErase->eraseFromParentAndMarkDBGValuesForRemoval();
+        if (!SrcReg || SrcReg.isPhysical())
+          break;
+        InstToErase = MRI->getVRegDef(SrcReg);
+        if (!InstToErase || !TII->isFoldableCopy(*InstToErase))
+          break;
+      }
+      if (InstToErase && InstToErase->isRegSequence() &&
+          MRI->use_nodbg_empty(InstToErase->getOperand(0).getReg()))
+        InstToErase->eraseFromParentAndMarkDBGValuesForRemoval();
     }
   }
   return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100188.336427.patch
Type: text/x-patch
Size: 1743 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210409/8e96d5aa/attachment.bin>


More information about the llvm-commits mailing list