blob: bc73cdda88b64a9c88ad4d5794ae03fbc44b1dfa [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"
Daniel Berlin554dcd82017-04-11 20:06:36 +000042#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000043#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet79ac42a2016-07-18 16:29:21 +000044#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000063#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000066#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000067#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000068#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000069#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000070#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000071#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000072#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000073#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000074#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000075#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000076#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000077#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000078#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000079#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000080#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000081#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000082#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000083#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000084#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000085#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000086#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000087#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000088#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000089#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000090#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000091#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000092#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000093#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000094#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000095#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000096#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000097#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000098#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000099#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000100#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000101#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000102#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000103#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000104#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000105#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000106#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000107#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000108#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000109#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000110#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000111#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000112#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000113#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000114#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000115#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000116#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000117#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000118#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000119#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000120#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000121#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000122#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000123#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000124#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000125#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000126#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000127#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000128#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000129#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000130#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000131#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000132#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000133#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000134#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000135#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000136#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000137#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000138#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000139#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000140#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000141#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000142#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000143#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000144#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000145#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000146
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000147#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000148
149using namespace llvm;
150
Chandler Carruth719ffe12017-02-12 05:38:04 +0000151static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
152 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000153static cl::opt<bool>
154 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
155 cl::Hidden, cl::ZeroOrMore,
156 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000157
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000158static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000159 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000160 cl::Hidden, cl::ZeroOrMore,
161 cl::desc("Run NewGVN instead of GVN"));
162
Geoff Berry3cca1da2017-06-10 15:20:03 +0000163static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000164 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
165 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000166
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000167static cl::opt<bool> EnableGVNHoist(
168 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
169 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
170
Davide Italianobe1b6a92017-06-03 23:18:29 +0000171static cl::opt<bool> EnableGVNSink(
172 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
173 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
174
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000175static Regex DefaultAliasRegex(
176 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000177
Chandler Carruthe3f50642016-12-22 06:59:15 +0000178static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
179 switch (Level) {
180 case PassBuilder::O0:
181 case PassBuilder::O1:
182 case PassBuilder::O2:
183 case PassBuilder::O3:
184 return false;
185
186 case PassBuilder::Os:
187 case PassBuilder::Oz:
188 return true;
189 }
190 llvm_unreachable("Invalid optimization level!");
191}
192
Chandler Carruth66445382014-01-11 08:16:35 +0000193namespace {
194
Chandler Carruthd8330982014-01-12 09:34:22 +0000195/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000196struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000197 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000198 return PreservedAnalyses::all();
199 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000200 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000201};
202
Chandler Carruth0b576b32015-01-06 02:50:06 +0000203/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000204class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
205 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000206 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000207
208public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000209 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000210 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000211 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212};
213
Chandler Carruth572e3402014-04-21 11:12:00 +0000214/// \brief No-op CGSCC pass which does nothing.
215struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000216 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
217 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000218 return PreservedAnalyses::all();
219 }
220 static StringRef name() { return "NoOpCGSCCPass"; }
221};
222
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000224class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
225 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000226 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000227
228public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000229 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000230 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000231 return Result();
232 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000233 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234};
235
Chandler Carruthd8330982014-01-12 09:34:22 +0000236/// \brief No-op function pass which does nothing.
237struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000238 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000239 return PreservedAnalyses::all();
240 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000241 static StringRef name() { return "NoOpFunctionPass"; }
242};
243
Chandler Carruth0b576b32015-01-06 02:50:06 +0000244/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000245class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
246 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000247 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000248
249public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000250 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000251 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000252 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253};
254
Justin Bognereecc3c82016-02-25 07:23:08 +0000255/// \brief No-op loop pass which does nothing.
256struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000257 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
258 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000259 return PreservedAnalyses::all();
260 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000261 static StringRef name() { return "NoOpLoopPass"; }
262};
263
264/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000265class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
266 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000267 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000268
269public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000270 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000271 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
272 return Result();
273 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000274 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000275};
276
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000277AnalysisKey NoOpModuleAnalysis::Key;
278AnalysisKey NoOpCGSCCAnalysis::Key;
279AnalysisKey NoOpFunctionAnalysis::Key;
280AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000281
Chandler Carruth66445382014-01-11 08:16:35 +0000282} // End anonymous namespace.
283
Chandler Carruth1ff77242015-03-07 09:02:36 +0000284void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000285#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000286 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000287#include "PassRegistry.def"
288}
289
Chandler Carruth1ff77242015-03-07 09:02:36 +0000290void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000291#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000292 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000293#include "PassRegistry.def"
294}
295
Chandler Carruth1ff77242015-03-07 09:02:36 +0000296void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000297#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000298 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000299#include "PassRegistry.def"
300}
301
Justin Bognereecc3c82016-02-25 07:23:08 +0000302void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000303#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000304 LAM.registerPass([&] { return CREATE_PASS; });
305#include "PassRegistry.def"
306}
307
Chandler Carruthe3f50642016-12-22 06:59:15 +0000308FunctionPassManager
309PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
310 bool DebugLogging) {
311 assert(Level != O0 && "Must request optimizations!");
312 FunctionPassManager FPM(DebugLogging);
313
314 // Form SSA out of local memory accesses after breaking apart aggregates into
315 // scalars.
316 FPM.addPass(SROA());
317
318 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000319 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000320
Davide Italianoc3688312017-06-01 23:08:14 +0000321 // Hoisting of scalars and load expressions.
322 if (EnableGVNHoist)
323 FPM.addPass(GVNHoistPass());
324
Davide Italianobe1b6a92017-06-03 23:18:29 +0000325 // Global value numbering based sinking.
326 if (EnableGVNSink) {
327 FPM.addPass(GVNSinkPass());
328 FPM.addPass(SimplifyCFGPass());
329 }
330
Chandler Carruthe3f50642016-12-22 06:59:15 +0000331 // Speculative execution if the target has divergent branches; otherwise nop.
332 FPM.addPass(SpeculativeExecutionPass());
333
334 // Optimize based on known information about branches, and cleanup afterward.
335 FPM.addPass(JumpThreadingPass());
336 FPM.addPass(CorrelatedValuePropagationPass());
337 FPM.addPass(SimplifyCFGPass());
338 FPM.addPass(InstCombinePass());
339
340 if (!isOptimizingForSize(Level))
341 FPM.addPass(LibCallsShrinkWrapPass());
342
343 FPM.addPass(TailCallElimPass());
344 FPM.addPass(SimplifyCFGPass());
345
346 // Form canonically associated expression trees, and simplify the trees using
347 // basic mathematical properties. For example, this will form (nearly)
348 // minimal multiplication trees.
349 FPM.addPass(ReassociatePass());
350
351 // Add the primary loop simplification pipeline.
352 // FIXME: Currently this is split into two loop pass pipelines because we run
353 // some function passes in between them. These can and should be replaced by
354 // loop pass equivalenst but those aren't ready yet. Specifically,
355 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
356 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
357 // the other we have is `LoopInstSimplify`.
358 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
359
360 // Rotate Loop - disable header duplication at -Oz
361 LPM1.addPass(LoopRotatePass(Level != Oz));
362 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000363 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000364 LPM2.addPass(IndVarSimplifyPass());
365 LPM2.addPass(LoopIdiomRecognizePass());
366 LPM2.addPass(LoopDeletionPass());
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000367 // FIXME: The old pass manager has a hack to disable loop unrolling during
368 // ThinLTO when using sample PGO. Need to either fix it or port some
369 // workaround.
Dehao Chen7d230322017-02-18 03:46:51 +0000370 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000371
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000372 // We provide the opt remark emitter pass for LICM to use. We only need to do
373 // this once as it is immutable.
374 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000375 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
376 FPM.addPass(SimplifyCFGPass());
377 FPM.addPass(InstCombinePass());
378 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
379
380 // Eliminate redundancies.
381 if (Level != O1) {
382 // These passes add substantial compile time so skip them at O1.
383 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000384 if (RunNewGVN)
385 FPM.addPass(NewGVNPass());
386 else
387 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000388 }
389
390 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
391 FPM.addPass(MemCpyOptPass());
392
393 // Sparse conditional constant propagation.
394 // FIXME: It isn't clear why we do this *after* loop passes rather than
395 // before...
396 FPM.addPass(SCCPPass());
397
398 // Delete dead bit computations (instcombine runs after to fold away the dead
399 // computations, and then ADCE will run later to exploit any new DCE
400 // opportunities that creates).
401 FPM.addPass(BDCEPass());
402
403 // Run instcombine after redundancy and dead bit elimination to exploit
404 // opportunities opened up by them.
405 FPM.addPass(InstCombinePass());
406
407 // Re-consider control flow based optimizations after redundancy elimination,
408 // redo DCE, etc.
409 FPM.addPass(JumpThreadingPass());
410 FPM.addPass(CorrelatedValuePropagationPass());
411 FPM.addPass(DSEPass());
412 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
413
414 // Finally, do an expensive DCE pass to catch all the dead code exposed by
415 // the simplifications and basic cleanup after all the simplifications.
416 FPM.addPass(ADCEPass());
417 FPM.addPass(SimplifyCFGPass());
418 FPM.addPass(InstCombinePass());
419
420 return FPM;
421}
422
Davide Italiano513dfaa2017-02-13 15:26:22 +0000423static void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
424 PassBuilder::OptimizationLevel Level,
425 bool RunProfileGen, std::string ProfileGenFile,
426 std::string ProfileUseFile) {
427 // Generally running simplification passes and the inliner with an high
428 // threshold results in smaller executables, but there may be cases where
429 // the size grows, so let's be conservative here and skip this simplification
430 // at -Os/Oz.
431 if (!isOptimizingForSize(Level)) {
432 InlineParams IP;
433
434 // In the old pass manager, this is a cl::opt. Should still this be one?
435 IP.DefaultThreshold = 75;
436
437 // FIXME: The hint threshold has the same value used by the regular inliner.
438 // This should probably be lowered after performance testing.
439 // FIXME: this comment is cargo culted from the old pass manager, revisit).
440 IP.HintThreshold = 325;
441
442 CGSCCPassManager CGPipeline(DebugLogging);
443
444 CGPipeline.addPass(InlinerPass(IP));
445
446 FunctionPassManager FPM;
447 FPM.addPass(SROA());
448 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
449 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
450 FPM.addPass(InstCombinePass()); // Combine silly sequences.
451
452 // FIXME: Here the old pass manager inserts peephole extensions.
453 // Add them when they're supported.
454 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
455
456 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
457 }
458
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000459 // Delete anything that is now dead to make sure that we don't instrument
460 // dead code. Instrumentation can end up keeping dead code around and
461 // dramatically increase code size.
462 MPM.addPass(GlobalDCEPass());
463
Davide Italiano513dfaa2017-02-13 15:26:22 +0000464 if (RunProfileGen) {
465 MPM.addPass(PGOInstrumentationGen());
466
Xinliang David Lib67530e2017-06-25 00:26:43 +0000467 FunctionPassManager FPM;
468 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
469 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
470
Davide Italiano513dfaa2017-02-13 15:26:22 +0000471 // Add the profile lowering pass.
472 InstrProfOptions Options;
473 if (!ProfileGenFile.empty())
474 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000475 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000476 MPM.addPass(InstrProfiling(Options));
477 }
478
479 if (!ProfileUseFile.empty())
480 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
481}
482
Easwaran Raman8249fac2017-06-28 13:33:49 +0000483static InlineParams
484getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
485 auto O3 = PassBuilder::O3;
486 unsigned OptLevel = Level > O3 ? 2 : Level;
487 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
488 return getInlineParams(OptLevel, SizeLevel);
489}
490
Chandler Carruthe3f50642016-12-22 06:59:15 +0000491ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000492PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
493 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000494 ModulePassManager MPM(DebugLogging);
495
Chandler Carruthe3f50642016-12-22 06:59:15 +0000496 // Do basic inference of function attributes from known properties of system
497 // libraries and other oracles.
498 MPM.addPass(InferFunctionAttrsPass());
499
500 // Create an early function pass manager to cleanup the output of the
501 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000502 FunctionPassManager EarlyFPM(DebugLogging);
503 EarlyFPM.addPass(SimplifyCFGPass());
504 EarlyFPM.addPass(SROA());
505 EarlyFPM.addPass(EarlyCSEPass());
506 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000507 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000508
509 // Interprocedural constant propagation now that basic cleanup has occured
510 // and prior to optimizing globals.
511 // FIXME: This position in the pipeline hasn't been carefully considered in
512 // years, it should be re-analyzed.
513 MPM.addPass(IPSCCPPass());
514
515 // Optimize globals to try and fold them into constants.
516 MPM.addPass(GlobalOptPass());
517
518 // Promote any localized globals to SSA registers.
519 // FIXME: Should this instead by a run of SROA?
520 // FIXME: We should probably run instcombine and simplify-cfg afterward to
521 // delete control flows that are dead once globals have been folded to
522 // constants.
523 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
524
525 // Remove any dead arguments exposed by cleanups and constand folding
526 // globals.
527 MPM.addPass(DeadArgumentEliminationPass());
528
529 // Create a small function pass pipeline to cleanup after all the global
530 // optimizations.
531 FunctionPassManager GlobalCleanupPM(DebugLogging);
532 GlobalCleanupPM.addPass(InstCombinePass());
533 GlobalCleanupPM.addPass(SimplifyCFGPass());
534 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
535
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000536 // Add all the requested passes for PGO, if requested.
Davide Italiano513dfaa2017-02-13 15:26:22 +0000537 if (PGOOpt) {
Dehao Chencc75d242017-02-23 22:15:18 +0000538 assert(PGOOpt->RunProfileGen || PGOOpt->SamplePGO ||
539 !PGOOpt->ProfileUseFile.empty());
Davide Italiano513dfaa2017-02-13 15:26:22 +0000540 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
541 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000542
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000543 // Indirect call promotion that promotes intra-module targes only.
544 MPM.addPass(PGOIndirectCallPromotion(false, PGOOpt && PGOOpt->SamplePGO));
545 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000546
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000547 // Require the GlobalsAA analysis for the module so we can query it within
548 // the CGSCC pipeline.
549 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000550
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000551 // Require the ProfileSummaryAnalysis for the module so we can query it within
552 // the inliner pass.
553 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
554
Chandler Carruthe3f50642016-12-22 06:59:15 +0000555 // Now begin the main postorder CGSCC pipeline.
556 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
557 // manager and trying to emulate its precise behavior. Much of this doesn't
558 // make a lot of sense and we should revisit the core CGSCC structure.
559 CGSCCPassManager MainCGPipeline(DebugLogging);
560
561 // Note: historically, the PruneEH pass was run first to deduce nounwind and
562 // generally clean up exception handling overhead. It isn't clear this is
563 // valuable as the inliner doesn't currently care whether it is inlining an
564 // invoke or a call.
565
566 // Run the inliner first. The theory is that we are walking bottom-up and so
567 // the callees have already been fully optimized, and we want to inline them
568 // into the callers so that our optimizations can reflect that.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000569 MainCGPipeline.addPass(InlinerPass(getInlineParamsFromOptLevel(Level)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000570
571 // Now deduce any function attributes based in the current code.
572 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
573
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000574 // When at O3 add argument promotion to the pass pipeline.
575 // FIXME: It isn't at all clear why this should be limited to O3.
576 if (Level == O3)
577 MainCGPipeline.addPass(ArgumentPromotionPass());
578
Chandler Carruthe3f50642016-12-22 06:59:15 +0000579 // Lastly, add the core function simplification pipeline nested inside the
580 // CGSCC walk.
581 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
582 buildFunctionSimplificationPipeline(Level, DebugLogging)));
583
Chandler Carruth719ffe12017-02-12 05:38:04 +0000584 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
585 // to detect when we devirtualize indirect calls and iterate the SCC passes
586 // in that case to try and catch knock-on inlining or function attrs
587 // opportunities. Then we add it to the module pipeline by walking the SCCs
588 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000589 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000590 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
591 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000592
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000593 return MPM;
594}
595
596ModulePassManager
597PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
598 bool DebugLogging) {
599 ModulePassManager MPM(DebugLogging);
600
601 // Optimize globals now that the module is fully simplified.
602 MPM.addPass(GlobalOptPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000603
Xinliang David Li126157c2017-05-22 16:41:57 +0000604 // Run partial inlining pass to partially inline functions that have
605 // large bodies.
606 if (RunPartialInlining)
607 MPM.addPass(PartialInlinerPass());
608
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000609 // Remove avail extern fns and globals definitions since we aren't compiling
610 // an object file for later LTO. For LTO we want to preserve these so they
611 // are eligible for inlining at link-time. Note if they are unreferenced they
612 // will be removed by GlobalDCE later, so this only impacts referenced
613 // available externally globals. Eventually they will be suppressed during
614 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
615 // may make globals referenced by available external functions dead and saves
616 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000617 MPM.addPass(EliminateAvailableExternallyPass());
618
619 // Do RPO function attribute inference across the module to forward-propagate
620 // attributes where applicable.
621 // FIXME: Is this really an optimization rather than a canonicalization?
622 MPM.addPass(ReversePostOrderFunctionAttrsPass());
623
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000624 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000625 // useful as the above will have inlined, DCE'ed, and function-attr
626 // propagated everything. We should at this point have a reasonably minimal
627 // and richly annotated call graph. By computing aliasing and mod/ref
628 // information for all local globals here, the late loop passes and notably
629 // the vectorizer will be able to use them to help recognize vectorizable
630 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000631 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000632
633 FunctionPassManager OptimizePM(DebugLogging);
634 OptimizePM.addPass(Float2IntPass());
635 // FIXME: We need to run some loop optimizations to re-rotate loops after
636 // simplify-cfg and others undo their rotation.
637
638 // Optimize the loop execution. These passes operate on entire loop nests
639 // rather than on each loop in an inside-out manner, and so they are actually
640 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000641
642 // First rotate loops that may have been un-rotated by prior passes.
643 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
644
645 // Distribute loops to allow partial vectorization. I.e. isolate dependences
646 // into separate loop that would otherwise inhibit vectorization. This is
647 // currently only performed for loops marked with the metadata
648 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000649 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000650
651 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000652 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000653
Chandler Carruthbaabda92017-01-27 01:32:26 +0000654 // Eliminate loads by forwarding stores from the previous iteration to loads
655 // of the current iteration.
656 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000657
658 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000659 OptimizePM.addPass(InstCombinePass());
660
Chandler Carrutha95ff382017-01-27 00:50:21 +0000661
662 // Now that we've formed fast to execute loop structures, we do further
663 // optimizations. These are run afterward as they might block doing complex
664 // analyses and transforms such as what are needed for loop vectorization.
665
Chandler Carruthe3f50642016-12-22 06:59:15 +0000666 // Optimize parallel scalar instruction chains into SIMD instructions.
667 OptimizePM.addPass(SLPVectorizerPass());
668
Chandler Carrutha95ff382017-01-27 00:50:21 +0000669 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000670 OptimizePM.addPass(SimplifyCFGPass());
671 OptimizePM.addPass(InstCombinePass());
672
673 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000674 // execution resources of an out-of-order processor. We also then need to
675 // clean up redundancies and loop invariant code.
676 // FIXME: It would be really good to use a loop-integrated instruction
677 // combiner for cleanup here so that the unrolling and LICM can be pipelined
678 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000679 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000680 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000681 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000682 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000683
684 // Now that we've vectorized and unrolled loops, we may have more refined
685 // alignment information, try to re-derive it here.
686 OptimizePM.addPass(AlignmentFromAssumptionsPass());
687
Chandler Carrutha95ff382017-01-27 00:50:21 +0000688 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
689 // canonicalization pass that enables other optimizations. As a result,
690 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
691 // result too early.
692 OptimizePM.addPass(LoopSinkPass());
693
694 // And finally clean up LCSSA form before generating code.
695 OptimizePM.addPass(InstSimplifierPass());
696
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000697 // LoopSink (and other loop passes since the last simplifyCFG) might have
698 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
699 OptimizePM.addPass(SimplifyCFGPass());
700
Chandler Carrutha95ff382017-01-27 00:50:21 +0000701 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000702 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
703
704 // Now we need to do some global optimization transforms.
705 // FIXME: It would seem like these should come first in the optimization
706 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
707 // ordering here.
708 MPM.addPass(GlobalDCEPass());
709 MPM.addPass(ConstantMergePass());
710
711 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000712}
713
Chandler Carruthe3f50642016-12-22 06:59:15 +0000714ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000715PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
716 bool DebugLogging) {
717 assert(Level != O0 && "Must request optimizations for the default pipeline!");
718
719 ModulePassManager MPM(DebugLogging);
720
721 // Force any function attributes we want the rest of the pipeline to observe.
722 MPM.addPass(ForceFunctionAttrsPass());
723
724 // Add the core simplification pipeline.
725 MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging));
726
727 // Now add the optimization pipeline.
728 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
729
730 return MPM;
731}
732
733ModulePassManager
734PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
735 bool DebugLogging) {
736 assert(Level != O0 && "Must request optimizations for the default pipeline!");
737
738 ModulePassManager MPM(DebugLogging);
739
740 // Force any function attributes we want the rest of the pipeline to observe.
741 MPM.addPass(ForceFunctionAttrsPass());
742
743 // If we are planning to perform ThinLTO later, we don't bloat the code with
744 // unrolling/vectorization/... now. Just simplify the module as much as we
745 // can.
746 MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging));
747
748 // Run partial inlining pass to partially inline functions that have
749 // large bodies.
750 // FIXME: It isn't clear whether this is really the right place to run this
751 // in ThinLTO. Because there is another canonicalization and simplification
752 // phase that will run after the thin link, running this here ends up with
753 // less information than will be available later and it may grow functions in
754 // ways that aren't beneficial.
755 if (RunPartialInlining)
756 MPM.addPass(PartialInlinerPass());
757
758 // Reduce the size of the IR as much as possible.
759 MPM.addPass(GlobalOptPass());
760
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000761 return MPM;
762}
763
764ModulePassManager
765PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
766 bool DebugLogging) {
767 // FIXME: The summary index is not hooked in the new pass manager yet.
768 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
769 // here.
770
771 ModulePassManager MPM(DebugLogging);
772
773 // Force any function attributes we want the rest of the pipeline to observe.
774 MPM.addPass(ForceFunctionAttrsPass());
775
776 // During the ThinLTO backend phase we perform early indirect call promotion
777 // here, before globalopt. Otherwise imported available_externally functions
778 // look unreferenced and are removed.
779 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
780 PGOOpt && PGOOpt->SamplePGO &&
781 !PGOOpt->ProfileUseFile.empty()));
782
783 // Add the core simplification pipeline.
784 MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging));
785
786 // Now add the optimization pipeline.
787 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
788
789 return MPM;
790}
791
792ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000793PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
794 bool DebugLogging) {
795 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000796 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000797 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000798}
799
Chandler Carruthe3f50642016-12-22 06:59:15 +0000800ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
801 bool DebugLogging) {
802 assert(Level != O0 && "Must request optimizations for the default pipeline!");
803 ModulePassManager MPM(DebugLogging);
804
Davide Italiano089a9122017-01-24 00:57:39 +0000805 // Remove unused virtual tables to improve the quality of code generated by
806 // whole-program devirtualization and bitset lowering.
807 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000808
Davide Italiano089a9122017-01-24 00:57:39 +0000809 // Force any function attributes we want the rest of the pipeline to observe.
810 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000811
Davide Italiano089a9122017-01-24 00:57:39 +0000812 // Do basic inference of function attributes from known properties of system
813 // libraries and other oracles.
814 MPM.addPass(InferFunctionAttrsPass());
815
816 if (Level > 1) {
817 // Indirect call promotion. This should promote all the targets that are
818 // left by the earlier promotion pass that promotes intra-module targets.
819 // This two-step promotion is to save the compile time. For LTO, it should
820 // produce the same result as if we only do promotion here.
Dehao Chencc75d242017-02-23 22:15:18 +0000821 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
822 PGOOpt && PGOOpt->SamplePGO));
Davide Italiano089a9122017-01-24 00:57:39 +0000823
824 // Propagate constants at call sites into the functions they call. This
825 // opens opportunities for globalopt (and inlining) by substituting function
826 // pointers passed as arguments to direct uses of functions.
827 MPM.addPass(IPSCCPPass());
828 }
829
830 // Now deduce any function attributes based in the current code.
831 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
832 PostOrderFunctionAttrsPass()));
833
834 // Do RPO function attribute inference across the module to forward-propagate
835 // attributes where applicable.
836 // FIXME: Is this really an optimization rather than a canonicalization?
837 MPM.addPass(ReversePostOrderFunctionAttrsPass());
838
839 // Use inragne annotations on GEP indices to split globals where beneficial.
840 MPM.addPass(GlobalSplitPass());
841
842 // Run whole program optimization of virtual call when the list of callees
843 // is fixed.
844 MPM.addPass(WholeProgramDevirtPass());
845
846 // Stop here at -O1.
847 if (Level == 1)
848 return MPM;
849
850 // Optimize globals to try and fold them into constants.
851 MPM.addPass(GlobalOptPass());
852
853 // Promote any localized globals to SSA registers.
854 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
855
856 // Linking modules together can lead to duplicate global constant, only
857 // keep one copy of each constant.
858 MPM.addPass(ConstantMergePass());
859
860 // Remove unused arguments from functions.
861 MPM.addPass(DeadArgumentEliminationPass());
862
863 // Reduce the code after globalopt and ipsccp. Both can open up significant
864 // simplification opportunities, and both can propagate functions through
865 // function pointers. When this happens, we often have to resolve varargs
866 // calls, etc, so let instcombine do this.
867 // FIXME: add peephole extensions here as the legacy PM does.
868 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
869
870 // Note: historically, the PruneEH pass was run first to deduce nounwind and
871 // generally clean up exception handling overhead. It isn't clear this is
872 // valuable as the inliner doesn't currently care whether it is inlining an
873 // invoke or a call.
874 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000875 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
876 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000877
878 // Optimize globals again after we ran the inliner.
879 MPM.addPass(GlobalOptPass());
880
881 // Garbage collect dead functions.
882 // FIXME: Add ArgumentPromotion pass after once it's ported.
883 MPM.addPass(GlobalDCEPass());
884
885 FunctionPassManager FPM(DebugLogging);
886
887 // The IPO Passes may leave cruft around. Clean up after them.
888 // FIXME: add peephole extensions here as the legacy PM does.
889 FPM.addPass(InstCombinePass());
890 FPM.addPass(JumpThreadingPass());
891
892 // Break up allocas
893 FPM.addPass(SROA());
894
895 // Run a few AA driver optimizations here and now to cleanup the code.
896 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
897
898 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
899 PostOrderFunctionAttrsPass()));
900 // FIXME: here we run IP alias analysis in the legacy PM.
901
902 FunctionPassManager MainFPM;
903
904 // FIXME: once we fix LoopPass Manager, add LICM here.
905 // FIXME: once we provide support for enabling MLSM, add it here.
906 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000907 if (RunNewGVN)
908 MainFPM.addPass(NewGVNPass());
909 else
910 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +0000911
912 // Remove dead memcpy()'s.
913 MainFPM.addPass(MemCpyOptPass());
914
915 // Nuke dead stores.
916 MainFPM.addPass(DSEPass());
917
918 // FIXME: at this point, we run a bunch of loop passes:
919 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
920 // loopVectorize. Enable them once the remaining issue with LPM
921 // are sorted out.
922
923 MainFPM.addPass(InstCombinePass());
924 MainFPM.addPass(SimplifyCFGPass());
925 MainFPM.addPass(SCCPPass());
926 MainFPM.addPass(InstCombinePass());
927 MainFPM.addPass(BDCEPass());
928
929 // FIXME: We may want to run SLPVectorizer here.
930 // After vectorization, assume intrinsics may tell us more
931 // about pointer alignments.
932#if 0
933 MainFPM.add(AlignmentFromAssumptionsPass());
934#endif
935
Davide Italiano089a9122017-01-24 00:57:39 +0000936 // FIXME: add peephole extensions to the PM here.
937 MainFPM.addPass(InstCombinePass());
938 MainFPM.addPass(JumpThreadingPass());
939 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
940
941 // Create a function that performs CFI checks for cross-DSO calls with
942 // targets in the current module.
943 MPM.addPass(CrossDSOCFIPass());
944
945 // Lower type metadata and the type.test intrinsic. This pass supports
946 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
947 // to be run at link time if CFI is enabled. This pass does nothing if
948 // CFI is disabled.
949 // Enable once we add support for the summary in the new PM.
950#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000951 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
952 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000953 Summary));
954#endif
955
956 // Add late LTO optimization passes.
957 // Delete basic blocks, which optimization passes may have killed.
958 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
959
960 // Drop bodies of available eternally objects to improve GlobalDCE.
961 MPM.addPass(EliminateAvailableExternallyPass());
962
963 // Now that we have optimized the program, discard unreachable functions.
964 MPM.addPass(GlobalDCEPass());
965
966 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000967 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000968}
969
Chandler Carruth060ad612016-12-23 20:38:19 +0000970AAManager PassBuilder::buildDefaultAAPipeline() {
971 AAManager AA;
972
973 // The order in which these are registered determines their priority when
974 // being queried.
975
976 // First we register the basic alias analysis that provides the majority of
977 // per-function local AA logic. This is a stateless, on-demand local set of
978 // AA techniques.
979 AA.registerFunctionAnalysis<BasicAA>();
980
981 // Next we query fast, specialized alias analyses that wrap IR-embedded
982 // information about aliasing.
983 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
984 AA.registerFunctionAnalysis<TypeBasedAA>();
985
986 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000987 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
988 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000989 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +0000990 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +0000991
992 return AA;
993}
994
Chandler Carruth241bf242016-08-03 07:44:48 +0000995static Optional<int> parseRepeatPassName(StringRef Name) {
996 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
997 return None;
998 int Count;
999 if (Name.getAsInteger(0, Count) || Count <= 0)
1000 return None;
1001 return Count;
1002}
1003
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001004static Optional<int> parseDevirtPassName(StringRef Name) {
1005 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1006 return None;
1007 int Count;
1008 if (Name.getAsInteger(0, Count) || Count <= 0)
1009 return None;
1010 return Count;
1011}
1012
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001013/// Tests whether a pass name starts with a valid prefix for a default pipeline
1014/// alias.
1015static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1016 return Name.startswith("default") || Name.startswith("thinlto") ||
1017 Name.startswith("lto");
1018}
1019
Chandler Carruth66445382014-01-11 08:16:35 +00001020static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001021 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001022 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001023 return DefaultAliasRegex.match(Name);
1024
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001025 // Explicitly handle pass manager names.
1026 if (Name == "module")
1027 return true;
1028 if (Name == "cgscc")
1029 return true;
1030 if (Name == "function")
1031 return true;
1032
Chandler Carruth241bf242016-08-03 07:44:48 +00001033 // Explicitly handle custom-parsed pass names.
1034 if (parseRepeatPassName(Name))
1035 return true;
1036
Chandler Carruth74a8a222016-06-17 07:15:29 +00001037#define MODULE_PASS(NAME, CREATE_PASS) \
1038 if (Name == NAME) \
1039 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001040#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001041 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001042 return true;
1043#include "PassRegistry.def"
1044
Chandler Carruth66445382014-01-11 08:16:35 +00001045 return false;
1046}
1047
Chandler Carruth572e3402014-04-21 11:12:00 +00001048static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001049 // Explicitly handle pass manager names.
1050 if (Name == "cgscc")
1051 return true;
1052 if (Name == "function")
1053 return true;
1054
Chandler Carruth241bf242016-08-03 07:44:48 +00001055 // Explicitly handle custom-parsed pass names.
1056 if (parseRepeatPassName(Name))
1057 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001058 if (parseDevirtPassName(Name))
1059 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001060
Chandler Carruth74a8a222016-06-17 07:15:29 +00001061#define CGSCC_PASS(NAME, CREATE_PASS) \
1062 if (Name == NAME) \
1063 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001064#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001065 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001066 return true;
1067#include "PassRegistry.def"
1068
Chandler Carruth572e3402014-04-21 11:12:00 +00001069 return false;
1070}
1071
Chandler Carruthd8330982014-01-12 09:34:22 +00001072static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001073 // Explicitly handle pass manager names.
1074 if (Name == "function")
1075 return true;
1076 if (Name == "loop")
1077 return true;
1078
Chandler Carruth241bf242016-08-03 07:44:48 +00001079 // Explicitly handle custom-parsed pass names.
1080 if (parseRepeatPassName(Name))
1081 return true;
1082
Chandler Carruth74a8a222016-06-17 07:15:29 +00001083#define FUNCTION_PASS(NAME, CREATE_PASS) \
1084 if (Name == NAME) \
1085 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001086#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001087 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001088 return true;
1089#include "PassRegistry.def"
1090
Chandler Carruthd8330982014-01-12 09:34:22 +00001091 return false;
1092}
1093
Justin Bognereecc3c82016-02-25 07:23:08 +00001094static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001095 // Explicitly handle pass manager names.
1096 if (Name == "loop")
1097 return true;
1098
Chandler Carruth241bf242016-08-03 07:44:48 +00001099 // Explicitly handle custom-parsed pass names.
1100 if (parseRepeatPassName(Name))
1101 return true;
1102
Chandler Carruth74a8a222016-06-17 07:15:29 +00001103#define LOOP_PASS(NAME, CREATE_PASS) \
1104 if (Name == NAME) \
1105 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001106#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1107 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1108 return true;
1109#include "PassRegistry.def"
1110
1111 return false;
1112}
1113
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001114Optional<std::vector<PassBuilder::PipelineElement>>
1115PassBuilder::parsePipelineText(StringRef Text) {
1116 std::vector<PipelineElement> ResultPipeline;
1117
1118 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1119 &ResultPipeline};
1120 for (;;) {
1121 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1122 size_t Pos = Text.find_first_of(",()");
1123 Pipeline.push_back({Text.substr(0, Pos), {}});
1124
1125 // If we have a single terminating name, we're done.
1126 if (Pos == Text.npos)
1127 break;
1128
1129 char Sep = Text[Pos];
1130 Text = Text.substr(Pos + 1);
1131 if (Sep == ',')
1132 // Just a name ending in a comma, continue.
1133 continue;
1134
1135 if (Sep == '(') {
1136 // Push the inner pipeline onto the stack to continue processing.
1137 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1138 continue;
1139 }
1140
1141 assert(Sep == ')' && "Bogus separator!");
1142 // When handling the close parenthesis, we greedily consume them to avoid
1143 // empty strings in the pipeline.
1144 do {
1145 // If we try to pop the outer pipeline we have unbalanced parentheses.
1146 if (PipelineStack.size() == 1)
1147 return None;
1148
1149 PipelineStack.pop_back();
1150 } while (Text.consume_front(")"));
1151
1152 // Check if we've finished parsing.
1153 if (Text.empty())
1154 break;
1155
1156 // Otherwise, the end of an inner pipeline always has to be followed by
1157 // a comma, and then we can continue.
1158 if (!Text.consume_front(","))
1159 return None;
1160 }
1161
1162 if (PipelineStack.size() > 1)
1163 // Unbalanced paretheses.
1164 return None;
1165
1166 assert(PipelineStack.back() == &ResultPipeline &&
1167 "Wrong pipeline at the bottom of the stack!");
1168 return {std::move(ResultPipeline)};
1169}
1170
1171bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1172 const PipelineElement &E, bool VerifyEachPass,
1173 bool DebugLogging) {
1174 auto &Name = E.Name;
1175 auto &InnerPipeline = E.InnerPipeline;
1176
1177 // First handle complex passes like the pass managers which carry pipelines.
1178 if (!InnerPipeline.empty()) {
1179 if (Name == "module") {
1180 ModulePassManager NestedMPM(DebugLogging);
1181 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1182 DebugLogging))
1183 return false;
1184 MPM.addPass(std::move(NestedMPM));
1185 return true;
1186 }
1187 if (Name == "cgscc") {
1188 CGSCCPassManager CGPM(DebugLogging);
1189 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1190 DebugLogging))
1191 return false;
1192 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1193 DebugLogging));
1194 return true;
1195 }
1196 if (Name == "function") {
1197 FunctionPassManager FPM(DebugLogging);
1198 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1199 DebugLogging))
1200 return false;
1201 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1202 return true;
1203 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001204 if (auto Count = parseRepeatPassName(Name)) {
1205 ModulePassManager NestedMPM(DebugLogging);
1206 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1207 DebugLogging))
1208 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001209 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001210 return true;
1211 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001212 // Normal passes can't have pipelines.
1213 return false;
1214 }
1215
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001216 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001217 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001218 SmallVector<StringRef, 3> Matches;
1219 if (!DefaultAliasRegex.match(Name, &Matches))
1220 return false;
1221 assert(Matches.size() == 3 && "Must capture two matched strings!");
1222
Jordan Rosef85a95f2016-07-25 18:34:51 +00001223 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1224 .Case("O0", O0)
1225 .Case("O1", O1)
1226 .Case("O2", O2)
1227 .Case("O3", O3)
1228 .Case("Os", Os)
1229 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001230 if (L == O0)
1231 // At O0 we do nothing at all!
1232 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001233
1234 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001235 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001236 } else if (Matches[1] == "thinlto-pre-link") {
1237 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1238 } else if (Matches[1] == "thinlto") {
1239 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001240 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001241 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001242 } else {
1243 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001244 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001245 }
1246 return true;
1247 }
1248
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001249 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001250#define MODULE_PASS(NAME, CREATE_PASS) \
1251 if (Name == NAME) { \
1252 MPM.addPass(CREATE_PASS); \
1253 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001254 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001255#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1256 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001257 MPM.addPass( \
1258 RequireAnalysisPass< \
1259 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001260 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001261 } \
1262 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001263 MPM.addPass(InvalidateAnalysisPass< \
1264 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001265 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001266 }
1267#include "PassRegistry.def"
1268
Chandler Carruth66445382014-01-11 08:16:35 +00001269 return false;
1270}
1271
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001272bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1273 const PipelineElement &E, bool VerifyEachPass,
1274 bool DebugLogging) {
1275 auto &Name = E.Name;
1276 auto &InnerPipeline = E.InnerPipeline;
1277
1278 // First handle complex passes like the pass managers which carry pipelines.
1279 if (!InnerPipeline.empty()) {
1280 if (Name == "cgscc") {
1281 CGSCCPassManager NestedCGPM(DebugLogging);
1282 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1283 DebugLogging))
1284 return false;
1285 // Add the nested pass manager with the appropriate adaptor.
1286 CGPM.addPass(std::move(NestedCGPM));
1287 return true;
1288 }
1289 if (Name == "function") {
1290 FunctionPassManager FPM(DebugLogging);
1291 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1292 DebugLogging))
1293 return false;
1294 // Add the nested pass manager with the appropriate adaptor.
1295 CGPM.addPass(
1296 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1297 return true;
1298 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001299 if (auto Count = parseRepeatPassName(Name)) {
1300 CGSCCPassManager NestedCGPM(DebugLogging);
1301 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1302 DebugLogging))
1303 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001304 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001305 return true;
1306 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001307 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1308 CGSCCPassManager NestedCGPM(DebugLogging);
1309 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1310 DebugLogging))
1311 return false;
1312 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1313 *MaxRepetitions, DebugLogging));
1314 return true;
1315 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001316 // Normal passes can't have pipelines.
1317 return false;
1318 }
1319
1320 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001321#define CGSCC_PASS(NAME, CREATE_PASS) \
1322 if (Name == NAME) { \
1323 CGPM.addPass(CREATE_PASS); \
1324 return true; \
1325 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001326#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1327 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001328 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001329 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001330 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1331 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001332 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001333 } \
1334 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001335 CGPM.addPass(InvalidateAnalysisPass< \
1336 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001337 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001338 }
1339#include "PassRegistry.def"
1340
Chandler Carruth572e3402014-04-21 11:12:00 +00001341 return false;
1342}
1343
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001344bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1345 const PipelineElement &E,
1346 bool VerifyEachPass, bool DebugLogging) {
1347 auto &Name = E.Name;
1348 auto &InnerPipeline = E.InnerPipeline;
1349
1350 // First handle complex passes like the pass managers which carry pipelines.
1351 if (!InnerPipeline.empty()) {
1352 if (Name == "function") {
1353 FunctionPassManager NestedFPM(DebugLogging);
1354 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1355 DebugLogging))
1356 return false;
1357 // Add the nested pass manager with the appropriate adaptor.
1358 FPM.addPass(std::move(NestedFPM));
1359 return true;
1360 }
1361 if (Name == "loop") {
1362 LoopPassManager LPM(DebugLogging);
1363 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1364 DebugLogging))
1365 return false;
1366 // Add the nested pass manager with the appropriate adaptor.
1367 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1368 return true;
1369 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001370 if (auto Count = parseRepeatPassName(Name)) {
1371 FunctionPassManager NestedFPM(DebugLogging);
1372 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1373 DebugLogging))
1374 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001375 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001376 return true;
1377 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001378 // Normal passes can't have pipelines.
1379 return false;
1380 }
1381
1382 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001383#define FUNCTION_PASS(NAME, CREATE_PASS) \
1384 if (Name == NAME) { \
1385 FPM.addPass(CREATE_PASS); \
1386 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001387 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001388#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1389 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001390 FPM.addPass( \
1391 RequireAnalysisPass< \
1392 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001393 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001394 } \
1395 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001396 FPM.addPass(InvalidateAnalysisPass< \
1397 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001398 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001399 }
1400#include "PassRegistry.def"
1401
Chandler Carruthd8330982014-01-12 09:34:22 +00001402 return false;
1403}
1404
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001405bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001406 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001407 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001408 auto &InnerPipeline = E.InnerPipeline;
1409
1410 // First handle complex passes like the pass managers which carry pipelines.
1411 if (!InnerPipeline.empty()) {
1412 if (Name == "loop") {
1413 LoopPassManager NestedLPM(DebugLogging);
1414 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1415 DebugLogging))
1416 return false;
1417 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001418 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001419 return true;
1420 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001421 if (auto Count = parseRepeatPassName(Name)) {
1422 LoopPassManager NestedLPM(DebugLogging);
1423 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1424 DebugLogging))
1425 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001426 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001427 return true;
1428 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001429 // Normal passes can't have pipelines.
1430 return false;
1431 }
1432
1433 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001434#define LOOP_PASS(NAME, CREATE_PASS) \
1435 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001436 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001437 return true; \
1438 }
1439#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1440 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001441 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001442 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1443 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1444 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001445 return true; \
1446 } \
1447 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001448 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001449 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001450 return true; \
1451 }
1452#include "PassRegistry.def"
1453
1454 return false;
1455}
1456
Chandler Carruthedf59962016-02-18 09:45:17 +00001457bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001458#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1459 if (Name == NAME) { \
1460 AA.registerModuleAnalysis< \
1461 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1462 return true; \
1463 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001464#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1465 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001466 AA.registerFunctionAnalysis< \
1467 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001468 return true; \
1469 }
1470#include "PassRegistry.def"
1471
1472 return false;
1473}
1474
Justin Bognereecc3c82016-02-25 07:23:08 +00001475bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001476 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001477 bool VerifyEachPass,
1478 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001479 for (const auto &Element : Pipeline) {
1480 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1481 return false;
1482 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001483 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001484 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001485}
1486
Chandler Carruth1ff77242015-03-07 09:02:36 +00001487bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001488 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001489 bool VerifyEachPass,
1490 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001491 for (const auto &Element : Pipeline) {
1492 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1493 return false;
1494 if (VerifyEachPass)
1495 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001496 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001497 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001498}
1499
Chandler Carruth1ff77242015-03-07 09:02:36 +00001500bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001501 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001502 bool VerifyEachPass,
1503 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001504 for (const auto &Element : Pipeline) {
1505 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1506 return false;
1507 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001508 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001509 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001510}
1511
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001512void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1513 FunctionAnalysisManager &FAM,
1514 CGSCCAnalysisManager &CGAM,
1515 ModuleAnalysisManager &MAM) {
1516 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1517 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001518 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1519 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1520 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1521 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1522 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1523}
1524
Chandler Carruth1ff77242015-03-07 09:02:36 +00001525bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001526 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001527 bool VerifyEachPass,
1528 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001529 for (const auto &Element : Pipeline) {
1530 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1531 return false;
1532 if (VerifyEachPass)
1533 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001534 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001535 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001536}
1537
1538// Primary pass pipeline description parsing routine.
1539// FIXME: Should this routine accept a TargetMachine or require the caller to
1540// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001541bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1542 StringRef PipelineText, bool VerifyEachPass,
1543 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001544 auto Pipeline = parsePipelineText(PipelineText);
1545 if (!Pipeline || Pipeline->empty())
1546 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001547
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001548 // If the first name isn't at the module layer, wrap the pipeline up
1549 // automatically.
1550 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001551
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001552 if (!isModulePassName(FirstName)) {
1553 if (isCGSCCPassName(FirstName))
1554 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1555 else if (isFunctionPassName(FirstName))
1556 Pipeline = {{"function", std::move(*Pipeline)}};
1557 else if (isLoopPassName(FirstName))
1558 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1559 else
1560 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001561 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001562 }
1563
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001564 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001565}
Chandler Carruthedf59962016-02-18 09:45:17 +00001566
1567bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001568 // If the pipeline just consists of the word 'default' just replace the AA
1569 // manager with our default one.
1570 if (PipelineText == "default") {
1571 AA = buildDefaultAAPipeline();
1572 return true;
1573 }
1574
Chandler Carruthedf59962016-02-18 09:45:17 +00001575 while (!PipelineText.empty()) {
1576 StringRef Name;
1577 std::tie(Name, PipelineText) = PipelineText.split(',');
1578 if (!parseAAPassName(AA, Name))
1579 return false;
1580 }
1581
1582 return true;
1583}