Move all of the header files which are involved in modelling the LLVM IR
[oota-llvm.git] / lib / Transforms / InstCombine / InstCombineCompares.cpp
index b0fc82f6f66e57ada7e17861aab40a1640a83527..40e559eda5e479d66c859b4f7e55534ce10e7db0 100644 (file)
@@ -15,8 +15,8 @@
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/InstructionSimplify.h"
 #include "llvm/Analysis/MemoryBuiltins.h"
-#include "llvm/DataLayout.h"
-#include "llvm/IntrinsicInst.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/IntrinsicInst.h"
 #include "llvm/Support/ConstantRange.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
 #include "llvm/Support/PatternMatch.h"
@@ -1226,6 +1226,16 @@ Instruction *InstCombiner::visitICmpInstWithInstAndIntCst(ICmpInst &ICI,
         ICI.setOperand(0, NewAnd);
         return &ICI;
       }
+
+      // Replace ((X & AndCST) > RHSV) with ((X & AndCST) != 0), if any
+      // bit set in (X & AndCST) will produce a result greater than RHSV.
+      if (ICI.getPredicate() == ICmpInst::ICMP_UGT) {
+        unsigned NTZ = AndCST->getValue().countTrailingZeros();
+        if ((NTZ < AndCST->getBitWidth()) &&
+            APInt::getOneBitSet(AndCST->getBitWidth(), NTZ).ugt(RHSV))
+          return new ICmpInst(ICmpInst::ICMP_NE, LHSI,
+                              Constant::getNullValue(RHS->getType()));
+      }
     }
 
     // Try to optimize things like "A[i]&42 == 0" to index computations.
@@ -2034,15 +2044,6 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
                                                CI->countTrailingZeros()));
       }
 
-      // Turn x&~y == 0 into x&y != 0 if x is a power of 2.
-      Value *X = 0, *Y = 0;
-      if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
-          match(Op1, m_Zero()) && isPowerOfTwo(X, TD)) {
-        return new ICmpInst(ICmpInst::ICMP_NE,
-                            Builder->CreateAnd(X, Y),
-                            Op1);
-      }
-
       break;
     }
     case ICmpInst::ICMP_NE: {
@@ -2080,15 +2081,6 @@ Instruction *InstCombiner::visitICmpInst(ICmpInst &I) {
                                                CI->countTrailingZeros()));
       }
 
-      // Turn x&~y != 0 into x&y == 0 if x is a power of 2.
-      Value *X = 0, *Y = 0;
-      if (match(Op0, m_And(m_Value(X), m_Not(m_Value(Y)))) &&
-          match(Op1, m_Zero()) && isPowerOfTwo(X, TD)) {
-        return new ICmpInst(ICmpInst::ICMP_EQ,
-                            Builder->CreateAnd(X, Y),
-                            Op1);
-      }
-
       break;
     }
     case ICmpInst::ICMP_ULT: