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