[llvm] r279133 - [InstCombine] use m_APInt to allow icmp (trunc X, Y), C folds for splat constant vectors

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 18 13:28:54 PDT 2016


Author: spatel
Date: Thu Aug 18 15:28:54 2016
New Revision: 279133

URL: http://llvm.org/viewvc/llvm-project?rev=279133&view=rev
Log:
[InstCombine] use m_APInt to allow icmp (trunc X, Y), C folds for splat constant vectors

This is a sibling of:
https://reviews.llvm.org/rL278859
https://reviews.llvm.org/rL278935
https://reviews.llvm.org/rL278945
https://reviews.llvm.org/rL279066
https://reviews.llvm.org/rL279077
https://reviews.llvm.org/rL279101

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstCombine/cast.ll
    llvm/trunk/test/Transforms/InstCombine/compare-signs.ll

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=279133&r1=279132&r2=279133&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu Aug 18 15:28:54 2016
@@ -1538,14 +1538,9 @@ Instruction *InstCombiner::foldICmpCstSh
 Instruction *InstCombiner::foldICmpTruncConstant(ICmpInst &Cmp,
                                                  Instruction *Trunc,
                                                  const APInt *C) {
-  // FIXME: This check restricts all folds under here to scalar types.
-  ConstantInt *RHS = dyn_cast<ConstantInt>(Cmp.getOperand(1));
-  if (!RHS)
-    return nullptr;
-
   ICmpInst::Predicate Pred = Cmp.getPredicate();
   Value *X = Trunc->getOperand(0);
-  if (RHS->isOne() && C->getBitWidth() > 1) {
+  if (*C == 1 && C->getBitWidth() > 1) {
     // icmp slt trunc(signum(V)) 1 --> icmp slt V, 1
     Value *V = nullptr;
     if (Pred == ICmpInst::ICMP_SLT && match(X, m_Signum(m_Value(V))))
@@ -1556,8 +1551,8 @@ Instruction *InstCombiner::foldICmpTrunc
   if (Cmp.isEquality() && Trunc->hasOneUse()) {
     // Simplify icmp eq (trunc x to i8), 42 -> icmp eq x, 42|highbits if all
     // of the high bits truncated out of x are known.
-    unsigned DstBits = Trunc->getType()->getPrimitiveSizeInBits(),
-             SrcBits = X->getType()->getPrimitiveSizeInBits();
+    unsigned DstBits = Trunc->getType()->getScalarSizeInBits(),
+             SrcBits = X->getType()->getScalarSizeInBits();
     APInt KnownZero(SrcBits, 0), KnownOne(SrcBits, 0);
     computeKnownBits(X, KnownZero, KnownOne, 0, &Cmp);
 
@@ -1566,7 +1561,7 @@ Instruction *InstCombiner::foldICmpTrunc
       // Pull in the high bits from known-ones set.
       APInt NewRHS = C->zext(SrcBits);
       NewRHS |= KnownOne & APInt::getHighBitsSet(SrcBits, SrcBits - DstBits);
-      return new ICmpInst(Pred, X, Builder->getInt(NewRHS));
+      return new ICmpInst(Pred, X, ConstantInt::get(X->getType(), NewRHS));
     }
   }
 

Modified: llvm/trunk/test/Transforms/InstCombine/cast.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/cast.ll?rev=279133&r1=279132&r2=279133&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/cast.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/cast.ll Thu Aug 18 15:28:54 2016
@@ -356,7 +356,7 @@ define i1 @test31(i64 %A) {
   ret i1 %D
 }
 
-; FIXME: Vectors should fold too...or not? 
+; FIXME: Vectors should fold too...or not?
 ; Does this depend on the whether the source/dest types of the trunc are legal in the data layout?
 define <2 x i1> @test31vec(<2 x i64> %A) {
 ; CHECK-LABEL: @test31vec(
@@ -414,12 +414,11 @@ define i1 @test36(i32 %a) {
   ret i1 %d
 }
 
-; FIXME: Vectors should fold too.
+; FIXME: The trunc is removed, but the icmp+lshr fold is missing.
 define <2 x i1> @test36vec(<2 x i32> %a) {
 ; CHECK-LABEL: @test36vec(
 ; CHECK-NEXT:    [[B:%.*]] = lshr <2 x i32> %a, <i32 31, i32 31>
-; CHECK-NEXT:    [[C:%.*]] = trunc <2 x i32> [[B]] to <2 x i8>
-; CHECK-NEXT:    [[D:%.*]] = icmp eq <2 x i8> [[C]], zeroinitializer
+; CHECK-NEXT:    [[D:%.*]] = icmp eq <2 x i32> [[B]], zeroinitializer
 ; CHECK-NEXT:    ret <2 x i1> [[D]]
 ;
   %b = lshr <2 x i32> %a, <i32 31, i32 31>

Modified: llvm/trunk/test/Transforms/InstCombine/compare-signs.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/compare-signs.ll?rev=279133&r1=279132&r2=279133&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/compare-signs.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/compare-signs.ll Thu Aug 18 15:28:54 2016
@@ -107,15 +107,9 @@ define i1 @test4c(i64 %a) {
   ret i1 %c
 }
 
-; FIXME: Vectors should fold too.
 define <2 x i1> @test4c_vec(<2 x i64> %a) {
 ; CHECK-LABEL: @test4c_vec(
-; CHECK-NEXT:    [[L:%.*]] = ashr <2 x i64> %a, <i64 63, i64 63>
-; CHECK-NEXT:    [[NA:%.*]] = sub <2 x i64> zeroinitializer, %a
-; CHECK-NEXT:    [[R:%.*]] = lshr <2 x i64> [[NA]], <i64 63, i64 63>
-; CHECK-NEXT:    [[SIGNUM:%.*]] = or <2 x i64> [[L]], [[R]]
-; CHECK-NEXT:    [[SIGNUM_TRUNC:%.*]] = trunc <2 x i64> [[SIGNUM]] to <2 x i32>
-; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i32> [[SIGNUM_TRUNC]], <i32 1, i32 1>
+; CHECK-NEXT:    [[C:%.*]] = icmp slt <2 x i64> %a, <i64 1, i64 1>
 ; CHECK-NEXT:    ret <2 x i1> [[C]]
 ;
   %l = ashr <2 x i64> %a, <i64 63, i64 63>




More information about the llvm-commits mailing list