Similarly, analyze truncate through multiply.
authorNick Lewycky <nicholas@mxc.ca>
Wed, 19 Jan 2011 18:56:00 +0000 (18:56 +0000)
committerNick Lewycky <nicholas@mxc.ca>
Wed, 19 Jan 2011 18:56:00 +0000 (18:56 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@123842 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolution.cpp
test/Analysis/ScalarEvolution/fold.ll

index 7258feca6c7453c4a6e5cdc0fb939257fadd41a2..424f74427be48949250f36ad6711b0e7f5d9d4c2 100644 (file)
@@ -833,6 +833,20 @@ const SCEV *ScalarEvolution::getTruncateExpr(const SCEV *Op,
       return getAddExpr(Operands, false, false);
   }
 
+  // trunc(x1*x2*...*xN) --> trunc(x1)*trunc(x2)*...*trunc(xN) if we can
+  // eliminate all the truncates.
+  if (const SCEVMulExpr *SM = dyn_cast<SCEVMulExpr>(Op)) {
+    SmallVector<const SCEV *, 4> Operands;
+    bool hasTrunc = false;
+    for (unsigned i = 0, e = SM->getNumOperands(); i != e && !hasTrunc; ++i) {
+      const SCEV *S = getTruncateExpr(SM->getOperand(i), Ty);
+      hasTrunc = isa<SCEVTruncateExpr>(S);
+      Operands.push_back(S);
+    }
+    if (!hasTrunc)
+      return getMulExpr(Operands, false, false);
+  }
+
   // If the input value is a chrec scev, truncate the chrec's operands.
   if (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Op)) {
     SmallVector<const SCEV *, 4> Operands;
index f46c691f46fd735dc1f285f867b4de7fd68219b9..3c1b6ae0fec445f8a1c7c9ad6f08a708096ede72 100644 (file)
@@ -14,3 +14,11 @@ define i8 @test2(i8 %x) {
 ; CHECK: (1 + %x)
   ret i8 %C
 }
+
+define i8 @test3(i8 %x) {
+  %A = zext i8 %x to i16
+  %B = mul i16 %A, 1027
+  %C = trunc i16 %B to i8
+; CHECK: (3 * %x)
+  ret i8 %C
+}