[PM] Add module and function printing passes for the new pass manager.
[oota-llvm.git] / tools / opt / Passes.cpp
1 //===- Passes.cpp - Parsing, selection, and running of 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 /// \file
10 ///
11 /// This file provides the infrastructure to parse and build a custom pass
12 /// manager based on a commandline flag. It also provides helpers to aid in
13 /// analyzing, debugging, and testing pass structures.
14 ///
15 //===----------------------------------------------------------------------===//
16
17 #include "Passes.h"
18 #include "llvm/IR/IRPrintingPasses.h"
19 #include "llvm/IR/PassManager.h"
20 #include "llvm/Support/Debug.h"
21
22 using namespace llvm;
23
24 namespace {
25
26 /// \brief No-op module pass which does nothing.
27 struct NoOpModulePass {
28   PreservedAnalyses run(Module *M) { return PreservedAnalyses::all(); }
29   static StringRef name() { return "NoOpModulePass"; }
30 };
31
32 /// \brief No-op function pass which does nothing.
33 struct NoOpFunctionPass {
34   PreservedAnalyses run(Function *F) { return PreservedAnalyses::all(); }
35   static StringRef name() { return "NoOpFunctionPass"; }
36 };
37
38 } // End anonymous namespace.
39
40 // FIXME: Factor all of the parsing logic into a .def file that we include
41 // under different macros.
42 static bool isModulePassName(StringRef Name) {
43   if (Name == "no-op-module") return true;
44   if (Name == "print") return true;
45
46   return false;
47 }
48
49 static bool isFunctionPassName(StringRef Name) {
50   if (Name == "no-op-function") return true;
51   if (Name == "print") return true;
52
53   return false;
54 }
55
56 static bool parseModulePassName(ModulePassManager &MPM, StringRef Name) {
57   if (Name == "no-op-module") {
58     MPM.addPass(NoOpModulePass());
59     return true;
60   }
61   if (Name == "print") {
62     MPM.addPass(PrintModulePass(dbgs()));
63     return true;
64   }
65   return false;
66 }
67
68 static bool parseFunctionPassName(FunctionPassManager &FPM, StringRef Name) {
69   if (Name == "no-op-function") {
70     FPM.addPass(NoOpFunctionPass());
71     return true;
72   }
73   if (Name == "print") {
74     FPM.addPass(PrintFunctionPass(dbgs()));
75     return true;
76   }
77   return false;
78 }
79
80 static bool parseFunctionPassPipeline(FunctionPassManager &FPM,
81                                       StringRef &PipelineText) {
82   for (;;) {
83     // Parse nested pass managers by recursing.
84     if (PipelineText.startswith("function(")) {
85       FunctionPassManager NestedFPM;
86
87       // Parse the inner pipeline inte the nested manager.
88       PipelineText = PipelineText.substr(strlen("function("));
89       if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
90           PipelineText.empty())
91         return false;
92       assert(PipelineText[0] == ')');
93       PipelineText = PipelineText.substr(1);
94
95       // Add the nested pass manager with the appropriate adaptor.
96       FPM.addPass(NestedFPM);
97     } else {
98       // Otherwise try to parse a pass name.
99       size_t End = PipelineText.find_first_of(",)");
100       if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
101         return false;
102
103       PipelineText = PipelineText.substr(End);
104     }
105
106     if (PipelineText.empty() || PipelineText[0] == ')')
107       return true;
108
109     assert(PipelineText[0] == ',');
110     PipelineText = PipelineText.substr(1);
111   }
112 }
113
114 static bool parseModulePassPipeline(ModulePassManager &MPM,
115                                     StringRef &PipelineText) {
116   for (;;) {
117     // Parse nested pass managers by recursing.
118     if (PipelineText.startswith("module(")) {
119       ModulePassManager NestedMPM;
120
121       // Parse the inner pipeline into the nested manager.
122       PipelineText = PipelineText.substr(strlen("module("));
123       if (!parseModulePassPipeline(NestedMPM, PipelineText) ||
124           PipelineText.empty())
125         return false;
126       assert(PipelineText[0] == ')');
127       PipelineText = PipelineText.substr(1);
128
129       // Now add the nested manager as a module pass.
130       MPM.addPass(NestedMPM);
131     } else if (PipelineText.startswith("function(")) {
132       FunctionPassManager NestedFPM;
133
134       // Parse the inner pipeline inte the nested manager.
135       PipelineText = PipelineText.substr(strlen("function("));
136       if (!parseFunctionPassPipeline(NestedFPM, PipelineText) ||
137           PipelineText.empty())
138         return false;
139       assert(PipelineText[0] == ')');
140       PipelineText = PipelineText.substr(1);
141
142       // Add the nested pass manager with the appropriate adaptor.
143       MPM.addPass(createModuleToFunctionPassAdaptor(NestedFPM));
144     } else {
145       // Otherwise try to parse a pass name.
146       size_t End = PipelineText.find_first_of(",)");
147       if (!parseModulePassName(MPM, PipelineText.substr(0, End)))
148         return false;
149
150       PipelineText = PipelineText.substr(End);
151     }
152
153     if (PipelineText.empty() || PipelineText[0] == ')')
154       return true;
155
156     assert(PipelineText[0] == ',');
157     PipelineText = PipelineText.substr(1);
158   }
159 }
160
161 // Primary pass pipeline description parsing routine.
162 // FIXME: Should this routine accept a TargetMachine or require the caller to
163 // pre-populate the analysis managers with target-specific stuff?
164 bool llvm::parsePassPipeline(ModulePassManager &MPM, StringRef PipelineText) {
165   // Look at the first entry to figure out which layer to start parsing at.
166   if (PipelineText.startswith("module("))
167     return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
168   if (PipelineText.startswith("function(")) {
169     FunctionPassManager FPM;
170     if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
171       return false;
172     MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
173     return true;
174   }
175
176   // This isn't a direct pass manager name, look for the end of a pass name.
177   StringRef FirstName =
178       PipelineText.substr(0, PipelineText.find_first_of(",)"));
179   if (isModulePassName(FirstName))
180     return parseModulePassPipeline(MPM, PipelineText) && PipelineText.empty();
181
182   if (isFunctionPassName(FirstName)) {
183     FunctionPassManager FPM;
184     if (!parseFunctionPassPipeline(FPM, PipelineText) || !PipelineText.empty())
185       return false;
186     MPM.addPass(createModuleToFunctionPassAdaptor(FPM));
187     return true;
188   }
189
190   return false;
191 }