[llvm-commits] [llvm] r123838 - in /llvm/trunk: lib/Analysis/ScalarEvolution.cpp test/Analysis/ScalarEvolution/fold.ll

Nick Lewycky nicholas at mxc.ca
Wed Jan 19 08:59:46 PST 2011


Author: nicholas
Date: Wed Jan 19 10:59:46 2011
New Revision: 123838

URL: http://llvm.org/viewvc/llvm-project?rev=123838&view=rev
Log:
Add a missed SCEV fold that is required to continue analyzing the IR produced
by indvars through the scev expander.

trunc(add x, y) --> add(trunc x, y). Currently SCEV largely folds the other way
which is probably wrong, but preserved to minimize churn. Instcombine doesn't
do this fold either, demonstrating a missed optz'n opportunity on code doing
add+trunc+add.

Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp
    llvm/trunk/test/Analysis/ScalarEvolution/fold.ll

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=123838&r1=123837&r2=123838&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Wed Jan 19 10:59:46 2011
@@ -819,6 +819,20 @@
   if (const SCEVZeroExtendExpr *SZ = dyn_cast<SCEVZeroExtendExpr>(Op))
     return getTruncateOrZeroExtend(SZ->getOperand(), Ty);
 
+  // trunc(x1+x2+...+xN) --> trunc(x1)+trunc(x2)+...+trunc(xN) if we can
+  // eliminate all the truncates.
+  if (const SCEVAddExpr *SA = dyn_cast<SCEVAddExpr>(Op)) {
+    SmallVector<const SCEV *, 4> Operands;
+    bool hasTrunc = false;
+    for (unsigned i = 0, e = SA->getNumOperands(); i != e && !hasTrunc; ++i) {
+      const SCEV *S = getTruncateExpr(SA->getOperand(i), Ty);
+      hasTrunc = isa<SCEVTruncateExpr>(S);
+      Operands.push_back(S);
+    }
+    if (!hasTrunc)
+      return getAddExpr(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;

Modified: llvm/trunk/test/Analysis/ScalarEvolution/fold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/fold.ll?rev=123838&r1=123837&r2=123838&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/fold.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/fold.ll Wed Jan 19 10:59:46 2011
@@ -1,8 +1,16 @@
 ; RUN: opt -analyze -scalar-evolution %s -S | FileCheck %s
 
-define i16 @test(i8 %x) {
+define i16 @test1(i8 %x) {
   %A = zext i8 %x to i12
   %B = sext i12 %A to i16
 ; CHECK: zext i8 %x to i16
   ret i16 %B
 }
+
+define i8 @test2(i8 %x) {
+  %A = zext i8 %x to i16
+  %B = add i16 %A, 1025
+  %C = trunc i16 %B to i8
+; CHECK: (1 + %x)
+  ret i8 %C
+}





More information about the llvm-commits mailing list