STATISTIC(NumBruteForceTripCountsComputed,
"Number of loops with trip counts computed by force");
-cl::opt<unsigned>
+static cl::opt<unsigned>
MaxBruteForceIterations("scalar-evolution-max-iterations", cl::ReallyHidden,
cl::desc("Maximum number of iterations SCEV will "
"symbolically execute a constant derived loop"),
cl::init(100));
-namespace {
- RegisterPass<ScalarEvolution>
- R("scalar-evolution", "Scalar Evolution Analysis");
-}
+static RegisterPass<ScalarEvolution>
+R("scalar-evolution", "Scalar Evolution Analysis", false, true);
char ScalarEvolution::ID = 0;
//===----------------------------------------------------------------------===//
return 0;
}
+bool SCEV::isZero() const {
+ if (const SCEVConstant *SC = dyn_cast<SCEVConstant>(this))
+ return SC->getValue()->isZero();
+ return false;
+}
+
SCEVCouldNotCompute::SCEVCouldNotCompute() : SCEV(scCouldNotCompute) {}
/// than the complexity of the RHS. This comparator is used to canonicalize
/// expressions.
struct VISIBILITY_HIDDEN SCEVComplexityCompare {
- bool operator()(SCEV *LHS, SCEV *RHS) {
+ bool operator()(const SCEV *LHS, const SCEV *RHS) const {
return LHS->getSCEVType() < RHS->getSCEVType();
}
};
if (Ops.size() == 2) {
// This is the common case, which also happens to be trivially simple.
// Special case it.
- if (Ops[0]->getSCEVType() > Ops[1]->getSCEVType())
+ if (SCEVComplexityCompare()(Ops[1], Ops[0]))
std::swap(Ops[0], Ops[1]);
return;
}
if (Val == 0)
C = Constant::getNullValue(Ty);
else if (Ty->isFloatingPoint())
- C = ConstantFP::get(Ty, APFloat(Ty==Type::FloatTy ? APFloat::IEEEsingle :
- APFloat::IEEEdouble, Val));
+ C = ConstantFP::get(APFloat(Ty==Type::FloatTy ? APFloat::IEEEsingle :
+ APFloat::IEEEdouble, Val));
else
C = ConstantInt::get(Ty, Val);
return getUnknown(C);
}
-/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion of the
-/// input value to the specified type. If the type must be extended, it is zero
-/// extended.
-static SCEVHandle getTruncateOrZeroExtend(const SCEVHandle &V, const Type *Ty,
- ScalarEvolution &SE) {
- const Type *SrcTy = V->getType();
- assert(SrcTy->isInteger() && Ty->isInteger() &&
- "Cannot truncate or zero extend with non-integer arguments!");
- if (SrcTy->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
- return V; // No conversion
- if (SrcTy->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits())
- return SE.getTruncateExpr(V, Ty);
- return SE.getZeroExtendExpr(V, Ty);
-}
-
/// getNegativeSCEV - Return a SCEV corresponding to -V = -1*V
///
SCEVHandle ScalarEvolution::getNegativeSCEV(const SCEVHandle &V) {
#endif
const IntegerType *DividendTy = IntegerType::get(DividendBits);
- const SCEVHandle ExIt = SE.getZeroExtendExpr(It, DividendTy);
+ const SCEVHandle ExIt = SE.getTruncateOrZeroExtend(It, DividendTy);
// The final number of bits we need to perform the division is the maximum of
// dividend and divisor bitwidths.
Dividend *= N-(K-1);
if (DividendTy != DivisionTy)
Dividend = Dividend.zext(DivisionTy->getBitWidth());
- return SE.getConstant(Dividend.udiv(Divisor).trunc(It->getBitWidth()));
+
+ APInt Result = Dividend.udiv(Divisor);
+ if (Result.getBitWidth() != It->getBitWidth())
+ Result = Result.trunc(It->getBitWidth());
+
+ return SE.getConstant(Result);
}
SCEVHandle Dividend = ExIt;
Dividend =
SE.getMulExpr(Dividend,
SE.getMinusSCEV(ExIt, SE.getIntegerSCEV(i, DividendTy)));
- if (DividendTy != DivisionTy)
- Dividend = SE.getZeroExtendExpr(Dividend, DivisionTy);
- return
- SE.getTruncateExpr(SE.getUDivExpr(Dividend, SE.getConstant(Divisor)),
- It->getType());
+
+ return SE.getTruncateOrZeroExtend(
+ SE.getUDivExpr(
+ SE.getTruncateOrZeroExtend(Dividend, DivisionTy),
+ SE.getConstant(Divisor)
+ ), It->getType());
}
/// evaluateAtIteration - Return the value of this chain of recurrences at
return Result;
}
+/// getTruncateOrZeroExtend - Return a SCEV corresponding to a conversion
+/// of the input value to the specified type. If the type must be
+/// extended, it is zero extended.
+SCEVHandle ScalarEvolution::getTruncateOrZeroExtend(const SCEVHandle &V,
+ const Type *Ty) {
+ const Type *SrcTy = V->getType();
+ assert(SrcTy->isInteger() && Ty->isInteger() &&
+ "Cannot truncate or zero extend with non-integer arguments!");
+ if (SrcTy->getPrimitiveSizeInBits() == Ty->getPrimitiveSizeInBits())
+ return V; // No conversion
+ if (SrcTy->getPrimitiveSizeInBits() > Ty->getPrimitiveSizeInBits())
+ return getTruncateExpr(V, Ty);
+ return getZeroExtendExpr(V, Ty);
+}
+
// get - Get a canonical add expression, or something simpler if possible.
SCEVHandle ScalarEvolution::getAddExpr(std::vector<SCEVHandle> &Ops) {
assert(!Ops.empty() && "Cannot get empty add!");
const Loop *L) {
if (Operands.size() == 1) return Operands[0];
- if (SCEVConstant *StepC = dyn_cast<SCEVConstant>(Operands.back()))
- if (StepC->getValue()->isZero()) {
- Operands.pop_back();
- return getAddRecExpr(Operands, L); // { X,+,0 } --> X
- }
+ if (Operands.back()->isZero()) {
+ Operands.pop_back();
+ return getAddRecExpr(Operands, L); // { X,+,0 } --> X
+ }
SCEVAddRecExpr *&Result =
(*SCEVAddRecExprs)[std::make_pair(L, std::vector<SCEV*>(Operands.begin(),
// At this point, we would like to compute how many iterations of the
// loop the predicate will return true for these inputs.
- if (LHS->isLoopInvariant(L) && !RHS->isLoopInvariant(L)) {
- // If there is a loop-invariant, force it into the RHS.
+ if (isa<SCEVConstant>(LHS) && !isa<SCEVConstant>(RHS)) {
+ // If there is a constant, force it into the RHS.
std::swap(LHS, RHS);
Cond = ICmpInst::getSwappedPredicate(Cond);
}
break;
}
case ICmpInst::ICMP_UGT: {
- SCEVHandle TC = HowManyLessThans(SE.getNegativeSCEV(LHS),
- SE.getNegativeSCEV(RHS), L, false);
+ SCEVHandle TC = HowManyLessThans(SE.getNotSCEV(LHS),
+ SE.getNotSCEV(RHS), L, false);
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
break;
}
}
/// ComputeLoadConstantCompareIterationCount - Given an exit condition of
-/// 'icmp op load X, cst', try to se if we can compute the trip count.
+/// 'icmp op load X, cst', try to see if we can compute the trip count.
SCEVHandle ScalarEvolutionsImpl::
ComputeLoadConstantCompareIterationCount(LoadInst *LI, Constant *RHS,
const Loop *L,
// loop iterates. Compute this now.
SCEVHandle IterationCount = getIterationCount(AddRec->getLoop());
if (IterationCount == UnknownValue) return UnknownValue;
- IterationCount = getTruncateOrZeroExtend(IterationCount,
- AddRec->getType(), SE);
+ IterationCount = SE.getTruncateOrZeroExtend(IterationCount,
+ AddRec->getType());
// If the value is affine, simplify the expression evaluation to just
// Start + Step*IterationCount.
if (SCEVConstant *StartC = dyn_cast<SCEVConstant>(Start)) {
ConstantInt *StartCC = StartC->getValue();
Constant *StartNegC = ConstantExpr::getNeg(StartCC);
- Constant *Rem = ConstantExpr::getSRem(StartNegC, StepC->getValue());
+ Constant *Rem = ConstantExpr::getURem(StartNegC, StepC->getValue());
if (Rem->isNullValue()) {
- Constant *Result =ConstantExpr::getSDiv(StartNegC,StepC->getValue());
+ Constant *Result = ConstantExpr::getUDiv(StartNegC,StepC->getValue());
return SE.getUnknown(Result);
}
}
// value at this index. When solving for "X*X != 5", for example, we
// should not accept a root of 2.
SCEVHandle Val = AddRec->evaluateAtIteration(R1, SE);
- if (SCEVConstant *EvalVal = dyn_cast<SCEVConstant>(Val))
- if (EvalVal->getValue()->isZero())
- return R1; // We found a quadratic root!
+ if (Val->isZero())
+ return R1; // We found a quadratic root!
}
}
}
// If the value is a constant, check to see if it is known to be non-zero
// already. If so, the backedge will execute zero times.
if (SCEVConstant *C = dyn_cast<SCEVConstant>(V)) {
- Constant *Zero = Constant::getNullValue(C->getValue()->getType());
- Constant *NonZero =
- ConstantExpr::getICmp(ICmpInst::ICMP_NE, C->getValue(), Zero);
- if (NonZero == ConstantInt::getTrue())
- return getSCEV(Zero);
+ if (!C->getValue()->isNullValue())
+ return SE.getIntegerSCEV(0, C->getType());
return UnknownValue; // Otherwise it will loop infinitely.
}