Fix ScalarEvolution's Xor handling to not assume that an And
authorDan Gohman <gohman@apple.com>
Wed, 17 Jun 2009 01:22:39 +0000 (01:22 +0000)
committerDan Gohman <gohman@apple.com>
Wed, 17 Jun 2009 01:22:39 +0000 (01:22 +0000)
that gets recognized with a SCEVZeroExtendExpr must be an And
with a low-bits mask. With r73540, this is no longer the case.

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

lib/Analysis/ScalarEvolution.cpp
test/Analysis/ScalarEvolution/xor-and.ll [new file with mode: 0644]

index 3731fdfc712d16cf42eb3cab42ec55598b110af7..2546e81952b00be39f35720b24f408fdf756b1a9 100644 (file)
@@ -2453,9 +2453,12 @@ SCEVHandle ScalarEvolution::createSCEV(Value *V) {
           if (BO->getOpcode() == Instruction::And &&
               LCI->getValue() == CI->getValue())
             if (const SCEVZeroExtendExpr *Z =
-                  dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0))))
-              return getZeroExtendExpr(getNotSCEV(Z->getOperand()),
-                                       U->getType());
+                  dyn_cast<SCEVZeroExtendExpr>(getSCEV(U->getOperand(0)))) {
+              SCEVHandle ZO = Z->getOperand();
+              if (APIntOps::isMask(getTypeSizeInBits(ZO->getType()),
+                                   CI->getValue()))
+                return getZeroExtendExpr(getNotSCEV(ZO), U->getType());
+            }
     }
     break;
 
diff --git a/test/Analysis/ScalarEvolution/xor-and.ll b/test/Analysis/ScalarEvolution/xor-and.ll
new file mode 100644 (file)
index 0000000..9b02bb8
--- /dev/null
@@ -0,0 +1,11 @@
+; RUN: llvm-as < %s | opt -scalar-evolution -disable-output -analyze | grep {\\-->  %z}
+
+; ScalarEvolution shouldn't try to analyze %s into something like
+;   -->  (zext i4 (-1 + (-1 * (trunc i64 (8 * %x) to i4))) to i64)
+
+define i64 @foo(i64 %x) {
+  %a = shl i64 %x, 3
+  %t = and i64 %a, 8
+  %z = xor i64 %t, 8
+  ret i64 %z
+}