LLVM 22.0.0git
LoopAnalysisManager.cpp
Go to the documentation of this file.
1//===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
14#include "llvm/IR/Dominators.h"
17#include <optional>
18
19using namespace llvm;
20
21namespace llvm {
22// Explicit template instantiations and specialization definitions for core
23// template typedefs.
25template class LLVM_EXPORT_TEMPLATE
27template class LLVM_EXPORT_TEMPLATE
31
32bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
33 Function &F, const PreservedAnalyses &PA,
34 FunctionAnalysisManager::Invalidator &Inv) {
35 // First compute the sequence of IR units covered by this proxy. We will want
36 // to visit this in postorder, but because this is a tree structure we can do
37 // this by building a preorder sequence and walking it backwards. We also
38 // want siblings in forward program order to match the LoopPassManager so we
39 // get the preorder with siblings reversed.
40 SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
41
42 // If this proxy or the loop info is going to be invalidated, we also need
43 // to clear all the keys coming from that analysis. We also completely blow
44 // away the loop analyses if any of the standard analyses provided by the
45 // loop pass manager go away so that loop analyses can freely use these
46 // without worrying about declaring dependencies on them etc.
47 // FIXME: It isn't clear if this is the right tradeoff. We could instead make
48 // loop analyses declare any dependencies on these and use the more general
49 // invalidation logic below to act on that.
50 auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
51 bool invalidateMemorySSAAnalysis = false;
52 if (MSSAUsed)
53 invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
54 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
55 Inv.invalidate<AAManager>(F, PA) ||
56 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
57 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
58 Inv.invalidate<LoopAnalysis>(F, PA) ||
59 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
60 invalidateMemorySSAAnalysis) {
61 // Note that the LoopInfo may be stale at this point, however the loop
62 // objects themselves remain the only viable keys that could be in the
63 // analysis manager's cache. So we just walk the keys and forcibly clear
64 // those results. Note that the order doesn't matter here as this will just
65 // directly destroy the results without calling methods on them.
66 for (Loop *L : PreOrderLoops) {
67 // NB! `L` may not be in a good enough state to run Loop::getName.
68 InnerAM->clear(*L, "<possibly invalidated loop>");
69 }
70
71 // We also need to null out the inner AM so that when the object gets
72 // destroyed as invalid we don't try to clear the inner AM again. At that
73 // point we won't be able to reliably walk the loops for this function and
74 // only clear results associated with those loops the way we do here.
75 // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
76 // try to remain valid during invalidation. Maybe we should add an
77 // `IsClean` flag?
78 InnerAM = nullptr;
79
80 // Now return true to indicate this *is* invalid and a fresh proxy result
81 // needs to be built. This is especially important given the null InnerAM.
82 return true;
83 }
84
85 // Directly check if the relevant set is preserved so we can short circuit
86 // invalidating loops.
87 bool AreLoopAnalysesPreserved =
89
90 // Since we have a valid LoopInfo we can actually leave the cached results in
91 // the analysis manager associated with the Loop keys, but we need to
92 // propagate any necessary invalidation logic into them. We'd like to
93 // invalidate things in roughly the same order as they were put into the
94 // cache and so we walk the preorder list in reverse to form a valid
95 // postorder.
96 for (Loop *L : reverse(PreOrderLoops)) {
97 std::optional<PreservedAnalyses> InnerPA;
98
99 // Check to see whether the preserved set needs to be adjusted based on
100 // function-level analysis invalidation triggering deferred invalidation
101 // for this loop.
102 if (auto *OuterProxy =
103 InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
104 for (const auto &OuterInvalidationPair :
105 OuterProxy->getOuterInvalidations()) {
106 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
107 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
108 if (Inv.invalidate(OuterAnalysisID, F, PA)) {
109 if (!InnerPA)
110 InnerPA = PA;
111 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
112 InnerPA->abandon(InnerAnalysisID);
113 }
114 }
115
116 // Check if we needed a custom PA set. If so we'll need to run the inner
117 // invalidation.
118 if (InnerPA) {
119 InnerAM->invalidate(*L, *InnerPA);
120 continue;
121 }
122
123 // Otherwise we only need to do invalidation if the original PA set didn't
124 // preserve all Loop analyses.
125 if (!AreLoopAnalysesPreserved)
126 InnerAM->invalidate(*L, PA);
127 }
128
129 // Return false to indicate that this result is still a valid proxy.
130 return false;
131}
132
133template <>
134LoopAnalysisManagerFunctionProxy::Result
137 return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
138}
139} // namespace llvm
140
145 PA.preserve<LoopAnalysisManagerFunctionProxy>();
147 return PA;
148}
#define LLVM_EXPORT_TEMPLATE
Definition Compiler.h:215
This header provides classes for managing per-loop analyses.
#define F(x, y, z)
Definition MD5.cpp:55
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
Provides implementations for PassManager and AnalysisManager template methods.
A manager for alias analyses.
This templated class represents "all analyses that operate over <aparticular IR unit>" (e....
Definition Analysis.h:50
A container for analyses that lazily runs them and caches their results.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
A function analysis which provides an AssumptionCache.
Analysis pass which computes a DominatorTree.
Definition Dominators.h:284
An analysis over an "outer" IR unit that provides access to an analysis manager over an "inner" IR un...
Result run(Function &IR, AnalysisManager< Function, ExtraArgTs... > &AM, ExtraArgTs...)
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:570
Represents a single loop in the control flow graph.
Definition LoopInfo.h:40
An analysis that produces MemorySSA for a function.
Definition MemorySSA.h:936
An analysis over an "inner" IR unit that provides access to an analysis manager over a "outer" IR uni...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
bool allAnalysesInSetPreserved() const
Directly test whether a set of analyses is preserved.
Definition Analysis.h:300
PreservedAnalyses & preserve()
Mark an analysis as preserved.
Definition Analysis.h:132
PreservedAnalysisChecker getChecker() const
Build a checker for this PreservedAnalyses and the specified analysis type.
Definition Analysis.h:275
Analysis pass that exposes the ScalarEvolution for a function.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
This is an optimization pass for GlobalISel generic memory operations.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:420
OuterAnalysisManagerProxy< FunctionAnalysisManager, Loop, LoopStandardAnalysisResults & > FunctionAnalysisManagerLoopProxy
A proxy from a FunctionAnalysisManager to a Loop.
LLVM_ABI PreservedAnalyses getLoopPassPreservedAnalyses()
Returns the minimum set of Analyses that all loop passes must preserve.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A special type used by analysis passes to provide an address that identifies that particular analysis...
Definition Analysis.h:29
The adaptor from a function pass to a loop pass computes these analyses and makes them available to t...