[llvm] [Peephole] Check instructions from CopyMIs are still COPY (PR #69511)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 19 14:44:06 PDT 2023


================
@@ -1445,7 +1445,9 @@ bool PeepholeOptimizer::foldRedundantCopy(
   }
 
   MachineInstr *PrevCopy = CopyMIs.find(SrcPair)->second;
-  if (!LocalMIs.count(PrevCopy))
+  // A COPY instruction can be deleted or changed by other optimizations.
+  // Check if the previous COPY instruction is existing and still a COPY.
+  if (!LocalMIs.count(PrevCopy) || !PrevCopy->isCopy())
----------------
weiguozhi wrote:

To make it clear, if I use the delegate method, I will use the MachineFunction::Delegate. It already has a function MF_HandleRemoval which is useful for our purpose.
```
// add a new callback function in MachineFunction::Delegate
virtual void MF_HandleChangeDesc(MachineInstr &MI, const MCInstrDesc &TID)


void MachineFunction::handleChangeDesc(MachineInstr &MI, const MCInstrDesc &TID) {
  if (TheDelegate)
    TheDelegate->MF_HandleChangeDesc(MI, TID);
}

// Call the callback when an MachineInstr is modified.
void MachineInstr::setDesc(const MCInstrDesc &TID) {
  getMF()->handleChangeDesc(*this, TID)
   MCID = &TID; 
}
```

Then we can implement MachineFunction::Delegate in class PeepholeOptimizer, and maintains CopyMIs in the callback functions.

Is this OK?

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


More information about the llvm-commits mailing list