[llvm-branch-commits] [llvm] release/23.x: [PeepholeOpt] Erase optimized compare from LocalMIs earlier (#217848) (PR #217874)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Fri Aug 21 03:14:28 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-backend-x86

Author: llvmbot

<details>
<summary>Changes</summary>

Backport b26a359753175bd5cc53fb0f2a168d12bb0498c8

Requested by: @<!-- -->nikic

---
Full diff: https://github.com/llvm/llvm-project/pull/217874.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 ec8a0336a2105..5580db70c3bbe 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -961,6 +961,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/217874


More information about the llvm-branch-commits mailing list