blob: fdf8c6982d58bd0461f7087188c1f9c38b1357a9 [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"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
25#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000026#include "llvm/Analysis/BranchProbabilityInfo.h"
Chandler Carruth342c6712016-02-20 03:52:02 +000027#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000028#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000029#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000030#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000031#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000032#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000033#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000034#include "llvm/Analysis/LazyCallGraph.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000035#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000036#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000037#include "llvm/Analysis/PostDominators.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000038#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000039#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000040#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000041#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000042#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000043#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000044#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000045#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000046#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000047#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000048#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000049#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000050#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000051#include "llvm/Target/TargetMachine.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000052#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano344e8382016-05-05 02:37:32 +000053#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000054#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000055#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000056#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000057#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000058#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000059#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000060#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000061#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000062#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000063#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000064#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000065#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000066#include "llvm/Transforms/Scalar/ADCE.h"
Davide Italiano655a1452016-05-25 01:57:04 +000067#include "llvm/Transforms/Scalar/BDCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000068#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000069#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000070#include "llvm/Transforms/Scalar/EarlyCSE.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000071#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano655a1452016-05-25 01:57:04 +000072#include "llvm/Transforms/Scalar/GuardWidening.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000073#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000074#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Davide Italiano99223442016-05-13 22:52:35 +000075#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000076#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Davide Italiano1021c682016-05-25 23:38:53 +000077#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000078#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +000079#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000080#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000081#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000082#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruth58dde8c2016-02-26 12:17:54 +000083#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +000084
85using namespace llvm;
86
Chandler Carruth8b5a74192016-02-28 22:16:03 +000087static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
88
Chandler Carruth66445382014-01-11 08:16:35 +000089namespace {
90
Chandler Carruthd8330982014-01-12 09:34:22 +000091/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +000092struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +000093 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +000094 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +000095};
96
Chandler Carruth0b576b32015-01-06 02:50:06 +000097/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +000098class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
99 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000100 static char PassID;
101
102public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000103 struct Result {};
104 Result run(Module &) { return Result(); }
105 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000106};
107
Chandler Carruth572e3402014-04-21 11:12:00 +0000108/// \brief No-op CGSCC pass which does nothing.
109struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000110 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000111 return PreservedAnalyses::all();
112 }
113 static StringRef name() { return "NoOpCGSCCPass"; }
114};
115
Chandler Carruth0b576b32015-01-06 02:50:06 +0000116/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000117class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
118 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000119 static char PassID;
120
121public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000122 struct Result {};
123 Result run(LazyCallGraph::SCC &) { return Result(); }
124 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000125};
126
Chandler Carruthd8330982014-01-12 09:34:22 +0000127/// \brief No-op function pass which does nothing.
128struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000129 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000130 static StringRef name() { return "NoOpFunctionPass"; }
131};
132
Chandler Carruth0b576b32015-01-06 02:50:06 +0000133/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000134class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
135 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000136 static char PassID;
137
138public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000139 struct Result {};
140 Result run(Function &) { return Result(); }
141 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000142};
143
Justin Bognereecc3c82016-02-25 07:23:08 +0000144/// \brief No-op loop pass which does nothing.
145struct NoOpLoopPass {
146 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
147 static StringRef name() { return "NoOpLoopPass"; }
148};
149
150/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000151class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
152 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000153 static char PassID;
154
155public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000156 struct Result {};
157 Result run(Loop &) { return Result(); }
158 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000159};
160
Chandler Carruthb4faf132016-03-11 10:22:49 +0000161char NoOpModuleAnalysis::PassID;
162char NoOpCGSCCAnalysis::PassID;
163char NoOpFunctionAnalysis::PassID;
164char NoOpLoopAnalysis::PassID;
165
Chandler Carruth66445382014-01-11 08:16:35 +0000166} // End anonymous namespace.
167
Chandler Carruth1ff77242015-03-07 09:02:36 +0000168void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000169#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000170 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000171#include "PassRegistry.def"
172}
173
Chandler Carruth1ff77242015-03-07 09:02:36 +0000174void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000175#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000176 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000177#include "PassRegistry.def"
178}
179
Chandler Carruth1ff77242015-03-07 09:02:36 +0000180void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000181#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000182 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000183#include "PassRegistry.def"
184}
185
Justin Bognereecc3c82016-02-25 07:23:08 +0000186void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
187#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
188 LAM.registerPass([&] { return CREATE_PASS; });
189#include "PassRegistry.def"
190}
191
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000192void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
193 OptimizationLevel Level,
194 bool DebugLogging) {
195 // FIXME: Finish fleshing this out to match the legacy pipelines.
196 FunctionPassManager EarlyFPM(DebugLogging);
197 EarlyFPM.addPass(SimplifyCFGPass());
198 EarlyFPM.addPass(SROA());
199 EarlyFPM.addPass(EarlyCSEPass());
200 EarlyFPM.addPass(LowerExpectIntrinsicPass());
201
202 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
203}
204
205void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
206 OptimizationLevel Level,
207 bool DebugLogging) {
208 // FIXME: We should use a customized pre-link pipeline!
209 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
210}
211
212void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
213 OptimizationLevel Level,
214 bool DebugLogging) {
215 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
216 FunctionPassManager LateFPM(DebugLogging);
217 LateFPM.addPass(InstCombinePass());
218 LateFPM.addPass(SimplifyCFGPass());
219
220 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
221}
222
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000223#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000224static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000225 // Manually handle aliases for pre-configured pipeline fragments.
226 if (Name.startswith("default") || Name.startswith("lto"))
227 return DefaultAliasRegex.match(Name);
228
Chandler Carruth58944182014-04-21 08:08:50 +0000229#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000230#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000231 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000232 return true;
233#include "PassRegistry.def"
234
Chandler Carruth66445382014-01-11 08:16:35 +0000235 return false;
236}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000237#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000238
Chandler Carruth572e3402014-04-21 11:12:00 +0000239static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000240#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000241#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000242 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000243 return true;
244#include "PassRegistry.def"
245
Chandler Carruth572e3402014-04-21 11:12:00 +0000246 return false;
247}
248
Chandler Carruthd8330982014-01-12 09:34:22 +0000249static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000250#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000251#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000252 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000253 return true;
254#include "PassRegistry.def"
255
Chandler Carruthd8330982014-01-12 09:34:22 +0000256 return false;
257}
258
Justin Bognereecc3c82016-02-25 07:23:08 +0000259static bool isLoopPassName(StringRef Name) {
260#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
261#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
262 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
263 return true;
264#include "PassRegistry.def"
265
266 return false;
267}
268
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000269bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
270 bool DebugLogging) {
271 // Manually handle aliases for pre-configured pipeline fragments.
272 if (Name.startswith("default") || Name.startswith("lto")) {
273 SmallVector<StringRef, 3> Matches;
274 if (!DefaultAliasRegex.match(Name, &Matches))
275 return false;
276 assert(Matches.size() == 3 && "Must capture two matched strings!");
277
278 auto L = StringSwitch<OptimizationLevel>(Matches[2])
279 .Case("O0", O0)
280 .Case("O1", O1)
281 .Case("O2", O2)
282 .Case("O3", O3)
283 .Case("Os", Os)
284 .Case("Oz", Oz);
285
286 if (Matches[1] == "default") {
287 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
288 } else if (Matches[1] == "lto-pre-link") {
289 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
290 } else {
291 assert(Matches[1] == "lto" && "Not one of the matched options!");
292 addLTODefaultPipeline(MPM, L, DebugLogging);
293 }
294 return true;
295 }
296
Chandler Carruth58944182014-04-21 08:08:50 +0000297#define MODULE_PASS(NAME, CREATE_PASS) \
298 if (Name == NAME) { \
299 MPM.addPass(CREATE_PASS); \
300 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000301 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000302#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
303 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000304 MPM.addPass(RequireAnalysisPass< \
305 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000306 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000307 } \
308 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000309 MPM.addPass(InvalidateAnalysisPass< \
310 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000311 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000312 }
313#include "PassRegistry.def"
314
Chandler Carruth66445382014-01-11 08:16:35 +0000315 return false;
316}
317
Chandler Carruth1ff77242015-03-07 09:02:36 +0000318bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000319#define CGSCC_PASS(NAME, CREATE_PASS) \
320 if (Name == NAME) { \
321 CGPM.addPass(CREATE_PASS); \
322 return true; \
323 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000324#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
325 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000326 CGPM.addPass(RequireAnalysisPass< \
327 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000328 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000329 } \
330 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000331 CGPM.addPass(InvalidateAnalysisPass< \
332 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000333 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000334 }
335#include "PassRegistry.def"
336
Chandler Carruth572e3402014-04-21 11:12:00 +0000337 return false;
338}
339
Chandler Carruth1ff77242015-03-07 09:02:36 +0000340bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
341 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000342#define FUNCTION_PASS(NAME, CREATE_PASS) \
343 if (Name == NAME) { \
344 FPM.addPass(CREATE_PASS); \
345 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000346 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000347#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
348 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000349 FPM.addPass(RequireAnalysisPass< \
350 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000351 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000352 } \
353 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000354 FPM.addPass(InvalidateAnalysisPass< \
355 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000356 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000357 }
358#include "PassRegistry.def"
359
Chandler Carruthd8330982014-01-12 09:34:22 +0000360 return false;
361}
362
Justin Bognereecc3c82016-02-25 07:23:08 +0000363bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
364 StringRef Name) {
365#define LOOP_PASS(NAME, CREATE_PASS) \
366 if (Name == NAME) { \
367 FPM.addPass(CREATE_PASS); \
368 return true; \
369 }
370#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
371 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000372 FPM.addPass(RequireAnalysisPass< \
373 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000374 return true; \
375 } \
376 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000377 FPM.addPass(InvalidateAnalysisPass< \
378 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000379 return true; \
380 }
381#include "PassRegistry.def"
382
383 return false;
384}
385
Chandler Carruthedf59962016-02-18 09:45:17 +0000386bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000387#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
388 if (Name == NAME) { \
389 AA.registerModuleAnalysis< \
390 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
391 return true; \
392 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000393#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
394 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000395 AA.registerFunctionAnalysis< \
396 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000397 return true; \
398 }
399#include "PassRegistry.def"
400
401 return false;
402}
403
Justin Bognereecc3c82016-02-25 07:23:08 +0000404bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
405 StringRef &PipelineText,
406 bool VerifyEachPass,
407 bool DebugLogging) {
408 for (;;) {
409 // Parse nested pass managers by recursing.
410 if (PipelineText.startswith("loop(")) {
411 LoopPassManager NestedLPM(DebugLogging);
412
413 // Parse the inner pipeline inte the nested manager.
414 PipelineText = PipelineText.substr(strlen("loop("));
415 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
416 DebugLogging) ||
417 PipelineText.empty())
418 return false;
419 assert(PipelineText[0] == ')');
420 PipelineText = PipelineText.substr(1);
421
422 // Add the nested pass manager with the appropriate adaptor.
423 LPM.addPass(std::move(NestedLPM));
424 } else {
425 // Otherwise try to parse a pass name.
426 size_t End = PipelineText.find_first_of(",)");
427 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
428 return false;
429 // TODO: Ideally, we would run a LoopVerifierPass() here in the
430 // VerifyEachPass case, but we don't have such a verifier yet.
431
432 PipelineText = PipelineText.substr(End);
433 }
434
435 if (PipelineText.empty() || PipelineText[0] == ')')
436 return true;
437
438 assert(PipelineText[0] == ',');
439 PipelineText = PipelineText.substr(1);
440 }
441}
442
Chandler Carruth1ff77242015-03-07 09:02:36 +0000443bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
444 StringRef &PipelineText,
445 bool VerifyEachPass,
446 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000447 for (;;) {
448 // Parse nested pass managers by recursing.
449 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000450 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000451
452 // Parse the inner pipeline inte the nested manager.
453 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000454 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
455 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000456 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000457 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000458 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000459 PipelineText = PipelineText.substr(1);
460
461 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000462 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000463 } else if (PipelineText.startswith("loop(")) {
464 LoopPassManager NestedLPM(DebugLogging);
465
466 // Parse the inner pipeline inte the nested manager.
467 PipelineText = PipelineText.substr(strlen("loop("));
468 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
469 DebugLogging) ||
470 PipelineText.empty())
471 return false;
472 assert(PipelineText[0] == ')');
473 PipelineText = PipelineText.substr(1);
474
475 // Add the nested pass manager with the appropriate adaptor.
476 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000477 } else {
478 // Otherwise try to parse a pass name.
479 size_t End = PipelineText.find_first_of(",)");
480 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
481 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000482 if (VerifyEachPass)
483 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000484
485 PipelineText = PipelineText.substr(End);
486 }
487
488 if (PipelineText.empty() || PipelineText[0] == ')')
489 return true;
490
491 assert(PipelineText[0] == ',');
492 PipelineText = PipelineText.substr(1);
493 }
494}
495
Chandler Carruth1ff77242015-03-07 09:02:36 +0000496bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
497 StringRef &PipelineText,
498 bool VerifyEachPass,
499 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000500 for (;;) {
501 // Parse nested pass managers by recursing.
502 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000503 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000504
505 // Parse the inner pipeline into the nested manager.
506 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000507 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
508 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000509 PipelineText.empty())
510 return false;
511 assert(PipelineText[0] == ')');
512 PipelineText = PipelineText.substr(1);
513
514 // Add the nested pass manager with the appropriate adaptor.
515 CGPM.addPass(std::move(NestedCGPM));
516 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000517 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000518
519 // Parse the inner pipeline inte the nested manager.
520 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000521 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
522 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000523 PipelineText.empty())
524 return false;
525 assert(PipelineText[0] == ')');
526 PipelineText = PipelineText.substr(1);
527
528 // Add the nested pass manager with the appropriate adaptor.
529 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
530 } else {
531 // Otherwise try to parse a pass name.
532 size_t End = PipelineText.find_first_of(",)");
533 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
534 return false;
535 // FIXME: No verifier support for CGSCC passes!
536
537 PipelineText = PipelineText.substr(End);
538 }
539
540 if (PipelineText.empty() || PipelineText[0] == ')')
541 return true;
542
543 assert(PipelineText[0] == ',');
544 PipelineText = PipelineText.substr(1);
545 }
546}
547
NAKAMURA Takumia42f5282016-05-16 10:13:37 +0000548void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
549 FunctionAnalysisManager &FAM,
550 CGSCCAnalysisManager &CGAM,
551 ModuleAnalysisManager &MAM) {
552 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
553 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
554 CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
555 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
556 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
557 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
558 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
559 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
560}
561
Chandler Carruth1ff77242015-03-07 09:02:36 +0000562bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
563 StringRef &PipelineText,
564 bool VerifyEachPass,
565 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000566 for (;;) {
567 // Parse nested pass managers by recursing.
568 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000569 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000570
571 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000572 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000573 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
574 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000575 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000576 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000577 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000578 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000579
580 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000581 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000582 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000583 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000584
585 // Parse the inner pipeline inte the nested manager.
586 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000587 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
588 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000589 PipelineText.empty())
590 return false;
591 assert(PipelineText[0] == ')');
592 PipelineText = PipelineText.substr(1);
593
594 // Add the nested pass manager with the appropriate adaptor.
595 MPM.addPass(
596 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000597 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000598 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000599
600 // Parse the inner pipeline inte the nested manager.
601 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000602 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
603 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000604 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000605 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000606 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000607 PipelineText = PipelineText.substr(1);
608
609 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000610 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000611 } else {
612 // Otherwise try to parse a pass name.
613 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000614 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000615 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000616 if (VerifyEachPass)
617 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000618
619 PipelineText = PipelineText.substr(End);
620 }
621
622 if (PipelineText.empty() || PipelineText[0] == ')')
623 return true;
624
625 assert(PipelineText[0] == ',');
626 PipelineText = PipelineText.substr(1);
627 }
628}
629
630// Primary pass pipeline description parsing routine.
631// FIXME: Should this routine accept a TargetMachine or require the caller to
632// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000633bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
634 StringRef PipelineText, bool VerifyEachPass,
635 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000636 // By default, try to parse the pipeline as-if it were within an implicit
637 // 'module(...)' pass pipeline. If this will parse at all, it needs to
638 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000639 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000640 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000641
Chandler Carruthea368f12015-01-06 08:37:58 +0000642 // This isn't parsable as a module pipeline, look for the end of a pass name
643 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000644 StringRef FirstName =
645 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000646 assert(!isModulePassName(FirstName) &&
647 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000648
Chandler Carruthea368f12015-01-06 08:37:58 +0000649 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
650 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000651 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000652 CGSCCPassManager CGPM(DebugLogging);
653 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
654 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000655 !PipelineText.empty())
656 return false;
657 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
658 return true;
659 }
660
Chandler Carruthea368f12015-01-06 08:37:58 +0000661 // Similarly, if this looks like a Function pass, parse the whole thing as
662 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000663 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000664 FunctionPassManager FPM(DebugLogging);
665 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
666 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000667 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000668 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000669 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000670 return true;
671 }
Chandler Carruth66445382014-01-11 08:16:35 +0000672
Justin Bognereecc3c82016-02-25 07:23:08 +0000673 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
674 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
675 LoopPassManager LPM(DebugLogging);
676 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
677 DebugLogging) ||
678 !PipelineText.empty())
679 return false;
680 FunctionPassManager FPM(DebugLogging);
681 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
682 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
683 return true;
684 }
685
686
Chandler Carruth66445382014-01-11 08:16:35 +0000687 return false;
688}
Chandler Carruthedf59962016-02-18 09:45:17 +0000689
690bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
691 while (!PipelineText.empty()) {
692 StringRef Name;
693 std::tie(Name, PipelineText) = PipelineText.split(',');
694 if (!parseAAPassName(AA, Name))
695 return false;
696 }
697
698 return true;
699}