Fix Polly
[polly-mirror.git] / lib / Analysis / ScopPass.cpp
blob6e29998024dfa438fbd0d1b8428d8f1c0063f923
1 //===- ScopPass.cpp - The base class of Passes that operate on Polly IR ---===//
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 // This file contains the definitions of the ScopPass members.
11 //===----------------------------------------------------------------------===//
13 #include "polly/ScopPass.h"
14 #include "polly/ScopInfo.h"
15 #include "llvm/Analysis/BasicAliasAnalysis.h"
16 #include "llvm/Analysis/GlobalsModRef.h"
17 #include "llvm/Analysis/OptimizationRemarkEmitter.h"
18 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
19 #include "llvm/Analysis/TargetTransformInfo.h"
21 using namespace llvm;
22 using namespace polly;
24 bool ScopPass::runOnRegion(Region *R, RGPassManager &RGM) {
25 S = nullptr;
27 if (skipRegion(*R))
28 return false;
30 if ((S = getAnalysis<ScopInfoRegionPass>().getScop()))
31 return runOnScop(*S);
33 return false;
36 void ScopPass::print(raw_ostream &OS, const Module *M) const {
37 if (S)
38 printScop(OS, *S);
41 void ScopPass::getAnalysisUsage(AnalysisUsage &AU) const {
42 AU.addRequired<ScopInfoRegionPass>();
44 AU.addPreserved<AAResultsWrapperPass>();
45 AU.addPreserved<BasicAAWrapperPass>();
46 AU.addPreserved<LoopInfoWrapperPass>();
47 AU.addPreserved<DominatorTreeWrapperPass>();
48 AU.addPreserved<GlobalsAAWrapperPass>();
49 AU.addPreserved<ScopDetectionWrapperPass>();
50 AU.addPreserved<ScalarEvolutionWrapperPass>();
51 AU.addPreserved<SCEVAAWrapperPass>();
52 AU.addPreserved<OptimizationRemarkEmitterWrapperPass>();
53 AU.addPreserved<RegionInfoPass>();
54 AU.addPreserved<ScopInfoRegionPass>();
55 AU.addPreserved<TargetTransformInfoWrapperPass>();
58 namespace polly {
59 template class OwningInnerAnalysisManagerProxy<ScopAnalysisManager, Function>;
62 namespace llvm {
64 template class PassManager<Scop, ScopAnalysisManager,
65 ScopStandardAnalysisResults &, SPMUpdater &>;
66 template class InnerAnalysisManagerProxy<ScopAnalysisManager, Function>;
67 template class OuterAnalysisManagerProxy<FunctionAnalysisManager, Scop,
68 ScopStandardAnalysisResults &>;
70 template <>
71 PreservedAnalyses
72 PassManager<Scop, ScopAnalysisManager, ScopStandardAnalysisResults &,
73 SPMUpdater &>::run(Scop &S, ScopAnalysisManager &AM,
74 ScopStandardAnalysisResults &AR, SPMUpdater &U) {
75 auto PA = PreservedAnalyses::all();
76 for (auto &Pass : Passes) {
77 auto PassPA = Pass->run(S, AM, AR, U);
79 AM.invalidate(S, PassPA);
80 PA.intersect(std::move(PassPA));
83 // All analyses for 'this' Scop have been invalidated above.
84 // If ScopPasses affect break other scops they have to propagate this
85 // information through the updater
86 PA.preserveSet<AllAnalysesOn<Scop>>();
87 return PA;
90 bool ScopAnalysisManagerFunctionProxy::Result::invalidate(
91 Function &F, const PreservedAnalyses &PA,
92 FunctionAnalysisManager::Invalidator &Inv) {
94 // First, check whether our ScopInfo is about to be invalidated
95 auto PAC = PA.getChecker<ScopAnalysisManagerFunctionProxy>();
96 if (!(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>()) ||
97 Inv.invalidate<ScopInfoAnalysis>(F, PA) ||
98 Inv.invalidate<ScalarEvolutionAnalysis>(F, PA) ||
99 Inv.invalidate<LoopAnalysis>(F, PA) ||
100 Inv.invalidate<DominatorTreeAnalysis>(F, PA)) {
102 // As everything depends on ScopInfo, we must drop all existing results
103 for (auto &S : *SI)
104 if (auto *scop = S.second.get())
105 if (InnerAM)
106 InnerAM->clear(*scop, scop->getName());
108 InnerAM = nullptr;
109 return true; // Invalidate the proxy result as well.
112 bool allPreserved = PA.allAnalysesInSetPreserved<AllAnalysesOn<Scop>>();
114 // Invalidate all non-preserved analyses
115 // Even if all analyses were preserved, we still need to run deferred
116 // invalidation
117 for (auto &S : *SI) {
118 Optional<PreservedAnalyses> InnerPA;
119 auto *scop = S.second.get();
120 if (!scop)
121 continue;
123 if (auto *OuterProxy =
124 InnerAM->getCachedResult<FunctionAnalysisManagerScopProxy>(*scop)) {
125 for (const auto &InvPair : OuterProxy->getOuterInvalidations()) {
126 auto *OuterAnalysisID = InvPair.first;
127 const auto &InnerAnalysisIDs = InvPair.second;
129 if (Inv.invalidate(OuterAnalysisID, F, PA)) {
130 if (!InnerPA)
131 InnerPA = PA;
132 for (auto *InnerAnalysisID : InnerAnalysisIDs)
133 InnerPA->abandon(InnerAnalysisID);
137 if (InnerPA) {
138 InnerAM->invalidate(*scop, *InnerPA);
139 continue;
143 if (!allPreserved)
144 InnerAM->invalidate(*scop, PA);
147 return false; // This proxy is still valid
150 template <>
151 ScopAnalysisManagerFunctionProxy::Result
152 ScopAnalysisManagerFunctionProxy::run(Function &F,
153 FunctionAnalysisManager &FAM) {
154 return Result(*InnerAM, FAM.getResult<ScopInfoAnalysis>(F));
156 } // namespace llvm
158 namespace polly {
159 template <>
160 OwningScopAnalysisManagerFunctionProxy::Result
161 OwningScopAnalysisManagerFunctionProxy::run(Function &F,
162 FunctionAnalysisManager &FAM) {
163 return Result(InnerAM, FAM.getResult<ScopInfoAnalysis>(F));
165 } // namespace polly