91533cc175a147664f03bb0ae9f4ec3c46569249
[oota-llvm.git] / lib / IR / PassManager.cpp
1 //===- PassManager.cpp - 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/ADT/STLExtras.h"
11 #include "llvm/IR/LLVMContext.h"
12 #include "llvm/IR/PassManager.h"
13
14 using namespace llvm;
15 using llvm::detail::DebugPM;
16
17 cl::opt<bool> llvm::detail::DebugPM(
18     "debug-pass-manager", cl::Hidden,
19     cl::desc("Print pass management debugging information"));
20
21 PreservedAnalyses ModulePassManager::run(Module &M, ModuleAnalysisManager *AM) {
22   PreservedAnalyses PA = PreservedAnalyses::all();
23
24   if (DebugPM)
25     dbgs() << "Starting module pass manager run.\n";
26
27   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
28     if (DebugPM)
29       dbgs() << "Running module pass: " << Passes[Idx]->name() << "\n";
30
31     PreservedAnalyses PassPA = Passes[Idx]->run(M, AM);
32
33     // If we have an active analysis manager at this level we want to ensure we
34     // update it as each pass runs and potentially invalidates analyses. We
35     // also update the preserved set of analyses based on what analyses we have
36     // already handled the invalidation for here and don't need to invalidate
37     // when finished.
38     if (AM)
39       PassPA = AM->invalidate(M, std::move(PassPA));
40
41     // Finally, we intersect the final preserved analyses to compute the
42     // aggregate preserved set for this pass manager.
43     PA.intersect(std::move(PassPA));
44
45     M.getContext().yield();
46   }
47
48   if (DebugPM)
49     dbgs() << "Finished module pass manager run.\n";
50
51   return PA;
52 }
53
54 PreservedAnalyses FunctionPassManager::run(Function &F,
55                                            FunctionAnalysisManager *AM) {
56   PreservedAnalyses PA = PreservedAnalyses::all();
57
58   if (DebugPM)
59     dbgs() << "Starting function pass manager run.\n";
60
61   for (unsigned Idx = 0, Size = Passes.size(); Idx != Size; ++Idx) {
62     if (DebugPM)
63       dbgs() << "Running function pass: " << Passes[Idx]->name() << "\n";
64
65     PreservedAnalyses PassPA = Passes[Idx]->run(F, AM);
66
67     // If we have an active analysis manager at this level we want to ensure we
68     // update it as each pass runs and potentially invalidates analyses. We
69     // also update the preserved set of analyses based on what analyses we have
70     // already handled the invalidation for here and don't need to invalidate
71     // when finished.
72     if (AM)
73       PassPA = AM->invalidate(F, std::move(PassPA));
74
75     // Finally, we intersect the final preserved analyses to compute the
76     // aggregate preserved set for this pass manager.
77     PA.intersect(std::move(PassPA));
78
79     F.getContext().yield();
80   }
81
82   if (DebugPM)
83     dbgs() << "Finished function pass manager run.\n";
84
85   return PA;
86 }
87
88 char FunctionAnalysisManagerModuleProxy::PassID;
89
90 FunctionAnalysisManagerModuleProxy::Result
91 FunctionAnalysisManagerModuleProxy::run(Module &M) {
92   assert(FAM->empty() && "Function analyses ran prior to the module proxy!");
93   return Result(*FAM);
94 }
95
96 FunctionAnalysisManagerModuleProxy::Result::~Result() {
97   // Clear out the analysis manager if we're being destroyed -- it means we
98   // didn't even see an invalidate call when we got invalidated.
99   FAM->clear();
100 }
101
102 bool FunctionAnalysisManagerModuleProxy::Result::invalidate(
103     Module &M, const PreservedAnalyses &PA) {
104   // If this proxy isn't marked as preserved, then we can't even invalidate
105   // individual function analyses, there may be an invalid set of Function
106   // objects in the cache making it impossible to incrementally preserve them.
107   // Just clear the entire manager.
108   if (!PA.preserved(ID()))
109     FAM->clear();
110
111   // Return false to indicate that this result is still a valid proxy.
112   return false;
113 }
114
115 char ModuleAnalysisManagerFunctionProxy::PassID;