[llvm] [InstCombine] Ensure i1 smulh(x,1) fold to zero (PR #229004)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 01:19:23 PDT 2026


https://github.com/RKSimon created https://github.com/llvm/llvm-project/pull/229004

Alive2: https://alive2.llvm.org/ce/z/ZEDAx8

>From 86a4239ee5d4660e5980b587c0552e7885a90d49 Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Mon, 5 Oct 2026 09:13:32 +0100
Subject: [PATCH 1/2] [InstCombine] Add i1 mulh(x,1) test coverage

Shows smulh bug as smulh(x, i1 1) should fold to zero
---
 llvm/test/Transforms/InstCombine/smulh.ll | 16 ++++++++++++++++
 llvm/test/Transforms/InstCombine/umulh.ll | 16 ++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/llvm/test/Transforms/InstCombine/smulh.ll b/llvm/test/Transforms/InstCombine/smulh.ll
index f1ee17aeddeb357..1ff7566b684bc71 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 [[A0:%.*]]
+;
+  %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> [[A0:%.*]]
+;
+  %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 85b069a518520f7..987b83563c81f95 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
 ;

>From cc619590abcee00e71a0dd12861c99ef8991446a Mon Sep 17 00:00:00 2001
From: Simon Pilgrim <llvm-dev at redking.me.uk>
Date: Mon, 5 Oct 2026 09:15:47 +0100
Subject: [PATCH 2/2] [InstCombine] Ensure i1 smulh(x,1) fold to zero

Alive2: https://alive2.llvm.org/ce/z/ZEDAx8
---
 llvm/lib/Analysis/InstructionSimplify.cpp            | 8 ++++----
 llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 2 +-
 llvm/test/Transforms/InstCombine/smulh.ll            | 4 ++--
 3 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 29172c96d587b0a..74a266660b23417 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 cab9b9f65b10ff2..e9b99d87cbbb5db 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 1ff7566b684bc71..3edceff9e9bf347 100644
--- a/llvm/test/Transforms/InstCombine/smulh.ll
+++ b/llvm/test/Transforms/InstCombine/smulh.ll
@@ -151,7 +151,7 @@ define <16 x i16> @one_smulh_v16i16_commute(<16 x i16> %a0) {
 
 define i1 @one_smulh_i1(i1 %a0) {
 ; CHECK-LABEL: @one_smulh_i1(
-; CHECK-NEXT:    ret i1 [[A0:%.*]]
+; CHECK-NEXT:    ret i1 false
 ;
   %1 = call i1 @llvm.smulh.i1(i1 %a0, i1 1)
   ret i1 %1
@@ -159,7 +159,7 @@ define i1 @one_smulh_i1(i1 %a0) {
 
 define <2 x i1> @one_smulh_v2i1_commute(<2 x i1> %a0) {
 ; CHECK-LABEL: @one_smulh_v2i1_commute(
-; CHECK-NEXT:    ret <2 x i1> [[A0:%.*]]
+; 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



More information about the llvm-commits mailing list