blob: 76210a31ed7739783ad1aefede5798c99fdb8eae [file] [log] [blame]
Chandler Carruth74015a72013-11-13 01:12:08 +00001//===- PassManager.h - Infrastructure for managing & running IR passes ----===//
2//
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
10#include "llvm/IR/PassManager.h"
11#include "llvm/ADT/STLExtras.h"
12
13using namespace llvm;
14
Chandler Carruthb3e72192013-11-22 00:43:29 +000015PreservedAnalyses ModulePassManager::run(Module *M, ModuleAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000016 PreservedAnalyses PA = PreservedAnalyses::all();
17 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carruthb3e72192013-11-22 00:43:29 +000018 PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000019 if (AM)
20 AM->invalidate(M, PassPA);
21 PA.intersect(llvm_move(PassPA));
22 }
23 return PA;
Chandler Carruthed1ffe02013-11-20 04:01:38 +000024}
25
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000026void ModuleAnalysisManager::invalidate(Module *M, const PreservedAnalyses &PA) {
Chandler Carruthed1ffe02013-11-20 04:01:38 +000027 // FIXME: This is a total hack based on the fact that erasure doesn't
28 // invalidate iteration for DenseMap.
29 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
30 E = ModuleAnalysisResults.end();
31 I != E; ++I)
Chandler Carruth2846e9e2013-11-21 10:53:05 +000032 if (I->second->invalidate(M, PA))
Chandler Carruthed1ffe02013-11-20 04:01:38 +000033 ModuleAnalysisResults.erase(I);
34}
35
Chandler Carruth5bf5e312013-11-22 11:34:43 +000036const detail::AnalysisResultConcept<Module *> &
Chandler Carruthed1ffe02013-11-20 04:01:38 +000037ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
38 ModuleAnalysisResultMapT::iterator RI;
39 bool Inserted;
40 llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
Chandler Carruth5bf5e312013-11-22 11:34:43 +000041 PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module *> >()));
Chandler Carruthed1ffe02013-11-20 04:01:38 +000042
43 if (Inserted) {
44 // We don't have a cached result for this result. Look up the pass and run
45 // it to produce a result, which we then add to the cache.
46 ModuleAnalysisPassMapT::const_iterator PI =
47 ModuleAnalysisPasses.find(PassID);
48 assert(PI != ModuleAnalysisPasses.end() &&
49 "Analysis passes must be registered prior to being queried!");
Chandler Carruthf2edc072013-11-22 12:11:02 +000050 RI->second = PI->second->run(M, this);
Chandler Carruthed1ffe02013-11-20 04:01:38 +000051 }
52
53 return *RI->second;
54}
55
56void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
57 ModuleAnalysisResults.erase(PassID);
Chandler Carruth74015a72013-11-13 01:12:08 +000058}
59
Chandler Carruthb3e72192013-11-22 00:43:29 +000060PreservedAnalyses FunctionPassManager::run(Function *F, FunctionAnalysisManager *AM) {
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000061 PreservedAnalyses PA = PreservedAnalyses::all();
62 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
Chandler Carruthb3e72192013-11-22 00:43:29 +000063 PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000064 if (AM)
65 AM->invalidate(F, PassPA);
66 PA.intersect(llvm_move(PassPA));
67 }
68 return PA;
Chandler Carruth74015a72013-11-13 01:12:08 +000069}
70
Chandler Carruthc0bfa8c2013-11-20 11:31:50 +000071void FunctionAnalysisManager::invalidate(Function *F, const PreservedAnalyses &PA) {
Chandler Carruthed1ffe02013-11-20 04:01:38 +000072 // Clear all the invalidated results associated specifically with this
Chandler Carruth74015a72013-11-13 01:12:08 +000073 // function.
74 SmallVector<void *, 8> InvalidatedPassIDs;
75 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
76 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
77 E = ResultsList.end();
Chandler Carruth8c60bc92013-11-15 21:56:44 +000078 I != E;)
Chandler Carruth2846e9e2013-11-21 10:53:05 +000079 if (I->second->invalidate(F, PA)) {
Chandler Carruth8c60bc92013-11-15 21:56:44 +000080 InvalidatedPassIDs.push_back(I->first);
81 I = ResultsList.erase(I);
82 } else {
83 ++I;
Chandler Carruth74015a72013-11-13 01:12:08 +000084 }
85 while (!InvalidatedPassIDs.empty())
86 FunctionAnalysisResults.erase(
87 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
88}
89
Chandler Carruth851a2aa2013-11-21 02:11:31 +000090bool FunctionAnalysisManager::empty() const {
91 assert(FunctionAnalysisResults.empty() ==
92 FunctionAnalysisResultLists.empty() &&
93 "The storage and index of analysis results disagree on how many there "
94 "are!");
95 return FunctionAnalysisResults.empty();
96}
97
98void FunctionAnalysisManager::clear() {
99 FunctionAnalysisResults.clear();
100 FunctionAnalysisResultLists.clear();
101}
102
Chandler Carruth5bf5e312013-11-22 11:34:43 +0000103const detail::AnalysisResultConcept<Function *> &
Chandler Carruthed1ffe02013-11-20 04:01:38 +0000104FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
Chandler Carruth74015a72013-11-13 01:12:08 +0000105 FunctionAnalysisResultMapT::iterator RI;
106 bool Inserted;
107 llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
108 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
109
110 if (Inserted) {
111 // We don't have a cached result for this result. Look up the pass and run
112 // it to produce a result, which we then add to the cache.
113 FunctionAnalysisPassMapT::const_iterator PI =
114 FunctionAnalysisPasses.find(PassID);
115 assert(PI != FunctionAnalysisPasses.end() &&
116 "Analysis passes must be registered prior to being queried!");
117 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
Chandler Carruthf2edc072013-11-22 12:11:02 +0000118 ResultList.push_back(std::make_pair(PassID, PI->second->run(F, this)));
Chandler Carruth74015a72013-11-13 01:12:08 +0000119 RI->second = llvm::prior(ResultList.end());
120 }
121
122 return *RI->second->second;
123}
124
Chandler Carruthed1ffe02013-11-20 04:01:38 +0000125void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
Chandler Carruth1205a6c2013-11-15 21:44:35 +0000126 FunctionAnalysisResultMapT::iterator RI =
127 FunctionAnalysisResults.find(std::make_pair(PassID, F));
Chandler Carruth74015a72013-11-13 01:12:08 +0000128 if (RI == FunctionAnalysisResults.end())
129 return;
130
131 FunctionAnalysisResultLists[F].erase(RI->second);
132}
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000133
Chandler Carruthb3e72192013-11-22 00:43:29 +0000134char FunctionAnalysisManagerModuleProxy::PassID;
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000135
Chandler Carruthb3e72192013-11-22 00:43:29 +0000136FunctionAnalysisManagerModuleProxy::Result
137FunctionAnalysisManagerModuleProxy::run(Module *M) {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000138 assert(FAM.empty() && "Function analyses ran prior to the module proxy!");
139 return Result(FAM);
140}
141
Chandler Carruthb3e72192013-11-22 00:43:29 +0000142FunctionAnalysisManagerModuleProxy::Result::~Result() {
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000143 // Clear out the analysis manager if we're being destroyed -- it means we
144 // didn't even see an invalidate call when we got invalidated.
145 FAM.clear();
146}
147
Chandler Carruthb3e72192013-11-22 00:43:29 +0000148bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
149 Module *M, const PreservedAnalyses &PA) {
Chandler Carruthbceeb222013-11-22 23:38:07 +0000150 // If this proxy isn't marked as preserved, then we can't even invalidate
151 // individual function analyses, there may be an invalid set of Function
152 // objects in the cache making it impossible to incrementally preserve them.
153 // Just clear the entire manager.
154 if (!PA.preserved(ID()))
Chandler Carruth2846e9e2013-11-21 10:53:05 +0000155 FAM.clear();
Chandler Carruth851a2aa2013-11-21 02:11:31 +0000156
157 // Return false to indicate that this result is still a valid proxy.
158 return false;
159}