https://github.com/llvmbot created https://github.com/llvm/llvm-project/pull/217874
Backport b26a359753175bd5cc53fb0f2a168d12bb0498c8 Requested by: @nikic >From 4240b115fd454b0611ac1077e7d752aa53fb4c40 Mon Sep 17 00:00:00 2001 From: Nikita Popov <[email protected]> Date: Fri, 21 Aug 2026 12:06:13 +0200 Subject: [PATCH] [PeepholeOpt] Erase optimized compare from LocalMIs earlier (#217848) 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 #194662. Fixes https://github.com/llvm/llvm-project/issues/208746. --------- Co-authored-by: woruyu <[email protected]> (cherry picked from commit b26a359753175bd5cc53fb0f2a168d12bb0498c8) --- llvm/lib/CodeGen/PeepholeOptimizer.cpp | 2 +- .../X86/peephole-compare-load-fold-ext.mir | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 llvm/test/CodeGen/X86/peephole-compare-load-fold-ext.mir 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 +... _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
