blob: e26a8e2f1ed670818cbccb55443ffc596e760beb [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
Chandler Carruth81c8eda2016-12-27 10:16:46 +0000321 // FIXME: Enable these when the loop pass manager can support enforcing loop
322 // simplified and LCSSA form as well as updating the loop nest after
323 // transformations and we finsih porting the loop passes.
324#if 0
Chandler Carruthe3f50642016-12-22 06:59:15 +0000325 // Rotate Loop - disable header duplication at -Oz
326 LPM1.addPass(LoopRotatePass(Level != Oz));
327 LPM1.addPass(LICMPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000328 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
329 LPM2.addPass(IndVarSimplifyPass());
330 LPM2.addPass(LoopIdiomPass());
331 LPM2.addPass(LoopDeletionPass());
332 LPM2.addPass(SimpleLoopUnrollPass());
333#endif
334 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
335 FPM.addPass(SimplifyCFGPass());
336 FPM.addPass(InstCombinePass());
337 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
338
339 // Eliminate redundancies.
340 if (Level != O1) {
341 // These passes add substantial compile time so skip them at O1.
342 FPM.addPass(MergedLoadStoreMotionPass());
343 FPM.addPass(GVN());
344 }
345
346 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
347 FPM.addPass(MemCpyOptPass());
348
349 // Sparse conditional constant propagation.
350 // FIXME: It isn't clear why we do this *after* loop passes rather than
351 // before...
352 FPM.addPass(SCCPPass());
353
354 // Delete dead bit computations (instcombine runs after to fold away the dead
355 // computations, and then ADCE will run later to exploit any new DCE
356 // opportunities that creates).
357 FPM.addPass(BDCEPass());
358
359 // Run instcombine after redundancy and dead bit elimination to exploit
360 // opportunities opened up by them.
361 FPM.addPass(InstCombinePass());
362
363 // Re-consider control flow based optimizations after redundancy elimination,
364 // redo DCE, etc.
365 FPM.addPass(JumpThreadingPass());
366 FPM.addPass(CorrelatedValuePropagationPass());
367 FPM.addPass(DSEPass());
Chandler Carruth81c8eda2016-12-27 10:16:46 +0000368 // FIXME: Enable this when the loop pass manager can support enforcing loop
369 // simplified and LCSSA form as well as updating the loop nest after
370 // transformations and we finsih porting the loop passes.
371#if 0
Chandler Carruthe3f50642016-12-22 06:59:15 +0000372 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruth81c8eda2016-12-27 10:16:46 +0000373#endif
Chandler Carruthe3f50642016-12-22 06:59:15 +0000374
375 // Finally, do an expensive DCE pass to catch all the dead code exposed by
376 // the simplifications and basic cleanup after all the simplifications.
377 FPM.addPass(ADCEPass());
378 FPM.addPass(SimplifyCFGPass());
379 FPM.addPass(InstCombinePass());
380
381 return FPM;
382}
383
384ModulePassManager
385PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
386 bool DebugLogging) {
387 assert(Level != O0 && "Must request optimizations for the default pipeline!");
388 ModulePassManager MPM(DebugLogging);
389
390 // Force any function attributes we want the rest of the pipeline te observe.
391 MPM.addPass(ForceFunctionAttrsPass());
392
393 // Do basic inference of function attributes from known properties of system
394 // libraries and other oracles.
395 MPM.addPass(InferFunctionAttrsPass());
396
397 // Create an early function pass manager to cleanup the output of the
398 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000399 FunctionPassManager EarlyFPM(DebugLogging);
400 EarlyFPM.addPass(SimplifyCFGPass());
401 EarlyFPM.addPass(SROA());
402 EarlyFPM.addPass(EarlyCSEPass());
403 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000404 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000405 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000406
407 // Interprocedural constant propagation now that basic cleanup has occured
408 // and prior to optimizing globals.
409 // FIXME: This position in the pipeline hasn't been carefully considered in
410 // years, it should be re-analyzed.
411 MPM.addPass(IPSCCPPass());
412
413 // Optimize globals to try and fold them into constants.
414 MPM.addPass(GlobalOptPass());
415
416 // Promote any localized globals to SSA registers.
417 // FIXME: Should this instead by a run of SROA?
418 // FIXME: We should probably run instcombine and simplify-cfg afterward to
419 // delete control flows that are dead once globals have been folded to
420 // constants.
421 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
422
423 // Remove any dead arguments exposed by cleanups and constand folding
424 // globals.
425 MPM.addPass(DeadArgumentEliminationPass());
426
427 // Create a small function pass pipeline to cleanup after all the global
428 // optimizations.
429 FunctionPassManager GlobalCleanupPM(DebugLogging);
430 GlobalCleanupPM.addPass(InstCombinePass());
431 GlobalCleanupPM.addPass(SimplifyCFGPass());
432 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
433
434 // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
435#if 0
436 MPM.addPass(RequireAnalysisPass<GlobalsAA>());
437#endif
438
439 // Now begin the main postorder CGSCC pipeline.
440 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
441 // manager and trying to emulate its precise behavior. Much of this doesn't
442 // make a lot of sense and we should revisit the core CGSCC structure.
443 CGSCCPassManager MainCGPipeline(DebugLogging);
444
445 // Note: historically, the PruneEH pass was run first to deduce nounwind and
446 // generally clean up exception handling overhead. It isn't clear this is
447 // valuable as the inliner doesn't currently care whether it is inlining an
448 // invoke or a call.
449
450 // Run the inliner first. The theory is that we are walking bottom-up and so
451 // the callees have already been fully optimized, and we want to inline them
452 // into the callers so that our optimizations can reflect that.
453 // FIXME; Customize the threshold based on optimization level.
454 MainCGPipeline.addPass(InlinerPass());
455
456 // Now deduce any function attributes based in the current code.
457 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
458
459 // Lastly, add the core function simplification pipeline nested inside the
460 // CGSCC walk.
461 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
462 buildFunctionSimplificationPipeline(Level, DebugLogging)));
463
464 MPM.addPass(
465 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
466
467 // This ends the canonicalization and simplification phase of the pipeline.
468 // At this point, we expect to have canonical and simple IR which we begin
469 // *optimizing* for efficient execution going forward.
470
471 // Eliminate externally available functions now that inlining is over -- we
472 // won't emit these anyways.
473 MPM.addPass(EliminateAvailableExternallyPass());
474
475 // Do RPO function attribute inference across the module to forward-propagate
476 // attributes where applicable.
477 // FIXME: Is this really an optimization rather than a canonicalization?
478 MPM.addPass(ReversePostOrderFunctionAttrsPass());
479
480 // Recompute GloblasAA here prior to function passes. This is particularly
481 // useful as the above will have inlined, DCE'ed, and function-attr
482 // propagated everything. We should at this point have a reasonably minimal
483 // and richly annotated call graph. By computing aliasing and mod/ref
484 // information for all local globals here, the late loop passes and notably
485 // the vectorizer will be able to use them to help recognize vectorizable
486 // memory operations.
487 // FIXME: Enable this once analysis invalidation is fully supported.
488#if 0
489 MPM.addPass(Require<GlobalsAA>());
490#endif
491
492 FunctionPassManager OptimizePM(DebugLogging);
493 OptimizePM.addPass(Float2IntPass());
494 // FIXME: We need to run some loop optimizations to re-rotate loops after
495 // simplify-cfg and others undo their rotation.
496
497 // Optimize the loop execution. These passes operate on entire loop nests
498 // rather than on each loop in an inside-out manner, and so they are actually
499 // function passes.
500 OptimizePM.addPass(LoopDistributePass());
501 OptimizePM.addPass(LoopVectorizePass());
502 // FIXME: Need to port Loop Load Elimination and add it here.
503 OptimizePM.addPass(InstCombinePass());
504
505 // Optimize parallel scalar instruction chains into SIMD instructions.
506 OptimizePM.addPass(SLPVectorizerPass());
507
508 // Cleanup after vectorizers.
509 OptimizePM.addPass(SimplifyCFGPass());
510 OptimizePM.addPass(InstCombinePass());
511
512 // Unroll small loops to hide loop backedge latency and saturate any parallel
513 // execution resources of an out-of-order processor.
514 // FIXME: Need to add once loop pass pipeline is available.
515
516 // FIXME: Add the loop sink pass when ported.
517
518 // FIXME: Add cleanup from the loop pass manager when we're forming LCSSA
519 // here.
520
521 // Now that we've vectorized and unrolled loops, we may have more refined
522 // alignment information, try to re-derive it here.
523 OptimizePM.addPass(AlignmentFromAssumptionsPass());
524
525 // ADd the core optimizing pipeline.
526 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
527
528 // Now we need to do some global optimization transforms.
529 // FIXME: It would seem like these should come first in the optimization
530 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
531 // ordering here.
532 MPM.addPass(GlobalDCEPass());
533 MPM.addPass(ConstantMergePass());
534
535 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000536}
537
Chandler Carruthe3f50642016-12-22 06:59:15 +0000538ModulePassManager
539PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
540 bool DebugLogging) {
541 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000542 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000543 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000544}
545
Chandler Carruthe3f50642016-12-22 06:59:15 +0000546ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
547 bool DebugLogging) {
548 assert(Level != O0 && "Must request optimizations for the default pipeline!");
549 ModulePassManager MPM(DebugLogging);
550
Davide Italiano089a9122017-01-24 00:57:39 +0000551 // Remove unused virtual tables to improve the quality of code generated by
552 // whole-program devirtualization and bitset lowering.
553 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000554
Davide Italiano089a9122017-01-24 00:57:39 +0000555 // Force any function attributes we want the rest of the pipeline to observe.
556 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000557
Davide Italiano089a9122017-01-24 00:57:39 +0000558 // Do basic inference of function attributes from known properties of system
559 // libraries and other oracles.
560 MPM.addPass(InferFunctionAttrsPass());
561
562 if (Level > 1) {
563 // Indirect call promotion. This should promote all the targets that are
564 // left by the earlier promotion pass that promotes intra-module targets.
565 // This two-step promotion is to save the compile time. For LTO, it should
566 // produce the same result as if we only do promotion here.
567 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */));
568
569 // Propagate constants at call sites into the functions they call. This
570 // opens opportunities for globalopt (and inlining) by substituting function
571 // pointers passed as arguments to direct uses of functions.
572 MPM.addPass(IPSCCPPass());
573 }
574
575 // Now deduce any function attributes based in the current code.
576 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
577 PostOrderFunctionAttrsPass()));
578
579 // Do RPO function attribute inference across the module to forward-propagate
580 // attributes where applicable.
581 // FIXME: Is this really an optimization rather than a canonicalization?
582 MPM.addPass(ReversePostOrderFunctionAttrsPass());
583
584 // Use inragne annotations on GEP indices to split globals where beneficial.
585 MPM.addPass(GlobalSplitPass());
586
587 // Run whole program optimization of virtual call when the list of callees
588 // is fixed.
589 MPM.addPass(WholeProgramDevirtPass());
590
591 // Stop here at -O1.
592 if (Level == 1)
593 return MPM;
594
595 // Optimize globals to try and fold them into constants.
596 MPM.addPass(GlobalOptPass());
597
598 // Promote any localized globals to SSA registers.
599 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
600
601 // Linking modules together can lead to duplicate global constant, only
602 // keep one copy of each constant.
603 MPM.addPass(ConstantMergePass());
604
605 // Remove unused arguments from functions.
606 MPM.addPass(DeadArgumentEliminationPass());
607
608 // Reduce the code after globalopt and ipsccp. Both can open up significant
609 // simplification opportunities, and both can propagate functions through
610 // function pointers. When this happens, we often have to resolve varargs
611 // calls, etc, so let instcombine do this.
612 // FIXME: add peephole extensions here as the legacy PM does.
613 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
614
615 // Note: historically, the PruneEH pass was run first to deduce nounwind and
616 // generally clean up exception handling overhead. It isn't clear this is
617 // valuable as the inliner doesn't currently care whether it is inlining an
618 // invoke or a call.
619 // Run the inliner now.
620 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
621
622 // Optimize globals again after we ran the inliner.
623 MPM.addPass(GlobalOptPass());
624
625 // Garbage collect dead functions.
626 // FIXME: Add ArgumentPromotion pass after once it's ported.
627 MPM.addPass(GlobalDCEPass());
628
629 FunctionPassManager FPM(DebugLogging);
630
631 // The IPO Passes may leave cruft around. Clean up after them.
632 // FIXME: add peephole extensions here as the legacy PM does.
633 FPM.addPass(InstCombinePass());
634 FPM.addPass(JumpThreadingPass());
635
636 // Break up allocas
637 FPM.addPass(SROA());
638
639 // Run a few AA driver optimizations here and now to cleanup the code.
640 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
641
642 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
643 PostOrderFunctionAttrsPass()));
644 // FIXME: here we run IP alias analysis in the legacy PM.
645
646 FunctionPassManager MainFPM;
647
648 // FIXME: once we fix LoopPass Manager, add LICM here.
649 // FIXME: once we provide support for enabling MLSM, add it here.
650 // FIXME: once we provide support for enabling NewGVN, add it here.
651 MainFPM.addPass(GVN());
652
653 // Remove dead memcpy()'s.
654 MainFPM.addPass(MemCpyOptPass());
655
656 // Nuke dead stores.
657 MainFPM.addPass(DSEPass());
658
659 // FIXME: at this point, we run a bunch of loop passes:
660 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
661 // loopVectorize. Enable them once the remaining issue with LPM
662 // are sorted out.
663
664 MainFPM.addPass(InstCombinePass());
665 MainFPM.addPass(SimplifyCFGPass());
666 MainFPM.addPass(SCCPPass());
667 MainFPM.addPass(InstCombinePass());
668 MainFPM.addPass(BDCEPass());
669
670 // FIXME: We may want to run SLPVectorizer here.
671 // After vectorization, assume intrinsics may tell us more
672 // about pointer alignments.
673#if 0
674 MainFPM.add(AlignmentFromAssumptionsPass());
675#endif
676
677 // FIXME: Conditionally run LoadCombine here, after it's ported
678 // (in case we still have this pass, given its questionable usefulness).
679
680 // FIXME: add peephole extensions to the PM here.
681 MainFPM.addPass(InstCombinePass());
682 MainFPM.addPass(JumpThreadingPass());
683 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
684
685 // Create a function that performs CFI checks for cross-DSO calls with
686 // targets in the current module.
687 MPM.addPass(CrossDSOCFIPass());
688
689 // Lower type metadata and the type.test intrinsic. This pass supports
690 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
691 // to be run at link time if CFI is enabled. This pass does nothing if
692 // CFI is disabled.
693 // Enable once we add support for the summary in the new PM.
694#if 0
695 MPM.addPass(LowerTypeTestsPass(Summary ? LowerTypeTestsSummaryAction::Export :
696 LowerTypeTestsSummaryAction::None,
697 Summary));
698#endif
699
700 // Add late LTO optimization passes.
701 // Delete basic blocks, which optimization passes may have killed.
702 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
703
704 // Drop bodies of available eternally objects to improve GlobalDCE.
705 MPM.addPass(EliminateAvailableExternallyPass());
706
707 // Now that we have optimized the program, discard unreachable functions.
708 MPM.addPass(GlobalDCEPass());
709
710 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000711 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000712}
713
Chandler Carruth060ad612016-12-23 20:38:19 +0000714AAManager PassBuilder::buildDefaultAAPipeline() {
715 AAManager AA;
716
717 // The order in which these are registered determines their priority when
718 // being queried.
719
720 // First we register the basic alias analysis that provides the majority of
721 // per-function local AA logic. This is a stateless, on-demand local set of
722 // AA techniques.
723 AA.registerFunctionAnalysis<BasicAA>();
724
725 // Next we query fast, specialized alias analyses that wrap IR-embedded
726 // information about aliasing.
727 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
728 AA.registerFunctionAnalysis<TypeBasedAA>();
729
730 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000731 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
732 // analysis, all that the `AAManager` can do is query for any *cached*
733 // results from `GlobalsAA` through a readonly proxy..
Chandler Carruth060ad612016-12-23 20:38:19 +0000734#if 0
Chandler Carruth534d6442016-12-24 05:11:17 +0000735 // FIXME: Enable once the invalidation logic supports this. Currently, the
736 // `AAManager` will hold stale references to the module analyses.
Chandler Carruth060ad612016-12-23 20:38:19 +0000737 AA.registerModuleAnalysis<GlobalsAA>();
738#endif
739
740 return AA;
741}
742
Chandler Carruth241bf242016-08-03 07:44:48 +0000743static Optional<int> parseRepeatPassName(StringRef Name) {
744 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
745 return None;
746 int Count;
747 if (Name.getAsInteger(0, Count) || Count <= 0)
748 return None;
749 return Count;
750}
751
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000752static Optional<int> parseDevirtPassName(StringRef Name) {
753 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
754 return None;
755 int Count;
756 if (Name.getAsInteger(0, Count) || Count <= 0)
757 return None;
758 return Count;
759}
760
Chandler Carruth66445382014-01-11 08:16:35 +0000761static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000762 // Manually handle aliases for pre-configured pipeline fragments.
763 if (Name.startswith("default") || Name.startswith("lto"))
764 return DefaultAliasRegex.match(Name);
765
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000766 // Explicitly handle pass manager names.
767 if (Name == "module")
768 return true;
769 if (Name == "cgscc")
770 return true;
771 if (Name == "function")
772 return true;
773
Chandler Carruth241bf242016-08-03 07:44:48 +0000774 // Explicitly handle custom-parsed pass names.
775 if (parseRepeatPassName(Name))
776 return true;
777
Chandler Carruth74a8a222016-06-17 07:15:29 +0000778#define MODULE_PASS(NAME, CREATE_PASS) \
779 if (Name == NAME) \
780 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000781#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000782 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000783 return true;
784#include "PassRegistry.def"
785
Chandler Carruth66445382014-01-11 08:16:35 +0000786 return false;
787}
788
Chandler Carruth572e3402014-04-21 11:12:00 +0000789static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000790 // Explicitly handle pass manager names.
791 if (Name == "cgscc")
792 return true;
793 if (Name == "function")
794 return true;
795
Chandler Carruth241bf242016-08-03 07:44:48 +0000796 // Explicitly handle custom-parsed pass names.
797 if (parseRepeatPassName(Name))
798 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000799 if (parseDevirtPassName(Name))
800 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000801
Chandler Carruth74a8a222016-06-17 07:15:29 +0000802#define CGSCC_PASS(NAME, CREATE_PASS) \
803 if (Name == NAME) \
804 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000805#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000806 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000807 return true;
808#include "PassRegistry.def"
809
Chandler Carruth572e3402014-04-21 11:12:00 +0000810 return false;
811}
812
Chandler Carruthd8330982014-01-12 09:34:22 +0000813static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000814 // Explicitly handle pass manager names.
815 if (Name == "function")
816 return true;
817 if (Name == "loop")
818 return true;
819
Chandler Carruth241bf242016-08-03 07:44:48 +0000820 // Explicitly handle custom-parsed pass names.
821 if (parseRepeatPassName(Name))
822 return true;
823
Chandler Carruth74a8a222016-06-17 07:15:29 +0000824#define FUNCTION_PASS(NAME, CREATE_PASS) \
825 if (Name == NAME) \
826 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000827#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000828 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000829 return true;
830#include "PassRegistry.def"
831
Chandler Carruthd8330982014-01-12 09:34:22 +0000832 return false;
833}
834
Justin Bognereecc3c82016-02-25 07:23:08 +0000835static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000836 // Explicitly handle pass manager names.
837 if (Name == "loop")
838 return true;
839
Chandler Carruth241bf242016-08-03 07:44:48 +0000840 // Explicitly handle custom-parsed pass names.
841 if (parseRepeatPassName(Name))
842 return true;
843
Chandler Carruth74a8a222016-06-17 07:15:29 +0000844#define LOOP_PASS(NAME, CREATE_PASS) \
845 if (Name == NAME) \
846 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000847#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
848 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
849 return true;
850#include "PassRegistry.def"
851
852 return false;
853}
854
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000855Optional<std::vector<PassBuilder::PipelineElement>>
856PassBuilder::parsePipelineText(StringRef Text) {
857 std::vector<PipelineElement> ResultPipeline;
858
859 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
860 &ResultPipeline};
861 for (;;) {
862 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
863 size_t Pos = Text.find_first_of(",()");
864 Pipeline.push_back({Text.substr(0, Pos), {}});
865
866 // If we have a single terminating name, we're done.
867 if (Pos == Text.npos)
868 break;
869
870 char Sep = Text[Pos];
871 Text = Text.substr(Pos + 1);
872 if (Sep == ',')
873 // Just a name ending in a comma, continue.
874 continue;
875
876 if (Sep == '(') {
877 // Push the inner pipeline onto the stack to continue processing.
878 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
879 continue;
880 }
881
882 assert(Sep == ')' && "Bogus separator!");
883 // When handling the close parenthesis, we greedily consume them to avoid
884 // empty strings in the pipeline.
885 do {
886 // If we try to pop the outer pipeline we have unbalanced parentheses.
887 if (PipelineStack.size() == 1)
888 return None;
889
890 PipelineStack.pop_back();
891 } while (Text.consume_front(")"));
892
893 // Check if we've finished parsing.
894 if (Text.empty())
895 break;
896
897 // Otherwise, the end of an inner pipeline always has to be followed by
898 // a comma, and then we can continue.
899 if (!Text.consume_front(","))
900 return None;
901 }
902
903 if (PipelineStack.size() > 1)
904 // Unbalanced paretheses.
905 return None;
906
907 assert(PipelineStack.back() == &ResultPipeline &&
908 "Wrong pipeline at the bottom of the stack!");
909 return {std::move(ResultPipeline)};
910}
911
912bool PassBuilder::parseModulePass(ModulePassManager &MPM,
913 const PipelineElement &E, bool VerifyEachPass,
914 bool DebugLogging) {
915 auto &Name = E.Name;
916 auto &InnerPipeline = E.InnerPipeline;
917
918 // First handle complex passes like the pass managers which carry pipelines.
919 if (!InnerPipeline.empty()) {
920 if (Name == "module") {
921 ModulePassManager NestedMPM(DebugLogging);
922 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
923 DebugLogging))
924 return false;
925 MPM.addPass(std::move(NestedMPM));
926 return true;
927 }
928 if (Name == "cgscc") {
929 CGSCCPassManager CGPM(DebugLogging);
930 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
931 DebugLogging))
932 return false;
933 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
934 DebugLogging));
935 return true;
936 }
937 if (Name == "function") {
938 FunctionPassManager FPM(DebugLogging);
939 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
940 DebugLogging))
941 return false;
942 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
943 return true;
944 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000945 if (auto Count = parseRepeatPassName(Name)) {
946 ModulePassManager NestedMPM(DebugLogging);
947 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
948 DebugLogging))
949 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000950 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000951 return true;
952 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000953 // Normal passes can't have pipelines.
954 return false;
955 }
956
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000957 // Manually handle aliases for pre-configured pipeline fragments.
958 if (Name.startswith("default") || Name.startswith("lto")) {
959 SmallVector<StringRef, 3> Matches;
960 if (!DefaultAliasRegex.match(Name, &Matches))
961 return false;
962 assert(Matches.size() == 3 && "Must capture two matched strings!");
963
Jordan Rosef85a95f2016-07-25 18:34:51 +0000964 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
965 .Case("O0", O0)
966 .Case("O1", O1)
967 .Case("O2", O2)
968 .Case("O3", O3)
969 .Case("Os", Os)
970 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000971 if (L == O0)
972 // At O0 we do nothing at all!
973 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000974
975 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000976 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000977 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000978 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000979 } else {
980 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +0000981 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000982 }
983 return true;
984 }
985
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000986 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +0000987#define MODULE_PASS(NAME, CREATE_PASS) \
988 if (Name == NAME) { \
989 MPM.addPass(CREATE_PASS); \
990 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000991 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000992#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
993 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +0000994 MPM.addPass( \
995 RequireAnalysisPass< \
996 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000997 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000998 } \
999 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001000 MPM.addPass(InvalidateAnalysisPass< \
1001 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001002 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001003 }
1004#include "PassRegistry.def"
1005
Chandler Carruth66445382014-01-11 08:16:35 +00001006 return false;
1007}
1008
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001009bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1010 const PipelineElement &E, bool VerifyEachPass,
1011 bool DebugLogging) {
1012 auto &Name = E.Name;
1013 auto &InnerPipeline = E.InnerPipeline;
1014
1015 // First handle complex passes like the pass managers which carry pipelines.
1016 if (!InnerPipeline.empty()) {
1017 if (Name == "cgscc") {
1018 CGSCCPassManager NestedCGPM(DebugLogging);
1019 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1020 DebugLogging))
1021 return false;
1022 // Add the nested pass manager with the appropriate adaptor.
1023 CGPM.addPass(std::move(NestedCGPM));
1024 return true;
1025 }
1026 if (Name == "function") {
1027 FunctionPassManager FPM(DebugLogging);
1028 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1029 DebugLogging))
1030 return false;
1031 // Add the nested pass manager with the appropriate adaptor.
1032 CGPM.addPass(
1033 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1034 return true;
1035 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001036 if (auto Count = parseRepeatPassName(Name)) {
1037 CGSCCPassManager NestedCGPM(DebugLogging);
1038 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1039 DebugLogging))
1040 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001041 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001042 return true;
1043 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001044 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1045 CGSCCPassManager NestedCGPM(DebugLogging);
1046 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1047 DebugLogging))
1048 return false;
1049 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1050 *MaxRepetitions, DebugLogging));
1051 return true;
1052 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001053 // Normal passes can't have pipelines.
1054 return false;
1055 }
1056
1057 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001058#define CGSCC_PASS(NAME, CREATE_PASS) \
1059 if (Name == NAME) { \
1060 CGPM.addPass(CREATE_PASS); \
1061 return true; \
1062 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001063#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1064 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001065 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001066 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001067 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1068 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001069 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001070 } \
1071 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001072 CGPM.addPass(InvalidateAnalysisPass< \
1073 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001074 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001075 }
1076#include "PassRegistry.def"
1077
Chandler Carruth572e3402014-04-21 11:12:00 +00001078 return false;
1079}
1080
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001081bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1082 const PipelineElement &E,
1083 bool VerifyEachPass, bool DebugLogging) {
1084 auto &Name = E.Name;
1085 auto &InnerPipeline = E.InnerPipeline;
1086
1087 // First handle complex passes like the pass managers which carry pipelines.
1088 if (!InnerPipeline.empty()) {
1089 if (Name == "function") {
1090 FunctionPassManager NestedFPM(DebugLogging);
1091 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1092 DebugLogging))
1093 return false;
1094 // Add the nested pass manager with the appropriate adaptor.
1095 FPM.addPass(std::move(NestedFPM));
1096 return true;
1097 }
1098 if (Name == "loop") {
1099 LoopPassManager LPM(DebugLogging);
1100 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1101 DebugLogging))
1102 return false;
1103 // Add the nested pass manager with the appropriate adaptor.
1104 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1105 return true;
1106 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001107 if (auto Count = parseRepeatPassName(Name)) {
1108 FunctionPassManager NestedFPM(DebugLogging);
1109 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1110 DebugLogging))
1111 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001112 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001113 return true;
1114 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001115 // Normal passes can't have pipelines.
1116 return false;
1117 }
1118
1119 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001120#define FUNCTION_PASS(NAME, CREATE_PASS) \
1121 if (Name == NAME) { \
1122 FPM.addPass(CREATE_PASS); \
1123 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001124 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001125#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1126 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001127 FPM.addPass( \
1128 RequireAnalysisPass< \
1129 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001130 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001131 } \
1132 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001133 FPM.addPass(InvalidateAnalysisPass< \
1134 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001135 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001136 }
1137#include "PassRegistry.def"
1138
Chandler Carruthd8330982014-01-12 09:34:22 +00001139 return false;
1140}
1141
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001142bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001143 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001144 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001145 auto &InnerPipeline = E.InnerPipeline;
1146
1147 // First handle complex passes like the pass managers which carry pipelines.
1148 if (!InnerPipeline.empty()) {
1149 if (Name == "loop") {
1150 LoopPassManager NestedLPM(DebugLogging);
1151 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1152 DebugLogging))
1153 return false;
1154 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001155 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001156 return true;
1157 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001158 if (auto Count = parseRepeatPassName(Name)) {
1159 LoopPassManager NestedLPM(DebugLogging);
1160 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1161 DebugLogging))
1162 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001163 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001164 return true;
1165 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001166 // Normal passes can't have pipelines.
1167 return false;
1168 }
1169
1170 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001171#define LOOP_PASS(NAME, CREATE_PASS) \
1172 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001173 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001174 return true; \
1175 }
1176#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1177 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001178 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001179 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1180 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1181 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001182 return true; \
1183 } \
1184 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001185 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001186 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001187 return true; \
1188 }
1189#include "PassRegistry.def"
1190
1191 return false;
1192}
1193
Chandler Carruthedf59962016-02-18 09:45:17 +00001194bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001195#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1196 if (Name == NAME) { \
1197 AA.registerModuleAnalysis< \
1198 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1199 return true; \
1200 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001201#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1202 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001203 AA.registerFunctionAnalysis< \
1204 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001205 return true; \
1206 }
1207#include "PassRegistry.def"
1208
1209 return false;
1210}
1211
Justin Bognereecc3c82016-02-25 07:23:08 +00001212bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001213 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001214 bool VerifyEachPass,
1215 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001216 for (const auto &Element : Pipeline) {
1217 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1218 return false;
1219 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001220 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001221 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001222}
1223
Chandler Carruth1ff77242015-03-07 09:02:36 +00001224bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001225 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001226 bool VerifyEachPass,
1227 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001228 for (const auto &Element : Pipeline) {
1229 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1230 return false;
1231 if (VerifyEachPass)
1232 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001233 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001234 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001235}
1236
Chandler Carruth1ff77242015-03-07 09:02:36 +00001237bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001238 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001239 bool VerifyEachPass,
1240 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001241 for (const auto &Element : Pipeline) {
1242 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1243 return false;
1244 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001245 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001246 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001247}
1248
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001249void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1250 FunctionAnalysisManager &FAM,
1251 CGSCCAnalysisManager &CGAM,
1252 ModuleAnalysisManager &MAM) {
1253 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1254 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001255 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1256 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1257 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1258 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1259 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1260}
1261
Chandler Carruth1ff77242015-03-07 09:02:36 +00001262bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001263 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001264 bool VerifyEachPass,
1265 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001266 for (const auto &Element : Pipeline) {
1267 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1268 return false;
1269 if (VerifyEachPass)
1270 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001271 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001272 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001273}
1274
1275// Primary pass pipeline description parsing routine.
1276// FIXME: Should this routine accept a TargetMachine or require the caller to
1277// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001278bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1279 StringRef PipelineText, bool VerifyEachPass,
1280 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001281 auto Pipeline = parsePipelineText(PipelineText);
1282 if (!Pipeline || Pipeline->empty())
1283 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001284
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001285 // If the first name isn't at the module layer, wrap the pipeline up
1286 // automatically.
1287 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001288
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001289 if (!isModulePassName(FirstName)) {
1290 if (isCGSCCPassName(FirstName))
1291 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1292 else if (isFunctionPassName(FirstName))
1293 Pipeline = {{"function", std::move(*Pipeline)}};
1294 else if (isLoopPassName(FirstName))
1295 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1296 else
1297 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001298 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001299 }
1300
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001301 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001302}
Chandler Carruthedf59962016-02-18 09:45:17 +00001303
1304bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001305 // If the pipeline just consists of the word 'default' just replace the AA
1306 // manager with our default one.
1307 if (PipelineText == "default") {
1308 AA = buildDefaultAAPipeline();
1309 return true;
1310 }
1311
Chandler Carruthedf59962016-02-18 09:45:17 +00001312 while (!PipelineText.empty()) {
1313 StringRef Name;
1314 std::tie(Name, PipelineText) = PipelineText.split(',');
1315 if (!parseAAPassName(AA, Name))
1316 return false;
1317 }
1318
1319 return true;
1320}