blob: 7647f85019d5e0accc67fa1372e75927e7a0b913 [file] [log] [blame]
Chandler Carruth3bab7e12017-01-11 09:43:56 +00001//===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
Justin Bognereecc3c82016-02-25 07:23:08 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
Chandler Carruth3bab7e12017-01-11 09:43:56 +000010#include "llvm/Analysis/LoopAnalysisManager.h"
Justin Bognere839c3e2016-05-03 21:35:08 +000011#include "llvm/Analysis/BasicAliasAnalysis.h"
12#include "llvm/Analysis/GlobalsModRef.h"
13#include "llvm/Analysis/LoopInfo.h"
14#include "llvm/Analysis/ScalarEvolution.h"
15#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
16#include "llvm/IR/Dominators.h"
Justin Bognereecc3c82016-02-25 07:23:08 +000017
18using namespace llvm;
19
Chandler Carruth6b981642016-12-10 06:34:44 +000020// Explicit template instantiations and specialization defininitions for core
21// template typedefs.
Chandler Carruth2a540942016-02-27 10:38:10 +000022namespace llvm {
Chandler Carruth410eaeb2017-01-11 06:23:21 +000023template class AllAnalysesOn<Loop>;
24template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
Chandler Carruth2a540942016-02-27 10:38:10 +000025template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
Chandler Carruth346542b2017-02-07 01:50:48 +000026template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
27 LoopStandardAnalysisResults &>;
Chandler Carruth6b981642016-12-10 06:34:44 +000028
Chandler Carruth6b981642016-12-10 06:34:44 +000029bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
30 Function &F, const PreservedAnalyses &PA,
31 FunctionAnalysisManager::Invalidator &Inv) {
Chandler Carruth410eaeb2017-01-11 06:23:21 +000032 // 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
Chandler Carruthf0022642017-01-20 02:41:20 +000034 // 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.
37 SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
Chandler Carruth6b981642016-12-10 06:34:44 +000038
Chandler Carruth410eaeb2017-01-11 06:23:21 +000039 // 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.
47 auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
48 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
49 Inv.invalidate<AAManager>(F, PA) ||
50 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
51 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
52 Inv.invalidate<LoopAnalysis>(F, PA) ||
53 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA)) {
54 // Note that the LoopInfo may be stale at this point, however the loop
55 // objects themselves remain the only viable keys that could be in the
56 // analysis manager's cache. So we just walk the keys and forcibly clear
57 // those results. Note that the order doesn't matter here as this will just
58 // directly destroy the results without calling methods on them.
Sanjoy Das005b88c2017-10-04 22:02:27 +000059 for (Loop *L : PreOrderLoops) {
60 // NB! `L` may not be in a good enough state to run Loop::getName.
61 InnerAM->clear(*L, "<possibly invalidated loop>");
62 }
Chandler Carruth410eaeb2017-01-11 06:23:21 +000063
64 // We also need to null out the inner AM so that when the object gets
65 // destroyed as invalid we don't try to clear the inner AM again. At that
66 // point we won't be able to reliably walk the loops for this function and
67 // only clear results associated with those loops the way we do here.
68 // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
69 // try to remain valid during invalidation. Maybe we should add an
70 // `IsClean` flag?
71 InnerAM = nullptr;
72
73 // Now return true to indicate this *is* invalid and a fresh proxy result
74 // needs to be built. This is especially important given the null InnerAM.
75 return true;
76 }
77
78 // Directly check if the relevant set is preserved so we can short circuit
79 // invalidating loops.
80 bool AreLoopAnalysesPreserved =
81 PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
82
83 // Since we have a valid LoopInfo we can actually leave the cached results in
84 // the analysis manager associated with the Loop keys, but we need to
85 // propagate any necessary invalidation logic into them. We'd like to
86 // invalidate things in roughly the same order as they were put into the
87 // cache and so we walk the preorder list in reverse to form a valid
88 // postorder.
89 for (Loop *L : reverse(PreOrderLoops)) {
90 Optional<PreservedAnalyses> InnerPA;
91
92 // Check to see whether the preserved set needs to be adjusted based on
93 // function-level analysis invalidation triggering deferred invalidation
94 // for this loop.
95 if (auto *OuterProxy =
96 InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
97 for (const auto &OuterInvalidationPair :
98 OuterProxy->getOuterInvalidations()) {
99 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
100 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
101 if (Inv.invalidate(OuterAnalysisID, F, PA)) {
102 if (!InnerPA)
103 InnerPA = PA;
104 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
105 InnerPA->abandon(InnerAnalysisID);
106 }
107 }
108
109 // Check if we needed a custom PA set. If so we'll need to run the inner
110 // invalidation.
111 if (InnerPA) {
112 InnerAM->invalidate(*L, *InnerPA);
113 continue;
114 }
115
116 // Otherwise we only need to do invalidation if the original PA set didn't
117 // preserve all Loop analyses.
118 if (!AreLoopAnalysesPreserved)
119 InnerAM->invalidate(*L, PA);
120 }
Chandler Carruth6b981642016-12-10 06:34:44 +0000121
122 // Return false to indicate that this result is still a valid proxy.
123 return false;
124}
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000125
126template <>
127LoopAnalysisManagerFunctionProxy::Result
128LoopAnalysisManagerFunctionProxy::run(Function &F,
129 FunctionAnalysisManager &AM) {
130 return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
131}
Justin Bognereecc3c82016-02-25 07:23:08 +0000132}
Justin Bognere839c3e2016-05-03 21:35:08 +0000133
134PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
135 PreservedAnalyses PA;
136 PA.preserve<DominatorTreeAnalysis>();
137 PA.preserve<LoopAnalysis>();
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000138 PA.preserve<LoopAnalysisManagerFunctionProxy>();
Justin Bognere839c3e2016-05-03 21:35:08 +0000139 PA.preserve<ScalarEvolutionAnalysis>();
140 // TODO: What we really want to do here is preserve an AA category, but that
141 // concept doesn't exist yet.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000142 PA.preserve<AAManager>();
Justin Bognere839c3e2016-05-03 21:35:08 +0000143 PA.preserve<BasicAA>();
144 PA.preserve<GlobalsAA>();
145 PA.preserve<SCEVAA>();
146 return PA;
147}