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