blob: efe2f043f6aef30209d71e2b68e42fece29821bd [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
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///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Chandler Carruthdf8b2232015-01-22 21:53:09 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000024#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000025#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000026#include "llvm/Analysis/CallGraph.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000027#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000028#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000029#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000030#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000031#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000032#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000033#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000034#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000035#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000036#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000038#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000039#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000040#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000041#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000042#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000043#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000044#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000045#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000046#include "llvm/Target/TargetMachine.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000047#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000048#include "llvm/Transforms/IPO/FunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000049#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner21e15372015-10-30 23:28:12 +000050#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000051#include "llvm/Transforms/InstCombine/InstCombine.h"
Justin Bogner19b67992015-10-30 23:13:18 +000052#include "llvm/Transforms/Scalar/ADCE.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000053#include "llvm/Transforms/Scalar/EarlyCSE.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000054#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Chandler Carruth89c45a12016-03-11 08:50:55 +000055#include "llvm/Transforms/Scalar/GVN.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000056#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000057#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000058#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000059
60using namespace llvm;
61
Chandler Carruth8b5a74192016-02-28 22:16:03 +000062static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
63
Chandler Carruth66445382014-01-11 08:16:35 +000064namespace {
65
Chandler Carruthd8330982014-01-12 09:34:22 +000066/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000067struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000068 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000069 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000070};
71
Chandler Carruth0b576b32015-01-06 02:50:06 +000072/// \brief No-op module analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000073struct NoOpModuleAnalysis : AnalysisBase<NoOpModuleAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000074 struct Result {};
75 Result run(Module &) { return Result(); }
76 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000077};
78
Chandler Carruth572e3402014-04-21 11:12:00 +000079/// \brief No-op CGSCC pass which does nothing.
80struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000081 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +000082 return PreservedAnalyses::all();
83 }
84 static StringRef name() { return "NoOpCGSCCPass"; }
85};
86
Chandler Carruth0b576b32015-01-06 02:50:06 +000087/// \brief No-op CGSCC analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +000088struct NoOpCGSCCAnalysis : AnalysisBase<NoOpCGSCCAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +000089 struct Result {};
90 Result run(LazyCallGraph::SCC &) { return Result(); }
91 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +000092};
93
Chandler Carruthd8330982014-01-12 09:34:22 +000094/// \brief No-op function pass which does nothing.
95struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000096 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +000097 static StringRef name() { return "NoOpFunctionPass"; }
98};
99
Chandler Carruth0b576b32015-01-06 02:50:06 +0000100/// \brief No-op function analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +0000101struct NoOpFunctionAnalysis : AnalysisBase<NoOpFunctionAnalysis> {
Chandler Carruth0b576b32015-01-06 02:50:06 +0000102 struct Result {};
103 Result run(Function &) { return Result(); }
104 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000105};
106
Justin Bognereecc3c82016-02-25 07:23:08 +0000107/// \brief No-op loop pass which does nothing.
108struct NoOpLoopPass {
109 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
110 static StringRef name() { return "NoOpLoopPass"; }
111};
112
113/// \brief No-op loop analysis.
Chandler Carruth3a634352016-02-26 11:44:45 +0000114struct NoOpLoopAnalysis : AnalysisBase<NoOpLoopAnalysis> {
Justin Bognereecc3c82016-02-25 07:23:08 +0000115 struct Result {};
116 Result run(Loop &) { return Result(); }
117 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000118};
119
Chandler Carruth66445382014-01-11 08:16:35 +0000120} // End anonymous namespace.
121
Chandler Carruth1ff77242015-03-07 09:02:36 +0000122void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000123#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000124 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000125#include "PassRegistry.def"
126}
127
Chandler Carruth1ff77242015-03-07 09:02:36 +0000128void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000129#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000130 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000131#include "PassRegistry.def"
132}
133
Chandler Carruth1ff77242015-03-07 09:02:36 +0000134void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000135#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000136 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000137#include "PassRegistry.def"
138}
139
Justin Bognereecc3c82016-02-25 07:23:08 +0000140void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
141#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
142 LAM.registerPass([&] { return CREATE_PASS; });
143#include "PassRegistry.def"
144}
145
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000146void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
147 OptimizationLevel Level,
148 bool DebugLogging) {
149 // FIXME: Finish fleshing this out to match the legacy pipelines.
150 FunctionPassManager EarlyFPM(DebugLogging);
151 EarlyFPM.addPass(SimplifyCFGPass());
152 EarlyFPM.addPass(SROA());
153 EarlyFPM.addPass(EarlyCSEPass());
154 EarlyFPM.addPass(LowerExpectIntrinsicPass());
155
156 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
157}
158
159void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
160 OptimizationLevel Level,
161 bool DebugLogging) {
162 // FIXME: We should use a customized pre-link pipeline!
163 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
164}
165
166void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
167 OptimizationLevel Level,
168 bool DebugLogging) {
169 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
170 FunctionPassManager LateFPM(DebugLogging);
171 LateFPM.addPass(InstCombinePass());
172 LateFPM.addPass(SimplifyCFGPass());
173
174 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
175}
176
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000177#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000178static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000179 // Manually handle aliases for pre-configured pipeline fragments.
180 if (Name.startswith("default") || Name.startswith("lto"))
181 return DefaultAliasRegex.match(Name);
182
Chandler Carruth58944182014-04-21 08:08:50 +0000183#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000184#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000185 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000186 return true;
187#include "PassRegistry.def"
188
Chandler Carruth66445382014-01-11 08:16:35 +0000189 return false;
190}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000191#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000192
Chandler Carruth572e3402014-04-21 11:12:00 +0000193static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000194#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000195#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000196 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000197 return true;
198#include "PassRegistry.def"
199
Chandler Carruth572e3402014-04-21 11:12:00 +0000200 return false;
201}
202
Chandler Carruthd8330982014-01-12 09:34:22 +0000203static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000204#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000205#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000206 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000207 return true;
208#include "PassRegistry.def"
209
Chandler Carruthd8330982014-01-12 09:34:22 +0000210 return false;
211}
212
Justin Bognereecc3c82016-02-25 07:23:08 +0000213static bool isLoopPassName(StringRef Name) {
214#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
215#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
216 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
217 return true;
218#include "PassRegistry.def"
219
220 return false;
221}
222
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000223bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
224 bool DebugLogging) {
225 // Manually handle aliases for pre-configured pipeline fragments.
226 if (Name.startswith("default") || Name.startswith("lto")) {
227 SmallVector<StringRef, 3> Matches;
228 if (!DefaultAliasRegex.match(Name, &Matches))
229 return false;
230 assert(Matches.size() == 3 && "Must capture two matched strings!");
231
232 auto L = StringSwitch<OptimizationLevel>(Matches[2])
233 .Case("O0", O0)
234 .Case("O1", O1)
235 .Case("O2", O2)
236 .Case("O3", O3)
237 .Case("Os", Os)
238 .Case("Oz", Oz);
239
240 if (Matches[1] == "default") {
241 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
242 } else if (Matches[1] == "lto-pre-link") {
243 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
244 } else {
245 assert(Matches[1] == "lto" && "Not one of the matched options!");
246 addLTODefaultPipeline(MPM, L, DebugLogging);
247 }
248 return true;
249 }
250
Chandler Carruth58944182014-04-21 08:08:50 +0000251#define MODULE_PASS(NAME, CREATE_PASS) \
252 if (Name == NAME) { \
253 MPM.addPass(CREATE_PASS); \
254 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000255 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000256#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
257 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000258 MPM.addPass(RequireAnalysisPass< \
259 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000260 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000261 } \
262 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000263 MPM.addPass(InvalidateAnalysisPass< \
264 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000265 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000266 }
267#include "PassRegistry.def"
268
Chandler Carruth66445382014-01-11 08:16:35 +0000269 return false;
270}
271
Chandler Carruth1ff77242015-03-07 09:02:36 +0000272bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000273#define CGSCC_PASS(NAME, CREATE_PASS) \
274 if (Name == NAME) { \
275 CGPM.addPass(CREATE_PASS); \
276 return true; \
277 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000278#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
279 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000280 CGPM.addPass(RequireAnalysisPass< \
281 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000282 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000283 } \
284 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000285 CGPM.addPass(InvalidateAnalysisPass< \
286 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000287 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000288 }
289#include "PassRegistry.def"
290
Chandler Carruth572e3402014-04-21 11:12:00 +0000291 return false;
292}
293
Chandler Carruth1ff77242015-03-07 09:02:36 +0000294bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
295 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000296#define FUNCTION_PASS(NAME, CREATE_PASS) \
297 if (Name == NAME) { \
298 FPM.addPass(CREATE_PASS); \
299 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000300 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000301#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
302 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000303 FPM.addPass(RequireAnalysisPass< \
304 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000305 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000306 } \
307 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000308 FPM.addPass(InvalidateAnalysisPass< \
309 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000310 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000311 }
312#include "PassRegistry.def"
313
Chandler Carruthd8330982014-01-12 09:34:22 +0000314 return false;
315}
316
Justin Bognereecc3c82016-02-25 07:23:08 +0000317bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
318 StringRef Name) {
319#define LOOP_PASS(NAME, CREATE_PASS) \
320 if (Name == NAME) { \
321 FPM.addPass(CREATE_PASS); \
322 return true; \
323 }
324#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
325 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000326 FPM.addPass(RequireAnalysisPass< \
327 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000328 return true; \
329 } \
330 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000331 FPM.addPass(InvalidateAnalysisPass< \
332 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000333 return true; \
334 }
335#include "PassRegistry.def"
336
337 return false;
338}
339
Chandler Carruthedf59962016-02-18 09:45:17 +0000340bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000341#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
342 if (Name == NAME) { \
343 AA.registerModuleAnalysis< \
344 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
345 return true; \
346 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000347#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
348 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000349 AA.registerFunctionAnalysis< \
350 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000351 return true; \
352 }
353#include "PassRegistry.def"
354
355 return false;
356}
357
Justin Bognereecc3c82016-02-25 07:23:08 +0000358bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
359 StringRef &PipelineText,
360 bool VerifyEachPass,
361 bool DebugLogging) {
362 for (;;) {
363 // Parse nested pass managers by recursing.
364 if (PipelineText.startswith("loop(")) {
365 LoopPassManager NestedLPM(DebugLogging);
366
367 // Parse the inner pipeline inte the nested manager.
368 PipelineText = PipelineText.substr(strlen("loop("));
369 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
370 DebugLogging) ||
371 PipelineText.empty())
372 return false;
373 assert(PipelineText[0] == ')');
374 PipelineText = PipelineText.substr(1);
375
376 // Add the nested pass manager with the appropriate adaptor.
377 LPM.addPass(std::move(NestedLPM));
378 } else {
379 // Otherwise try to parse a pass name.
380 size_t End = PipelineText.find_first_of(",)");
381 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
382 return false;
383 // TODO: Ideally, we would run a LoopVerifierPass() here in the
384 // VerifyEachPass case, but we don't have such a verifier yet.
385
386 PipelineText = PipelineText.substr(End);
387 }
388
389 if (PipelineText.empty() || PipelineText[0] == ')')
390 return true;
391
392 assert(PipelineText[0] == ',');
393 PipelineText = PipelineText.substr(1);
394 }
395}
396
Chandler Carruth1ff77242015-03-07 09:02:36 +0000397bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
398 StringRef &PipelineText,
399 bool VerifyEachPass,
400 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000401 for (;;) {
402 // Parse nested pass managers by recursing.
403 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000404 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000405
406 // Parse the inner pipeline inte the nested manager.
407 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000408 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
409 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000410 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000411 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000412 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000413 PipelineText = PipelineText.substr(1);
414
415 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000416 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000417 } else if (PipelineText.startswith("loop(")) {
418 LoopPassManager NestedLPM(DebugLogging);
419
420 // Parse the inner pipeline inte the nested manager.
421 PipelineText = PipelineText.substr(strlen("loop("));
422 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
423 DebugLogging) ||
424 PipelineText.empty())
425 return false;
426 assert(PipelineText[0] == ')');
427 PipelineText = PipelineText.substr(1);
428
429 // Add the nested pass manager with the appropriate adaptor.
430 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000431 } else {
432 // Otherwise try to parse a pass name.
433 size_t End = PipelineText.find_first_of(",)");
434 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
435 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000436 if (VerifyEachPass)
437 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000438
439 PipelineText = PipelineText.substr(End);
440 }
441
442 if (PipelineText.empty() || PipelineText[0] == ')')
443 return true;
444
445 assert(PipelineText[0] == ',');
446 PipelineText = PipelineText.substr(1);
447 }
448}
449
Chandler Carruth1ff77242015-03-07 09:02:36 +0000450bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
451 StringRef &PipelineText,
452 bool VerifyEachPass,
453 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000454 for (;;) {
455 // Parse nested pass managers by recursing.
456 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000457 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000458
459 // Parse the inner pipeline into the nested manager.
460 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000461 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
462 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000463 PipelineText.empty())
464 return false;
465 assert(PipelineText[0] == ')');
466 PipelineText = PipelineText.substr(1);
467
468 // Add the nested pass manager with the appropriate adaptor.
469 CGPM.addPass(std::move(NestedCGPM));
470 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000471 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000472
473 // Parse the inner pipeline inte the nested manager.
474 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000475 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
476 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000477 PipelineText.empty())
478 return false;
479 assert(PipelineText[0] == ')');
480 PipelineText = PipelineText.substr(1);
481
482 // Add the nested pass manager with the appropriate adaptor.
483 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
484 } else {
485 // Otherwise try to parse a pass name.
486 size_t End = PipelineText.find_first_of(",)");
487 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
488 return false;
489 // FIXME: No verifier support for CGSCC passes!
490
491 PipelineText = PipelineText.substr(End);
492 }
493
494 if (PipelineText.empty() || PipelineText[0] == ')')
495 return true;
496
497 assert(PipelineText[0] == ',');
498 PipelineText = PipelineText.substr(1);
499 }
500}
501
Chandler Carruth1ff77242015-03-07 09:02:36 +0000502bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
503 StringRef &PipelineText,
504 bool VerifyEachPass,
505 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000506 for (;;) {
507 // Parse nested pass managers by recursing.
508 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000509 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000510
511 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000512 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000513 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
514 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000515 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000516 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000517 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000518 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000519
520 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000521 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000522 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000523 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000524
525 // Parse the inner pipeline inte the nested manager.
526 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000527 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
528 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000529 PipelineText.empty())
530 return false;
531 assert(PipelineText[0] == ')');
532 PipelineText = PipelineText.substr(1);
533
534 // Add the nested pass manager with the appropriate adaptor.
535 MPM.addPass(
536 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000537 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000538 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000539
540 // Parse the inner pipeline inte the nested manager.
541 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000542 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
543 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000544 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000545 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000546 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000547 PipelineText = PipelineText.substr(1);
548
549 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000550 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000551 } else {
552 // Otherwise try to parse a pass name.
553 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000554 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000555 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000556 if (VerifyEachPass)
557 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000558
559 PipelineText = PipelineText.substr(End);
560 }
561
562 if (PipelineText.empty() || PipelineText[0] == ')')
563 return true;
564
565 assert(PipelineText[0] == ',');
566 PipelineText = PipelineText.substr(1);
567 }
568}
569
570// Primary pass pipeline description parsing routine.
571// FIXME: Should this routine accept a TargetMachine or require the caller to
572// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000573bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
574 StringRef PipelineText, bool VerifyEachPass,
575 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000576 // By default, try to parse the pipeline as-if it were within an implicit
577 // 'module(...)' pass pipeline. If this will parse at all, it needs to
578 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000579 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000580 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000581
Chandler Carruthea368f12015-01-06 08:37:58 +0000582 // This isn't parsable as a module pipeline, look for the end of a pass name
583 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000584 StringRef FirstName =
585 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000586 assert(!isModulePassName(FirstName) &&
587 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000588
Chandler Carruthea368f12015-01-06 08:37:58 +0000589 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
590 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000591 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000592 CGSCCPassManager CGPM(DebugLogging);
593 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
594 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000595 !PipelineText.empty())
596 return false;
597 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
598 return true;
599 }
600
Chandler Carruthea368f12015-01-06 08:37:58 +0000601 // Similarly, if this looks like a Function pass, parse the whole thing as
602 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000603 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000604 FunctionPassManager FPM(DebugLogging);
605 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
606 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000607 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000608 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000609 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000610 return true;
611 }
Chandler Carruth66445382014-01-11 08:16:35 +0000612
Justin Bognereecc3c82016-02-25 07:23:08 +0000613 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
614 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
615 LoopPassManager LPM(DebugLogging);
616 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
617 DebugLogging) ||
618 !PipelineText.empty())
619 return false;
620 FunctionPassManager FPM(DebugLogging);
621 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
622 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
623 return true;
624 }
625
626
Chandler Carruth66445382014-01-11 08:16:35 +0000627 return false;
628}
Chandler Carruthedf59962016-02-18 09:45:17 +0000629
630bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
631 while (!PipelineText.empty()) {
632 StringRef Name;
633 std::tie(Name, PipelineText) = PipelineText.split(',');
634 if (!parseAAPassName(AA, Name))
635 return false;
636 }
637
638 return true;
639}