A shift of a power of two is a power of two or zero.
authorDuncan Sands <baldrick@free.fr>
Fri, 28 Oct 2011 18:30:05 +0000 (18:30 +0000)
committerDuncan Sands <baldrick@free.fr>
Fri, 28 Oct 2011 18:30:05 +0000 (18:30 +0000)
For completeness - not spotted in the wild.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@143211 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ValueTracking.cpp
test/Transforms/InstSimplify/AndOrXor.ll

index 90757f9798df2824d34705879d738f5a03351b36..9f7b5b501a3e26bd32225737af82b99d6a3a08ae 100644 (file)
@@ -769,6 +769,12 @@ bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, bool OrZero,
   if (Depth++ == MaxDepth)
     return false;
 
+  Value *X = 0, *Y = 0;
+  // A shift of a power of two is a power of two or zero.
+  if (OrZero && (match(V, m_Shl(m_Value(X), m_Value())) ||
+                 match(V, m_Shr(m_Value(X), m_Value()))))
+    return isPowerOfTwo(X, TD, /*OrZero*/true, Depth);
+
   if (ZExtInst *ZI = dyn_cast<ZExtInst>(V))
     return isPowerOfTwo(ZI->getOperand(0), TD, OrZero, Depth);
 
@@ -776,7 +782,6 @@ bool llvm::isPowerOfTwo(Value *V, const TargetData *TD, bool OrZero,
     return isPowerOfTwo(SI->getTrueValue(), TD, OrZero, Depth) &&
       isPowerOfTwo(SI->getFalseValue(), TD, OrZero, Depth);
 
-  Value *X = 0, *Y = 0;
   if (OrZero && match(V, m_And(m_Value(X), m_Value(Y)))) {
     // A power of two and'd with anything is a power of two or zero.
     if (isPowerOfTwo(X, TD, /*OrZero*/true, Depth) ||
index 3d04d584f48f483f163fd6323cc8108c117df8a7..33a4d6b02d638f1f0cfbb12626bd9fe6bc462c03 100644 (file)
@@ -10,3 +10,13 @@ define i64 @pow2(i32 %x) {
   ret i64 %e2
 ; CHECK: ret i64 %e
 }
+
+define i64 @pow2b(i32 %x) {
+; CHECK: @pow2b
+  %sh = shl i32 2, %x
+  %e = zext i32 %sh to i64
+  %nege = sub i64 0, %e
+  %e2 = and i64 %e, %nege
+  ret i64 %e2
+; CHECK: ret i64 %e
+}