[llvm] [CodeGen] Avoid stale LocalMIs entries after compare optimization (PR #211479)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 19 06:10:17 PDT 2026


================
@@ -959,8 +957,15 @@ bool PeepholeOptimizer::optimizeCmpInstr(
 
   // Attempt to optimize the comparison instruction.
   LLVM_DEBUG(dbgs() << "Attempting to optimize compare: " << MI);
-  if (!TII->optimizeCompareInstr(MI, SrcReg, SrcReg2, CmpMask, CmpValue, MRI))
+  // Stop tracking MI before optimizeCompareInstr may erase it. Any instruction
+  // created below could otherwise reuse MI's address and be confused with the
+  // erased instruction in LocalMIs.
+  LocalMIs.erase(&MI);
+  if (!TII->optimizeCompareInstr(MI, SrcReg, SrcReg2, CmpMask, CmpValue, MRI)) {
+    // MI was not erased, and is still part of the already visited region.
+    LocalMIs.insert(&MI);
----------------
nikic wrote:

Do you actually need this erase then insert? Wouldn't it be sufficient to unconditionally erase after this check?

As I understand it, the problem is that previously the erase() happened *after* the LocalMIs check a few lines down, so we need to do it before that. But I don't think it needs to happen before the optimizeCompareInstr() call?

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


More information about the llvm-commits mailing list