Index: llvm/lib/Analysis/ScalarEvolution.cpp =================================================================== --- llvm/lib/Analysis/ScalarEvolution.cpp +++ llvm/lib/Analysis/ScalarEvolution.cpp @@ -6589,6 +6589,21 @@ 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, + SmallPtrSetImpl &Ops) { + if (auto *S2 = dyn_cast(S)) + for (auto *Op : S2->operands()) + Ops.insert(Op); + else if (auto *S2 = dyn_cast(S)) + for (auto *Op : S2->operands()) + Ops.insert(Op); + else if (auto *S2 = dyn_cast(S)) + for (auto *Op : S2->operands()) + Ops.insert(Op); +} + const Instruction * ScalarEvolution::getDefiningScopeBound(ArrayRef Ops) { // Do a bounded search of the def relation of the requested SCEVs. @@ -6612,15 +6627,12 @@ if (auto *DefI = getNonTrivialDefiningScopeBound(S)) { if (!Bound || DT.dominates(Bound, DefI)) Bound = DefI; - } else if (auto *S2 = dyn_cast(S)) - for (auto *Op : S2->operands()) - pushOp(Op); - else if (auto *S2 = dyn_cast(S)) - for (auto *Op : S2->operands()) - pushOp(Op); - else if (auto *S2 = dyn_cast(S)) - for (auto *Op : S2->operands()) + } else { + SmallPtrSet Ops; + collectUniqueOps(S, Ops); + for (auto *Op : Ops) pushOp(Op); + } } return Bound ? Bound : &*F.getEntryBlock().begin(); } @@ -12909,14 +12921,7 @@ // Verify intergity of SCEV users. for (const auto &S : UniqueSCEVs) { SmallPtrSet Ops; - if (const auto *NS = dyn_cast(&S)) - Ops.insert(NS->op_begin(), NS->op_end()); - else if (const auto *CS = dyn_cast(&S)) - Ops.insert(CS->getOperand()); - else if (const auto *DS = dyn_cast(&S)) { - Ops.insert(DS->getLHS()); - Ops.insert(DS->getRHS()); - } + collectUniqueOps(&S, Ops); for (const auto *Op : Ops) { // We do not store dependencies of constants. if (isa(Op))