1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-19 02:52:53 +02:00

[SCEV] Don't walk uses of phis without SCEV expression when forgetting

I've run into some cases where a large fraction of compile-time is
spent invalidating SCEV. One of the causes is forgetLoop(), which
walks all values that are def-use reachable from the loop header
phis. When invalidating a topmost loop, that might be close to all
values in a function. Additionally, it's fairly common for there to
not actually be anything to invalidate, but we'll still be performing
this walk again and again.

My first thought was that we don't need to continue walking the uses
if the current value doesn't have a SCEV expression. However, this
isn't quite right, because SCEV construction can skip over values
(e.g. for a chain of adds, we might only create a SCEV expression
for the final value).

What this patch does instead is to only walk the (full) def-use chain
of loop phis that have a SCEV expression. If there's no expression
for a phi, then we also don't have any dependent expressions to
invalidate.

Differential Revision: https://reviews.llvm.org/D100264
This commit is contained in:
Nikita Popov 2021-04-10 23:50:23 +02:00
parent 326874808b
commit a79b75d192
2 changed files with 17 additions and 8 deletions

View File

@ -2053,6 +2053,12 @@ private:
std::tuple<SCEV *, FoldingSetNodeID, void *>
findExistingSCEVInCache(SCEVTypes SCEVType, ArrayRef<const SCEV *> Ops);
/// Push PHI nodes in the header of the given loop onto the given Worklist
/// if they have a cached SCEV expression. If no expression is cached, then
/// there also aren't any dependent expressions to invalidate.
void pushCachedLoopPHIs(const Loop *L,
SmallVectorImpl<Instruction *> &Worklist) const;
FoldingSet<SCEV> UniqueSCEVs;
FoldingSet<SCEVPredicate> UniquePreds;
BumpPtrAllocator SCEVAllocator;

View File

@ -7020,13 +7020,16 @@ bool ScalarEvolution::isBackedgeTakenCountMaxOrZero(const Loop *L) {
}
/// Push PHI nodes in the header of the given loop onto the given Worklist.
static void
PushLoopPHIs(const Loop *L, SmallVectorImpl<Instruction *> &Worklist) {
/// Only push PHIs for which a SCEV expression has been cached, otherwise there
/// cannot be any dependent expressions to invalidate.
void ScalarEvolution::pushCachedLoopPHIs(
const Loop *L, SmallVectorImpl<Instruction *> &Worklist) const {
BasicBlock *Header = L->getHeader();
// Push all Loop-header PHIs onto the Worklist stack.
for (PHINode &PN : Header->phis())
Worklist.push_back(&PN);
for (PHINode &PN : Header->phis()) {
auto It = ValueExprMap.find_as(static_cast<Value *>(&PN));
if (It != ValueExprMap.end())
Worklist.push_back(&PN);
}
}
const ScalarEvolution::BackedgeTakenInfo &
@ -7087,7 +7090,7 @@ ScalarEvolution::getBackedgeTakenInfo(const Loop *L) {
// it handles SCEVUnknown PHI nodes specially.
if (Result.hasAnyInfo()) {
SmallVector<Instruction *, 16> Worklist;
PushLoopPHIs(L, Worklist);
pushCachedLoopPHIs(L, Worklist);
SmallPtrSet<Instruction *, 8> Discovered;
while (!Worklist.empty()) {
@ -7210,7 +7213,7 @@ void ScalarEvolution::forgetLoop(const Loop *L) {
}
// Drop information about expressions based on loop-header PHIs.
PushLoopPHIs(CurrL, Worklist);
pushCachedLoopPHIs(CurrL, Worklist);
while (!Worklist.empty()) {
Instruction *I = Worklist.pop_back_val();