[llvm] 0283f49 - [InstCombine] Ensure i1 smulh(x, 1) fold to zero (#229004)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Oct 5 02:01:34 PDT 2026
Author: Simon Pilgrim
Date: 2026-10-05T09:01:16Z
New Revision: 0283f4982d128167a4bbcc692924c26390224074
URL: https://github.com/llvm/llvm-project/commit/0283f4982d128167a4bbcc692924c26390224074
DIFF: https://github.com/llvm/llvm-project/commit/0283f4982d128167a4bbcc692924c26390224074.diff
LOG: [InstCombine] Ensure i1 smulh(x,1) fold to zero (#229004)
Alive2: https://alive2.llvm.org/ce/z/ZEDAx8
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
llvm/test/Transforms/InstCombine/smulh.ll
llvm/test/Transforms/InstCombine/umulh.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 29172c96d587b..74a266660b234 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -7131,11 +7131,11 @@ static Value *simplifyBinaryIntrinsic(Intrinsic::ID IID, Type *ReturnType,
break;
}
case Intrinsic::umulh:
- // Multiply by one.
- if (match(Op0, m_One()) || match(Op1, m_One()))
- return Constant::getNullValue(ReturnType);
- [[fallthrough]];
case Intrinsic::smulh:
+ // Multiply by one.
+ if (IID == Intrinsic::umulh || BitWidth == 1)
+ if (match(Op0, m_One()) || match(Op1, m_One()))
+ return Constant::getNullValue(ReturnType);
// Multiply by undef -> zero (NOT undef!) as other arg could still be
// zero.
if (Q.isUndefValue(Op0) || Q.isUndefValue(Op1))
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index cab9b9f65b10f..e9b99d87cbbb5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -2895,7 +2895,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
unsigned BitWidth = II->getType()->getScalarSizeInBits();
// Multiply by one.
- if (match(Arg1, m_One()))
+ if (BitWidth > 1 && match(Arg1, m_One()))
return replaceInstUsesWith(CI, Builder.CreateAShr(Arg0, BitWidth - 1));
break;
}
diff --git a/llvm/test/Transforms/InstCombine/smulh.ll b/llvm/test/Transforms/InstCombine/smulh.ll
index f1ee17aeddeb3..3edceff9e9bf3 100644
--- a/llvm/test/Transforms/InstCombine/smulh.ll
+++ b/llvm/test/Transforms/InstCombine/smulh.ll
@@ -149,6 +149,22 @@ define <16 x i16> @one_smulh_v16i16_commute(<16 x i16> %a0) {
ret <16 x i16> %1
}
+define i1 @one_smulh_i1(i1 %a0) {
+; CHECK-LABEL: @one_smulh_i1(
+; CHECK-NEXT: ret i1 false
+;
+ %1 = call i1 @llvm.smulh.i1(i1 %a0, i1 1)
+ ret i1 %1
+}
+
+define <2 x i1> @one_smulh_v2i1_commute(<2 x i1> %a0) {
+; CHECK-LABEL: @one_smulh_v2i1_commute(
+; CHECK-NEXT: ret <2 x i1> zeroinitializer
+;
+ %1 = call <2 x i1> @llvm.smulh.v2i1(<2 x i1> splat (i1 1), <2 x i1> %a0)
+ ret <2 x i1> %1
+}
+
;
; Constant Folding
;
diff --git a/llvm/test/Transforms/InstCombine/umulh.ll b/llvm/test/Transforms/InstCombine/umulh.ll
index 85b069a518520..987b83563c81f 100644
--- a/llvm/test/Transforms/InstCombine/umulh.ll
+++ b/llvm/test/Transforms/InstCombine/umulh.ll
@@ -145,6 +145,22 @@ define <16 x i16> @one_umulh_v16i16_commute(<16 x i16> %a0) {
ret <16 x i16> %1
}
+define i1 @one_umulh_i1(i1 %a0) {
+; CHECK-LABEL: @one_umulh_i1(
+; CHECK-NEXT: ret i1 false
+;
+ %1 = call i1 @llvm.umulh.i1(i1 %a0, i1 1)
+ ret i1 %1
+}
+
+define <2 x i1> @one_umulh_v2i1_commute(<2 x i1> %a0) {
+; CHECK-LABEL: @one_umulh_v2i1_commute(
+; CHECK-NEXT: ret <2 x i1> zeroinitializer
+;
+ %1 = call <2 x i1> @llvm.umulh.v2i1(<2 x i1> splat (i1 1), <2 x i1> %a0)
+ ret <2 x i1> %1
+}
+
;
; Constant Folding
;
More information about the llvm-commits
mailing list