[llvm] r325048 - [InstCombine] (bool X) * Y --> X ? Y : 0

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 13 12:41:22 PST 2018


Author: spatel
Date: Tue Feb 13 12:41:22 2018
New Revision: 325048

URL: http://llvm.org/viewvc/llvm-project?rev=325048&view=rev
Log:
[InstCombine] (bool X) * Y --> X ? Y : 0

This is both a functional improvement for vectors and an
efficiency improvement for scalars. The existing code below
the new folds does the same thing for scalars, but in an 
indirect and expensive way.

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
    llvm/trunk/test/Transforms/InstCombine/mul.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp?rev=325048&r1=325047&r2=325048&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineMulDivRem.cpp Tue Feb 13 12:41:22 2018
@@ -340,6 +340,15 @@ Instruction *InstCombiner::visitMul(Bina
     }
   }
 
+  Value *X;
+  // (bool X) * Y --> X ? Y : 0
+  if (match(Op0, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
+    return SelectInst::Create(X, Op1, ConstantInt::get(I.getType(), 0));
+
+  // Y * (bool X) --> X ? Y : 0
+  if (match(Op1, m_ZExt(m_Value(X))) && X->getType()->isIntOrIntVectorTy(1))
+    return SelectInst::Create(X, Op0, ConstantInt::get(I.getType(), 0));
+
   // If one of the operands of the multiply is a cast from a boolean value, then
   // we know the bool is either zero or one, so this is a 'masking' multiply.
   //   X * Y (where Y is 0 or 1) -> X & (0-Y)

Modified: llvm/trunk/test/Transforms/InstCombine/mul.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/mul.ll?rev=325048&r1=325047&r2=325048&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/mul.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/mul.ll Tue Feb 13 12:41:22 2018
@@ -99,12 +99,21 @@ define i32 @mul_bool(i32 %x, i1 %y) {
   ret i32 %m
 }
 
-; FIXME: Commute and test vector type.
+; Commute and test vector type.
 
 define <2 x i32> @mul_bool_vec(<2 x i32> %x, <2 x i1> %y) {
 ; CHECK-LABEL: @mul_bool_vec(
-; CHECK-NEXT:    [[Z:%.*]] = zext <2 x i1> [[Y:%.*]] to <2 x i32>
-; CHECK-NEXT:    [[M:%.*]] = mul nuw <2 x i32> [[Z]], [[X:%.*]]
+; CHECK-NEXT:    [[M:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i32> [[X:%.*]], <2 x i32> zeroinitializer
+; CHECK-NEXT:    ret <2 x i32> [[M]]
+;
+  %z = zext <2 x i1> %y to <2 x i32>
+  %m = mul <2 x i32> %x, %z
+  ret <2 x i32> %m
+}
+
+define <2 x i32> @mul_bool_vec_commute(<2 x i32> %x, <2 x i1> %y) {
+; CHECK-LABEL: @mul_bool_vec_commute(
+; CHECK-NEXT:    [[M:%.*]] = select <2 x i1> [[Y:%.*]], <2 x i32> [[X:%.*]], <2 x i32> zeroinitializer
 ; CHECK-NEXT:    ret <2 x i32> [[M]]
 ;
   %z = zext <2 x i1> %y to <2 x i32>




More information about the llvm-commits mailing list