summaryrefslogtreecommitdiffstats
path: root/llvm/lib/Analysis
diff options
context:
space:
mode:
authorNick Lewycky <nicholas@mxc.ca>2011-01-23 06:20:19 +0000
committerNick Lewycky <nicholas@mxc.ca>2011-01-23 06:20:19 +0000
commitbc98f5b78edb03d1c395d7feee838f97c055851f (patch)
treef0d4a897b695c3ab52b901bd8082bc8eaf129c00 /llvm/lib/Analysis
parenta9339c6cd0a997c6cfe7d85407674d1dc9b892a2 (diff)
downloadbcm5719-llvm-bc98f5b78edb03d1c395d7feee838f97c055851f.tar.gz
bcm5719-llvm-bc98f5b78edb03d1c395d7feee838f97c055851f.zip
Use value ranges to fold ext(trunc) in SCEV when possible.
llvm-svn: 124062
Diffstat (limited to 'llvm/lib/Analysis')
-rw-r--r--llvm/lib/Analysis/ScalarEvolution.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 63602ffaa78..0f2afb2096c 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -898,6 +898,23 @@ const SCEV *ScalarEvolution::getZeroExtendExpr(const SCEV *Op,
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 @@ const SCEV *ScalarEvolution::getSignExtendExpr(const SCEV *Op,
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
OpenPOWER on IntegriCloud