diff --git a/include/llvm/Support/PatternMatch.h b/include/llvm/Support/PatternMatch.h index b203c1ee1a5..ea065a769ec 100644 --- a/include/llvm/Support/PatternMatch.h +++ b/include/llvm/Support/PatternMatch.h @@ -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 + bool match(ITy *V) { + if (const ConstantInt *CI = dyn_cast(V)) + return CI->getValue().isSignBit(); + if (const ConstantVector *CV = dyn_cast(V)) + if (ConstantInt *CI = cast_or_null(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 struct bind_ty { diff --git a/lib/Analysis/ValueTracking.cpp b/lib/Analysis/ValueTracking.cpp index 72b3f03a93a..44c1b5326bc 100644 --- a/lib/Analysis/ValueTracking.cpp +++ b/lib/Analysis/ValueTracking.cpp @@ -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.