Skip to content

Commit

Permalink
[SCEV][NFC] Factor out common API for getting unique operands of a SCEV
Browse files Browse the repository at this point in the history
This function is used at least in 2 places, to it makes sense to make it separate.

Differential Revision: https://reviews.llvm.org/D112516
Reviewed By: reames
  • Loading branch information
xortator committed Nov 1, 2021
1 parent eeed154 commit e512c5b
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions llvm/lib/Analysis/ScalarEvolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6589,6 +6589,26 @@ ScalarEvolution::getNonTrivialDefiningScopeBound(const SCEV *S) {
return nullptr;
}

/// Fills \p Ops with unique operands of \p S, if it has operands. If not,
/// \p Ops remains unmodified.
static void collectUniqueOps(const SCEV *S,
SmallVectorImpl<const SCEV *> &Ops) {
SmallPtrSet<const SCEV *, 4> Unique;
auto InsertUnique = [&](const SCEV *S) {
if (Unique.insert(S).second)
Ops.push_back(S);
};
if (auto *S2 = dyn_cast<SCEVCastExpr>(S))
for (auto *Op : S2->operands())
InsertUnique(Op);
else if (auto *S2 = dyn_cast<SCEVNAryExpr>(S))
for (auto *Op : S2->operands())
InsertUnique(Op);
else if (auto *S2 = dyn_cast<SCEVUDivExpr>(S))
for (auto *Op : S2->operands())
InsertUnique(Op);
}

const Instruction *
ScalarEvolution::getDefiningScopeBound(ArrayRef<const SCEV *> Ops) {
// Do a bounded search of the def relation of the requested SCEVs.
Expand All @@ -6612,15 +6632,12 @@ ScalarEvolution::getDefiningScopeBound(ArrayRef<const SCEV *> Ops) {
if (auto *DefI = getNonTrivialDefiningScopeBound(S)) {
if (!Bound || DT.dominates(Bound, DefI))
Bound = DefI;
} else if (auto *S2 = dyn_cast<SCEVCastExpr>(S))
for (auto *Op : S2->operands())
pushOp(Op);
else if (auto *S2 = dyn_cast<SCEVNAryExpr>(S))
for (auto *Op : S2->operands())
pushOp(Op);
else if (auto *S2 = dyn_cast<SCEVUDivExpr>(S))
for (auto *Op : S2->operands())
} else {
SmallVector<const SCEV *, 4> Ops;
collectUniqueOps(S, Ops);
for (auto *Op : Ops)
pushOp(Op);
}
}
return Bound ? Bound : &*F.getEntryBlock().begin();
}
Expand Down Expand Up @@ -12908,15 +12925,8 @@ void ScalarEvolution::verify() const {

// Verify intergity of SCEV users.
for (const auto &S : UniqueSCEVs) {
SmallPtrSet<const SCEV *, 4> Ops;
if (const auto *NS = dyn_cast<SCEVNAryExpr>(&S))
Ops.insert(NS->op_begin(), NS->op_end());
else if (const auto *CS = dyn_cast<SCEVCastExpr>(&S))
Ops.insert(CS->getOperand());
else if (const auto *DS = dyn_cast<SCEVUDivExpr>(&S)) {
Ops.insert(DS->getLHS());
Ops.insert(DS->getRHS());
}
SmallVector<const SCEV *, 4> Ops;
collectUniqueOps(&S, Ops);
for (const auto *Op : Ops) {
// We do not store dependencies of constants.
if (isa<SCEVConstant>(Op))
Expand Down

0 comments on commit e512c5b

Please sign in to comment.