1*0b57cec5SDimitry Andric //===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //
7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
8*0b57cec5SDimitry Andric
9*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopAnalysisManager.h"
10*0b57cec5SDimitry Andric #include "llvm/Analysis/AssumptionCache.h"
11*0b57cec5SDimitry Andric #include "llvm/Analysis/LoopInfo.h"
12*0b57cec5SDimitry Andric #include "llvm/Analysis/MemorySSA.h"
13*0b57cec5SDimitry Andric #include "llvm/Analysis/ScalarEvolution.h"
14*0b57cec5SDimitry Andric #include "llvm/IR/Dominators.h"
15*0b57cec5SDimitry Andric #include "llvm/IR/PassManagerImpl.h"
16*0b57cec5SDimitry Andric #include <optional>
17*0b57cec5SDimitry Andric
18*0b57cec5SDimitry Andric using namespace llvm;
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric namespace llvm {
21*0b57cec5SDimitry Andric // Explicit template instantiations and specialization definitions for core
22*0b57cec5SDimitry Andric // template typedefs.
23*0b57cec5SDimitry Andric template class AllAnalysesOn<Loop>;
24*0b57cec5SDimitry Andric template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
25*0b57cec5SDimitry Andric template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
26*0b57cec5SDimitry Andric template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
27*0b57cec5SDimitry Andric LoopStandardAnalysisResults &>;
28*0b57cec5SDimitry Andric
invalidate(Function & F,const PreservedAnalyses & PA,FunctionAnalysisManager::Invalidator & Inv)29*0b57cec5SDimitry Andric bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
30*0b57cec5SDimitry Andric Function &F, const PreservedAnalyses &PA,
31*0b57cec5SDimitry Andric FunctionAnalysisManager::Invalidator &Inv) {
32*0b57cec5SDimitry Andric // First compute the sequence of IR units covered by this proxy. We will want
33*0b57cec5SDimitry Andric // to visit this in postorder, but because this is a tree structure we can do
34*0b57cec5SDimitry Andric // this by building a preorder sequence and walking it backwards. We also
35*0b57cec5SDimitry Andric // want siblings in forward program order to match the LoopPassManager so we
36*0b57cec5SDimitry Andric // get the preorder with siblings reversed.
37*0b57cec5SDimitry Andric SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
38*0b57cec5SDimitry Andric
39*0b57cec5SDimitry Andric // If this proxy or the loop info is going to be invalidated, we also need
40*0b57cec5SDimitry Andric // to clear all the keys coming from that analysis. We also completely blow
41*0b57cec5SDimitry Andric // away the loop analyses if any of the standard analyses provided by the
42*0b57cec5SDimitry Andric // loop pass manager go away so that loop analyses can freely use these
43*0b57cec5SDimitry Andric // without worrying about declaring dependencies on them etc.
44*0b57cec5SDimitry Andric // FIXME: It isn't clear if this is the right tradeoff. We could instead make
45*0b57cec5SDimitry Andric // loop analyses declare any dependencies on these and use the more general
46*0b57cec5SDimitry Andric // invalidation logic below to act on that.
47*0b57cec5SDimitry Andric auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
48*0b57cec5SDimitry Andric bool invalidateMemorySSAAnalysis = false;
49*0b57cec5SDimitry Andric if (MSSAUsed)
50*0b57cec5SDimitry Andric invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
51*0b57cec5SDimitry Andric if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
52*0b57cec5SDimitry Andric Inv.invalidate<AAManager>(F, PA) ||
53*0b57cec5SDimitry Andric Inv.invalidate<AssumptionAnalysis>(F, PA) ||
54*0b57cec5SDimitry Andric Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
55*0b57cec5SDimitry Andric Inv.invalidate<LoopAnalysis>(F, PA) ||
56*0b57cec5SDimitry Andric Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
57*0b57cec5SDimitry Andric invalidateMemorySSAAnalysis) {
58*0b57cec5SDimitry Andric // Note that the LoopInfo may be stale at this point, however the loop
59*0b57cec5SDimitry Andric // objects themselves remain the only viable keys that could be in the
60*0b57cec5SDimitry Andric // analysis manager's cache. So we just walk the keys and forcibly clear
61*0b57cec5SDimitry Andric // those results. Note that the order doesn't matter here as this will just
62*0b57cec5SDimitry Andric // directly destroy the results without calling methods on them.
63*0b57cec5SDimitry Andric for (Loop *L : PreOrderLoops) {
64*0b57cec5SDimitry Andric // NB! `L` may not be in a good enough state to run Loop::getName.
65*0b57cec5SDimitry Andric InnerAM->clear(*L, "<possibly invalidated loop>");
66*0b57cec5SDimitry Andric }
67*0b57cec5SDimitry Andric
68*0b57cec5SDimitry Andric // We also need to null out the inner AM so that when the object gets
69*0b57cec5SDimitry Andric // destroyed as invalid we don't try to clear the inner AM again. At that
70*0b57cec5SDimitry Andric // point we won't be able to reliably walk the loops for this function and
71*0b57cec5SDimitry Andric // only clear results associated with those loops the way we do here.
72*0b57cec5SDimitry Andric // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
73*0b57cec5SDimitry Andric // try to remain valid during invalidation. Maybe we should add an
74*0b57cec5SDimitry Andric // `IsClean` flag?
75*0b57cec5SDimitry Andric InnerAM = nullptr;
76*0b57cec5SDimitry Andric
77*0b57cec5SDimitry Andric // Now return true to indicate this *is* invalid and a fresh proxy result
78*0b57cec5SDimitry Andric // needs to be built. This is especially important given the null InnerAM.
79*0b57cec5SDimitry Andric return true;
80*0b57cec5SDimitry Andric }
81*0b57cec5SDimitry Andric
82*0b57cec5SDimitry Andric // Directly check if the relevant set is preserved so we can short circuit
83*0b57cec5SDimitry Andric // invalidating loops.
84*0b57cec5SDimitry Andric bool AreLoopAnalysesPreserved =
85*0b57cec5SDimitry Andric PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
86*0b57cec5SDimitry Andric
87*0b57cec5SDimitry Andric // Since we have a valid LoopInfo we can actually leave the cached results in
88*0b57cec5SDimitry Andric // the analysis manager associated with the Loop keys, but we need to
89*0b57cec5SDimitry Andric // propagate any necessary invalidation logic into them. We'd like to
90*0b57cec5SDimitry Andric // invalidate things in roughly the same order as they were put into the
91*0b57cec5SDimitry Andric // cache and so we walk the preorder list in reverse to form a valid
92*0b57cec5SDimitry Andric // postorder.
93*0b57cec5SDimitry Andric for (Loop *L : reverse(PreOrderLoops)) {
94*0b57cec5SDimitry Andric std::optional<PreservedAnalyses> InnerPA;
95*0b57cec5SDimitry Andric
96*0b57cec5SDimitry Andric // Check to see whether the preserved set needs to be adjusted based on
97*0b57cec5SDimitry Andric // function-level analysis invalidation triggering deferred invalidation
98*0b57cec5SDimitry Andric // for this loop.
99*0b57cec5SDimitry Andric if (auto *OuterProxy =
100*0b57cec5SDimitry Andric InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
101*0b57cec5SDimitry Andric for (const auto &OuterInvalidationPair :
102*0b57cec5SDimitry Andric OuterProxy->getOuterInvalidations()) {
103*0b57cec5SDimitry Andric AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
104*0b57cec5SDimitry Andric const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
105*0b57cec5SDimitry Andric if (Inv.invalidate(OuterAnalysisID, F, PA)) {
106*0b57cec5SDimitry Andric if (!InnerPA)
107*0b57cec5SDimitry Andric InnerPA = PA;
108*0b57cec5SDimitry Andric for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
109*0b57cec5SDimitry Andric InnerPA->abandon(InnerAnalysisID);
110*0b57cec5SDimitry Andric }
111*0b57cec5SDimitry Andric }
112*0b57cec5SDimitry Andric
113*0b57cec5SDimitry Andric // Check if we needed a custom PA set. If so we'll need to run the inner
114*0b57cec5SDimitry Andric // invalidation.
115*0b57cec5SDimitry Andric if (InnerPA) {
116*0b57cec5SDimitry Andric InnerAM->invalidate(*L, *InnerPA);
117*0b57cec5SDimitry Andric continue;
118*0b57cec5SDimitry Andric }
119*0b57cec5SDimitry Andric
120*0b57cec5SDimitry Andric // Otherwise we only need to do invalidation if the original PA set didn't
121*0b57cec5SDimitry Andric // preserve all Loop analyses.
122*0b57cec5SDimitry Andric if (!AreLoopAnalysesPreserved)
123*0b57cec5SDimitry Andric InnerAM->invalidate(*L, PA);
124*0b57cec5SDimitry Andric }
125*0b57cec5SDimitry Andric
126*0b57cec5SDimitry Andric // Return false to indicate that this result is still a valid proxy.
127*0b57cec5SDimitry Andric return false;
128*0b57cec5SDimitry Andric }
129*0b57cec5SDimitry Andric
130*0b57cec5SDimitry Andric template <>
131*0b57cec5SDimitry Andric LoopAnalysisManagerFunctionProxy::Result
run(Function & F,FunctionAnalysisManager & AM)132*0b57cec5SDimitry Andric LoopAnalysisManagerFunctionProxy::run(Function &F,
133*0b57cec5SDimitry Andric FunctionAnalysisManager &AM) {
134*0b57cec5SDimitry Andric return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
135*0b57cec5SDimitry Andric }
136*0b57cec5SDimitry Andric }
137*0b57cec5SDimitry Andric
getLoopPassPreservedAnalyses()138*0b57cec5SDimitry Andric PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
139*0b57cec5SDimitry Andric PreservedAnalyses PA;
140*0b57cec5SDimitry Andric PA.preserve<DominatorTreeAnalysis>();
141*0b57cec5SDimitry Andric PA.preserve<LoopAnalysis>();
142*0b57cec5SDimitry Andric PA.preserve<LoopAnalysisManagerFunctionProxy>();
143*0b57cec5SDimitry Andric PA.preserve<ScalarEvolutionAnalysis>();
144*0b57cec5SDimitry Andric return PA;
145*0b57cec5SDimitry Andric }
146*0b57cec5SDimitry Andric