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

Nick Lewycky nicholas at mxc.ca
Sat Jan 22 22:20:20 PST 2011


Author: nicholas
Date: Sun Jan 23 00:20:19 2011
New Revision: 124062

URL: http://llvm.org/viewvc/llvm-project?rev=124062&view=rev
Log:
Use value ranges to fold ext(trunc) in SCEV when possible.

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=124062&r1=124061&r2=124062&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Sun Jan 23 00:20:19 2011
@@ -898,6 +898,23 @@
   void *IP = 0;
   if (const SCEV *S = UniqueSCEVs.FindNodeOrInsertPos(ID, IP)) return S;
 
+  // zext(trunc(x)) --> zext(x) or x or trunc(x)
+  if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
+    // It's possible the bits taken off by the truncate were all zero bits. If
+    // so, we should be able to simplify this further.
+    const SCEV *X = ST->getOperand();
+    ConstantRange CR = getUnsignedRange(X);
+    unsigned OrigBits = CR.getBitWidth();
+    unsigned TruncBits = getTypeSizeInBits(ST->getType());
+    unsigned NewBits = getTypeSizeInBits(Ty);
+    if (CR.truncate(TruncBits).zeroExtend(NewBits).contains(
+            CR.zextOrTrunc(NewBits))) {
+      if (NewBits > OrigBits) return getZeroExtendExpr(X, Ty);
+      if (NewBits < OrigBits) return getTruncateExpr(X, Ty);
+      return X;
+    }
+  }
+
   // If the input value is a chrec scev, and we can prove that the value
   // did not overflow the old, smaller, value, we can zero extend all of the
   // operands (often constants).  This allows analysis of something like
@@ -1039,6 +1056,23 @@
   if (isKnownNonNegative(Op))
     return getZeroExtendExpr(Op, Ty);
 
+  // sext(trunc(x)) --> sext(x) or x or trunc(x)
+  if (const SCEVTruncateExpr *ST = dyn_cast<SCEVTruncateExpr>(Op)) {
+    // It's possible the bits taken off by the truncate were all sign bits. If
+    // so, we should be able to simplify this further.
+    const SCEV *X = ST->getOperand();
+    ConstantRange CR = getSignedRange(X);
+    unsigned OrigBits = CR.getBitWidth();
+    unsigned TruncBits = getTypeSizeInBits(ST->getType());
+    unsigned NewBits = getTypeSizeInBits(Ty);
+    if (CR.truncate(TruncBits).signExtend(NewBits).contains(
+            CR.sextOrTrunc(NewBits))) {
+      if (NewBits > OrigBits) return getSignExtendExpr(X, Ty);
+      if (NewBits < OrigBits) return getTruncateExpr(X, Ty);
+      return X;
+    }
+  }
+
   // If the input value is a chrec scev, and we can prove that the value
   // did not overflow the old, smaller, value, we can sign extend all of the
   // operands (often constants).  This allows analysis of something like

Modified: llvm/trunk/test/Analysis/ScalarEvolution/fold.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Analysis/ScalarEvolution/fold.ll?rev=124062&r1=124061&r2=124062&view=diff
==============================================================================
--- llvm/trunk/test/Analysis/ScalarEvolution/fold.ll (original)
+++ llvm/trunk/test/Analysis/ScalarEvolution/fold.ll Sun Jan 23 00:20:19 2011
@@ -22,3 +22,37 @@
 ; CHECK: (3 * %x)
   ret i8 %C
 }
+
+define void @test4(i32 %x) {
+entry:
+  %0 = icmp sge i32 %x, 0
+  br i1 %0, label %loop, label %exit
+loop:
+  %A = phi i32 [0, %entry], [%I, %loop]
+  %rand = icmp sgt i32 %A, 10
+  %Z = select i1 %rand, i32 %A, i32 10
+  %B = trunc i32 %Z to i16
+  %C = sext i16 %B to i30
+; CHECK: %C =
+; CHECK-NEXT: (trunc i32 (10 smax {0,+,1}<%loop>) to i30)
+  %D = sext i16 %B to i32
+; CHECK: %D =
+; CHECK-NEXT: (10 smax {0,+,1}<%loop>)
+  %E = sext i16 %B to i34
+; CHECK: %E =
+; CHECK-NEXT: (zext i32 (10 smax {0,+,1}<%loop>) to i34)
+  %F = zext i16 %B to i30
+; CHECK: %F =
+; CHECK-NEXT: (trunc i32 (10 smax {0,+,1}<%loop>) to i30
+  %G = zext i16 %B to i32
+; CHECK: %G =
+; CHECK-NEXT: (10 smax {0,+,1}<%loop>)
+  %H = zext i16 %B to i34
+; CHECK: %H =
+; CHECK-NEXT: (zext i32 (10 smax {0,+,1}<%loop>) to i34)
+  %I = add i32 %A, 1
+  %1 = icmp ne i32 %A, 20
+  br i1 %1, label %loop, label %exit
+exit:
+  ret void
+}
\ No newline at end of file





More information about the llvm-commits mailing list