[llvm] [CodeGen] Fold load into LZCNT/TZCNT/POPCNT after compare elimination in peephole optimizer (PR #194662)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 28 09:02:24 PDT 2026
https://github.com/Michael-Chen-NJU created https://github.com/llvm/llvm-project/pull/194662
When a loaded value feeds both a bit-count instruction (LZCNT/TZCNT/POPCNT) and an `icmp eq zero` / `select` sequence, ISel emits the register form (`rr`) because the loaded value has two uses at that point. The peephole optimizer later eliminates the redundant compare (recognizing that LZCNT/TZCNT/POPCNT already set the necessary EFLAGS), but failed to retry folding the load into the now single-use bit-count instruction — because that instruction had already been
scanned.
The fix pre-saves the compared register before `optimizeCmpInstr` erases the compare. After successful elimination, if the EFLAGS producer and its defining load are both already-scanned and now have a single use, fold immediately via `TII->optimizeLoadInstr`.
Before:
```
movq (%rdi), %rax
lzcntq %rax, %rax
cmovbq %rsi, %rax
```
After:
```
lzcntq (%rdi), %rax
cmovbq %rsi, %rax
```
Fixes #164782
>From 07d8bbdad2b89b3df921cc7c2667734b83bcf119 Mon Sep 17 00:00:00 2001
From: Michael-Chen-NJU <221250121 at smail.nju.edu.cn>
Date: Tue, 28 Apr 2026 23:38:53 +0800
Subject: [PATCH] [CodeGen] Fold load into LZCNT/TZCNT/POPCNT after compare
elimination in peephole optimizer
---
llvm/lib/CodeGen/PeepholeOptimizer.cpp | 42 +++++++
llvm/test/CodeGen/X86/cttz.ll | 13 +--
llvm/test/CodeGen/X86/freeze-unary.ll | 3 +-
.../lzcnt-tzcnt-popcnt-load-fold-with-cmov.ll | 104 ++++++++++++++++++
4 files changed, 153 insertions(+), 9 deletions(-)
create mode 100644 llvm/test/CodeGen/X86/lzcnt-tzcnt-popcnt-load-fold-with-cmov.ll
diff --git a/llvm/lib/CodeGen/PeepholeOptimizer.cpp b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
index 9365ea883eec9..e9812ff9e2770 100644
--- a/llvm/lib/CodeGen/PeepholeOptimizer.cpp
+++ b/llvm/lib/CodeGen/PeepholeOptimizer.cpp
@@ -1792,6 +1792,16 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
NAPhysToVirtMIs.clear();
}
+ // SrcReg must be captured before MI is erased inside optimizeCmpInstr.
+ Register CmpSrcReg;
+ if (MI->isCompare()) {
+ Register SrcReg2;
+ int64_t CmpMask, CmpValue;
+ if (!TII->analyzeCompare(*MI, CmpSrcReg, SrcReg2, CmpMask, CmpValue) ||
+ CmpSrcReg.isPhysical() || SrcReg2.isPhysical())
+ CmpSrcReg = Register();
+ }
+
if ((isUncoalescableCopy(*MI) &&
optimizeUncoalescableCopy(*MI, LocalMIs)) ||
(MI->isCompare() && optimizeCmpInstr(*MI)) ||
@@ -1799,6 +1809,38 @@ bool PeepholeOptimizer::run(MachineFunction &MF) {
// MI is deleted.
LocalMIs.erase(MI);
Changed = true;
+
+ // The eliminated compare may have been the extra use blocking load
+ // folding into the EFLAGS producer; retry now that it is gone.
+ if (CmpSrcReg.isVirtual() && MRI->hasOneNonDBGUser(CmpSrcReg)) {
+ MachineInstr *FlagProducer =
+ MRI->use_nodbg_begin(CmpSrcReg)->getParent();
+ MachineInstr *LoadMI = MRI->getVRegDef(CmpSrcReg);
+ if (LocalMIs.count(FlagProducer) && LoadMI &&
+ LoadMI->canFoldAsLoad() && LoadMI->mayLoad() &&
+ LocalMIs.count(LoadMI)) {
+ Register FoldReg = CmpSrcReg;
+ MachineInstr *DefMI = nullptr;
+ MachineInstr *CopyMI = nullptr;
+ if (MachineInstr *FoldMI = TII->optimizeLoadInstr(
+ *FlagProducer, MRI, FoldReg, DefMI, CopyMI)) {
+ LLVM_DEBUG(dbgs() << "Replacing: " << *FlagProducer);
+ LLVM_DEBUG(dbgs() << " With: " << *FoldMI);
+ LocalMIs.erase(FlagProducer);
+ LocalMIs.erase(LoadMI);
+ LocalMIs.insert(FoldMI);
+ if (CopyMI)
+ LocalMIs.insert(CopyMI);
+ if (FlagProducer->shouldUpdateAdditionalCallInfo())
+ FlagProducer->getMF()->moveAdditionalCallInfo(FlagProducer,
+ FoldMI);
+ FlagProducer->eraseFromParent();
+ DefMI->eraseFromParent();
+ MRI->markUsesInDebugValueAsUndef(CmpSrcReg);
+ ++NumLoadFold;
+ }
+ }
+ }
continue;
}
diff --git a/llvm/test/CodeGen/X86/cttz.ll b/llvm/test/CodeGen/X86/cttz.ll
index a88fb96dd7c8c..9890450a1c195 100644
--- a/llvm/test/CodeGen/X86/cttz.ll
+++ b/llvm/test/CodeGen/X86/cttz.ll
@@ -379,13 +379,12 @@ define i64 @cttz_i64_zero_test(i64 %n) nounwind {
;
; X86-CMOV-LABEL: cttz_i64_zero_test:
; X86-CMOV: # %bb.0:
-; X86-CMOV-NEXT: movl {{[0-9]+}}(%esp), %eax
-; X86-CMOV-NEXT: bsfl {{[0-9]+}}(%esp), %ecx
-; X86-CMOV-NEXT: movl $32, %edx
-; X86-CMOV-NEXT: cmovnel %ecx, %edx
-; X86-CMOV-NEXT: addl $32, %edx
-; X86-CMOV-NEXT: bsfl %eax, %eax
-; X86-CMOV-NEXT: cmovel %edx, %eax
+; X86-CMOV-NEXT: bsfl {{[0-9]+}}(%esp), %eax
+; X86-CMOV-NEXT: movl $32, %ecx
+; X86-CMOV-NEXT: cmovnel %eax, %ecx
+; X86-CMOV-NEXT: addl $32, %ecx
+; X86-CMOV-NEXT: bsfl {{[0-9]+}}(%esp), %eax
+; X86-CMOV-NEXT: cmovel %ecx, %eax
; X86-CMOV-NEXT: xorl %edx, %edx
; X86-CMOV-NEXT: retl
;
diff --git a/llvm/test/CodeGen/X86/freeze-unary.ll b/llvm/test/CodeGen/X86/freeze-unary.ll
index bc9e29957c74a..4f4b2fbd77979 100644
--- a/llvm/test/CodeGen/X86/freeze-unary.ll
+++ b/llvm/test/CodeGen/X86/freeze-unary.ll
@@ -300,8 +300,7 @@ define i32 @freeze_cttz(i32 %a0) nounwind {
define i32 @freeze_cttz_undef(i32 %a0) nounwind {
; X86-LABEL: freeze_cttz_undef:
; X86: # %bb.0:
-; X86-NEXT: movl {{[0-9]+}}(%esp), %eax
-; X86-NEXT: bsfl %eax, %ecx
+; X86-NEXT: bsfl {{[0-9]+}}(%esp), %ecx
; X86-NEXT: movl $32, %eax
; X86-NEXT: cmovnel %ecx, %eax
; X86-NEXT: retl
diff --git a/llvm/test/CodeGen/X86/lzcnt-tzcnt-popcnt-load-fold-with-cmov.ll b/llvm/test/CodeGen/X86/lzcnt-tzcnt-popcnt-load-fold-with-cmov.ll
new file mode 100644
index 0000000000000..8a171859387a5
--- /dev/null
+++ b/llvm/test/CodeGen/X86/lzcnt-tzcnt-popcnt-load-fold-with-cmov.ll
@@ -0,0 +1,104 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
+; RUN: llc < %s -mtriple=x86_64-unknown-unknown -mattr=+lzcnt,+bmi,+popcnt | FileCheck %s
+
+; Test that loads are folded into the memory form of LZCNT/TZCNT/POPCNT when
+; the loaded value is also used in an icmp-eq-zero/select sequence whose
+; condition is satisfied by the EFLAGS already produced by the bit-count
+; instruction (LZCNT/TZCNT set CF=1, POPCNT sets ZF=1 when input is zero).
+;
+; Before the fix, ISel chose the register form (rr) because the loaded value
+; had two uses: the bit-count instruction and the compare. The peephole
+; optimizer later removed the redundant TEST but missed folding the load into
+; the now single-use bit-count instruction.
+; See https://github.com/llvm/llvm-project/issues/164782
+
+; --- lzcnt: CF=1 when input is zero, cmovb takes the fallback ---
+
+define i32 @lzcnt32_load_cmov(ptr %p0, i32 %a1) {
+; CHECK-LABEL: lzcnt32_load_cmov:
+; CHECK: # %bb.0:
+; CHECK-NEXT: lzcntl (%rdi), %eax
+; CHECK-NEXT: cmovbl %esi, %eax
+; CHECK-NEXT: retq
+ %v = load i32, ptr %p0, align 4
+ %cnt = tail call range(i32 0, 33) i32 @llvm.ctlz.i32(i32 %v, i1 true)
+ %iszero = icmp eq i32 %v, 0
+ %res = select i1 %iszero, i32 %a1, i32 %cnt
+ ret i32 %res
+}
+
+define i64 @lzcnt64_load_cmov(ptr %p0, i64 %a1) {
+; CHECK-LABEL: lzcnt64_load_cmov:
+; CHECK: # %bb.0:
+; CHECK-NEXT: lzcntq (%rdi), %rax
+; CHECK-NEXT: cmovbq %rsi, %rax
+; CHECK-NEXT: retq
+ %v = load i64, ptr %p0, align 8
+ %cnt = tail call range(i64 0, 65) i64 @llvm.ctlz.i64(i64 %v, i1 true)
+ %iszero = icmp eq i64 %v, 0
+ %res = select i1 %iszero, i64 %a1, i64 %cnt
+ ret i64 %res
+}
+
+; --- tzcnt: CF=1 when input is zero, cmovb takes the fallback ---
+
+define i32 @tzcnt32_load_cmov(ptr %p0, i32 %a1) {
+; CHECK-LABEL: tzcnt32_load_cmov:
+; CHECK: # %bb.0:
+; CHECK-NEXT: tzcntl (%rdi), %eax
+; CHECK-NEXT: cmovbl %esi, %eax
+; CHECK-NEXT: retq
+ %v = load i32, ptr %p0, align 4
+ %cnt = tail call range(i32 0, 33) i32 @llvm.cttz.i32(i32 %v, i1 true)
+ %iszero = icmp eq i32 %v, 0
+ %res = select i1 %iszero, i32 %a1, i32 %cnt
+ ret i32 %res
+}
+
+define i64 @tzcnt64_load_cmov(ptr %p0, i64 %a1) {
+; CHECK-LABEL: tzcnt64_load_cmov:
+; CHECK: # %bb.0:
+; CHECK-NEXT: tzcntq (%rdi), %rax
+; CHECK-NEXT: cmovbq %rsi, %rax
+; CHECK-NEXT: retq
+ %v = load i64, ptr %p0, align 8
+ %cnt = tail call range(i64 0, 65) i64 @llvm.cttz.i64(i64 %v, i1 true)
+ %iszero = icmp eq i64 %v, 0
+ %res = select i1 %iszero, i64 %a1, i64 %cnt
+ ret i64 %res
+}
+
+; --- popcnt: ZF=1 when input is zero, cmove takes the fallback ---
+
+define i32 @popcnt32_load_cmov(ptr %p0, i32 %a1) {
+; CHECK-LABEL: popcnt32_load_cmov:
+; CHECK: # %bb.0:
+; CHECK-NEXT: popcntl (%rdi), %eax
+; CHECK-NEXT: cmovel %esi, %eax
+; CHECK-NEXT: retq
+ %v = load i32, ptr %p0, align 4
+ %cnt = tail call i32 @llvm.ctpop.i32(i32 %v)
+ %iszero = icmp eq i32 %v, 0
+ %res = select i1 %iszero, i32 %a1, i32 %cnt
+ ret i32 %res
+}
+
+define i64 @popcnt64_load_cmov(ptr %p0, i64 %a1) {
+; CHECK-LABEL: popcnt64_load_cmov:
+; CHECK: # %bb.0:
+; CHECK-NEXT: popcntq (%rdi), %rax
+; CHECK-NEXT: cmoveq %rsi, %rax
+; CHECK-NEXT: retq
+ %v = load i64, ptr %p0, align 8
+ %cnt = tail call i64 @llvm.ctpop.i64(i64 %v)
+ %iszero = icmp eq i64 %v, 0
+ %res = select i1 %iszero, i64 %a1, i64 %cnt
+ ret i64 %res
+}
+
+declare i32 @llvm.ctlz.i32(i32, i1) nounwind readnone
+declare i64 @llvm.ctlz.i64(i64, i1) nounwind readnone
+declare i32 @llvm.cttz.i32(i32, i1) nounwind readnone
+declare i64 @llvm.cttz.i64(i64, i1) nounwind readnone
+declare i32 @llvm.ctpop.i32(i32) nounwind readnone
+declare i64 @llvm.ctpop.i64(i64) nounwind readnone
More information about the llvm-commits
mailing list