blob: fcfe3ce9b35e301386b40b4e88091cef1a4c196d [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"
Daniel Jasperaec2fa32016-12-19 08:22:17 +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"
Mehdi Amini27d23792016-09-16 16:56:30 +000027#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000028#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
29#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000030#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000031#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000032#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000033#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000034#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000035#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000036#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000037#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000038#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000039#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000040#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000041#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet79ac42a2016-07-18 16:29:21 +000043#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000044#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000045#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000046#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000047#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000048#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000049#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000050#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000051#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000052#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000053#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000054#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000055#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000056#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000057#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000058#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000059#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000060#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000061#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000062#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000063#include "llvm/Transforms/IPO/AlwaysInliner.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000064#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000065#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000066#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000067#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000068#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000069#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000070#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000071#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000072#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000073#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000074#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000075#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000076#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000077#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000078#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000079#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000080#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000081#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000082#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000083#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000084#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000085#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000086#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000087#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000088#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000089#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000090#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000091#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000092#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000093#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000094#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000095#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000096#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000097#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +000098#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +000099#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000100#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000101#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000102#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000103#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000104#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000105#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000106#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000107#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000108#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000109#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000110#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000111#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000112#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000113#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000114#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000115#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000116#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000117#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000118#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000119#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000120#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000121#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000122#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000123#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000124#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000125#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000126#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000127#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000128#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000129#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000130#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000131#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000132#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000133#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000134#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000135#include "llvm/Transforms/Utils/Mem2Reg.h"
Geoff Berryb96d3b22016-06-01 21:30:40 +0000136#include "llvm/Transforms/Utils/MemorySSA.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000137#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000138#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000139#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000140#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000141#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000142
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000143#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000144
145using namespace llvm;
146
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000147static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
148
Chandler Carruthe3f50642016-12-22 06:59:15 +0000149static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
150 switch (Level) {
151 case PassBuilder::O0:
152 case PassBuilder::O1:
153 case PassBuilder::O2:
154 case PassBuilder::O3:
155 return false;
156
157 case PassBuilder::Os:
158 case PassBuilder::Oz:
159 return true;
160 }
161 llvm_unreachable("Invalid optimization level!");
162}
163
Chandler Carruth66445382014-01-11 08:16:35 +0000164namespace {
165
Chandler Carruthd8330982014-01-12 09:34:22 +0000166/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000167struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000168 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000169 return PreservedAnalyses::all();
170 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000171 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000172};
173
Chandler Carruth0b576b32015-01-06 02:50:06 +0000174/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000175class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
176 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000177 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000178
179public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000180 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000181 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000182 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000183};
184
Chandler Carruth572e3402014-04-21 11:12:00 +0000185/// \brief No-op CGSCC pass which does nothing.
186struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000187 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
188 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000189 return PreservedAnalyses::all();
190 }
191 static StringRef name() { return "NoOpCGSCCPass"; }
192};
193
Chandler Carruth0b576b32015-01-06 02:50:06 +0000194/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000195class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
196 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000197 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000198
199public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000200 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000201 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000202 return Result();
203 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000204 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000205};
206
Chandler Carruthd8330982014-01-12 09:34:22 +0000207/// \brief No-op function pass which does nothing.
208struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000209 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000210 return PreservedAnalyses::all();
211 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000212 static StringRef name() { return "NoOpFunctionPass"; }
213};
214
Chandler Carruth0b576b32015-01-06 02:50:06 +0000215/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000216class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
217 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000218 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000219
220public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000221 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000222 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000224};
225
Justin Bognereecc3c82016-02-25 07:23:08 +0000226/// \brief No-op loop pass which does nothing.
227struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000228 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
229 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000230 return PreservedAnalyses::all();
231 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000232 static StringRef name() { return "NoOpLoopPass"; }
233};
234
235/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000236class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
237 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000238 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000239
240public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000241 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000242 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
243 return Result();
244 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000245 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000246};
247
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000248AnalysisKey NoOpModuleAnalysis::Key;
249AnalysisKey NoOpCGSCCAnalysis::Key;
250AnalysisKey NoOpFunctionAnalysis::Key;
251AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000252
Chandler Carruth66445382014-01-11 08:16:35 +0000253} // End anonymous namespace.
254
Chandler Carruth1ff77242015-03-07 09:02:36 +0000255void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000256#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000257 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000258#include "PassRegistry.def"
259}
260
Chandler Carruth1ff77242015-03-07 09:02:36 +0000261void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000262#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000263 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000264#include "PassRegistry.def"
265}
266
Chandler Carruth1ff77242015-03-07 09:02:36 +0000267void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000268#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000269 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000270#include "PassRegistry.def"
271}
272
Justin Bognereecc3c82016-02-25 07:23:08 +0000273void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000274#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000275 LAM.registerPass([&] { return CREATE_PASS; });
276#include "PassRegistry.def"
277}
278
Chandler Carruthe3f50642016-12-22 06:59:15 +0000279FunctionPassManager
280PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
281 bool DebugLogging) {
282 assert(Level != O0 && "Must request optimizations!");
283 FunctionPassManager FPM(DebugLogging);
284
285 // Form SSA out of local memory accesses after breaking apart aggregates into
286 // scalars.
287 FPM.addPass(SROA());
288
289 // Catch trivial redundancies
290 FPM.addPass(EarlyCSEPass());
291
292 // Speculative execution if the target has divergent branches; otherwise nop.
293 FPM.addPass(SpeculativeExecutionPass());
294
295 // Optimize based on known information about branches, and cleanup afterward.
296 FPM.addPass(JumpThreadingPass());
297 FPM.addPass(CorrelatedValuePropagationPass());
298 FPM.addPass(SimplifyCFGPass());
299 FPM.addPass(InstCombinePass());
300
301 if (!isOptimizingForSize(Level))
302 FPM.addPass(LibCallsShrinkWrapPass());
303
304 FPM.addPass(TailCallElimPass());
305 FPM.addPass(SimplifyCFGPass());
306
307 // Form canonically associated expression trees, and simplify the trees using
308 // basic mathematical properties. For example, this will form (nearly)
309 // minimal multiplication trees.
310 FPM.addPass(ReassociatePass());
311
312 // Add the primary loop simplification pipeline.
313 // FIXME: Currently this is split into two loop pass pipelines because we run
314 // some function passes in between them. These can and should be replaced by
315 // loop pass equivalenst but those aren't ready yet. Specifically,
316 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
317 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
318 // the other we have is `LoopInstSimplify`.
319 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
320
321 // Rotate Loop - disable header duplication at -Oz
322 LPM1.addPass(LoopRotatePass(Level != Oz));
323 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000324#if 0
325 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000326 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000327#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000328 LPM2.addPass(IndVarSimplifyPass());
329 LPM2.addPass(LoopIdiomRecognizePass());
330 LPM2.addPass(LoopDeletionPass());
331 LPM2.addPass(LoopUnrollPass::createFull());
332
Chandler Carruthe3f50642016-12-22 06:59:15 +0000333 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
334 FPM.addPass(SimplifyCFGPass());
335 FPM.addPass(InstCombinePass());
336 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
337
338 // Eliminate redundancies.
339 if (Level != O1) {
340 // These passes add substantial compile time so skip them at O1.
341 FPM.addPass(MergedLoadStoreMotionPass());
342 FPM.addPass(GVN());
343 }
344
345 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
346 FPM.addPass(MemCpyOptPass());
347
348 // Sparse conditional constant propagation.
349 // FIXME: It isn't clear why we do this *after* loop passes rather than
350 // before...
351 FPM.addPass(SCCPPass());
352
353 // Delete dead bit computations (instcombine runs after to fold away the dead
354 // computations, and then ADCE will run later to exploit any new DCE
355 // opportunities that creates).
356 FPM.addPass(BDCEPass());
357
358 // Run instcombine after redundancy and dead bit elimination to exploit
359 // opportunities opened up by them.
360 FPM.addPass(InstCombinePass());
361
362 // Re-consider control flow based optimizations after redundancy elimination,
363 // redo DCE, etc.
364 FPM.addPass(JumpThreadingPass());
365 FPM.addPass(CorrelatedValuePropagationPass());
366 FPM.addPass(DSEPass());
367 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
368
369 // Finally, do an expensive DCE pass to catch all the dead code exposed by
370 // the simplifications and basic cleanup after all the simplifications.
371 FPM.addPass(ADCEPass());
372 FPM.addPass(SimplifyCFGPass());
373 FPM.addPass(InstCombinePass());
374
375 return FPM;
376}
377
378ModulePassManager
379PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
380 bool DebugLogging) {
381 assert(Level != O0 && "Must request optimizations for the default pipeline!");
382 ModulePassManager MPM(DebugLogging);
383
384 // Force any function attributes we want the rest of the pipeline te observe.
385 MPM.addPass(ForceFunctionAttrsPass());
386
387 // Do basic inference of function attributes from known properties of system
388 // libraries and other oracles.
389 MPM.addPass(InferFunctionAttrsPass());
390
391 // Create an early function pass manager to cleanup the output of the
392 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000393 FunctionPassManager EarlyFPM(DebugLogging);
394 EarlyFPM.addPass(SimplifyCFGPass());
395 EarlyFPM.addPass(SROA());
396 EarlyFPM.addPass(EarlyCSEPass());
397 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000398 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000399 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000400
401 // Interprocedural constant propagation now that basic cleanup has occured
402 // and prior to optimizing globals.
403 // FIXME: This position in the pipeline hasn't been carefully considered in
404 // years, it should be re-analyzed.
405 MPM.addPass(IPSCCPPass());
406
407 // Optimize globals to try and fold them into constants.
408 MPM.addPass(GlobalOptPass());
409
410 // Promote any localized globals to SSA registers.
411 // FIXME: Should this instead by a run of SROA?
412 // FIXME: We should probably run instcombine and simplify-cfg afterward to
413 // delete control flows that are dead once globals have been folded to
414 // constants.
415 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
416
417 // Remove any dead arguments exposed by cleanups and constand folding
418 // globals.
419 MPM.addPass(DeadArgumentEliminationPass());
420
421 // Create a small function pass pipeline to cleanup after all the global
422 // optimizations.
423 FunctionPassManager GlobalCleanupPM(DebugLogging);
424 GlobalCleanupPM.addPass(InstCombinePass());
425 GlobalCleanupPM.addPass(SimplifyCFGPass());
426 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
427
428 // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
429#if 0
430 MPM.addPass(RequireAnalysisPass<GlobalsAA>());
431#endif
432
433 // Now begin the main postorder CGSCC pipeline.
434 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
435 // manager and trying to emulate its precise behavior. Much of this doesn't
436 // make a lot of sense and we should revisit the core CGSCC structure.
437 CGSCCPassManager MainCGPipeline(DebugLogging);
438
439 // Note: historically, the PruneEH pass was run first to deduce nounwind and
440 // generally clean up exception handling overhead. It isn't clear this is
441 // valuable as the inliner doesn't currently care whether it is inlining an
442 // invoke or a call.
443
444 // Run the inliner first. The theory is that we are walking bottom-up and so
445 // the callees have already been fully optimized, and we want to inline them
446 // into the callers so that our optimizations can reflect that.
447 // FIXME; Customize the threshold based on optimization level.
448 MainCGPipeline.addPass(InlinerPass());
449
450 // Now deduce any function attributes based in the current code.
451 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
452
453 // Lastly, add the core function simplification pipeline nested inside the
454 // CGSCC walk.
455 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
456 buildFunctionSimplificationPipeline(Level, DebugLogging)));
457
458 MPM.addPass(
459 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
460
461 // This ends the canonicalization and simplification phase of the pipeline.
462 // At this point, we expect to have canonical and simple IR which we begin
463 // *optimizing* for efficient execution going forward.
464
465 // Eliminate externally available functions now that inlining is over -- we
466 // won't emit these anyways.
467 MPM.addPass(EliminateAvailableExternallyPass());
468
469 // Do RPO function attribute inference across the module to forward-propagate
470 // attributes where applicable.
471 // FIXME: Is this really an optimization rather than a canonicalization?
472 MPM.addPass(ReversePostOrderFunctionAttrsPass());
473
474 // Recompute GloblasAA here prior to function passes. This is particularly
475 // useful as the above will have inlined, DCE'ed, and function-attr
476 // propagated everything. We should at this point have a reasonably minimal
477 // and richly annotated call graph. By computing aliasing and mod/ref
478 // information for all local globals here, the late loop passes and notably
479 // the vectorizer will be able to use them to help recognize vectorizable
480 // memory operations.
481 // FIXME: Enable this once analysis invalidation is fully supported.
482#if 0
483 MPM.addPass(Require<GlobalsAA>());
484#endif
485
486 FunctionPassManager OptimizePM(DebugLogging);
487 OptimizePM.addPass(Float2IntPass());
488 // FIXME: We need to run some loop optimizations to re-rotate loops after
489 // simplify-cfg and others undo their rotation.
490
491 // Optimize the loop execution. These passes operate on entire loop nests
492 // rather than on each loop in an inside-out manner, and so they are actually
493 // function passes.
494 OptimizePM.addPass(LoopDistributePass());
495 OptimizePM.addPass(LoopVectorizePass());
496 // FIXME: Need to port Loop Load Elimination and add it here.
497 OptimizePM.addPass(InstCombinePass());
498
499 // Optimize parallel scalar instruction chains into SIMD instructions.
500 OptimizePM.addPass(SLPVectorizerPass());
501
502 // Cleanup after vectorizers.
503 OptimizePM.addPass(SimplifyCFGPass());
504 OptimizePM.addPass(InstCombinePass());
505
506 // Unroll small loops to hide loop backedge latency and saturate any parallel
507 // execution resources of an out-of-order processor.
508 // FIXME: Need to add once loop pass pipeline is available.
509
510 // FIXME: Add the loop sink pass when ported.
511
512 // FIXME: Add cleanup from the loop pass manager when we're forming LCSSA
513 // here.
514
515 // Now that we've vectorized and unrolled loops, we may have more refined
516 // alignment information, try to re-derive it here.
517 OptimizePM.addPass(AlignmentFromAssumptionsPass());
518
519 // ADd the core optimizing pipeline.
520 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
521
522 // Now we need to do some global optimization transforms.
523 // FIXME: It would seem like these should come first in the optimization
524 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
525 // ordering here.
526 MPM.addPass(GlobalDCEPass());
527 MPM.addPass(ConstantMergePass());
528
529 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000530}
531
Chandler Carruthe3f50642016-12-22 06:59:15 +0000532ModulePassManager
533PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
534 bool DebugLogging) {
535 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000536 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000537 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000538}
539
Chandler Carruthe3f50642016-12-22 06:59:15 +0000540ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
541 bool DebugLogging) {
542 assert(Level != O0 && "Must request optimizations for the default pipeline!");
543 ModulePassManager MPM(DebugLogging);
544
Davide Italiano089a9122017-01-24 00:57:39 +0000545 // Remove unused virtual tables to improve the quality of code generated by
546 // whole-program devirtualization and bitset lowering.
547 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000548
Davide Italiano089a9122017-01-24 00:57:39 +0000549 // Force any function attributes we want the rest of the pipeline to observe.
550 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000551
Davide Italiano089a9122017-01-24 00:57:39 +0000552 // Do basic inference of function attributes from known properties of system
553 // libraries and other oracles.
554 MPM.addPass(InferFunctionAttrsPass());
555
556 if (Level > 1) {
557 // Indirect call promotion. This should promote all the targets that are
558 // left by the earlier promotion pass that promotes intra-module targets.
559 // This two-step promotion is to save the compile time. For LTO, it should
560 // produce the same result as if we only do promotion here.
561 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */));
562
563 // Propagate constants at call sites into the functions they call. This
564 // opens opportunities for globalopt (and inlining) by substituting function
565 // pointers passed as arguments to direct uses of functions.
566 MPM.addPass(IPSCCPPass());
567 }
568
569 // Now deduce any function attributes based in the current code.
570 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
571 PostOrderFunctionAttrsPass()));
572
573 // Do RPO function attribute inference across the module to forward-propagate
574 // attributes where applicable.
575 // FIXME: Is this really an optimization rather than a canonicalization?
576 MPM.addPass(ReversePostOrderFunctionAttrsPass());
577
578 // Use inragne annotations on GEP indices to split globals where beneficial.
579 MPM.addPass(GlobalSplitPass());
580
581 // Run whole program optimization of virtual call when the list of callees
582 // is fixed.
583 MPM.addPass(WholeProgramDevirtPass());
584
585 // Stop here at -O1.
586 if (Level == 1)
587 return MPM;
588
589 // Optimize globals to try and fold them into constants.
590 MPM.addPass(GlobalOptPass());
591
592 // Promote any localized globals to SSA registers.
593 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
594
595 // Linking modules together can lead to duplicate global constant, only
596 // keep one copy of each constant.
597 MPM.addPass(ConstantMergePass());
598
599 // Remove unused arguments from functions.
600 MPM.addPass(DeadArgumentEliminationPass());
601
602 // Reduce the code after globalopt and ipsccp. Both can open up significant
603 // simplification opportunities, and both can propagate functions through
604 // function pointers. When this happens, we often have to resolve varargs
605 // calls, etc, so let instcombine do this.
606 // FIXME: add peephole extensions here as the legacy PM does.
607 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
608
609 // Note: historically, the PruneEH pass was run first to deduce nounwind and
610 // generally clean up exception handling overhead. It isn't clear this is
611 // valuable as the inliner doesn't currently care whether it is inlining an
612 // invoke or a call.
613 // Run the inliner now.
614 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
615
616 // Optimize globals again after we ran the inliner.
617 MPM.addPass(GlobalOptPass());
618
619 // Garbage collect dead functions.
620 // FIXME: Add ArgumentPromotion pass after once it's ported.
621 MPM.addPass(GlobalDCEPass());
622
623 FunctionPassManager FPM(DebugLogging);
624
625 // The IPO Passes may leave cruft around. Clean up after them.
626 // FIXME: add peephole extensions here as the legacy PM does.
627 FPM.addPass(InstCombinePass());
628 FPM.addPass(JumpThreadingPass());
629
630 // Break up allocas
631 FPM.addPass(SROA());
632
633 // Run a few AA driver optimizations here and now to cleanup the code.
634 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
635
636 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
637 PostOrderFunctionAttrsPass()));
638 // FIXME: here we run IP alias analysis in the legacy PM.
639
640 FunctionPassManager MainFPM;
641
642 // FIXME: once we fix LoopPass Manager, add LICM here.
643 // FIXME: once we provide support for enabling MLSM, add it here.
644 // FIXME: once we provide support for enabling NewGVN, add it here.
645 MainFPM.addPass(GVN());
646
647 // Remove dead memcpy()'s.
648 MainFPM.addPass(MemCpyOptPass());
649
650 // Nuke dead stores.
651 MainFPM.addPass(DSEPass());
652
653 // FIXME: at this point, we run a bunch of loop passes:
654 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
655 // loopVectorize. Enable them once the remaining issue with LPM
656 // are sorted out.
657
658 MainFPM.addPass(InstCombinePass());
659 MainFPM.addPass(SimplifyCFGPass());
660 MainFPM.addPass(SCCPPass());
661 MainFPM.addPass(InstCombinePass());
662 MainFPM.addPass(BDCEPass());
663
664 // FIXME: We may want to run SLPVectorizer here.
665 // After vectorization, assume intrinsics may tell us more
666 // about pointer alignments.
667#if 0
668 MainFPM.add(AlignmentFromAssumptionsPass());
669#endif
670
671 // FIXME: Conditionally run LoadCombine here, after it's ported
672 // (in case we still have this pass, given its questionable usefulness).
673
674 // FIXME: add peephole extensions to the PM here.
675 MainFPM.addPass(InstCombinePass());
676 MainFPM.addPass(JumpThreadingPass());
677 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
678
679 // Create a function that performs CFI checks for cross-DSO calls with
680 // targets in the current module.
681 MPM.addPass(CrossDSOCFIPass());
682
683 // Lower type metadata and the type.test intrinsic. This pass supports
684 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
685 // to be run at link time if CFI is enabled. This pass does nothing if
686 // CFI is disabled.
687 // Enable once we add support for the summary in the new PM.
688#if 0
689 MPM.addPass(LowerTypeTestsPass(Summary ? LowerTypeTestsSummaryAction::Export :
690 LowerTypeTestsSummaryAction::None,
691 Summary));
692#endif
693
694 // Add late LTO optimization passes.
695 // Delete basic blocks, which optimization passes may have killed.
696 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
697
698 // Drop bodies of available eternally objects to improve GlobalDCE.
699 MPM.addPass(EliminateAvailableExternallyPass());
700
701 // Now that we have optimized the program, discard unreachable functions.
702 MPM.addPass(GlobalDCEPass());
703
704 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000705 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000706}
707
Chandler Carruth060ad612016-12-23 20:38:19 +0000708AAManager PassBuilder::buildDefaultAAPipeline() {
709 AAManager AA;
710
711 // The order in which these are registered determines their priority when
712 // being queried.
713
714 // First we register the basic alias analysis that provides the majority of
715 // per-function local AA logic. This is a stateless, on-demand local set of
716 // AA techniques.
717 AA.registerFunctionAnalysis<BasicAA>();
718
719 // Next we query fast, specialized alias analyses that wrap IR-embedded
720 // information about aliasing.
721 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
722 AA.registerFunctionAnalysis<TypeBasedAA>();
723
724 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000725 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
726 // analysis, all that the `AAManager` can do is query for any *cached*
727 // results from `GlobalsAA` through a readonly proxy..
Chandler Carruth060ad612016-12-23 20:38:19 +0000728#if 0
Chandler Carruth534d6442016-12-24 05:11:17 +0000729 // FIXME: Enable once the invalidation logic supports this. Currently, the
730 // `AAManager` will hold stale references to the module analyses.
Chandler Carruth060ad612016-12-23 20:38:19 +0000731 AA.registerModuleAnalysis<GlobalsAA>();
732#endif
733
734 return AA;
735}
736
Chandler Carruth241bf242016-08-03 07:44:48 +0000737static Optional<int> parseRepeatPassName(StringRef Name) {
738 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
739 return None;
740 int Count;
741 if (Name.getAsInteger(0, Count) || Count <= 0)
742 return None;
743 return Count;
744}
745
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000746static Optional<int> parseDevirtPassName(StringRef Name) {
747 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
748 return None;
749 int Count;
750 if (Name.getAsInteger(0, Count) || Count <= 0)
751 return None;
752 return Count;
753}
754
Chandler Carruth66445382014-01-11 08:16:35 +0000755static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000756 // Manually handle aliases for pre-configured pipeline fragments.
757 if (Name.startswith("default") || Name.startswith("lto"))
758 return DefaultAliasRegex.match(Name);
759
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000760 // Explicitly handle pass manager names.
761 if (Name == "module")
762 return true;
763 if (Name == "cgscc")
764 return true;
765 if (Name == "function")
766 return true;
767
Chandler Carruth241bf242016-08-03 07:44:48 +0000768 // Explicitly handle custom-parsed pass names.
769 if (parseRepeatPassName(Name))
770 return true;
771
Chandler Carruth74a8a222016-06-17 07:15:29 +0000772#define MODULE_PASS(NAME, CREATE_PASS) \
773 if (Name == NAME) \
774 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000775#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000776 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000777 return true;
778#include "PassRegistry.def"
779
Chandler Carruth66445382014-01-11 08:16:35 +0000780 return false;
781}
782
Chandler Carruth572e3402014-04-21 11:12:00 +0000783static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000784 // Explicitly handle pass manager names.
785 if (Name == "cgscc")
786 return true;
787 if (Name == "function")
788 return true;
789
Chandler Carruth241bf242016-08-03 07:44:48 +0000790 // Explicitly handle custom-parsed pass names.
791 if (parseRepeatPassName(Name))
792 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000793 if (parseDevirtPassName(Name))
794 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000795
Chandler Carruth74a8a222016-06-17 07:15:29 +0000796#define CGSCC_PASS(NAME, CREATE_PASS) \
797 if (Name == NAME) \
798 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000799#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000800 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000801 return true;
802#include "PassRegistry.def"
803
Chandler Carruth572e3402014-04-21 11:12:00 +0000804 return false;
805}
806
Chandler Carruthd8330982014-01-12 09:34:22 +0000807static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000808 // Explicitly handle pass manager names.
809 if (Name == "function")
810 return true;
811 if (Name == "loop")
812 return true;
813
Chandler Carruth241bf242016-08-03 07:44:48 +0000814 // Explicitly handle custom-parsed pass names.
815 if (parseRepeatPassName(Name))
816 return true;
817
Chandler Carruth74a8a222016-06-17 07:15:29 +0000818#define FUNCTION_PASS(NAME, CREATE_PASS) \
819 if (Name == NAME) \
820 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000821#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000822 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000823 return true;
824#include "PassRegistry.def"
825
Chandler Carruthd8330982014-01-12 09:34:22 +0000826 return false;
827}
828
Justin Bognereecc3c82016-02-25 07:23:08 +0000829static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000830 // Explicitly handle pass manager names.
831 if (Name == "loop")
832 return true;
833
Chandler Carruth241bf242016-08-03 07:44:48 +0000834 // Explicitly handle custom-parsed pass names.
835 if (parseRepeatPassName(Name))
836 return true;
837
Chandler Carruth74a8a222016-06-17 07:15:29 +0000838#define LOOP_PASS(NAME, CREATE_PASS) \
839 if (Name == NAME) \
840 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000841#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
842 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
843 return true;
844#include "PassRegistry.def"
845
846 return false;
847}
848
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000849Optional<std::vector<PassBuilder::PipelineElement>>
850PassBuilder::parsePipelineText(StringRef Text) {
851 std::vector<PipelineElement> ResultPipeline;
852
853 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
854 &ResultPipeline};
855 for (;;) {
856 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
857 size_t Pos = Text.find_first_of(",()");
858 Pipeline.push_back({Text.substr(0, Pos), {}});
859
860 // If we have a single terminating name, we're done.
861 if (Pos == Text.npos)
862 break;
863
864 char Sep = Text[Pos];
865 Text = Text.substr(Pos + 1);
866 if (Sep == ',')
867 // Just a name ending in a comma, continue.
868 continue;
869
870 if (Sep == '(') {
871 // Push the inner pipeline onto the stack to continue processing.
872 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
873 continue;
874 }
875
876 assert(Sep == ')' && "Bogus separator!");
877 // When handling the close parenthesis, we greedily consume them to avoid
878 // empty strings in the pipeline.
879 do {
880 // If we try to pop the outer pipeline we have unbalanced parentheses.
881 if (PipelineStack.size() == 1)
882 return None;
883
884 PipelineStack.pop_back();
885 } while (Text.consume_front(")"));
886
887 // Check if we've finished parsing.
888 if (Text.empty())
889 break;
890
891 // Otherwise, the end of an inner pipeline always has to be followed by
892 // a comma, and then we can continue.
893 if (!Text.consume_front(","))
894 return None;
895 }
896
897 if (PipelineStack.size() > 1)
898 // Unbalanced paretheses.
899 return None;
900
901 assert(PipelineStack.back() == &ResultPipeline &&
902 "Wrong pipeline at the bottom of the stack!");
903 return {std::move(ResultPipeline)};
904}
905
906bool PassBuilder::parseModulePass(ModulePassManager &MPM,
907 const PipelineElement &E, bool VerifyEachPass,
908 bool DebugLogging) {
909 auto &Name = E.Name;
910 auto &InnerPipeline = E.InnerPipeline;
911
912 // First handle complex passes like the pass managers which carry pipelines.
913 if (!InnerPipeline.empty()) {
914 if (Name == "module") {
915 ModulePassManager NestedMPM(DebugLogging);
916 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
917 DebugLogging))
918 return false;
919 MPM.addPass(std::move(NestedMPM));
920 return true;
921 }
922 if (Name == "cgscc") {
923 CGSCCPassManager CGPM(DebugLogging);
924 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
925 DebugLogging))
926 return false;
927 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
928 DebugLogging));
929 return true;
930 }
931 if (Name == "function") {
932 FunctionPassManager FPM(DebugLogging);
933 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
934 DebugLogging))
935 return false;
936 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
937 return true;
938 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000939 if (auto Count = parseRepeatPassName(Name)) {
940 ModulePassManager NestedMPM(DebugLogging);
941 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
942 DebugLogging))
943 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000944 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000945 return true;
946 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000947 // Normal passes can't have pipelines.
948 return false;
949 }
950
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000951 // Manually handle aliases for pre-configured pipeline fragments.
952 if (Name.startswith("default") || Name.startswith("lto")) {
953 SmallVector<StringRef, 3> Matches;
954 if (!DefaultAliasRegex.match(Name, &Matches))
955 return false;
956 assert(Matches.size() == 3 && "Must capture two matched strings!");
957
Jordan Rosef85a95f2016-07-25 18:34:51 +0000958 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
959 .Case("O0", O0)
960 .Case("O1", O1)
961 .Case("O2", O2)
962 .Case("O3", O3)
963 .Case("Os", Os)
964 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000965 if (L == O0)
966 // At O0 we do nothing at all!
967 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000968
969 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000970 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000971 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000972 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000973 } else {
974 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +0000975 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000976 }
977 return true;
978 }
979
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000980 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +0000981#define MODULE_PASS(NAME, CREATE_PASS) \
982 if (Name == NAME) { \
983 MPM.addPass(CREATE_PASS); \
984 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000985 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000986#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
987 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +0000988 MPM.addPass( \
989 RequireAnalysisPass< \
990 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000991 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000992 } \
993 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000994 MPM.addPass(InvalidateAnalysisPass< \
995 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000996 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000997 }
998#include "PassRegistry.def"
999
Chandler Carruth66445382014-01-11 08:16:35 +00001000 return false;
1001}
1002
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001003bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1004 const PipelineElement &E, bool VerifyEachPass,
1005 bool DebugLogging) {
1006 auto &Name = E.Name;
1007 auto &InnerPipeline = E.InnerPipeline;
1008
1009 // First handle complex passes like the pass managers which carry pipelines.
1010 if (!InnerPipeline.empty()) {
1011 if (Name == "cgscc") {
1012 CGSCCPassManager NestedCGPM(DebugLogging);
1013 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1014 DebugLogging))
1015 return false;
1016 // Add the nested pass manager with the appropriate adaptor.
1017 CGPM.addPass(std::move(NestedCGPM));
1018 return true;
1019 }
1020 if (Name == "function") {
1021 FunctionPassManager FPM(DebugLogging);
1022 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1023 DebugLogging))
1024 return false;
1025 // Add the nested pass manager with the appropriate adaptor.
1026 CGPM.addPass(
1027 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1028 return true;
1029 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001030 if (auto Count = parseRepeatPassName(Name)) {
1031 CGSCCPassManager NestedCGPM(DebugLogging);
1032 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1033 DebugLogging))
1034 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001035 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001036 return true;
1037 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001038 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1039 CGSCCPassManager NestedCGPM(DebugLogging);
1040 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1041 DebugLogging))
1042 return false;
1043 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1044 *MaxRepetitions, DebugLogging));
1045 return true;
1046 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001047 // Normal passes can't have pipelines.
1048 return false;
1049 }
1050
1051 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001052#define CGSCC_PASS(NAME, CREATE_PASS) \
1053 if (Name == NAME) { \
1054 CGPM.addPass(CREATE_PASS); \
1055 return true; \
1056 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001057#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1058 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001059 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001060 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001061 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1062 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001063 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001064 } \
1065 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001066 CGPM.addPass(InvalidateAnalysisPass< \
1067 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001068 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001069 }
1070#include "PassRegistry.def"
1071
Chandler Carruth572e3402014-04-21 11:12:00 +00001072 return false;
1073}
1074
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001075bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1076 const PipelineElement &E,
1077 bool VerifyEachPass, bool DebugLogging) {
1078 auto &Name = E.Name;
1079 auto &InnerPipeline = E.InnerPipeline;
1080
1081 // First handle complex passes like the pass managers which carry pipelines.
1082 if (!InnerPipeline.empty()) {
1083 if (Name == "function") {
1084 FunctionPassManager NestedFPM(DebugLogging);
1085 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1086 DebugLogging))
1087 return false;
1088 // Add the nested pass manager with the appropriate adaptor.
1089 FPM.addPass(std::move(NestedFPM));
1090 return true;
1091 }
1092 if (Name == "loop") {
1093 LoopPassManager LPM(DebugLogging);
1094 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1095 DebugLogging))
1096 return false;
1097 // Add the nested pass manager with the appropriate adaptor.
1098 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1099 return true;
1100 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001101 if (auto Count = parseRepeatPassName(Name)) {
1102 FunctionPassManager NestedFPM(DebugLogging);
1103 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1104 DebugLogging))
1105 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001106 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001107 return true;
1108 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001109 // Normal passes can't have pipelines.
1110 return false;
1111 }
1112
1113 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001114#define FUNCTION_PASS(NAME, CREATE_PASS) \
1115 if (Name == NAME) { \
1116 FPM.addPass(CREATE_PASS); \
1117 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001118 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001119#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1120 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001121 FPM.addPass( \
1122 RequireAnalysisPass< \
1123 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001124 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001125 } \
1126 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001127 FPM.addPass(InvalidateAnalysisPass< \
1128 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001129 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001130 }
1131#include "PassRegistry.def"
1132
Chandler Carruthd8330982014-01-12 09:34:22 +00001133 return false;
1134}
1135
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001136bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001137 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001138 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001139 auto &InnerPipeline = E.InnerPipeline;
1140
1141 // First handle complex passes like the pass managers which carry pipelines.
1142 if (!InnerPipeline.empty()) {
1143 if (Name == "loop") {
1144 LoopPassManager NestedLPM(DebugLogging);
1145 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1146 DebugLogging))
1147 return false;
1148 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001149 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001150 return true;
1151 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001152 if (auto Count = parseRepeatPassName(Name)) {
1153 LoopPassManager NestedLPM(DebugLogging);
1154 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1155 DebugLogging))
1156 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001157 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001158 return true;
1159 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001160 // Normal passes can't have pipelines.
1161 return false;
1162 }
1163
1164 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001165#define LOOP_PASS(NAME, CREATE_PASS) \
1166 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001167 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001168 return true; \
1169 }
1170#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1171 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001172 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001173 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1174 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1175 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001176 return true; \
1177 } \
1178 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001179 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001180 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001181 return true; \
1182 }
1183#include "PassRegistry.def"
1184
1185 return false;
1186}
1187
Chandler Carruthedf59962016-02-18 09:45:17 +00001188bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001189#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1190 if (Name == NAME) { \
1191 AA.registerModuleAnalysis< \
1192 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1193 return true; \
1194 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001195#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1196 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001197 AA.registerFunctionAnalysis< \
1198 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001199 return true; \
1200 }
1201#include "PassRegistry.def"
1202
1203 return false;
1204}
1205
Justin Bognereecc3c82016-02-25 07:23:08 +00001206bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001207 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001208 bool VerifyEachPass,
1209 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001210 for (const auto &Element : Pipeline) {
1211 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1212 return false;
1213 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001214 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001215 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001216}
1217
Chandler Carruth1ff77242015-03-07 09:02:36 +00001218bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001219 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001220 bool VerifyEachPass,
1221 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001222 for (const auto &Element : Pipeline) {
1223 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1224 return false;
1225 if (VerifyEachPass)
1226 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001227 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001228 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001229}
1230
Chandler Carruth1ff77242015-03-07 09:02:36 +00001231bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001232 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001233 bool VerifyEachPass,
1234 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001235 for (const auto &Element : Pipeline) {
1236 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1237 return false;
1238 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001239 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001240 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001241}
1242
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001243void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1244 FunctionAnalysisManager &FAM,
1245 CGSCCAnalysisManager &CGAM,
1246 ModuleAnalysisManager &MAM) {
1247 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1248 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001249 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1250 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1251 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1252 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1253 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1254}
1255
Chandler Carruth1ff77242015-03-07 09:02:36 +00001256bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001257 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001258 bool VerifyEachPass,
1259 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001260 for (const auto &Element : Pipeline) {
1261 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1262 return false;
1263 if (VerifyEachPass)
1264 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001265 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001266 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001267}
1268
1269// Primary pass pipeline description parsing routine.
1270// FIXME: Should this routine accept a TargetMachine or require the caller to
1271// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001272bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1273 StringRef PipelineText, bool VerifyEachPass,
1274 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001275 auto Pipeline = parsePipelineText(PipelineText);
1276 if (!Pipeline || Pipeline->empty())
1277 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001278
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001279 // If the first name isn't at the module layer, wrap the pipeline up
1280 // automatically.
1281 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001282
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001283 if (!isModulePassName(FirstName)) {
1284 if (isCGSCCPassName(FirstName))
1285 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1286 else if (isFunctionPassName(FirstName))
1287 Pipeline = {{"function", std::move(*Pipeline)}};
1288 else if (isLoopPassName(FirstName))
1289 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1290 else
1291 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001292 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001293 }
1294
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001295 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001296}
Chandler Carruthedf59962016-02-18 09:45:17 +00001297
1298bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001299 // If the pipeline just consists of the word 'default' just replace the AA
1300 // manager with our default one.
1301 if (PipelineText == "default") {
1302 AA = buildDefaultAAPipeline();
1303 return true;
1304 }
1305
Chandler Carruthedf59962016-02-18 09:45:17 +00001306 while (!PipelineText.empty()) {
1307 StringRef Name;
1308 std::tie(Name, PipelineText) = PipelineText.split(',');
1309 if (!parseAAPassName(AA, Name))
1310 return false;
1311 }
1312
1313 return true;
1314}