Wind SCEV back in time, to Nov 18th. This 'fixes' PR3275, PR3294, PR3295,

PR3296 and PR3302.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@62160 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Nick Lewycky
2009-01-13 09:18:58 +00:00
parent 3ff704fa2b
commit 789558db70
12 changed files with 50 additions and 266 deletions

View File

@@ -225,7 +225,6 @@ namespace llvm {
return getMulExpr(Ops); return getMulExpr(Ops);
} }
SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS); SCEVHandle getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
SCEVHandle getSDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS);
SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step, SCEVHandle getAddRecExpr(const SCEVHandle &Start, const SCEVHandle &Step,
const Loop *L); const Loop *L);
SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands, SCEVHandle getAddRecExpr(std::vector<SCEVHandle> &Operands,

View File

@@ -14,7 +14,7 @@
#ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H #ifndef LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
#define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H #define LLVM_ANALYSIS_SCALAREVOLUTION_EXPANDER_H
#include "llvm/Instruction.h" #include "llvm/Instructions.h"
#include "llvm/Type.h" #include "llvm/Type.h"
#include "llvm/Analysis/ScalarEvolution.h" #include "llvm/Analysis/ScalarEvolution.h"
#include "llvm/Analysis/ScalarEvolutionExpressions.h" #include "llvm/Analysis/ScalarEvolutionExpressions.h"
@@ -104,8 +104,6 @@ namespace llvm {
Value *visitUDivExpr(SCEVUDivExpr *S); Value *visitUDivExpr(SCEVUDivExpr *S);
Value *visitSDivExpr(SCEVSDivExpr *S);
Value *visitAddRecExpr(SCEVAddRecExpr *S); Value *visitAddRecExpr(SCEVAddRecExpr *S);
Value *visitSMaxExpr(SCEVSMaxExpr *S); Value *visitSMaxExpr(SCEVSMaxExpr *S);
@@ -119,3 +117,4 @@ namespace llvm {
} }
#endif #endif

View File

@@ -25,7 +25,7 @@ namespace llvm {
// These should be ordered in terms of increasing complexity to make the // These should be ordered in terms of increasing complexity to make the
// folders simpler. // folders simpler.
scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr, scConstant, scTruncate, scZeroExtend, scSignExtend, scAddExpr, scMulExpr,
scUDivExpr, scSDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, scUnknown, scUDivExpr, scAddRecExpr, scUMaxExpr, scSMaxExpr, scUnknown,
scCouldNotCompute scCouldNotCompute
}; };
@@ -357,55 +357,6 @@ namespace llvm {
}; };
//===--------------------------------------------------------------------===//
/// SCEVSDivExpr - This class represents a binary signed division operation.
///
class SCEVSDivExpr : public SCEV {
friend class ScalarEvolution;
SCEVHandle LHS, RHS;
SCEVSDivExpr(const SCEVHandle &lhs, const SCEVHandle &rhs)
: SCEV(scSDivExpr), LHS(lhs), RHS(rhs) {}
virtual ~SCEVSDivExpr();
public:
const SCEVHandle &getLHS() const { return LHS; }
const SCEVHandle &getRHS() const { return RHS; }
virtual bool isLoopInvariant(const Loop *L) const {
return LHS->isLoopInvariant(L) && RHS->isLoopInvariant(L);
}
virtual bool hasComputableLoopEvolution(const Loop *L) const {
return LHS->hasComputableLoopEvolution(L) &&
RHS->hasComputableLoopEvolution(L);
}
SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
const SCEVHandle &Conc,
ScalarEvolution &SE) const {
SCEVHandle L = LHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
SCEVHandle R = RHS->replaceSymbolicValuesWithConcrete(Sym, Conc, SE);
if (L == LHS && R == RHS)
return this;
else
return SE.getSDivExpr(L, R);
}
virtual const Type *getType() const;
void print(std::ostream &OS) const;
void print(std::ostream *OS) const { if (OS) print(*OS); }
/// Methods for support type inquiry through isa, cast, and dyn_cast:
static inline bool classof(const SCEVSDivExpr *S) { return true; }
static inline bool classof(const SCEV *S) {
return S->getSCEVType() == scSDivExpr;
}
};
//===--------------------------------------------------------------------===// //===--------------------------------------------------------------------===//
/// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip /// SCEVAddRecExpr - This node represents a polynomial recurrence on the trip
/// count of the specified loop. /// count of the specified loop.
@@ -599,8 +550,6 @@ namespace llvm {
return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S); return ((SC*)this)->visitMulExpr((SCEVMulExpr*)S);
case scUDivExpr: case scUDivExpr:
return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S); return ((SC*)this)->visitUDivExpr((SCEVUDivExpr*)S);
case scSDivExpr:
return ((SC*)this)->visitSDivExpr((SCEVSDivExpr*)S);
case scAddRecExpr: case scAddRecExpr:
return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S); return ((SC*)this)->visitAddRecExpr((SCEVAddRecExpr*)S);
case scSMaxExpr: case scSMaxExpr:

View File

@@ -112,7 +112,6 @@ char ScalarEvolution::ID = 0;
SCEV::~SCEV() {} SCEV::~SCEV() {}
void SCEV::dump() const { void SCEV::dump() const {
print(cerr); print(cerr);
cerr << '\n';
} }
uint32_t SCEV::getBitWidth() const { uint32_t SCEV::getBitWidth() const {
@@ -325,26 +324,6 @@ const Type *SCEVUDivExpr::getType() const {
return LHS->getType(); return LHS->getType();
} }
// SCEVSDivs - Only allow the creation of one SCEVSDivExpr for any particular
// input. Don't use a SCEVHandle here, or else the object will never be
// deleted!
static ManagedStatic<std::map<std::pair<SCEV*, SCEV*>,
SCEVSDivExpr*> > SCEVSDivs;
SCEVSDivExpr::~SCEVSDivExpr() {
SCEVSDivs->erase(std::make_pair(LHS, RHS));
}
void SCEVSDivExpr::print(std::ostream &OS) const {
OS << "(" << *LHS << " /s " << *RHS << ")";
}
const Type *SCEVSDivExpr::getType() const {
return LHS->getType();
}
// SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any // SCEVAddRecExprs - Only allow the creation of one SCEVAddRecExpr for any
// particular input. Don't use a SCEVHandle here, or else the object will never // particular input. Don't use a SCEVHandle here, or else the object will never
// be deleted! // be deleted!
@@ -1130,12 +1109,9 @@ SCEVHandle ScalarEvolution::getMulExpr(std::vector<SCEVHandle> &Ops) {
} }
SCEVHandle ScalarEvolution::getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) { SCEVHandle ScalarEvolution::getUDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
if (LHS == RHS)
return getIntegerSCEV(1, LHS->getType()); // X udiv X --> 1
if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) { if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
if (RHSC->getValue()->equalsInt(1)) if (RHSC->getValue()->equalsInt(1))
return LHS; // X udiv 1 --> X return LHS; // X udiv 1 --> x
if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) { if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Constant *LHSCV = LHSC->getValue(); Constant *LHSCV = LHSC->getValue();
@@ -1144,34 +1120,13 @@ SCEVHandle ScalarEvolution::getUDivExpr(const SCEVHandle &LHS, const SCEVHandle
} }
} }
// FIXME: implement folding of (X*4)/4 when we know X*4 doesn't overflow.
SCEVUDivExpr *&Result = (*SCEVUDivs)[std::make_pair(LHS, RHS)]; SCEVUDivExpr *&Result = (*SCEVUDivs)[std::make_pair(LHS, RHS)];
if (Result == 0) Result = new SCEVUDivExpr(LHS, RHS); if (Result == 0) Result = new SCEVUDivExpr(LHS, RHS);
return Result; return Result;
} }
SCEVHandle ScalarEvolution::getSDivExpr(const SCEVHandle &LHS, const SCEVHandle &RHS) {
if (LHS == RHS)
return getIntegerSCEV(1, LHS->getType()); // X sdiv X --> 1
if (SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
if (RHSC->getValue()->equalsInt(1))
return LHS; // X sdiv 1 --> X
if (RHSC->getValue()->isAllOnesValue())
return getNegativeSCEV(LHS); // X sdiv -1 --> -X
if (SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
Constant *LHSCV = LHSC->getValue();
Constant *RHSCV = RHSC->getValue();
return getUnknown(ConstantExpr::getSDiv(LHSCV, RHSCV));
}
}
SCEVSDivExpr *&Result = (*SCEVSDivs)[std::make_pair(LHS, RHS)];
if (Result == 0) Result = new SCEVSDivExpr(LHS, RHS);
return Result;
}
/// SCEVAddRecExpr::get - Get a add recurrence expression for the /// SCEVAddRecExpr::get - Get a add recurrence expression for the
/// specified loop. Simplify the expression as much as possible. /// specified loop. Simplify the expression as much as possible.
@@ -1522,7 +1477,7 @@ namespace {
/// specified less-than comparison will execute. If not computable, return /// specified less-than comparison will execute. If not computable, return
/// UnknownValue. isSigned specifies whether the less-than is signed. /// UnknownValue. isSigned specifies whether the less-than is signed.
SCEVHandle HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, SCEVHandle HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L,
bool isSigned, bool trueWhenEqual); bool isSigned);
/// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB /// getPredecessorWithUniqueSuccessorForBB - Return a predecessor of BB
/// (which may not be an immediate predecessor) which has exactly one /// (which may not be an immediate predecessor) which has exactly one
@@ -1532,13 +1487,7 @@ namespace {
/// executesAtLeastOnce - Test whether entry to the loop is protected by /// executesAtLeastOnce - Test whether entry to the loop is protected by
/// a conditional between LHS and RHS. /// a conditional between LHS and RHS.
bool executesAtLeastOnce(const Loop *L, bool isSigned, bool trueWhenEqual, bool executesAtLeastOnce(const Loop *L, bool isSigned, SCEV *LHS, SCEV *RHS);
SCEV *LHS, SCEV *RHS);
/// potentialInfiniteLoop - Test whether the loop might jump over the exit value
/// due to wrapping.
bool potentialInfiniteLoop(SCEV *Stride, SCEV *RHS, bool isSigned,
bool trueWhenEqual);
/// getConstantEvolutionLoopExitValue - If we know that the specified Phi is /// getConstantEvolutionLoopExitValue - If we know that the specified Phi is
/// in the header of its containing loop, we know the loop executes a /// in the header of its containing loop, we know the loop executes a
@@ -1777,7 +1726,7 @@ static uint32_t GetMinTrailingZeros(SCEVHandle S) {
return MinOpRes; return MinOpRes;
} }
// SCEVUDivExpr, SCEVSDivExpr, SCEVUnknown // SCEVUDivExpr, SCEVUnknown
return 0; return 0;
} }
@@ -1807,9 +1756,6 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
case Instruction::UDiv: case Instruction::UDiv:
return SE.getUDivExpr(getSCEV(U->getOperand(0)), return SE.getUDivExpr(getSCEV(U->getOperand(0)),
getSCEV(U->getOperand(1))); getSCEV(U->getOperand(1)));
case Instruction::SDiv:
return SE.getSDivExpr(getSCEV(U->getOperand(0)),
getSCEV(U->getOperand(1)));
case Instruction::Sub: case Instruction::Sub:
return SE.getMinusSCEV(getSCEV(U->getOperand(0)), return SE.getMinusSCEV(getSCEV(U->getOperand(0)),
getSCEV(U->getOperand(1))); getSCEV(U->getOperand(1)));
@@ -1853,7 +1799,7 @@ SCEVHandle ScalarEvolutionsImpl::createSCEV(Value *V) {
break; break;
case Instruction::LShr: case Instruction::LShr:
// Turn logical shift right of a constant into an unsigned divide. // Turn logical shift right of a constant into a unsigned divide.
if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) { if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth(); uint32_t BitWidth = cast<IntegerType>(V->getType())->getBitWidth();
Constant *X = ConstantInt::get( Constant *X = ConstantInt::get(
@@ -2079,46 +2025,24 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
break; break;
} }
case ICmpInst::ICMP_SLT: { case ICmpInst::ICMP_SLT: {
SCEVHandle TC = HowManyLessThans(LHS, RHS, L, true, false); SCEVHandle TC = HowManyLessThans(LHS, RHS, L, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC; if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break; break;
} }
case ICmpInst::ICMP_SGT: { case ICmpInst::ICMP_SGT: {
SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS), SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
SE.getNotSCEV(RHS), L, true, false); SE.getNotSCEV(RHS), L, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC; if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break; break;
} }
case ICmpInst::ICMP_ULT: { case ICmpInst::ICMP_ULT: {
SCEVHandle TC = HowManyLessThans(LHS, RHS, L, false, false); SCEVHandle TC = HowManyLessThans(LHS, RHS, L, false);
if (!isa<SCEVCouldNotCompute>(TC)) return TC; if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break; break;
} }
case ICmpInst::ICMP_UGT: { case ICmpInst::ICMP_UGT: {
SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS), SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
SE.getNotSCEV(RHS), L, false, false); SE.getNotSCEV(RHS), L, false);
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break;
}
case ICmpInst::ICMP_SLE: {
SCEVHandle TC = HowManyLessThans(LHS, RHS, L, true, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break;
}
case ICmpInst::ICMP_SGE: {
SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
SE.getNotSCEV(RHS), L, true, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break;
}
case ICmpInst::ICMP_ULE: {
SCEVHandle TC = HowManyLessThans(LHS, RHS, L, false, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break;
}
case ICmpInst::ICMP_UGE: {
SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
SE.getNotSCEV(RHS), L, false, true);
if (!isa<SCEVCouldNotCompute>(TC)) return TC; if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break; break;
} }
@@ -2553,26 +2477,16 @@ SCEVHandle ScalarEvolutionsImpl::getSCEVAtScope(SCEV *V, const Loop *L) {
return Comm; return Comm;
} }
if (SCEVUDivExpr *UDiv = dyn_cast<SCEVUDivExpr>(V)) { if (SCEVUDivExpr *Div = dyn_cast<SCEVUDivExpr>(V)) {
SCEVHandle LHS = getSCEVAtScope(UDiv->getLHS(), L); SCEVHandle LHS = getSCEVAtScope(Div->getLHS(), L);
if (LHS == UnknownValue) return LHS; if (LHS == UnknownValue) return LHS;
SCEVHandle RHS = getSCEVAtScope(UDiv->getRHS(), L); SCEVHandle RHS = getSCEVAtScope(Div->getRHS(), L);
if (RHS == UnknownValue) return RHS; if (RHS == UnknownValue) return RHS;
if (LHS == UDiv->getLHS() && RHS == UDiv->getRHS()) if (LHS == Div->getLHS() && RHS == Div->getRHS())
return UDiv; // must be loop invariant return Div; // must be loop invariant
return SE.getUDivExpr(LHS, RHS); return SE.getUDivExpr(LHS, RHS);
} }
if (SCEVSDivExpr *SDiv = dyn_cast<SCEVSDivExpr>(V)) {
SCEVHandle LHS = getSCEVAtScope(SDiv->getLHS(), L);
if (LHS == UnknownValue) return LHS;
SCEVHandle RHS = getSCEVAtScope(SDiv->getRHS(), L);
if (RHS == UnknownValue) return RHS;
if (LHS == SDiv->getLHS() && RHS == SDiv->getRHS())
return SDiv; // must be loop invariant
return SE.getSDivExpr(LHS, RHS);
}
// If this is a loop recurrence for a loop that does not contain L, then we // If this is a loop recurrence for a loop that does not contain L, then we
// are dealing with the final value computed by the loop. // are dealing with the final value computed by the loop.
if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) { if (SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(V)) {
@@ -2824,7 +2738,6 @@ ScalarEvolutionsImpl::getPredecessorWithUniqueSuccessorForBB(BasicBlock *BB) {
/// executesAtLeastOnce - Test whether entry to the loop is protected by /// executesAtLeastOnce - Test whether entry to the loop is protected by
/// a conditional between LHS and RHS. /// a conditional between LHS and RHS.
bool ScalarEvolutionsImpl::executesAtLeastOnce(const Loop *L, bool isSigned, bool ScalarEvolutionsImpl::executesAtLeastOnce(const Loop *L, bool isSigned,
bool trueWhenEqual,
SCEV *LHS, SCEV *RHS) { SCEV *LHS, SCEV *RHS) {
BasicBlock *Preheader = L->getLoopPreheader(); BasicBlock *Preheader = L->getLoopPreheader();
BasicBlock *PreheaderDest = L->getHeader(); BasicBlock *PreheaderDest = L->getHeader();
@@ -2857,36 +2770,20 @@ bool ScalarEvolutionsImpl::executesAtLeastOnce(const Loop *L, bool isSigned,
switch (Cond) { switch (Cond) {
case ICmpInst::ICMP_UGT: case ICmpInst::ICMP_UGT:
if (isSigned || trueWhenEqual) continue; if (isSigned) continue;
std::swap(PreCondLHS, PreCondRHS); std::swap(PreCondLHS, PreCondRHS);
Cond = ICmpInst::ICMP_ULT; Cond = ICmpInst::ICMP_ULT;
break; break;
case ICmpInst::ICMP_SGT: case ICmpInst::ICMP_SGT:
if (!isSigned || trueWhenEqual) continue; if (!isSigned) continue;
std::swap(PreCondLHS, PreCondRHS); std::swap(PreCondLHS, PreCondRHS);
Cond = ICmpInst::ICMP_SLT; Cond = ICmpInst::ICMP_SLT;
break; break;
case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULT:
if (isSigned || trueWhenEqual) continue; if (isSigned) continue;
break; break;
case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLT:
if (!isSigned || trueWhenEqual) continue; if (!isSigned) continue;
break;
case ICmpInst::ICMP_UGE:
if (isSigned || !trueWhenEqual) continue;
std::swap(PreCondLHS, PreCondRHS);
Cond = ICmpInst::ICMP_ULE;
break;
case ICmpInst::ICMP_SGE:
if (!isSigned || !trueWhenEqual) continue;
std::swap(PreCondLHS, PreCondRHS);
Cond = ICmpInst::ICMP_SLE;
break;
case ICmpInst::ICMP_ULE:
if (isSigned || !trueWhenEqual) continue;
break;
case ICmpInst::ICMP_SLE:
if (!isSigned || !trueWhenEqual) continue;
break; break;
default: default:
continue; continue;
@@ -2905,47 +2802,11 @@ bool ScalarEvolutionsImpl::executesAtLeastOnce(const Loop *L, bool isSigned,
return false; return false;
} }
/// potentialInfiniteLoop - Test whether the loop might jump over the exit value
/// due to wrapping around 2^n.
bool ScalarEvolutionsImpl::potentialInfiniteLoop(SCEV *Stride, SCEV *RHS,
bool isSigned, bool trueWhenEqual) {
// Return true when the distance from RHS to maxint > Stride.
SCEVConstant *SC = dyn_cast<SCEVConstant>(Stride);
if (!SC)
return true;
if (SC->getValue()->isZero())
return true;
if (!trueWhenEqual && SC->getValue()->isOne())
return false;
SCEVConstant *R = dyn_cast<SCEVConstant>(RHS);
if (!R)
return true;
// If negative, it wraps around every iteration, but we don't care about that.
APInt S = SC->getValue()->getValue().abs();
uint32_t Width = R->getValue()->getBitWidth();
APInt Dist = (isSigned ? APInt::getSignedMaxValue(Width)
: APInt::getMaxValue(Width))
- R->getValue()->getValue();
// Because we're looking at distance, we perform an unsigned comparison,
// regardless of the sign of the computation.
if (trueWhenEqual)
return !S.ult(Dist);
else
return !S.ule(Dist);
}
/// HowManyLessThans - Return the number of times a backedge containing the /// HowManyLessThans - Return the number of times a backedge containing the
/// specified less-than comparison will execute. If not computable, return /// specified less-than comparison will execute. If not computable, return
/// UnknownValue. /// UnknownValue.
SCEVHandle ScalarEvolutionsImpl:: SCEVHandle ScalarEvolutionsImpl::
HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L, bool isSigned) {
bool isSigned, bool trueWhenEqual) {
// Only handle: "ADDREC < LoopInvariant". // Only handle: "ADDREC < LoopInvariant".
if (!RHS->isLoopInvariant(L)) return UnknownValue; if (!RHS->isLoopInvariant(L)) return UnknownValue;
@@ -2954,56 +2815,34 @@ HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L,
return UnknownValue; return UnknownValue;
if (AddRec->isAffine()) { if (AddRec->isAffine()) {
SCEVHandle Stride = AddRec->getOperand(1); // FORNOW: We only support unit strides.
if (potentialInfiniteLoop(Stride, RHS, isSigned, trueWhenEqual)) SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType());
if (AddRec->getOperand(1) != One)
return UnknownValue; return UnknownValue;
// We don't handle this correctly at the moment. The problem is that when // We know the LHS is of the form {n,+,1} and the RHS is some loop-invariant
// the stride is negative, we're not counting how many times 'less-than' is // m. So, we count the number of iterations in which {n,+,1} < m is true.
// true as we approach it, we're counting how far away we are from wrapping // Note that we cannot simply return max(m-n,0) because it's not safe to
// around the backside.
if (isSigned &&
cast<SCEVConstant>(Stride)->getValue()->getValue().isNegative())
return UnknownValue;
// We know the LHS is of the form {n,+,s} and the RHS is some loop-invariant
// m. So, we count the number of iterations in which {n,+,s} < m is true.
// Note that we cannot simply return max(m-n,0)/s because it's not safe to
// treat m-n as signed nor unsigned due to overflow possibility. // treat m-n as signed nor unsigned due to overflow possibility.
//
// Assuming that the loop will run at least once, we know that it will
// run (m-n)/s times.
// First, we get the value of the LHS in the first iteration: n // First, we get the value of the LHS in the first iteration: n
SCEVHandle Start = AddRec->getOperand(0); SCEVHandle Start = AddRec->getOperand(0);
SCEVHandle One = SE.getIntegerSCEV(1, RHS->getType()); if (executesAtLeastOnce(L, isSigned,
SE.getMinusSCEV(AddRec->getOperand(0), One), RHS)) {
// If the expression is less-than-or-equal to, we need to extend the // Since we know that the condition is true in order to enter the loop,
// loop by one iteration. // we know that it will run exactly m-n times.
// return SE.getMinusSCEV(RHS, Start);
// The loop won't actually run (m-n)/s times because the loop iterations } else {
// might not divide cleanly. For example, if you have {2,+,5} u< 10 the // Then, we get the value of the LHS in the first iteration in which the
// division would equal one, but the loop runs twice putting the // above condition doesn't hold. This equals to max(m,n).
// induction variable at 12. SCEVHandle End = isSigned ? SE.getSMaxExpr(RHS, Start)
SCEVHandle End = SE.getAddExpr(RHS, Stride); : SE.getUMaxExpr(RHS, Start);
if (!trueWhenEqual)
End = SE.getMinusSCEV(End, One);
if (!executesAtLeastOnce(L, isSigned, trueWhenEqual,
SE.getMinusSCEV(Start, One), RHS)) {
// If not, we get the value of the LHS in the first iteration in which
// the above condition doesn't hold. This equals to max(m,n).
End = isSigned ? SE.getSMaxExpr(End, Start)
: SE.getUMaxExpr(End, Start);
}
// Finally, we subtract these two values to get the number of times the // Finally, we subtract these two values to get the number of times the
// backedge is executed: (max(m,n)-n)/s. // backedge is executed: max(m,n)-n.
// return SE.getMinusSCEV(End, Start);
// Note that a trip count is always positive. Using SDiv here produces }
// wrong answers when Start < End.
return SE.getUDivExpr(SE.getMinusSCEV(End, Start), Stride);
} }
return UnknownValue; return UnknownValue;

View File

@@ -143,15 +143,6 @@ Value *SCEVExpander::visitUDivExpr(SCEVUDivExpr *S) {
return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt); return InsertBinop(Instruction::UDiv, LHS, RHS, InsertPt);
} }
Value *SCEVExpander::visitSDivExpr(SCEVSDivExpr *S) {
// Do not fold sdiv into ashr, unless you know that LHS is positive. On
// negative values, it rounds the wrong way.
Value *LHS = expand(S->getLHS());
Value *RHS = expand(S->getRHS());
return InsertBinop(Instruction::SDiv, LHS, RHS, InsertPt);
}
Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) { Value *SCEVExpander::visitAddRecExpr(SCEVAddRecExpr *S) {
const Type *Ty = S->getType(); const Type *Ty = S->getType();
const Loop *L = S->getLoop(); const Loop *L = S->getLoop();

View File

@@ -1,5 +1,6 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \ ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \
; RUN: grep {Loop bb: (7 + (-1 \\* %argc)) iterations!} ; RUN: grep {Loop bb: (7 + (-1 \\* %argc)) iterations!}
; XFAIL: *
define i32 @main(i32 %argc, i8** %argv) nounwind { define i32 @main(i32 %argc, i8** %argv) nounwind {
entry: entry:

View File

@@ -1,4 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 3} ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 3}
; XFAIL: *
define i32 @f(i32 %x) nounwind readnone { define i32 @f(i32 %x) nounwind readnone {
entry: entry:

View File

@@ -1,4 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 3} ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 3}
; XFAIL: *
define i32 @f(i32 %x) nounwind readnone { define i32 @f(i32 %x) nounwind readnone {
entry: entry:

View File

@@ -1,4 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {255 iterations} ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {255 iterations}
; XFAIL: *
define i32 @foo(i32 %x, i32 %y, i32* %lam, i32* %alp) nounwind { define i32 @foo(i32 %x, i32 %y, i32* %lam, i32* %alp) nounwind {
bb1.thread: bb1.thread:

View File

@@ -1,4 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {0 smax} ; RUN: llvm-as < %s | opt -analyze -scalar-evolution | grep {0 smax}
; XFAIL: *
define i32 @f(i32 %c.idx.val) { define i32 @f(i32 %c.idx.val) {

View File

@@ -1,5 +1,6 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \ ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& \
; RUN: grep {(((-1 \\* %i0) + (100005 smax %i0)) /u 5)} ; RUN: grep {(((-1 \\* %i0) + (100005 smax %i0)) /u 5)}
; XFAIL: *
define i32 @foo0(i32 %i0) nounwind { define i32 @foo0(i32 %i0) nounwind {
entry: entry:

View File

@@ -1,4 +1,5 @@
; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 5} ; RUN: llvm-as < %s | opt -analyze -scalar-evolution |& grep {/u 5}
; XFAIL: *
define i8 @foo0(i8 %i0) nounwind { define i8 @foo0(i8 %i0) nounwind {
entry: entry: