OSDN Git Service

ValueTracking: Teach isKnownToBeAPowerOfTwo about (ADD X, (XOR X, Y)) where X is...
authorDavid Majnemer <david.majnemer@gmail.com>
Sat, 29 Jun 2013 23:44:53 +0000 (23:44 +0000)
committerDavid Majnemer <david.majnemer@gmail.com>
Sat, 29 Jun 2013 23:44:53 +0000 (23:44 +0000)
This allows us to simplify urem instructions involving the add+xor to
turn into simpler math.

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

lib/Analysis/ValueTracking.cpp
test/Transforms/InstCombine/rem.ll

index d2e13b7..39bc1a3 100644 (file)
@@ -855,16 +855,24 @@ bool llvm::isKnownToBeAPowerOfTwo(Value *V, bool OrZero, unsigned Depth) {
     return false;
   }
 
-  // Adding a power of two to the same power of two is a power of two or zero.
-  if (OrZero && match(V, m_Add(m_Value(X), m_Value(Y)))) {
-    if (match(X, m_And(m_Value(), m_Specific(Y)))) {
-      if (isKnownToBeAPowerOfTwo(Y, /*OrZero*/true, Depth))
-        return true;
-    } else if (match(Y, m_And(m_Value(), m_Specific(X)))) {
-      if (isKnownToBeAPowerOfTwo(X, /*OrZero*/true, Depth))
-        return true;
-    }
-  }
+  if (match(V, m_Add(m_Value(X), m_Value(Y))))
+    if (OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V))
+      if (OrZero || VOBO->hasNoUnsignedWrap() || VOBO->hasNoSignedWrap()) {
+        // Adding a power of two to the same power of two is a power of two or
+        // zero.
+        if (BinaryOperator *XBO = dyn_cast<BinaryOperator>(X))
+          if (XBO->getOpcode() == Instruction::And ||
+              XBO->getOpcode() == Instruction::Xor)
+            if (XBO->getOperand(0) == Y || XBO->getOperand(1) == Y)
+              if (isKnownToBeAPowerOfTwo(Y, /*OrZero*/true, Depth))
+                return true;
+        if (BinaryOperator *YBO = dyn_cast<BinaryOperator>(Y))
+          if (YBO->getOpcode() == Instruction::And ||
+              YBO->getOpcode() == Instruction::Xor)
+            if (YBO->getOperand(0) == X || YBO->getOperand(1) == X)
+              if (isKnownToBeAPowerOfTwo(X, /*OrYero*/true, Depth))
+                return true;
+      }
 
   // An exact divide or right shift can only shift off zero bits, so the result
   // is a power of two only if the first operand is a power of two and not
index 808d51e..2b49385 100644 (file)
@@ -163,3 +163,18 @@ define i32 @test16(i32 %x, i32 %y) {
        %rem = urem i32 %x, %add
        ret i32 %rem
 }
+
+define i32 @test17(i16 %x, i32 %y) {
+; CHECK: @test17
+; CHECK-NEXT: [[AND:%.*]] = and i16 %x, 4
+; CHECK-NEXT: [[EXT:%.*]] = zext i16 [[AND]] to i32
+; CHECK-NEXT: [[SHL:%.*]] = shl nuw nsw i32 [[EXT]], 3
+; CHECK-NEXT: [[XOR:%.*]] = xor i32 [[SHL]], 63
+; CHECK-NEXT: [[REM:%.*]] = and i32 [[XOR]], %y
+; CHECK-NEXT: ret i32 [[REM]]
+       %1 = and i16 %x, 4
+       %2 = icmp ne i16 %1, 0
+       %3 = select i1 %2, i32 32, i32 64
+       %4 = urem i32 %y, %3
+       ret i32 %4
+}