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