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

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 18 10:55:59 PDT 2016


Author: spatel
Date: Thu Aug 18 12:55:59 2016
New Revision: 279101

URL: http://llvm.org/viewvc/llvm-project?rev=279101&view=rev
Log:
[InstCombine] use m_APInt to allow icmp (udiv 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

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

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=279101&r1=279100&r2=279101&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu Aug 18 12:55:59 2016
@@ -2105,24 +2105,26 @@ Instruction *InstCombiner::foldICmpShrCo
 Instruction *InstCombiner::foldICmpUDivConstant(ICmpInst &Cmp,
                                                 Instruction *UDiv,
                                                 const APInt *C) {
-  // FIXME: This check restricts all folds under here to scalar types.
-  if (ConstantInt *DivLHS = dyn_cast<ConstantInt>(UDiv->getOperand(0))) {
-    Value *X = UDiv->getOperand(1);
-    const APInt &C2 = DivLHS->getValue();
-    assert(C2 != 0 && "udiv 0, X should have been simplified already.");
-    // (icmp ugt (udiv C2, X), C1) -> (icmp ule X, C2/(C1+1))
-    if (Cmp.getPredicate() == ICmpInst::ICMP_UGT) {
-      assert(!C->isMaxValue() &&
-             "icmp ugt X, UINT_MAX should have been simplified already.");
-      return new ICmpInst(ICmpInst::ICMP_ULE, X,
-                          ConstantInt::get(X->getType(), C2.udiv(*C + 1)));
-    }
-    // (icmp ult (udiv C2, X), C1) -> (icmp ugt X, C2/C1)
-    if (Cmp.getPredicate() == ICmpInst::ICMP_ULT) {
-      assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
-      return new ICmpInst(ICmpInst::ICMP_UGT, X,
-                          ConstantInt::get(X->getType(), C2.udiv(*C)));
-    }
+  const APInt *C2;
+  if (!match(UDiv->getOperand(0), m_APInt(C2)))
+    return nullptr;
+
+  assert(C2 != 0 && "udiv 0, X should have been simplified already.");
+
+  // (icmp ugt (udiv C2, Y), C) -> (icmp ule Y, C2/(C+1))
+  Value *Y = UDiv->getOperand(1);
+  if (Cmp.getPredicate() == ICmpInst::ICMP_UGT) {
+    assert(!C->isMaxValue() &&
+           "icmp ugt X, UINT_MAX should have been simplified already.");
+    return new ICmpInst(ICmpInst::ICMP_ULE, Y,
+                        ConstantInt::get(Y->getType(), C2->udiv(*C + 1)));
+  }
+
+  // (icmp ult (udiv C2, Y), C) -> (icmp ugt Y, C2/C)
+  if (Cmp.getPredicate() == ICmpInst::ICMP_ULT) {
+    assert(C != 0 && "icmp ult X, 0 should have been simplified already.");
+    return new ICmpInst(ICmpInst::ICMP_UGT, Y,
+                        ConstantInt::get(Y->getType(), C2->udiv(*C)));
   }
 
   return nullptr;

Modified: llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll?rev=279101&r1=279100&r2=279101&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/compare-udiv.ll Thu Aug 18 12:55:59 2016
@@ -148,12 +148,9 @@ define i1 @test8(i32 %d) {
   ret i1 %cmp1
 }
 
-; FIXME: Vectors should get the same folds as scalars for all tests.
-
 define <2 x i1> @test8vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test8vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> [[DIV]], <i32 3, i32 3>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> %d, <i32 2, i32 2>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d
@@ -173,8 +170,7 @@ define i1 @test9(i32 %d) {
 
 define <2 x i1> @test9vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test9vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> [[DIV]], <i32 2, i32 2>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> %d, <i32 2, i32 2>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d
@@ -194,8 +190,7 @@ define i1 @test10(i32 %d) {
 
 define <2 x i1> @test10vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test10vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> [[DIV]], <i32 1, i32 1>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> %d, <i32 3, i32 3>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d
@@ -215,8 +210,7 @@ define i1 @test11(i32 %d) {
 
 define <2 x i1> @test11vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test11vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> [[DIV]], <i32 1, i32 1>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> %d, <i32 4, i32 4>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d
@@ -236,8 +230,7 @@ define i1 @test12(i32 %d) {
 
 define <2 x i1> @test12vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test12vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> [[DIV]], <i32 2, i32 2>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> %d, <i32 2, i32 2>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d
@@ -257,8 +250,7 @@ define i1 @test13(i32 %d) {
 
 define <2 x i1> @test13vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test13vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> [[DIV]], <i32 3, i32 3>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> %d, <i32 1, i32 1>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d
@@ -278,8 +270,7 @@ define i1 @test14(i32 %d) {
 
 define <2 x i1> @test14vec(<2 x i32> %d) {
 ; CHECK-LABEL: @test14vec(
-; CHECK-NEXT:    [[DIV:%.*]] = udiv <2 x i32> <i32 4, i32 4>, %d
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ult <2 x i32> [[DIV]], <i32 4, i32 4>
+; CHECK-NEXT:    [[CMP1:%.*]] = icmp ugt <2 x i32> %d, <i32 1, i32 1>
 ; CHECK-NEXT:    ret <2 x i1> [[CMP1]]
 ;
   %div = udiv <2 x i32> <i32 4, i32 4>, %d




More information about the llvm-commits mailing list