[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 20:23:06 PDT 2022


Allen created this revision.
Allen added reviewers: efriedma, spatel, sdesmalen.
Herald added a subscriber: hiraditya.
Herald added a project: All.
Allen requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Fixes PR55599: X * Y --> X & Y, iff X, Y can be only 0 or 1


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,15 @@
   %sub = add nsw i16 %cast, -1
   ret i16 %sub
 }
+
+define i64 @PR55599(i64 %x, i64 %y) {
+; CHECK-LABEL: @PR55599(
+; 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
+}
Index: llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
@@ -305,6 +305,13 @@
   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())))
+      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.430872.patch
Type: text/x-patch
Size: 1245 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220520/464e65fa/attachment.bin>


More information about the llvm-commits mailing list