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

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 4 10:48:04 PDT 2016


Author: spatel
Date: Thu Aug  4 12:48:04 2016
New Revision: 277738

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

I'm removing a misplaced pair of more specific folds from InstCombine in this patch as well,
so we know where those folds are happening in InstSimplify.


Modified:
    llvm/trunk/lib/Analysis/InstructionSimplify.cpp
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
    llvm/trunk/test/Transforms/InstSimplify/compare.ll

Modified: llvm/trunk/lib/Analysis/InstructionSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/InstructionSimplify.cpp?rev=277738&r1=277737&r2=277738&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/InstructionSimplify.cpp (original)
+++ llvm/trunk/lib/Analysis/InstructionSimplify.cpp Thu Aug  4 12:48:04 2016
@@ -3121,17 +3121,16 @@ static Value *SimplifyICmpInst(unsigned
   // If a bit is known to be zero for A and known to be one for B,
   // then A and B cannot be equal.
   if (ICmpInst::isEquality(Pred)) {
-    if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
-      uint32_t BitWidth = CI->getBitWidth();
+    const APInt *RHSVal;
+    if (match(RHS, m_APInt(RHSVal))) {
+      unsigned BitWidth = RHSVal->getBitWidth();
       APInt LHSKnownZero(BitWidth, 0);
       APInt LHSKnownOne(BitWidth, 0);
       computeKnownBits(LHS, LHSKnownZero, LHSKnownOne, Q.DL, /*Depth=*/0, Q.AC,
                        Q.CxtI, Q.DT);
-      const APInt &RHSVal = CI->getValue();
-      if (((LHSKnownZero & RHSVal) != 0) || ((LHSKnownOne & ~RHSVal) != 0))
-        return Pred == ICmpInst::ICMP_EQ
-                   ? ConstantInt::getFalse(CI->getContext())
-                   : ConstantInt::getTrue(CI->getContext());
+      if (((LHSKnownZero & *RHSVal) != 0) || ((LHSKnownOne & ~(*RHSVal)) != 0))
+        return Pred == ICmpInst::ICMP_EQ ? ConstantInt::getFalse(ITy)
+                                         : ConstantInt::getTrue(ITy);
     }
   }
 

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=277738&r1=277737&r2=277738&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Thu Aug  4 12:48:04 2016
@@ -2277,14 +2277,8 @@ Instruction *InstCombiner::foldICmpEqual
     }
     break;
   case Instruction::Or:
-    // If bits are being or'd in that are not present in the constant we
-    // are comparing against, then the comparison could never succeed!
     // FIXME: Vectors are excluded by ConstantInt.
     if (ConstantInt *BOC = dyn_cast<ConstantInt>(BOp1)) {
-      Constant *NotCI = ConstantExpr::getNot(RHS);
-      if (!ConstantExpr::getAnd(BOC, NotCI)->isNullValue())
-        return replaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
-
       // Comparing if all bits outside of a constant mask are set?
       // Replace (X | C) == -1 with (X & ~C) == ~C.
       // This removes the -1 constant.
@@ -2299,11 +2293,6 @@ Instruction *InstCombiner::foldICmpEqual
   case Instruction::And:
     // FIXME: Vectors are excluded by ConstantInt.
     if (ConstantInt *BOC = dyn_cast<ConstantInt>(BOp1)) {
-      // If bits are being compared against that are and'd out, then the
-      // comparison can never succeed!
-      if ((*RHSV & ~BOC->getValue()) != 0)
-        return replaceInstUsesWith(ICI, Builder->getInt1(isICMP_NE));
-
       // If we have ((X & C) == C), turn it into ((X & C) != 0).
       if (RHS == BOC && RHSV->isPowerOf2())
         return new ICmpInst(isICMP_NE ? ICmpInst::ICMP_EQ : ICmpInst::ICMP_NE,

Modified: llvm/trunk/test/Transforms/InstSimplify/compare.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstSimplify/compare.ll?rev=277738&r1=277737&r2=277738&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstSimplify/compare.ll (original)
+++ llvm/trunk/test/Transforms/InstSimplify/compare.ll Thu Aug  4 12:48:04 2016
@@ -1,3 +1,4 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -instsimplify -S | FileCheck %s
 target datalayout = "p:32:32"
 
@@ -1017,9 +1018,7 @@ define i1 @icmp_eq_const(i32 %a) {
 ; FIXME: Vectors should fold the same way.
 define <2 x i1> @icmp_eq_const_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @icmp_eq_const_vec(
-; CHECK-NEXT:    [[B:%.*]] = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
-; CHECK-NEXT:    [[C:%.*]] = icmp eq <2 x i32> [[B]], <i32 1, i32 1>
-; CHECK-NEXT:    ret <2 x i1> [[C]]
+; CHECK-NEXT:    ret <2 x i1> zeroinitializer
 ;
   %b = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
   %c = icmp eq <2 x i32> %b, <i32 1, i32 1>
@@ -1038,9 +1037,7 @@ define i1 @icmp_ne_const(i32 %a) {
 ; FIXME: Vectors should fold the same way.
 define <2 x i1> @icmp_ne_const_vec(<2 x i32> %a) {
 ; CHECK-LABEL: @icmp_ne_const_vec(
-; CHECK-NEXT:    [[B:%.*]] = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
-; CHECK-NEXT:    [[C:%.*]] = icmp ne <2 x i32> [[B]], <i32 1, i32 1>
-; CHECK-NEXT:    ret <2 x i1> [[C]]
+; CHECK-NEXT:    ret <2 x i1> <i1 true, i1 true>
 ;
   %b = mul nsw <2 x i32> %a, <i32 -2, i32 -2>
   %c = icmp ne <2 x i32> %b, <i32 1, i32 1>




More information about the llvm-commits mailing list