Add a m_SignBit pattern for convenience.
authorDuncan Sands <baldrick@free.fr>
Tue, 1 Feb 2011 08:50:33 +0000 (08:50 +0000)
committerDuncan Sands <baldrick@free.fr>
Tue, 1 Feb 2011 08:50:33 +0000 (08:50 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@124656 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Support/PatternMatch.h
lib/Analysis/ValueTracking.cpp

index b203c1ee1a5beb11a8eda18191c793bbeef4102c..ea065a769ecfe892ed0aceea66883bfede9dfb35 100644 (file)
@@ -116,6 +116,21 @@ struct all_ones_ty {
 /// m_AllOnes() - Match an integer or vector with all bits set to true.
 inline all_ones_ty m_AllOnes() { return all_ones_ty(); }
 
+struct signbit_ty {
+  template<typename ITy>
+  bool match(ITy *V) {
+    if (const ConstantInt *CI = dyn_cast<ConstantInt>(V))
+      return CI->getValue().isSignBit();
+    if (const ConstantVector *CV = dyn_cast<ConstantVector>(V))
+      if (ConstantInt *CI = cast_or_null<ConstantInt>(CV->getSplatValue()))
+        return CI->getValue().isSignBit();
+    return false;
+  }
+};
+
+/// m_SignBit() - Match an integer or vector with only the sign bit(s) set.
+inline signbit_ty m_SignBit() { return signbit_ty(); }
+
 
 template<typename Class>
 struct bind_ty {
index 72b3f03a93aacefdcbd3f174d135e2aec28193a9..44c1b5326bc1778bb986ba27acca0313723cf5a3 100644 (file)
@@ -666,9 +666,7 @@ bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, unsigned Depth) {
 
   // (signbit) >>l X is clearly a power of two if the one is not shifted off the
   // bottom.  If it is shifted off the bottom then the result is undefined.
-  ConstantInt *CI;
-  if (match(V, m_LShr(m_ConstantInt(CI), m_Value())) &&
-      CI->getValue().isSignBit())
+  if (match(V, m_LShr(m_SignBit(), m_Value())))
     return true;
 
   // The remaining tests are all recursive, so bail out if we hit the limit.