[llvm] r276523 - [InstCombine] allow icmp (bit-manipulation-intrinsic(), C) folds for vectors

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 23 06:06:51 PDT 2016


Author: spatel
Date: Sat Jul 23 08:06:49 2016
New Revision: 276523

URL: http://llvm.org/viewvc/llvm-project?rev=276523&view=rev
Log:
[InstCombine] allow icmp (bit-manipulation-intrinsic(), C) folds for vectors

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
    llvm/trunk/test/Transforms/InstCombine/intrinsics.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=276523&r1=276522&r2=276523&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Sat Jul 23 08:06:49 2016
@@ -2355,37 +2355,35 @@ Instruction *InstCombiner::foldICmpEqual
   return nullptr;
 }
 
-Instruction *InstCombiner::foldICmpIntrinsicWithConstant(ICmpInst &ICI,
-                                                         Instruction *LHSI,
-                                                         ConstantInt *RHS) {
-  IntrinsicInst *II = dyn_cast<IntrinsicInst>(LHSI);
-  if (!II || !ICI.isEquality())
+Instruction *InstCombiner::foldICmpIntrinsicWithConstant(ICmpInst &ICI) {
+  IntrinsicInst *II = dyn_cast<IntrinsicInst>(ICI.getOperand(0));
+  const APInt *Op1C;
+  if (!II || !ICI.isEquality() || !match(ICI.getOperand(1), m_APInt(Op1C)))
     return nullptr;
 
   // Handle icmp {eq|ne} <intrinsic>, intcst.
-  const APInt &RHSV = RHS->getValue();
   switch (II->getIntrinsicID()) {
   case Intrinsic::bswap:
     Worklist.Add(II);
     ICI.setOperand(0, II->getArgOperand(0));
-    ICI.setOperand(1, Builder->getInt(RHSV.byteSwap()));
+    ICI.setOperand(1, Builder->getInt(Op1C->byteSwap()));
     return &ICI;
   case Intrinsic::ctlz:
   case Intrinsic::cttz:
     // ctz(A) == bitwidth(a)  ->  A == 0 and likewise for !=
-    if (RHSV == RHS->getType()->getBitWidth()) {
+    if (*Op1C == Op1C->getBitWidth()) {
       Worklist.Add(II);
       ICI.setOperand(0, II->getArgOperand(0));
-      ICI.setOperand(1, ConstantInt::get(RHS->getType(), 0));
+      ICI.setOperand(1, ConstantInt::getNullValue(II->getType()));
       return &ICI;
     }
     break;
   case Intrinsic::ctpop:
     // popcount(A) == 0  ->  A == 0 and likewise for !=
-    if (RHS->isZero()) {
+    if (*Op1C == 0) {
       Worklist.Add(II);
       ICI.setOperand(0, II->getArgOperand(0));
-      ICI.setOperand(1, RHS);
+      ICI.setOperand(1, ConstantInt::getNullValue(II->getType()));
       return &ICI;
     }
     break;
@@ -3641,6 +3639,10 @@ Instruction *InstCombiner::visitICmpInst
 
   // See if we are doing a comparison between a constant and an instruction that
   // can be folded into the comparison.
+
+  // FIXME: Use m_APInt instead of dyn_cast<ConstantInt> to allow these
+  // transforms for vectors.
+
   if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) {
     // Since the RHS is a ConstantInt (CI), if the left hand side is an
     // instruction, see if that instruction also has constants so that the
@@ -3650,11 +3652,12 @@ Instruction *InstCombiner::visitICmpInst
         return Res;
       if (Instruction *Res = foldICmpEqualityWithConstant(I, LHSI, CI))
         return Res;
-      if (Instruction *Res = foldICmpIntrinsicWithConstant(I, LHSI, CI))
-        return Res;
     }
   }
 
+  if (Instruction *Res = foldICmpIntrinsicWithConstant(I))
+    return Res;
+
   // Handle icmp with constant (but not simple integer constant) RHS
   if (Constant *RHSC = dyn_cast<Constant>(Op1)) {
     if (Instruction *LHSI = dyn_cast<Instruction>(Op0))

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h?rev=276523&r1=276522&r2=276523&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineInternal.h Sat Jul 23 08:06:49 2016
@@ -585,8 +585,7 @@ private:
                                     ConstantInt *RHS);
   Instruction *foldICmpEqualityWithConstant(ICmpInst &ICI, Instruction *LHS,
                                             ConstantInt *RHS);
-  Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI, Instruction *LHS,
-                                             ConstantInt *RHS);
+  Instruction *foldICmpIntrinsicWithConstant(ICmpInst &ICI);
 
   Instruction *OptAndOp(Instruction *Op, ConstantInt *OpRHS,
                         ConstantInt *AndRHS, BinaryOperator &TheAnd);

Modified: llvm/trunk/test/Transforms/InstCombine/intrinsics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/intrinsics.ll?rev=276523&r1=276522&r2=276523&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/intrinsics.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/intrinsics.ll Sat Jul 23 08:06:49 2016
@@ -316,12 +316,9 @@ entry:
 ; CHECK-NEXT: store volatile i1 %pop.cmp, i1* %c
 }
 
-; FIXME: Vectors should get the same folds as scalars.
-
 define <2 x i1> @ctlz_cmp_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @ctlz_cmp_vec(
-; CHECK-NEXT:    [[X:%.*]] = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) #0
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[X]], <i32 32, i32 32>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> %a, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %x = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) nounwind readnone
@@ -331,8 +328,7 @@ define <2 x i1> @ctlz_cmp_vec(<2 x i32>
 
 define <2 x i1> @cttz_cmp_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @cttz_cmp_vec(
-; CHECK-NEXT:    [[X:%.*]] = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) #0
-; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i32> [[X]], <i32 32, i32 32>
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ne <2 x i32> %a, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %x = tail call <2 x i32> @llvm.ctlz.v2i32(<2 x i32> %a, i1 false) nounwind readnone
@@ -342,8 +338,7 @@ define <2 x i1> @cttz_cmp_vec(<2 x i32>
 
 define <2 x i1> @ctpop_cmp_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @ctpop_cmp_vec(
-; CHECK-NEXT:    [[X:%.*]] = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %a) #0
-; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> [[X]], zeroinitializer
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq <2 x i32> %a, zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[CMP]]
 ;
   %x = tail call <2 x i32> @llvm.ctpop.v2i32(<2 x i32> %a) nounwind readnone




More information about the llvm-commits mailing list