blob: 2a3b29d7fbcaabeed5259ccc537c5aa461457610 [file] [log] [blame]
Chandler Carruthc68d25f2017-01-11 09:43:56 +00001//===- LoopAnalysisManager.cpp - Loop analysis management -----------------===//
Justin Bognerda5e9242016-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 Carruthc68d25f2017-01-11 09:43:56 +000010#include "llvm/Analysis/LoopAnalysisManager.h"
Justin Bogner1a7c00a2016-05-03 21:35:08 +000011#include "llvm/Analysis/BasicAliasAnalysis.h"
12#include "llvm/Analysis/GlobalsModRef.h"
13#include "llvm/Analysis/LoopInfo.h"
Alina Sbirleaa694e222017-11-21 15:45:46 +000014#include "llvm/Analysis/MemorySSA.h"
Justin Bogner1a7c00a2016-05-03 21:35:08 +000015#include "llvm/Analysis/ScalarEvolution.h"
16#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
17#include "llvm/IR/Dominators.h"
Justin Bognerda5e9242016-02-25 07:23:08 +000018
19using namespace llvm;
20
Alina Sbirleaa694e222017-11-21 15:45:46 +000021namespace llvm {
22/// Enables memory ssa as a dependency for loop passes in legacy pass manager.
23cl::opt<bool> EnableMSSALoopDependency(
24 "enable-mssa-loop-dependency", cl::Hidden, cl::init(false),
25 cl::desc("Enable MemorySSA dependency for loop pass manager"));
26
Vedant Kumar06a26732018-02-28 19:08:52 +000027// Explicit template instantiations and specialization definitions for core
Chandler Carruth8bf27802016-12-10 06:34:44 +000028// template typedefs.
Chandler Carruthd27a39a2017-01-11 06:23:21 +000029template class AllAnalysesOn<Loop>;
30template class AnalysisManager<Loop, LoopStandardAnalysisResults &>;
Chandler Carruth500af852016-02-27 10:38:10 +000031template class InnerAnalysisManagerProxy<LoopAnalysisManager, Function>;
Chandler Carruthd53dbed2017-02-07 01:50:48 +000032template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Loop,
33 LoopStandardAnalysisResults &>;
Chandler Carruth8bf27802016-12-10 06:34:44 +000034
Chandler Carruth8bf27802016-12-10 06:34:44 +000035bool LoopAnalysisManagerFunctionProxy::Result::invalidate(
36 Function &F, const PreservedAnalyses &PA,
37 FunctionAnalysisManager::Invalidator &Inv) {
Chandler Carruthd27a39a2017-01-11 06:23:21 +000038 // First compute the sequence of IR units covered by this proxy. We will want
39 // to visit this in postorder, but because this is a tree structure we can do
Chandler Carruth50679292017-01-20 02:41:20 +000040 // this by building a preorder sequence and walking it backwards. We also
41 // want siblings in forward program order to match the LoopPassManager so we
42 // get the preorder with siblings reversed.
43 SmallVector<Loop *, 4> PreOrderLoops = LI->getLoopsInReverseSiblingPreorder();
Chandler Carruth8bf27802016-12-10 06:34:44 +000044
Chandler Carruthd27a39a2017-01-11 06:23:21 +000045 // If this proxy or the loop info is going to be invalidated, we also need
46 // to clear all the keys coming from that analysis. We also completely blow
47 // away the loop analyses if any of the standard analyses provided by the
48 // loop pass manager go away so that loop analyses can freely use these
49 // without worrying about declaring dependencies on them etc.
50 // FIXME: It isn't clear if this is the right tradeoff. We could instead make
51 // loop analyses declare any dependencies on these and use the more general
52 // invalidation logic below to act on that.
53 auto PAC = PA.getChecker<LoopAnalysisManagerFunctionProxy>();
Alina Sbirleaa694e222017-11-21 15:45:46 +000054 bool invalidateMemorySSAAnalysis = false;
55 if (EnableMSSALoopDependency)
56 invalidateMemorySSAAnalysis = Inv.invalidate<MemorySSAAnalysis>(F, PA);
Chandler Carruthd27a39a2017-01-11 06:23:21 +000057 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
58 Inv.invalidate<AAManager>(F, PA) ||
59 Inv.invalidate<AssumptionAnalysis>(F, PA) ||
60 Inv.invalidate<DominatorTreeAnalysis>(F, PA) ||
61 Inv.invalidate<LoopAnalysis>(F, PA) ||
Alina Sbirleaa694e222017-11-21 15:45:46 +000062 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
63 invalidateMemorySSAAnalysis) {
Chandler Carruthd27a39a2017-01-11 06:23:21 +000064 // Note that the LoopInfo may be stale at this point, however the loop
65 // objects themselves remain the only viable keys that could be in the
66 // analysis manager's cache. So we just walk the keys and forcibly clear
67 // those results. Note that the order doesn't matter here as this will just
68 // directly destroy the results without calling methods on them.
Sanjoy Das59b282a2017-10-04 22:02:27 +000069 for (Loop *L : PreOrderLoops) {
70 // NB! `L` may not be in a good enough state to run Loop::getName.
71 InnerAM->clear(*L, "<possibly invalidated loop>");
72 }
Chandler Carruthd27a39a2017-01-11 06:23:21 +000073
74 // We also need to null out the inner AM so that when the object gets
75 // destroyed as invalid we don't try to clear the inner AM again. At that
76 // point we won't be able to reliably walk the loops for this function and
77 // only clear results associated with those loops the way we do here.
78 // FIXME: Making InnerAM null at this point isn't very nice. Most analyses
79 // try to remain valid during invalidation. Maybe we should add an
80 // `IsClean` flag?
81 InnerAM = nullptr;
82
83 // Now return true to indicate this *is* invalid and a fresh proxy result
84 // needs to be built. This is especially important given the null InnerAM.
85 return true;
86 }
87
88 // Directly check if the relevant set is preserved so we can short circuit
89 // invalidating loops.
90 bool AreLoopAnalysesPreserved =
91 PA.allAnalysesInSetPreserved<AllAnalysesOn<Loop>>();
92
93 // Since we have a valid LoopInfo we can actually leave the cached results in
94 // the analysis manager associated with the Loop keys, but we need to
95 // propagate any necessary invalidation logic into them. We'd like to
96 // invalidate things in roughly the same order as they were put into the
97 // cache and so we walk the preorder list in reverse to form a valid
98 // postorder.
99 for (Loop *L : reverse(PreOrderLoops)) {
100 Optional<PreservedAnalyses> InnerPA;
101
102 // Check to see whether the preserved set needs to be adjusted based on
103 // function-level analysis invalidation triggering deferred invalidation
104 // for this loop.
105 if (auto *OuterProxy =
106 InnerAM->getCachedResult<FunctionAnalysisManagerLoopProxy>(*L))
107 for (const auto &OuterInvalidationPair :
108 OuterProxy->getOuterInvalidations()) {
109 AnalysisKey *OuterAnalysisID = OuterInvalidationPair.first;
110 const auto &InnerAnalysisIDs = OuterInvalidationPair.second;
111 if (Inv.invalidate(OuterAnalysisID, F, PA)) {
112 if (!InnerPA)
113 InnerPA = PA;
114 for (AnalysisKey *InnerAnalysisID : InnerAnalysisIDs)
115 InnerPA->abandon(InnerAnalysisID);
116 }
117 }
118
119 // Check if we needed a custom PA set. If so we'll need to run the inner
120 // invalidation.
121 if (InnerPA) {
122 InnerAM->invalidate(*L, *InnerPA);
123 continue;
124 }
125
126 // Otherwise we only need to do invalidation if the original PA set didn't
127 // preserve all Loop analyses.
128 if (!AreLoopAnalysesPreserved)
129 InnerAM->invalidate(*L, PA);
130 }
Chandler Carruth8bf27802016-12-10 06:34:44 +0000131
132 // Return false to indicate that this result is still a valid proxy.
133 return false;
134}
Chandler Carruthd27a39a2017-01-11 06:23:21 +0000135
136template <>
137LoopAnalysisManagerFunctionProxy::Result
138LoopAnalysisManagerFunctionProxy::run(Function &F,
139 FunctionAnalysisManager &AM) {
140 return Result(*InnerAM, AM.getResult<LoopAnalysis>(F));
141}
Justin Bognerda5e9242016-02-25 07:23:08 +0000142}
Justin Bogner1a7c00a2016-05-03 21:35:08 +0000143
144PreservedAnalyses llvm::getLoopPassPreservedAnalyses() {
145 PreservedAnalyses PA;
146 PA.preserve<DominatorTreeAnalysis>();
147 PA.preserve<LoopAnalysis>();
Chandler Carruthd27a39a2017-01-11 06:23:21 +0000148 PA.preserve<LoopAnalysisManagerFunctionProxy>();
Justin Bogner1a7c00a2016-05-03 21:35:08 +0000149 PA.preserve<ScalarEvolutionAnalysis>();
Alina Sbirleabb146ec2018-09-06 20:54:24 +0000150 if (EnableMSSALoopDependency)
151 PA.preserve<MemorySSAAnalysis>();
Alina Sbirleaa694e222017-11-21 15:45:46 +0000152 // FIXME: What we really want to do here is preserve an AA category, but that
Justin Bogner1a7c00a2016-05-03 21:35:08 +0000153 // concept doesn't exist yet.
Chandler Carruth2ffea512016-12-22 06:59:15 +0000154 PA.preserve<AAManager>();
Justin Bogner1a7c00a2016-05-03 21:35:08 +0000155 PA.preserve<BasicAA>();
156 PA.preserve<GlobalsAA>();
157 PA.preserve<SCEVAA>();
158 return PA;
159}