[llvm] 1ce0617 - [InstSimplify] reduce "mul nsw i1" to "false"

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 18 10:51:47 PST 2023


Author: Sanjay Patel
Date: 2023-01-18T13:51:40-05:00
New Revision: 1ce06176eccf324ccab0a386b024739e38637a53

URL: https://github.com/llvm/llvm-project/commit/1ce06176eccf324ccab0a386b024739e38637a53
DIFF: https://github.com/llvm/llvm-project/commit/1ce06176eccf324ccab0a386b024739e38637a53.diff

LOG: [InstSimplify] reduce "mul nsw i1" to "false"

https://alive2.llvm.org/ce/z/XYGvYg

Follow-up for:
68c197f07eeae71

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstCombine/sub.ll
    llvm/test/Transforms/InstSimplify/mul.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 53434c4cb2ac..20a036cd6143 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -948,10 +948,17 @@ static Value *simplifyMulInst(Value *Op0, Value *Op1, bool IsNSW, bool IsNUW,
        match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0)))))) // Y * (X / Y)
     return X;
 
-  // i1 mul -> and.
-  if (MaxRecurse && Op0->getType()->isIntOrIntVectorTy(1))
-    if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
-      return V;
+   if (Op0->getType()->isIntOrIntVectorTy(1)) {
+    // mul i1 nsw is a special-case because -1 * -1 is poison (+1 is not
+    // representable). All other cases reduce to 0, so just return 0.
+    if (IsNSW)
+      return ConstantInt::getNullValue(Op0->getType());
+
+    // Treat "mul i1" as "and i1".
+    if (MaxRecurse)
+      if (Value *V = simplifyAndInst(Op0, Op1, Q, MaxRecurse - 1))
+        return V;
+  }
 
   // Try some generic simplifications for associative operations.
   if (Value *V =

diff  --git a/llvm/test/Transforms/InstCombine/sub.ll b/llvm/test/Transforms/InstCombine/sub.ll
index 96a527ec0749..0bab904850c6 100644
--- a/llvm/test/Transforms/InstCombine/sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub.ll
@@ -2465,12 +2465,9 @@ define <2 x i5> @
diff _of_squares_partial_nsw(<2 x i5> %x, <2 x i5> %y) {
   ret <2 x i5> %r
 }
 
-; TODO: This should simplify more.
-
 define i1 @
diff _of_squares_nsw_i1(i1 %x, i1 %y) {
 ; CHECK-LABEL: @
diff _of_squares_nsw_i1(
-; CHECK-NEXT:    [[R:%.*]] = xor i1 [[Y:%.*]], [[X:%.*]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %x2 = mul nsw i1 %x, %x
   %y2 = mul nsw i1 %y, %y

diff  --git a/llvm/test/Transforms/InstSimplify/mul.ll b/llvm/test/Transforms/InstSimplify/mul.ll
index 443a2250b0a2..8ae7f1eaac92 100644
--- a/llvm/test/Transforms/InstSimplify/mul.ll
+++ b/llvm/test/Transforms/InstSimplify/mul.ll
@@ -61,8 +61,7 @@ define i1 @mul_i1(i1 %x, i1 %y) {
 
 define i1 @mul_i1_nsw(i1 %x, i1 %y) {
 ; CHECK-LABEL: @mul_i1_nsw(
-; CHECK-NEXT:    [[R:%.*]] = mul nsw i1 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT:    ret i1 [[R]]
+; CHECK-NEXT:    ret i1 false
 ;
   %r = mul nsw i1 %x, %y
   ret i1 %r
@@ -87,7 +86,7 @@ define i1 @square_i1(i1 %x) {
 
 define i1 @square_i1_nsw(i1 %x) {
 ; CHECK-LABEL: @square_i1_nsw(
-; CHECK-NEXT:    ret i1 [[X:%.*]]
+; CHECK-NEXT:    ret i1 false
 ;
   %r = mul nsw i1 %x, %x
   ret i1 %r


        


More information about the llvm-commits mailing list