[llvm] [MCP] Handle iterative simplification during forward copy prop (PR #140267)

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Fri May 16 08:30:51 PDT 2025


================
@@ -977,20 +924,36 @@ void MachineCopyPropagation::ForwardCopyPropagateBlock(MachineBasicBlock &MBB) {
 
     forwardUses(MI);
 
-    // It's possible that the previous transformation has resulted in a no-op
-    // register move (i.e. one where source and destination registers are the
-    // same and are not referring to a reserved register). If so, delete it.
-    CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr);
-    if (CopyOperands &&
-        CopyOperands->Source->getReg() == CopyOperands->Destination->getReg() &&
-        !MRI->isReserved(CopyOperands->Source->getReg())) {
-      MI.eraseFromParent();
-      NumDeletes++;
+    // Attempt to canonicalize/optimize the instruction now its arguments have
+    // been mutated.  This may convert MI from a non-copy to a copy instruction.
+    if (TII->simplifyInstruction(MI)) {
       Changed = true;
-      continue;
+      LLVM_DEBUG(dbgs() << "MCP: After simplifyInstruction: " << MI);
+    }
+
+    CopyOperands = isCopyInstr(MI, *TII, UseCopyInstr);
+    if (CopyOperands) {
+      Register RegSrc = CopyOperands->Source->getReg();
+      Register RegDef = CopyOperands->Destination->getReg();
+      // It's possible that the previous transformations have resulted in a
+      // no-op register move (i.e. one where source and destination registers
+      // are the same and are not referring to a reserved register). If so,
+      // delete it.
+      if (RegSrc == RegDef && !MRI->isReserved(RegSrc)) {
+        MI.eraseFromParent();
+        NumDeletes++;
+        Changed = true;
+        continue;
+      }
+
+      if (!TRI->regsOverlap(RegDef, RegSrc)) {
+        // Copy is now a candidate for deletion.
+        MCRegister Def = RegDef.asMCReg();
+        if (!MRI->isReserved(Def))
+          MaybeDeadCopies.insert(&MI);
----------------
preames wrote:

Again, off topic, possible follow up.  Looking at this code, it really looks like we have a nearly generic DCE lurking here.  For a general instruction (not just copies), we could track if a def is used.  If not and the instruction is safe to delete, we could do so.  Not sure how worthwhile this is, but interesting.  

https://github.com/llvm/llvm-project/pull/140267


More information about the llvm-commits mailing list