blob: b53a2b9671d93bc7c9244f0f0afa1aa4bc4a52c3 [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 Carruthed1ffe02013-11-20 04:01:38 +000015void ModulePassManager::run(Module *M) {
Chandler Carruth74015a72013-11-13 01:12:08 +000016 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
17 if (Passes[Idx]->run(M))
Chandler Carruthed1ffe02013-11-20 04:01:38 +000018 if (AM)
19 AM->invalidateAll(M);
20}
21
22void ModuleAnalysisManager::invalidateAll(Module *M) {
23 // FIXME: This is a total hack based on the fact that erasure doesn't
24 // invalidate iteration for DenseMap.
25 for (ModuleAnalysisResultMapT::iterator I = ModuleAnalysisResults.begin(),
26 E = ModuleAnalysisResults.end();
27 I != E; ++I)
28 if (I->second->invalidate(M))
29 ModuleAnalysisResults.erase(I);
30}
31
32const detail::AnalysisResultConcept<Module> &
33ModuleAnalysisManager::getResultImpl(void *PassID, Module *M) {
34 ModuleAnalysisResultMapT::iterator RI;
35 bool Inserted;
36 llvm::tie(RI, Inserted) = ModuleAnalysisResults.insert(std::make_pair(
37 PassID, polymorphic_ptr<detail::AnalysisResultConcept<Module> >()));
38
39 if (Inserted) {
40 // We don't have a cached result for this result. Look up the pass and run
41 // it to produce a result, which we then add to the cache.
42 ModuleAnalysisPassMapT::const_iterator PI =
43 ModuleAnalysisPasses.find(PassID);
44 assert(PI != ModuleAnalysisPasses.end() &&
45 "Analysis passes must be registered prior to being queried!");
46 RI->second = PI->second->run(M);
47 }
48
49 return *RI->second;
50}
51
52void ModuleAnalysisManager::invalidateImpl(void *PassID, Module *M) {
53 ModuleAnalysisResults.erase(PassID);
Chandler Carruth74015a72013-11-13 01:12:08 +000054}
55
56bool FunctionPassManager::run(Module *M) {
57 bool Changed = false;
58 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
59 for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx)
60 if (Passes[Idx]->run(I)) {
61 Changed = true;
Chandler Carruthed1ffe02013-11-20 04:01:38 +000062 if (AM)
63 AM->invalidateAll(I);
Chandler Carruth74015a72013-11-13 01:12:08 +000064 }
65 return Changed;
66}
67
Chandler Carruthed1ffe02013-11-20 04:01:38 +000068void FunctionAnalysisManager::invalidateAll(Function *F) {
69 // Clear all the invalidated results associated specifically with this
Chandler Carruth74015a72013-11-13 01:12:08 +000070 // function.
71 SmallVector<void *, 8> InvalidatedPassIDs;
72 FunctionAnalysisResultListT &ResultsList = FunctionAnalysisResultLists[F];
73 for (FunctionAnalysisResultListT::iterator I = ResultsList.begin(),
74 E = ResultsList.end();
Chandler Carruth8c60bc92013-11-15 21:56:44 +000075 I != E;)
Chandler Carruth74015a72013-11-13 01:12:08 +000076 if (I->second->invalidate(F)) {
Chandler Carruth8c60bc92013-11-15 21:56:44 +000077 InvalidatedPassIDs.push_back(I->first);
78 I = ResultsList.erase(I);
79 } else {
80 ++I;
Chandler Carruth74015a72013-11-13 01:12:08 +000081 }
82 while (!InvalidatedPassIDs.empty())
83 FunctionAnalysisResults.erase(
84 std::make_pair(InvalidatedPassIDs.pop_back_val(), F));
85}
86
Chandler Carruthed1ffe02013-11-20 04:01:38 +000087const detail::AnalysisResultConcept<Function> &
88FunctionAnalysisManager::getResultImpl(void *PassID, Function *F) {
Chandler Carruth74015a72013-11-13 01:12:08 +000089 FunctionAnalysisResultMapT::iterator RI;
90 bool Inserted;
91 llvm::tie(RI, Inserted) = FunctionAnalysisResults.insert(std::make_pair(
92 std::make_pair(PassID, F), FunctionAnalysisResultListT::iterator()));
93
94 if (Inserted) {
95 // We don't have a cached result for this result. Look up the pass and run
96 // it to produce a result, which we then add to the cache.
97 FunctionAnalysisPassMapT::const_iterator PI =
98 FunctionAnalysisPasses.find(PassID);
99 assert(PI != FunctionAnalysisPasses.end() &&
100 "Analysis passes must be registered prior to being queried!");
101 FunctionAnalysisResultListT &ResultList = FunctionAnalysisResultLists[F];
102 ResultList.push_back(std::make_pair(PassID, PI->second->run(F)));
103 RI->second = llvm::prior(ResultList.end());
104 }
105
106 return *RI->second->second;
107}
108
Chandler Carruthed1ffe02013-11-20 04:01:38 +0000109void FunctionAnalysisManager::invalidateImpl(void *PassID, Function *F) {
Chandler Carruth1205a6c2013-11-15 21:44:35 +0000110 FunctionAnalysisResultMapT::iterator RI =
111 FunctionAnalysisResults.find(std::make_pair(PassID, F));
Chandler Carruth74015a72013-11-13 01:12:08 +0000112 if (RI == FunctionAnalysisResults.end())
113 return;
114
115 FunctionAnalysisResultLists[F].erase(RI->second);
116}