[llvm] [PeepholeOpt] Erase optimized compare from LocalMIs earlier (PR #217848)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 01:49:45 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

We need to drop the compare instruction that was optimized away from LocalMIs before the LocalMIs-based load folding optimization a few lines below. Addresses a regression from https://github.com/llvm/llvm-project/pull/194662.

Fixes github.com/llvm/llvm-project/issues/208746.

This is a simplified version of https://github.com/llvm/llvm-project/pull/211479.

---
Full diff: https://github.com/llvm/llvm-project/pull/217848.diff


2 Files Affected:

- (modified) llvm/lib/CodeGen/PeepholeOptimizer.cpp (+1-1) 
- (added) llvm/test/CodeGen/X86/peephole-compare-load-fold-ext.mir (+40) 


``````````diff
diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index e7b0150ae3a72..ceab2f317e056 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -963,6 +963,7 @@ bool PeepholeOptimizer::optimizeCmpInstr(
     return false;
 
   LLVM_DEBUG(dbgs() << "  -> Successfully optimized compare!\n");
+  LocalMIs.erase(&MI);
   ++NumCmps;
 
   // The eliminated compare may have been the extra use preventing a
@@ -1843,7 +1844,6 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
       }
 
       if (MI->isCompare() && optimizeCmpInstr(*MI, MF, LocalMIs)) {
-        LocalMIs.erase(MI);
         Changed = true;
         continue;
       }
diff --git a/llvm/test/CodeGen/X86/peephole-compare-load-fold-ext.mir b/llvm/test/CodeGen/X86/peephole-compare-load-fold-ext.mir
new file mode 100644
index 0000000000000..ca0ec429902c9
--- /dev/null
+++ b/llvm/test/CodeGen/X86/peephole-compare-load-fold-ext.mir
@@ -0,0 +1,40 @@
+# RUN: llc -mtriple=x86_64-- -run-pass=peephole-opt -verify-machineinstrs %s -o - | FileCheck %s
+
+# When removing the second compare makes a load foldable into the first one,
+# make sure the folded compare remains marked as already visited. Otherwise,
+# the extension optimization may incorrectly replace its source with a subreg
+# of a result defined later in the block.
+
+---
+name: compare_load_fold_before_ext
+tracksRegLiveness: true
+body: |
+  bb.0:
+    liveins: $edi, $rsi
+
+    ; CHECK-LABEL: name: compare_load_fold_before_ext
+    ; CHECK: [[SRC:%[0-9]+]]:gr32 = COPY $edi
+    ; CHECK-NEXT: [[PTR:%[0-9]+]]:gr64 = COPY $rsi
+    ; CHECK-NEXT: CMP32mr [[PTR]], 1, $noreg, 0, $noreg, [[SRC]], implicit-def $eflags
+    ; CHECK-NEXT: [[SETCC:%[0-9]+]]:gr8 = SETCCr 14, implicit $eflags
+    ; CHECK-NEXT: [[EXT:%[0-9]+]]:gr64 = MOVSX64rr32 [[SRC]]
+    ; CHECK-NEXT: NOOP implicit [[EXT]]
+    ; CHECK-NEXT: JCC_1 %bb.1, 14, implicit $eflags
+
+    %0:gr32 = COPY $edi
+    %1:gr64 = COPY $rsi
+    %2:gr32 = MOV32rm %1, 1, $noreg, 0, $noreg :: (load (s32))
+    %3:gr32 = SUB32rr %2, %0, implicit-def $eflags
+    %4:gr8 = SETCCr 14, implicit $eflags
+    %5:gr32 = SUB32rr %2, %0, implicit-def $eflags
+    %6:gr64 = MOVSX64rr32 %0
+    NOOP implicit %6
+    JCC_1 %bb.1, 14, implicit $eflags
+    JMP_1 %bb.2
+
+  bb.1:
+    RET 0
+
+  bb.2:
+    RET 0
+...

``````````

</details>


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


More information about the llvm-commits mailing list