[llvm] 97ef3d4 - [InstCombine] Fold shl of constant by cttz into multiply of lowest set bit (#214517)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 16 01:15:36 PDT 2026
Author: shalom
Date: 2026-08-16T08:15:31Z
New Revision: 97ef3d4dcdda6116e4ec78683f1129626833ab54
URL: https://github.com/llvm/llvm-project/commit/97ef3d4dcdda6116e4ec78683f1129626833ab54
DIFF: https://github.com/llvm/llvm-project/commit/97ef3d4dcdda6116e4ec78683f1129626833ab54.diff
LOG: [InstCombine] Fold shl of constant by cttz into multiply of lowest set bit (#214517)
Currently, `C << cttz(X, true)` generates a DeBruijn lookup table on
RV64I (13 instructions).
And this patch adds a fold in InstCombine:
`C << cttz(X, true) --> (-X & X) * C`
This reduces the instruction count from 13 to 3 on RV64I.
The fold requires that cttz has a single use (to avoid increasing
instruction count)
Alive2 proof: https://alive2.llvm.org/ce/z/TmWxrT
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
llvm/test/Transforms/InstCombine/shift-cttz-ctlz.ll
llvm/test/Transforms/InstCombine/shift.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
index 241bc3bba51c2..7e32abcd6e9ba 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineShifts.cpp
@@ -1382,6 +1382,17 @@ Instruction *InstCombinerImpl::visitShl(BinaryOperator &I) {
}
}
+ // LHS << (cttz RHS) --> (RHS & -RHS) * LHS
+ if (match(Op1, m_OneUse(m_Cttz(m_Value(X), m_Value())))) {
+ Value *NegX = Builder.CreateNeg(X, "neg");
+ Value *LowBit = Builder.CreateAnd(NegX, X);
+ auto *Mul = BinaryOperator::CreateMul(LowBit, Op0);
+ // Propagate nuw from shl if present
+ if (I.hasNoUnsignedWrap())
+ Mul->setHasNoUnsignedWrap();
+ return Mul;
+ }
+
return nullptr;
}
diff --git a/llvm/test/Transforms/InstCombine/shift-cttz-ctlz.ll b/llvm/test/Transforms/InstCombine/shift-cttz-ctlz.ll
index e82e33e9d7f04..23b3d4b948211 100644
--- a/llvm/test/Transforms/InstCombine/shift-cttz-ctlz.ll
+++ b/llvm/test/Transforms/InstCombine/shift-cttz-ctlz.ll
@@ -5,8 +5,9 @@ define i32 @shl_cttz_false(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @shl_cttz_false(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CTTZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[Y]], i1 true)
-; CHECK-NEXT: [[RES:%.*]] = shl i32 [[X]], [[CTTZ]]
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[Y]]
+; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[Y]], [[NEG]]
+; CHECK-NEXT: [[RES:%.*]] = mul i32 [[TMP0]], [[X]]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
@@ -21,8 +22,9 @@ define i32 @shl_cttz_false_noundef(i32 %x, i32 %y) {
; CHECK-LABEL: define i32 @shl_cttz_false_noundef(
; CHECK-SAME: i32 [[X:%.*]], i32 [[Y:%.*]]) {
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[CTTZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[Y]], i1 true)
-; CHECK-NEXT: [[RES:%.*]] = shl i32 [[X]], [[CTTZ]]
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[Y]]
+; CHECK-NEXT: [[TMP0:%.*]] = and i32 [[Y]], [[NEG]]
+; CHECK-NEXT: [[RES:%.*]] = mul i32 [[TMP0]], [[X]]
; CHECK-NEXT: ret i32 [[RES]]
;
entry:
diff --git a/llvm/test/Transforms/InstCombine/shift.ll b/llvm/test/Transforms/InstCombine/shift.ll
index d63ba48830068..71914ec6393e6 100644
--- a/llvm/test/Transforms/InstCombine/shift.ll
+++ b/llvm/test/Transforms/InstCombine/shift.ll
@@ -2097,15 +2097,95 @@ define i32 @shl1_cttz_extra_use(i32 %x) {
define i32 @shl2_cttz(i32 %x) {
; CHECK-LABEL: @shl2_cttz(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[NEG]]
+; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[TMP1]], 1
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %tz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+ %shl = shl i32 2, %tz
+ ret i32 %shl
+}
+
+; C=4:
diff erent constant, should also fold
+define i32 @shl4_cttz(i32 %x) {
+; CHECK-LABEL: @shl4_cttz(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[NEG]]
+; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[TMP1]], 2
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %tz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+ %shl = shl i32 4, %tz
+ ret i32 %shl
+}
+
+; negative test: cttz has multiple uses, should not fold
+define i32 @shl2_cttz_multiuse(i32 %x) {
+; CHECK-LABEL: @shl2_cttz_multiuse(
; CHECK-NEXT: [[TZ:%.*]] = call range(i32 0, 33) i32 @llvm.cttz.i32(i32 [[X:%.*]], i1 true)
+; CHECK-NEXT: call void @use_i32(i32 [[TZ]])
; CHECK-NEXT: [[SHL:%.*]] = shl i32 2, [[TZ]]
; CHECK-NEXT: ret i32 [[SHL]]
;
%tz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+ call void @use_i32(i32 %tz)
+ %shl = shl i32 2, %tz
+ ret i32 %shl
+}
+
+; is_zero_poison = false is also optimized (LLVM auto-upgrades to true when used as shift amount)
+define i32 @shl2_cttz_zero_poison_false(i32 %x) {
+; CHECK-LABEL: @shl2_cttz_zero_poison_false(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[NEG]]
+; CHECK-NEXT: [[SHL:%.*]] = shl i32 [[TMP1]], 1
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %tz = call i32 @llvm.cttz.i32(i32 %x, i1 false)
%shl = shl i32 2, %tz
ret i32 %shl
}
+; variable LHS operand
+define i32 @shl_var_cttz(i32 %x, i32 %a) {
+; CHECK-LABEL: @shl_var_cttz(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[NEG]]
+; CHECK-NEXT: [[SHL:%.*]] = mul i32 [[TMP1]], [[A:%.*]]
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %tz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+ %shl = shl i32 %a, %tz
+ ret i32 %shl
+}
+
+; nuw flag propagation
+define i32 @shl_cttz_nuw(i32 %x, i32 %a) {
+; CHECK-LABEL: @shl_cttz_nuw(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[NEG]]
+; CHECK-NEXT: [[SHL:%.*]] = mul nuw i32 [[TMP1]], [[A:%.*]]
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %tz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+ %shl = shl nuw i32 %a, %tz
+ ret i32 %shl
+}
+
+; New test: constant with nuw flag
+define i32 @shl2_cttz_nuw(i32 %x) {
+; CHECK-LABEL: @shl2_cttz_nuw(
+; CHECK-NEXT: [[NEG:%.*]] = sub i32 0, [[X:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = and i32 [[X]], [[NEG]]
+; CHECK-NEXT: [[SHL:%.*]] = shl nuw i32 [[TMP1]], 1
+; CHECK-NEXT: ret i32 [[SHL]]
+;
+ %tz = call i32 @llvm.cttz.i32(i32 %x, i1 true)
+ %shl = shl nuw i32 2, %tz
+ ret i32 %shl
+}
+
; shift (X, amt | bitwidth - 1) -> shift (X, bitwidth - 1)
define i6 @shl_or7_eq_shl7(i6 %x, i6 %c) {
; CHECK-LABEL: @shl_or7_eq_shl7(
More information about the llvm-commits
mailing list