[PATCH] D126040: [InstCombine] Fold a mul with bool value into and

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu May 19 21:26:48 PDT 2022


Allen updated this revision to Diff 430876.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126040/new/

https://reviews.llvm.org/D126040

Files:
  llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
  llvm/test/Transforms/InstCombine/trunc.ll


Index: llvm/test/Transforms/InstCombine/trunc.ll
===================================================================
--- llvm/test/Transforms/InstCombine/trunc.ll
+++ llvm/test/Transforms/InstCombine/trunc.ll
@@ -1021,3 +1021,25 @@
   %sub = add nsw i16 %cast, -1
   ret i16 %sub
 }
+
+define i64 @PR55599_One(i64 %x, i64 %y) {
+; CHECK-LABEL: @PR55599_One(
+; CHECK-NEXT:    [[AND2:%.*]] = and i64 [[Y:%.*]], 1
+; CHECK-NEXT:    [[MUL:%.*]] = and i64 [[AND2]], [[X:%.*]]
+; CHECK-NEXT:    ret i64 [[MUL]]
+;
+  %and1 = and i64 %x, 1
+  %and2 = and i64 %y, 1
+  %mul = mul i64 %and1, %and2
+  ret i64 %mul
+}
+
+define i64 @PR55599_Zero(i64 %x, i64 %y, i64 %c) {
+; CHECK-LABEL: @PR55599_Zero(
+; CHECK-NEXT:    ret i64 0
+;
+  %and1 = and i64 %x, %c
+  %and2 = and i64 %y, 0
+  %mul = mul i64 %and1, %and2
+  ret i64 %mul
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -305,6 +305,15 @@
   if (I.getType()->isIntOrIntVectorTy(1))
     return BinaryOperator::CreateAnd(Op0, Op1);
 
+  // X * Y --> X & Y, iff X, Y can be only 0 or 1
+  {
+    if (match(Op0, m_And(m_Value(X), m_One())) &&
+        match(Op1, m_And(m_Value(Y), m_One())) &&
+        match(Op0, m_And(m_Value(X), m_Zero())) &&
+        match(Op1, m_And(m_Value(Y), m_Zero())))
+      return BinaryOperator::CreateAnd(Op0, Op1);
+  }
+
   // X*(1 << Y) --> X << Y
   // (1 << Y)*X --> X << Y
   {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D126040.430876.patch
Type: text/x-patch
Size: 1574 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220520/04a4d461/attachment.bin>


More information about the llvm-commits mailing list