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

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 01:49:51 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:

I'd like to have this fixed before the LLVM 23 release, so I submitted the version I had in mind here: https://github.com/llvm/llvm-project/pull/217848

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


More information about the llvm-commits mailing list