[llvm] r314153 - [InstCombine] Move an optimization from foldICmpAndConstConst to foldICmpUsingKnownBits

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 14:15:01 PDT 2017


Author: ctopper
Date: Mon Sep 25 14:15:00 2017
New Revision: 314153

URL: http://llvm.org/viewvc/llvm-project?rev=314153&view=rev
Log:
[InstCombine] Move an optimization from foldICmpAndConstConst to foldICmpUsingKnownBits

All this optimization cares about is knowing how many low bits of LHS is known to be zero and whether that means that the result is 0 or greater than the RHS constant. It doesn't matter where the zeros in the low bits came from. So we don't need to specifically look for an AND. Instead we can use known bits.

Differential Revision: https://reviews.llvm.org/D38195

Modified:
    llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp

Modified: llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp?rev=314153&r1=314152&r2=314153&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp (original)
+++ llvm/trunk/lib/Transforms/InstCombine/InstCombineCompares.cpp Mon Sep 25 14:15:00 2017
@@ -1714,22 +1714,6 @@ Instruction *InstCombiner::foldICmpAndCo
     }
   }
 
-  // (X & C2) > C1 --> (X & C2) != 0, if any bit set in (X & C2) will produce a
- // result greater than C1. Also handle (X & C2) < C1 --> (X & C2) == 0.
-  if (!C2->isNullValue()) {
-    unsigned NumTZ = C2->countTrailingZeros();
-    if (Cmp.getPredicate() == ICmpInst::ICMP_UGT &&
-        NumTZ >= C1->getActiveBits()) {
-      Constant *Zero = Constant::getNullValue(And->getType());
-      return new ICmpInst(ICmpInst::ICMP_NE, And, Zero);
-    }
-    if (Cmp.getPredicate() == ICmpInst::ICMP_ULT &&
-        NumTZ >= C1->ceilLogBase2()) {
-      Constant *Zero = Constant::getNullValue(And->getType());
-      return new ICmpInst(ICmpInst::ICMP_EQ, And, Zero);
-    }
-  }
-
   return nullptr;
 }
 
@@ -4221,6 +4205,11 @@ Instruction *InstCombiner::foldICmpUsing
       if (Op1Min == Op0Min + 1)
         return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
                             ConstantInt::get(Op0->getType(), Op1Min - 1));
+      // X <u C --> X == 0, if the number of zero bits in the bottom of X
+      // exceeds the log2 of C.
+      if (Op0Known.countMinTrailingZeros() >= Op1Min.ceilLogBase2())
+        return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
+                            Constant::getNullValue(Op1->getType()));
     }
     break;
   }
@@ -4237,6 +4226,11 @@ Instruction *InstCombiner::foldICmpUsing
       if (Op1Min == Op0Max - 1)
         return new ICmpInst(ICmpInst::ICMP_EQ, Op0,
                             ConstantInt::get(Op1->getType(), Op1Min + 1));
+      // X >u C --> X != 0, if the number of zero bits in the bottom of X
+      // exceeds the log2 of C.
+      if (Op0Known.countMinTrailingZeros() >= Op1Min.getActiveBits())
+        return new ICmpInst(ICmpInst::ICMP_NE, Op0,
+                            Constant::getNullValue(Op1->getType()));
     }
     break;
   }




More information about the llvm-commits mailing list