SCEVUnionPredicate &A);
/// Tries to convert the \p S expression to an AddRec expression,
/// adding additional predicates to \p Preds as required.
- const SCEV *convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L,
- SCEVUnionPredicate &Preds);
+ const SCEVAddRecExpr *
+ convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L,
+ SCEVUnionPredicate &Preds);
private:
/// Compute the backedge taken count knowing the interval difference, the
/// \brief Adds a new predicate.
void addPredicate(const SCEVPredicate &Pred);
/// \brief Attempts to produce an AddRecExpr for V by adding additional
- /// SCEV predicates.
- const SCEV *getAsAddRec(Value *V);
+ /// SCEV predicates. If we can't transform the expression into an
+ /// AddRecExpr we return nullptr and not add additional SCEV predicates
+ /// to the current context.
+ const SCEVAddRecExpr *getAsAddRec(Value *V);
/// \brief Proves that V doesn't overflow by adding SCEV predicate.
void setNoOverflow(Value *V, SCEVWrapPredicate::IncrementWrapFlags Flags);
/// \brief Returns true if we've proved that V doesn't wrap by means of a
return SCEVPredicateRewriter::rewrite(S, L, *this, Preds, false);
}
-const SCEV *
+const SCEVAddRecExpr *
ScalarEvolution::convertSCEVToAddRecWithPredicates(const SCEV *S, const Loop *L,
SCEVUnionPredicate &Preds) {
- return SCEVPredicateRewriter::rewrite(S, L, *this, Preds, true);
+ SCEVUnionPredicate TransformPreds;
+ S = SCEVPredicateRewriter::rewrite(S, L, *this, TransformPreds, true);
+ auto *AddRec = dyn_cast<SCEVAddRecExpr>(S);
+
+ if (!AddRec)
+ return nullptr;
+
+ // Since the transformation was successful, we can now transfer the SCEV
+ // predicates.
+ Preds.add(&TransformPreds);
+ return AddRec;
}
/// SCEV predicates
return Flags == SCEVWrapPredicate::IncrementAnyWrap;
}
-const SCEV *PredicatedScalarEvolution::getAsAddRec(Value *V) {
+const SCEVAddRecExpr *PredicatedScalarEvolution::getAsAddRec(Value *V) {
const SCEV *Expr = this->getSCEV(V);
- const SCEV *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, Preds);
+ auto *New = SE.convertSCEVToAddRecWithPredicates(Expr, &L, Preds);
+
+ if (!New)
+ return nullptr;
+
updateGeneration();
RewriteMap[SE.getSCEV(V)] = {Generation, New};
return New;