Movatterモバイル変換


[0]ホーム

URL:


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

Generated on Thu Jul 17 2025 10:57:05 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp