blob: fff2c0152f27bc8b901398e1cde3363618a94a0a [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"
Sean Silva687019f2016-06-13 22:01:25 +000035#include "llvm/Analysis/LazyValueInfo.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000036#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000037#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000038#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000039#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000040#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000041#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000042#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000043#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000044#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000045#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000046#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000047#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000048#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000049#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000050#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000051#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000052#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000053#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000054#include "llvm/Transforms/GCOVProfiler.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000055#include "llvm/Transforms/IPO/ConstantMerge.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000056#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000057#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000058#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000059#include "llvm/Transforms/IPO/FunctionAttrs.h"
Davide Italiano66228c42016-05-03 19:39:15 +000060#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000061#include "llvm/Transforms/IPO/GlobalOpt.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000062#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Justin Bogner4563a062016-04-26 20:15:52 +000063#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000064#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000065#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000066#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000067#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000068#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000069#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000070#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000071#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000072#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000073#include "llvm/Transforms/Scalar/BDCE.h"
Justin Bogner395c2122016-04-22 19:40:41 +000074#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000075#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000076#include "llvm/Transforms/Scalar/EarlyCSE.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000077#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000078#include "llvm/Transforms/Scalar/GuardWidening.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +000079#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +000080#include "llvm/Transforms/Scalar/JumpThreading.h"
Justin Bognerd0d23412016-05-03 22:02:31 +000081#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +000082#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Davide Italiano99223442016-05-13 22:52:35 +000083#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +000084#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +000085#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000086#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Davide Italiano1021c682016-05-25 23:38:53 +000087#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +000088#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +000089#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +000090#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000091#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +000092#include "llvm/Transforms/Scalar/Sink.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +000093#include "llvm/Transforms/Utils/AddDiscriminators.h"
Sean Silvae0a9e662016-06-15 08:43:40 +000094#include "llvm/Transforms/Scalar/SLPVectorizer.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +000095#include "llvm/Transforms/Utils/LCSSA.h"
Davide Italianocccf4f02016-06-14 03:22:22 +000096#include "llvm/Transforms/Utils/Mem2Reg.h"
Geoff Berryb96d3b22016-06-01 21:30:40 +000097#include "llvm/Transforms/Utils/MemorySSA.h"
Davide Italianocccf4f02016-06-14 03:22:22 +000098
Chandler Carruth58dde8c2016-02-26 12:17:54 +000099#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000100
101using namespace llvm;
102
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000103static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
104
Chandler Carruth66445382014-01-11 08:16:35 +0000105namespace {
106
Chandler Carruthd8330982014-01-12 09:34:22 +0000107/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000108struct NoOpModulePass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000109 PreservedAnalyses run(Module &M) { return PreservedAnalyses::all(); }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000110 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000111};
112
Chandler Carruth0b576b32015-01-06 02:50:06 +0000113/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000114class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
115 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000116 static char PassID;
117
118public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000119 struct Result {};
120 Result run(Module &) { return Result(); }
121 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000122};
123
Chandler Carruth572e3402014-04-21 11:12:00 +0000124/// \brief No-op CGSCC pass which does nothing.
125struct NoOpCGSCCPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000126 PreservedAnalyses run(LazyCallGraph::SCC &C) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000127 return PreservedAnalyses::all();
128 }
129 static StringRef name() { return "NoOpCGSCCPass"; }
130};
131
Chandler Carruth0b576b32015-01-06 02:50:06 +0000132/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000133class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
134 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000135 static char PassID;
136
137public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000138 struct Result {};
139 Result run(LazyCallGraph::SCC &) { return Result(); }
140 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000141};
142
Chandler Carruthd8330982014-01-12 09:34:22 +0000143/// \brief No-op function pass which does nothing.
144struct NoOpFunctionPass {
Chandler Carruthd174ce42015-01-05 02:47:05 +0000145 PreservedAnalyses run(Function &F) { return PreservedAnalyses::all(); }
Chandler Carruthd8330982014-01-12 09:34:22 +0000146 static StringRef name() { return "NoOpFunctionPass"; }
147};
148
Chandler Carruth0b576b32015-01-06 02:50:06 +0000149/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000150class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
151 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000152 static char PassID;
153
154public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000155 struct Result {};
156 Result run(Function &) { return Result(); }
157 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000158};
159
Justin Bognereecc3c82016-02-25 07:23:08 +0000160/// \brief No-op loop pass which does nothing.
161struct NoOpLoopPass {
162 PreservedAnalyses run(Loop &L) { return PreservedAnalyses::all(); }
163 static StringRef name() { return "NoOpLoopPass"; }
164};
165
166/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000167class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
168 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000169 static char PassID;
170
171public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000172 struct Result {};
173 Result run(Loop &) { return Result(); }
174 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000175};
176
Chandler Carruthb4faf132016-03-11 10:22:49 +0000177char NoOpModuleAnalysis::PassID;
178char NoOpCGSCCAnalysis::PassID;
179char NoOpFunctionAnalysis::PassID;
180char NoOpLoopAnalysis::PassID;
181
Chandler Carruth66445382014-01-11 08:16:35 +0000182} // End anonymous namespace.
183
Chandler Carruth1ff77242015-03-07 09:02:36 +0000184void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000185#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000186 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000187#include "PassRegistry.def"
188}
189
Chandler Carruth1ff77242015-03-07 09:02:36 +0000190void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000191#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000192 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000193#include "PassRegistry.def"
194}
195
Chandler Carruth1ff77242015-03-07 09:02:36 +0000196void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruthb70f6732015-01-06 02:21:37 +0000197#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000198 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000199#include "PassRegistry.def"
200}
201
Justin Bognereecc3c82016-02-25 07:23:08 +0000202void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
203#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
204 LAM.registerPass([&] { return CREATE_PASS; });
205#include "PassRegistry.def"
206}
207
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000208void PassBuilder::addPerModuleDefaultPipeline(ModulePassManager &MPM,
209 OptimizationLevel Level,
210 bool DebugLogging) {
211 // FIXME: Finish fleshing this out to match the legacy pipelines.
212 FunctionPassManager EarlyFPM(DebugLogging);
213 EarlyFPM.addPass(SimplifyCFGPass());
214 EarlyFPM.addPass(SROA());
215 EarlyFPM.addPass(EarlyCSEPass());
216 EarlyFPM.addPass(LowerExpectIntrinsicPass());
217
218 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
219}
220
221void PassBuilder::addLTOPreLinkDefaultPipeline(ModulePassManager &MPM,
222 OptimizationLevel Level,
223 bool DebugLogging) {
224 // FIXME: We should use a customized pre-link pipeline!
225 addPerModuleDefaultPipeline(MPM, Level, DebugLogging);
226}
227
228void PassBuilder::addLTODefaultPipeline(ModulePassManager &MPM,
229 OptimizationLevel Level,
230 bool DebugLogging) {
231 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
232 FunctionPassManager LateFPM(DebugLogging);
233 LateFPM.addPass(InstCombinePass());
234 LateFPM.addPass(SimplifyCFGPass());
235
236 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
237}
238
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000239#ifndef NDEBUG
Chandler Carruth66445382014-01-11 08:16:35 +0000240static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000241 // Manually handle aliases for pre-configured pipeline fragments.
242 if (Name.startswith("default") || Name.startswith("lto"))
243 return DefaultAliasRegex.match(Name);
244
Chandler Carruth58944182014-04-21 08:08:50 +0000245#define MODULE_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000246#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000247 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000248 return true;
249#include "PassRegistry.def"
250
Chandler Carruth66445382014-01-11 08:16:35 +0000251 return false;
252}
Chandler Carruth9d2e58f2015-01-06 09:10:47 +0000253#endif
Chandler Carruth66445382014-01-11 08:16:35 +0000254
Chandler Carruth572e3402014-04-21 11:12:00 +0000255static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000256#define CGSCC_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000257#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000258 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000259 return true;
260#include "PassRegistry.def"
261
Chandler Carruth572e3402014-04-21 11:12:00 +0000262 return false;
263}
264
Chandler Carruthd8330982014-01-12 09:34:22 +0000265static bool isFunctionPassName(StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000266#define FUNCTION_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000267#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000268 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000269 return true;
270#include "PassRegistry.def"
271
Chandler Carruthd8330982014-01-12 09:34:22 +0000272 return false;
273}
274
Justin Bognereecc3c82016-02-25 07:23:08 +0000275static bool isLoopPassName(StringRef Name) {
276#define LOOP_PASS(NAME, CREATE_PASS) if (Name == NAME) return true;
277#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
278 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
279 return true;
280#include "PassRegistry.def"
281
282 return false;
283}
284
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000285bool PassBuilder::parseModulePassName(ModulePassManager &MPM, StringRef Name,
286 bool DebugLogging) {
287 // Manually handle aliases for pre-configured pipeline fragments.
288 if (Name.startswith("default") || Name.startswith("lto")) {
289 SmallVector<StringRef, 3> Matches;
290 if (!DefaultAliasRegex.match(Name, &Matches))
291 return false;
292 assert(Matches.size() == 3 && "Must capture two matched strings!");
293
294 auto L = StringSwitch<OptimizationLevel>(Matches[2])
295 .Case("O0", O0)
296 .Case("O1", O1)
297 .Case("O2", O2)
298 .Case("O3", O3)
299 .Case("Os", Os)
300 .Case("Oz", Oz);
301
302 if (Matches[1] == "default") {
303 addPerModuleDefaultPipeline(MPM, L, DebugLogging);
304 } else if (Matches[1] == "lto-pre-link") {
305 addLTOPreLinkDefaultPipeline(MPM, L, DebugLogging);
306 } else {
307 assert(Matches[1] == "lto" && "Not one of the matched options!");
308 addLTODefaultPipeline(MPM, L, DebugLogging);
309 }
310 return true;
311 }
312
Chandler Carruth58944182014-04-21 08:08:50 +0000313#define MODULE_PASS(NAME, CREATE_PASS) \
314 if (Name == NAME) { \
315 MPM.addPass(CREATE_PASS); \
316 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000317 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000318#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
319 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000320 MPM.addPass(RequireAnalysisPass< \
321 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000322 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000323 } \
324 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000325 MPM.addPass(InvalidateAnalysisPass< \
326 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000327 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000328 }
329#include "PassRegistry.def"
330
Chandler Carruth66445382014-01-11 08:16:35 +0000331 return false;
332}
333
Chandler Carruth1ff77242015-03-07 09:02:36 +0000334bool PassBuilder::parseCGSCCPassName(CGSCCPassManager &CGPM, StringRef Name) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000335#define CGSCC_PASS(NAME, CREATE_PASS) \
336 if (Name == NAME) { \
337 CGPM.addPass(CREATE_PASS); \
338 return true; \
339 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000340#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
341 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000342 CGPM.addPass(RequireAnalysisPass< \
343 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000344 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000345 } \
346 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000347 CGPM.addPass(InvalidateAnalysisPass< \
348 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000349 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000350 }
351#include "PassRegistry.def"
352
Chandler Carruth572e3402014-04-21 11:12:00 +0000353 return false;
354}
355
Chandler Carruth1ff77242015-03-07 09:02:36 +0000356bool PassBuilder::parseFunctionPassName(FunctionPassManager &FPM,
357 StringRef Name) {
Chandler Carruth58944182014-04-21 08:08:50 +0000358#define FUNCTION_PASS(NAME, CREATE_PASS) \
359 if (Name == NAME) { \
360 FPM.addPass(CREATE_PASS); \
361 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000362 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000363#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
364 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000365 FPM.addPass(RequireAnalysisPass< \
366 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000367 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000368 } \
369 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000370 FPM.addPass(InvalidateAnalysisPass< \
371 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000372 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000373 }
374#include "PassRegistry.def"
375
Chandler Carruthd8330982014-01-12 09:34:22 +0000376 return false;
377}
378
Justin Bognereecc3c82016-02-25 07:23:08 +0000379bool PassBuilder::parseLoopPassName(LoopPassManager &FPM,
380 StringRef Name) {
381#define LOOP_PASS(NAME, CREATE_PASS) \
382 if (Name == NAME) { \
383 FPM.addPass(CREATE_PASS); \
384 return true; \
385 }
386#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
387 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000388 FPM.addPass(RequireAnalysisPass< \
389 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000390 return true; \
391 } \
392 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000393 FPM.addPass(InvalidateAnalysisPass< \
394 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000395 return true; \
396 }
397#include "PassRegistry.def"
398
399 return false;
400}
401
Chandler Carruthedf59962016-02-18 09:45:17 +0000402bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000403#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
404 if (Name == NAME) { \
405 AA.registerModuleAnalysis< \
406 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
407 return true; \
408 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000409#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
410 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000411 AA.registerFunctionAnalysis< \
412 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000413 return true; \
414 }
415#include "PassRegistry.def"
416
417 return false;
418}
419
Justin Bognereecc3c82016-02-25 07:23:08 +0000420bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
421 StringRef &PipelineText,
422 bool VerifyEachPass,
423 bool DebugLogging) {
424 for (;;) {
425 // Parse nested pass managers by recursing.
426 if (PipelineText.startswith("loop(")) {
427 LoopPassManager NestedLPM(DebugLogging);
428
429 // Parse the inner pipeline inte the nested manager.
430 PipelineText = PipelineText.substr(strlen("loop("));
431 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
432 DebugLogging) ||
433 PipelineText.empty())
434 return false;
435 assert(PipelineText[0] == ')');
436 PipelineText = PipelineText.substr(1);
437
438 // Add the nested pass manager with the appropriate adaptor.
439 LPM.addPass(std::move(NestedLPM));
440 } else {
441 // Otherwise try to parse a pass name.
442 size_t End = PipelineText.find_first_of(",)");
443 if (!parseLoopPassName(LPM, PipelineText.substr(0, End)))
444 return false;
445 // TODO: Ideally, we would run a LoopVerifierPass() here in the
446 // VerifyEachPass case, but we don't have such a verifier yet.
447
448 PipelineText = PipelineText.substr(End);
449 }
450
451 if (PipelineText.empty() || PipelineText[0] == ')')
452 return true;
453
454 assert(PipelineText[0] == ',');
455 PipelineText = PipelineText.substr(1);
456 }
457}
458
Chandler Carruth1ff77242015-03-07 09:02:36 +0000459bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
460 StringRef &PipelineText,
461 bool VerifyEachPass,
462 bool DebugLogging) {
Chandler Carruthd8330982014-01-12 09:34:22 +0000463 for (;;) {
464 // Parse nested pass managers by recursing.
465 if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000466 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000467
468 // Parse the inner pipeline inte the nested manager.
469 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000470 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
471 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000472 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000473 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000474 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000475 PipelineText = PipelineText.substr(1);
476
477 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000478 FPM.addPass(std::move(NestedFPM));
Justin Bognereecc3c82016-02-25 07:23:08 +0000479 } else if (PipelineText.startswith("loop(")) {
480 LoopPassManager NestedLPM(DebugLogging);
481
482 // Parse the inner pipeline inte the nested manager.
483 PipelineText = PipelineText.substr(strlen("loop("));
484 if (!parseLoopPassPipeline(NestedLPM, PipelineText, VerifyEachPass,
485 DebugLogging) ||
486 PipelineText.empty())
487 return false;
488 assert(PipelineText[0] == ')');
489 PipelineText = PipelineText.substr(1);
490
491 // Add the nested pass manager with the appropriate adaptor.
492 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(NestedLPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000493 } else {
494 // Otherwise try to parse a pass name.
495 size_t End = PipelineText.find_first_of(",)");
496 if (!parseFunctionPassName(FPM, PipelineText.substr(0, End)))
497 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000498 if (VerifyEachPass)
499 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +0000500
501 PipelineText = PipelineText.substr(End);
502 }
503
504 if (PipelineText.empty() || PipelineText[0] == ')')
505 return true;
506
507 assert(PipelineText[0] == ',');
508 PipelineText = PipelineText.substr(1);
509 }
510}
511
Chandler Carruth1ff77242015-03-07 09:02:36 +0000512bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
513 StringRef &PipelineText,
514 bool VerifyEachPass,
515 bool DebugLogging) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000516 for (;;) {
517 // Parse nested pass managers by recursing.
518 if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000519 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000520
521 // Parse the inner pipeline into the nested manager.
522 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000523 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
524 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000525 PipelineText.empty())
526 return false;
527 assert(PipelineText[0] == ')');
528 PipelineText = PipelineText.substr(1);
529
530 // Add the nested pass manager with the appropriate adaptor.
531 CGPM.addPass(std::move(NestedCGPM));
532 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000533 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000534
535 // Parse the inner pipeline inte the nested manager.
536 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000537 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
538 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000539 PipelineText.empty())
540 return false;
541 assert(PipelineText[0] == ')');
542 PipelineText = PipelineText.substr(1);
543
544 // Add the nested pass manager with the appropriate adaptor.
545 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(NestedFPM)));
546 } else {
547 // Otherwise try to parse a pass name.
548 size_t End = PipelineText.find_first_of(",)");
549 if (!parseCGSCCPassName(CGPM, PipelineText.substr(0, End)))
550 return false;
551 // FIXME: No verifier support for CGSCC passes!
552
553 PipelineText = PipelineText.substr(End);
554 }
555
556 if (PipelineText.empty() || PipelineText[0] == ')')
557 return true;
558
559 assert(PipelineText[0] == ',');
560 PipelineText = PipelineText.substr(1);
561 }
562}
563
NAKAMURA Takumia42f5282016-05-16 10:13:37 +0000564void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
565 FunctionAnalysisManager &FAM,
566 CGSCCAnalysisManager &CGAM,
567 ModuleAnalysisManager &MAM) {
568 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
569 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
570 CGAM.registerPass([&] { return FunctionAnalysisManagerCGSCCProxy(FAM); });
571 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
572 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
573 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
574 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
575 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
576}
577
Chandler Carruth1ff77242015-03-07 09:02:36 +0000578bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
579 StringRef &PipelineText,
580 bool VerifyEachPass,
581 bool DebugLogging) {
Chandler Carruth66445382014-01-11 08:16:35 +0000582 for (;;) {
583 // Parse nested pass managers by recursing.
584 if (PipelineText.startswith("module(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000585 ModulePassManager NestedMPM(DebugLogging);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000586
587 // Parse the inner pipeline into the nested manager.
Chandler Carruth66445382014-01-11 08:16:35 +0000588 PipelineText = PipelineText.substr(strlen("module("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000589 if (!parseModulePassPipeline(NestedMPM, PipelineText, VerifyEachPass,
590 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000591 PipelineText.empty())
Chandler Carruth66445382014-01-11 08:16:35 +0000592 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000593 assert(PipelineText[0] == ')');
Chandler Carruth66445382014-01-11 08:16:35 +0000594 PipelineText = PipelineText.substr(1);
Chandler Carruth258dbb32014-01-11 12:06:47 +0000595
596 // Now add the nested manager as a module pass.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000597 MPM.addPass(std::move(NestedMPM));
Chandler Carruth572e3402014-04-21 11:12:00 +0000598 } else if (PipelineText.startswith("cgscc(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000599 CGSCCPassManager NestedCGPM(DebugLogging);
Chandler Carruth572e3402014-04-21 11:12:00 +0000600
601 // Parse the inner pipeline inte the nested manager.
602 PipelineText = PipelineText.substr(strlen("cgscc("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000603 if (!parseCGSCCPassPipeline(NestedCGPM, PipelineText, VerifyEachPass,
604 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000605 PipelineText.empty())
606 return false;
607 assert(PipelineText[0] == ')');
608 PipelineText = PipelineText.substr(1);
609
610 // Add the nested pass manager with the appropriate adaptor.
611 MPM.addPass(
612 createModuleToPostOrderCGSCCPassAdaptor(std::move(NestedCGPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000613 } else if (PipelineText.startswith("function(")) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000614 FunctionPassManager NestedFPM(DebugLogging);
Chandler Carruthd8330982014-01-12 09:34:22 +0000615
616 // Parse the inner pipeline inte the nested manager.
617 PipelineText = PipelineText.substr(strlen("function("));
Chandler Carruth14a759e2015-01-13 22:42:38 +0000618 if (!parseFunctionPassPipeline(NestedFPM, PipelineText, VerifyEachPass,
619 DebugLogging) ||
Chandler Carruth6546cb62014-01-12 10:02:02 +0000620 PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000621 return false;
Chandler Carruth6546cb62014-01-12 10:02:02 +0000622 assert(PipelineText[0] == ')');
Chandler Carruthd8330982014-01-12 09:34:22 +0000623 PipelineText = PipelineText.substr(1);
624
625 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000626 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(NestedFPM)));
Chandler Carruth66445382014-01-11 08:16:35 +0000627 } else {
628 // Otherwise try to parse a pass name.
629 size_t End = PipelineText.find_first_of(",)");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000630 if (!parseModulePassName(MPM, PipelineText.substr(0, End), DebugLogging))
Chandler Carruth66445382014-01-11 08:16:35 +0000631 return false;
Chandler Carruth4d356312014-01-20 11:34:08 +0000632 if (VerifyEachPass)
633 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +0000634
635 PipelineText = PipelineText.substr(End);
636 }
637
638 if (PipelineText.empty() || PipelineText[0] == ')')
639 return true;
640
641 assert(PipelineText[0] == ',');
642 PipelineText = PipelineText.substr(1);
643 }
644}
645
646// Primary pass pipeline description parsing routine.
647// FIXME: Should this routine accept a TargetMachine or require the caller to
648// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +0000649bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
650 StringRef PipelineText, bool VerifyEachPass,
651 bool DebugLogging) {
Chandler Carruthea368f12015-01-06 08:37:58 +0000652 // By default, try to parse the pipeline as-if it were within an implicit
653 // 'module(...)' pass pipeline. If this will parse at all, it needs to
654 // consume the entire string.
Chandler Carruth14a759e2015-01-13 22:42:38 +0000655 if (parseModulePassPipeline(MPM, PipelineText, VerifyEachPass, DebugLogging))
Chandler Carruthea368f12015-01-06 08:37:58 +0000656 return PipelineText.empty();
Chandler Carruth66445382014-01-11 08:16:35 +0000657
Chandler Carruthea368f12015-01-06 08:37:58 +0000658 // This isn't parsable as a module pipeline, look for the end of a pass name
659 // and directly drop down to that layer.
Chandler Carruth6546cb62014-01-12 10:02:02 +0000660 StringRef FirstName =
661 PipelineText.substr(0, PipelineText.find_first_of(",)"));
Chandler Carruthea368f12015-01-06 08:37:58 +0000662 assert(!isModulePassName(FirstName) &&
663 "Already handled all module pipeline options.");
Chandler Carruth66445382014-01-11 08:16:35 +0000664
Chandler Carruthea368f12015-01-06 08:37:58 +0000665 // If this looks like a CGSCC pass, parse the whole thing as a CGSCC
666 // pipeline.
Justin Bognereecc3c82016-02-25 07:23:08 +0000667 if (PipelineText.startswith("cgscc(") || isCGSCCPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000668 CGSCCPassManager CGPM(DebugLogging);
669 if (!parseCGSCCPassPipeline(CGPM, PipelineText, VerifyEachPass,
670 DebugLogging) ||
Chandler Carruth572e3402014-04-21 11:12:00 +0000671 !PipelineText.empty())
672 return false;
673 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
674 return true;
675 }
676
Chandler Carruthea368f12015-01-06 08:37:58 +0000677 // Similarly, if this looks like a Function pass, parse the whole thing as
678 // a Function pipelien.
Justin Bognereecc3c82016-02-25 07:23:08 +0000679 if (PipelineText.startswith("function(") || isFunctionPassName(FirstName)) {
Chandler Carruth14a759e2015-01-13 22:42:38 +0000680 FunctionPassManager FPM(DebugLogging);
681 if (!parseFunctionPassPipeline(FPM, PipelineText, VerifyEachPass,
682 DebugLogging) ||
Chandler Carruth4d356312014-01-20 11:34:08 +0000683 !PipelineText.empty())
Chandler Carruthd8330982014-01-12 09:34:22 +0000684 return false;
Chandler Carruthc3f3da32014-03-09 11:49:53 +0000685 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruthd8330982014-01-12 09:34:22 +0000686 return true;
687 }
Chandler Carruth66445382014-01-11 08:16:35 +0000688
Justin Bognereecc3c82016-02-25 07:23:08 +0000689 // If this looks like a Loop pass, parse the whole thing as a Loop pipeline.
690 if (PipelineText.startswith("loop(") || isLoopPassName(FirstName)) {
691 LoopPassManager LPM(DebugLogging);
692 if (!parseLoopPassPipeline(LPM, PipelineText, VerifyEachPass,
693 DebugLogging) ||
694 !PipelineText.empty())
695 return false;
696 FunctionPassManager FPM(DebugLogging);
697 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
698 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
699 return true;
700 }
701
702
Chandler Carruth66445382014-01-11 08:16:35 +0000703 return false;
704}
Chandler Carruthedf59962016-02-18 09:45:17 +0000705
706bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
707 while (!PipelineText.empty()) {
708 StringRef Name;
709 std::tie(Name, PipelineText) = PipelineText.split(',');
710 if (!parseAAPassName(AA, Name))
711 return false;
712 }
713
714 return true;
715}