blob: 02889541650fcaf6c4a2477f7d16b43431dea9a3 [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"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000025#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000026#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000027#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
28#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000029#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000030#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000031#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000032#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000033#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000034#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000035#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000036#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000037#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000038#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000039#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000040#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000041#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000043#include "llvm/Analysis/OptimizationRemarkEmitter.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"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000062#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.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"
Matthew Simpsoncb585582017-10-25 13:40:08 +000066#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000067#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000068#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000069#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000070#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000071#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000072#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000073#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000074#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000075#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000076#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000077#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000078#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000079#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000080#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000081#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000082#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000083#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000084#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000085#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000086#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000087#include "llvm/Transforms/InstrProfiling.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000088#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000089#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000090#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000091#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000092#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000093#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000094#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000095#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000096#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000097#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000098#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000099#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000100#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000101#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000102#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000103#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000104#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000105#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000106#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000107#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000108#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000109#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000110#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000111#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000112#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000113#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000114#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000115#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000116#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000117#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000118#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000119#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000120#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000121#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000122#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000123#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000124#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000125#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000126#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000127#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000128#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000129#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000130#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000131#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000132#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000133#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000134#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000135#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000136#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000137#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000138#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000139#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000140#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000141#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000142#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000143#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000144#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000145#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000146#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000147#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000148#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000149#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000150#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000151#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000152
Chandler Carruth66445382014-01-11 08:16:35 +0000153using namespace llvm;
154
Chandler Carruth719ffe12017-02-12 05:38:04 +0000155static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
156 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000157static cl::opt<bool>
158 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
159 cl::Hidden, cl::ZeroOrMore,
160 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000161
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000162static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000163 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000164 cl::Hidden, cl::ZeroOrMore,
165 cl::desc("Run NewGVN instead of GVN"));
166
Geoff Berry3cca1da2017-06-10 15:20:03 +0000167static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000168 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
169 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000170
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000171static cl::opt<bool> EnableGVNHoist(
172 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
173 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
174
Davide Italianobe1b6a92017-06-03 23:18:29 +0000175static cl::opt<bool> EnableGVNSink(
176 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
177 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
178
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000179static cl::opt<bool> EnableSyntheticCounts(
180 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
181 cl::desc("Run synthetic function entry count generation "
182 "pass"));
183
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000184static Regex DefaultAliasRegex(
185 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000186
Chandler Carruthe3f50642016-12-22 06:59:15 +0000187static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
188 switch (Level) {
189 case PassBuilder::O0:
190 case PassBuilder::O1:
191 case PassBuilder::O2:
192 case PassBuilder::O3:
193 return false;
194
195 case PassBuilder::Os:
196 case PassBuilder::Oz:
197 return true;
198 }
199 llvm_unreachable("Invalid optimization level!");
200}
201
Chandler Carruth66445382014-01-11 08:16:35 +0000202namespace {
203
Chandler Carruthd8330982014-01-12 09:34:22 +0000204/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000205struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000206 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000207 return PreservedAnalyses::all();
208 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000209 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000210};
211
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000213class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
214 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000215 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000216
217public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000218 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000219 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000220 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000221};
222
Chandler Carruth572e3402014-04-21 11:12:00 +0000223/// \brief No-op CGSCC pass which does nothing.
224struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000225 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
226 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000227 return PreservedAnalyses::all();
228 }
229 static StringRef name() { return "NoOpCGSCCPass"; }
230};
231
Chandler Carruth0b576b32015-01-06 02:50:06 +0000232/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000233class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
234 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000235 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000236
237public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000238 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000239 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000240 return Result();
241 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000242 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000243};
244
Chandler Carruthd8330982014-01-12 09:34:22 +0000245/// \brief No-op function pass which does nothing.
246struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000247 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000248 return PreservedAnalyses::all();
249 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000250 static StringRef name() { return "NoOpFunctionPass"; }
251};
252
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000254class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
255 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000256 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000257
258public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000259 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000260 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000261 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000262};
263
Justin Bognereecc3c82016-02-25 07:23:08 +0000264/// \brief No-op loop pass which does nothing.
265struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000266 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
267 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000268 return PreservedAnalyses::all();
269 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000270 static StringRef name() { return "NoOpLoopPass"; }
271};
272
273/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000274class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
275 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000276 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000277
278public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000279 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000280 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
281 return Result();
282 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000283 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000284};
285
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000286AnalysisKey NoOpModuleAnalysis::Key;
287AnalysisKey NoOpCGSCCAnalysis::Key;
288AnalysisKey NoOpFunctionAnalysis::Key;
289AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000290
Chandler Carruth66445382014-01-11 08:16:35 +0000291} // End anonymous namespace.
292
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000293void PassBuilder::invokePeepholeEPCallbacks(
294 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
295 for (auto &C : PeepholeEPCallbacks)
296 C(FPM, Level);
297}
298
Chandler Carruth1ff77242015-03-07 09:02:36 +0000299void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000300#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000301 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000302#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000303
304 for (auto &C : ModuleAnalysisRegistrationCallbacks)
305 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000306}
307
Chandler Carruth1ff77242015-03-07 09:02:36 +0000308void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000309#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000310 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000311#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000312
313 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
314 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000315}
316
Chandler Carruth1ff77242015-03-07 09:02:36 +0000317void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000318#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000319 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000320#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000321
322 for (auto &C : FunctionAnalysisRegistrationCallbacks)
323 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000324}
325
Justin Bognereecc3c82016-02-25 07:23:08 +0000326void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000327#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000328 LAM.registerPass([&] { return CREATE_PASS; });
329#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000330
331 for (auto &C : LoopAnalysisRegistrationCallbacks)
332 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000333}
334
Chandler Carruthe3f50642016-12-22 06:59:15 +0000335FunctionPassManager
336PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000337 ThinLTOPhase Phase,
338 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000339 assert(Level != O0 && "Must request optimizations!");
340 FunctionPassManager FPM(DebugLogging);
341
342 // Form SSA out of local memory accesses after breaking apart aggregates into
343 // scalars.
344 FPM.addPass(SROA());
345
346 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000347 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000348
Davide Italianoc3688312017-06-01 23:08:14 +0000349 // Hoisting of scalars and load expressions.
350 if (EnableGVNHoist)
351 FPM.addPass(GVNHoistPass());
352
Davide Italianobe1b6a92017-06-03 23:18:29 +0000353 // Global value numbering based sinking.
354 if (EnableGVNSink) {
355 FPM.addPass(GVNSinkPass());
356 FPM.addPass(SimplifyCFGPass());
357 }
358
Chandler Carruthe3f50642016-12-22 06:59:15 +0000359 // Speculative execution if the target has divergent branches; otherwise nop.
360 FPM.addPass(SpeculativeExecutionPass());
361
362 // Optimize based on known information about branches, and cleanup afterward.
363 FPM.addPass(JumpThreadingPass());
364 FPM.addPass(CorrelatedValuePropagationPass());
365 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000366 if (Level == O3)
367 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000368 FPM.addPass(InstCombinePass());
369
370 if (!isOptimizingForSize(Level))
371 FPM.addPass(LibCallsShrinkWrapPass());
372
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000373 invokePeepholeEPCallbacks(FPM, Level);
374
Rong Xue1f42452017-10-23 22:21:29 +0000375 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
376 // using the size value profile. Don't perform this when optimizing for size.
377 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
378 !isOptimizingForSize(Level))
379 FPM.addPass(PGOMemOPSizeOpt());
380
Chandler Carruthe3f50642016-12-22 06:59:15 +0000381 FPM.addPass(TailCallElimPass());
382 FPM.addPass(SimplifyCFGPass());
383
384 // Form canonically associated expression trees, and simplify the trees using
385 // basic mathematical properties. For example, this will form (nearly)
386 // minimal multiplication trees.
387 FPM.addPass(ReassociatePass());
388
389 // Add the primary loop simplification pipeline.
390 // FIXME: Currently this is split into two loop pass pipelines because we run
391 // some function passes in between them. These can and should be replaced by
392 // loop pass equivalenst but those aren't ready yet. Specifically,
Vedant Kumar3a408532018-03-12 20:49:42 +0000393 // `SimplifyCFGPass` and `InstCombinePass` are used. We just have
394 // `LoopSimplifyCFGPass` which isn't yet powerful enough.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000395 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
396
397 // Rotate Loop - disable header duplication at -Oz
398 LPM1.addPass(LoopRotatePass(Level != Oz));
399 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000400 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000401 LPM2.addPass(IndVarSimplifyPass());
402 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000403
404 for (auto &C : LateLoopOptimizationsEPCallbacks)
405 C(LPM2, Level);
406
Chandler Carruth79b733b2017-01-26 23:21:17 +0000407 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000408 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000409 // because it changes IR to makes profile annotation in back compile
410 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000411 if (Phase != ThinLTOPhase::PreLink ||
412 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000413 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000414
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000415 for (auto &C : LoopOptimizerEndEPCallbacks)
416 C(LPM2, Level);
417
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000418 // We provide the opt remark emitter pass for LICM to use. We only need to do
419 // this once as it is immutable.
420 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000421 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000422 FPM.addPass(SimplifyCFGPass());
423 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000424 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000425
426 // Eliminate redundancies.
427 if (Level != O1) {
428 // These passes add substantial compile time so skip them at O1.
429 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000430 if (RunNewGVN)
431 FPM.addPass(NewGVNPass());
432 else
433 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000434 }
435
436 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
437 FPM.addPass(MemCpyOptPass());
438
439 // Sparse conditional constant propagation.
440 // FIXME: It isn't clear why we do this *after* loop passes rather than
441 // before...
442 FPM.addPass(SCCPPass());
443
444 // Delete dead bit computations (instcombine runs after to fold away the dead
445 // computations, and then ADCE will run later to exploit any new DCE
446 // opportunities that creates).
447 FPM.addPass(BDCEPass());
448
449 // Run instcombine after redundancy and dead bit elimination to exploit
450 // opportunities opened up by them.
451 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000452 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000453
454 // Re-consider control flow based optimizations after redundancy elimination,
455 // redo DCE, etc.
456 FPM.addPass(JumpThreadingPass());
457 FPM.addPass(CorrelatedValuePropagationPass());
458 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000459 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000460
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000461 for (auto &C : ScalarOptimizerLateEPCallbacks)
462 C(FPM, Level);
463
Chandler Carruthe3f50642016-12-22 06:59:15 +0000464 // Finally, do an expensive DCE pass to catch all the dead code exposed by
465 // the simplifications and basic cleanup after all the simplifications.
466 FPM.addPass(ADCEPass());
467 FPM.addPass(SimplifyCFGPass());
468 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000469 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000470
471 return FPM;
472}
473
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000474void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
475 PassBuilder::OptimizationLevel Level,
476 bool RunProfileGen,
477 std::string ProfileGenFile,
478 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000479 // Generally running simplification passes and the inliner with an high
480 // threshold results in smaller executables, but there may be cases where
481 // the size grows, so let's be conservative here and skip this simplification
482 // at -Os/Oz.
483 if (!isOptimizingForSize(Level)) {
484 InlineParams IP;
485
486 // In the old pass manager, this is a cl::opt. Should still this be one?
487 IP.DefaultThreshold = 75;
488
489 // FIXME: The hint threshold has the same value used by the regular inliner.
490 // This should probably be lowered after performance testing.
491 // FIXME: this comment is cargo culted from the old pass manager, revisit).
492 IP.HintThreshold = 325;
493
494 CGSCCPassManager CGPipeline(DebugLogging);
495
496 CGPipeline.addPass(InlinerPass(IP));
497
498 FunctionPassManager FPM;
499 FPM.addPass(SROA());
500 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
501 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
502 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000503 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000504
Davide Italiano513dfaa2017-02-13 15:26:22 +0000505 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
506
507 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
508 }
509
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000510 // Delete anything that is now dead to make sure that we don't instrument
511 // dead code. Instrumentation can end up keeping dead code around and
512 // dramatically increase code size.
513 MPM.addPass(GlobalDCEPass());
514
Davide Italiano513dfaa2017-02-13 15:26:22 +0000515 if (RunProfileGen) {
516 MPM.addPass(PGOInstrumentationGen());
517
Xinliang David Lib67530e2017-06-25 00:26:43 +0000518 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000519 FPM.addPass(
520 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000521 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
522
Davide Italiano513dfaa2017-02-13 15:26:22 +0000523 // Add the profile lowering pass.
524 InstrProfOptions Options;
525 if (!ProfileGenFile.empty())
526 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000527 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000528 MPM.addPass(InstrProfiling(Options));
529 }
530
531 if (!ProfileUseFile.empty())
532 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
533}
534
Easwaran Raman8249fac2017-06-28 13:33:49 +0000535static InlineParams
536getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
537 auto O3 = PassBuilder::O3;
538 unsigned OptLevel = Level > O3 ? 2 : Level;
539 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
540 return getInlineParams(OptLevel, SizeLevel);
541}
542
Chandler Carruthe3f50642016-12-22 06:59:15 +0000543ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000544PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000545 ThinLTOPhase Phase,
546 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000547 ModulePassManager MPM(DebugLogging);
548
Chandler Carruthe3f50642016-12-22 06:59:15 +0000549 // Do basic inference of function attributes from known properties of system
550 // libraries and other oracles.
551 MPM.addPass(InferFunctionAttrsPass());
552
553 // Create an early function pass manager to cleanup the output of the
554 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000555 FunctionPassManager EarlyFPM(DebugLogging);
556 EarlyFPM.addPass(SimplifyCFGPass());
557 EarlyFPM.addPass(SROA());
558 EarlyFPM.addPass(EarlyCSEPass());
559 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000560 if (Level == O3)
561 EarlyFPM.addPass(CallSiteSplittingPass());
562
Dehao Chen08f88312017-08-07 20:23:20 +0000563 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
564 // to convert bitcast to direct calls so that they can be inlined during the
565 // profile annotation prepration step.
566 // More details about SamplePGO design can be found in:
567 // https://research.google.com/pubs/pub45290.html
568 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
569 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
570 Phase == ThinLTOPhase::PostLink)
571 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000572 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000573
Dehao Chen08f88312017-08-07 20:23:20 +0000574 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
575 // Annotate sample profile right after early FPM to ensure freshness of
576 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000577 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
578 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000579 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
580 // for the profile annotation to be accurate in the ThinLTO backend.
581 if (Phase != ThinLTOPhase::PreLink)
582 // We perform early indirect call promotion here, before globalopt.
583 // This is important for the ThinLTO backend phase because otherwise
584 // imported available_externally functions look unreferenced and are
585 // removed.
586 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
587 true));
588 }
589
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000590 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000591 // and prior to optimizing globals.
592 // FIXME: This position in the pipeline hasn't been carefully considered in
593 // years, it should be re-analyzed.
594 MPM.addPass(IPSCCPPass());
595
Matthew Simpsoncb585582017-10-25 13:40:08 +0000596 // Attach metadata to indirect call sites indicating the set of functions
597 // they may target at run-time. This should follow IPSCCP.
598 MPM.addPass(CalledValuePropagationPass());
599
Chandler Carruthe3f50642016-12-22 06:59:15 +0000600 // Optimize globals to try and fold them into constants.
601 MPM.addPass(GlobalOptPass());
602
603 // Promote any localized globals to SSA registers.
604 // FIXME: Should this instead by a run of SROA?
605 // FIXME: We should probably run instcombine and simplify-cfg afterward to
606 // delete control flows that are dead once globals have been folded to
607 // constants.
608 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
609
610 // Remove any dead arguments exposed by cleanups and constand folding
611 // globals.
612 MPM.addPass(DeadArgumentEliminationPass());
613
614 // Create a small function pass pipeline to cleanup after all the global
615 // optimizations.
616 FunctionPassManager GlobalCleanupPM(DebugLogging);
617 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000618 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
619
Chandler Carruthe3f50642016-12-22 06:59:15 +0000620 GlobalCleanupPM.addPass(SimplifyCFGPass());
621 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
622
Dehao Chen89d32262017-08-02 01:28:31 +0000623 // Add all the requested passes for instrumentation PGO, if requested.
624 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
625 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
626 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
627 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
628 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000629 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000630
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000631 // Synthesize function entry counts for non-PGO compilation.
632 if (EnableSyntheticCounts && !PGOOpt)
633 MPM.addPass(SyntheticCountsPropagation());
634
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000635 // Require the GlobalsAA analysis for the module so we can query it within
636 // the CGSCC pipeline.
637 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000638
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000639 // Require the ProfileSummaryAnalysis for the module so we can query it within
640 // the inliner pass.
641 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
642
Chandler Carruthe3f50642016-12-22 06:59:15 +0000643 // Now begin the main postorder CGSCC pipeline.
644 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
645 // manager and trying to emulate its precise behavior. Much of this doesn't
646 // make a lot of sense and we should revisit the core CGSCC structure.
647 CGSCCPassManager MainCGPipeline(DebugLogging);
648
649 // Note: historically, the PruneEH pass was run first to deduce nounwind and
650 // generally clean up exception handling overhead. It isn't clear this is
651 // valuable as the inliner doesn't currently care whether it is inlining an
652 // invoke or a call.
653
654 // Run the inliner first. The theory is that we are walking bottom-up and so
655 // the callees have already been fully optimized, and we want to inline them
656 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000657 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000658 // because it makes profile annotation in the backend inaccurate.
659 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000660 if (Phase == ThinLTOPhase::PreLink &&
661 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000662 IP.HotCallSiteThreshold = 0;
663 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000664
665 // Now deduce any function attributes based in the current code.
666 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
667
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000668 // When at O3 add argument promotion to the pass pipeline.
669 // FIXME: It isn't at all clear why this should be limited to O3.
670 if (Level == O3)
671 MainCGPipeline.addPass(ArgumentPromotionPass());
672
Chandler Carruthe3f50642016-12-22 06:59:15 +0000673 // Lastly, add the core function simplification pipeline nested inside the
674 // CGSCC walk.
675 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000676 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000677
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000678 for (auto &C : CGSCCOptimizerLateEPCallbacks)
679 C(MainCGPipeline, Level);
680
Chandler Carruth719ffe12017-02-12 05:38:04 +0000681 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
682 // to detect when we devirtualize indirect calls and iterate the SCC passes
683 // in that case to try and catch knock-on inlining or function attrs
684 // opportunities. Then we add it to the module pipeline by walking the SCCs
685 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000686 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000687 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000688 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000689
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000690 return MPM;
691}
692
693ModulePassManager
694PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
695 bool DebugLogging) {
696 ModulePassManager MPM(DebugLogging);
697
698 // Optimize globals now that the module is fully simplified.
699 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000700 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000701
Xinliang David Li126157c2017-05-22 16:41:57 +0000702 // Run partial inlining pass to partially inline functions that have
703 // large bodies.
704 if (RunPartialInlining)
705 MPM.addPass(PartialInlinerPass());
706
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000707 // Remove avail extern fns and globals definitions since we aren't compiling
708 // an object file for later LTO. For LTO we want to preserve these so they
709 // are eligible for inlining at link-time. Note if they are unreferenced they
710 // will be removed by GlobalDCE later, so this only impacts referenced
711 // available externally globals. Eventually they will be suppressed during
712 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
713 // may make globals referenced by available external functions dead and saves
714 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000715 MPM.addPass(EliminateAvailableExternallyPass());
716
717 // Do RPO function attribute inference across the module to forward-propagate
718 // attributes where applicable.
719 // FIXME: Is this really an optimization rather than a canonicalization?
720 MPM.addPass(ReversePostOrderFunctionAttrsPass());
721
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000722 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000723 // useful as the above will have inlined, DCE'ed, and function-attr
724 // propagated everything. We should at this point have a reasonably minimal
725 // and richly annotated call graph. By computing aliasing and mod/ref
726 // information for all local globals here, the late loop passes and notably
727 // the vectorizer will be able to use them to help recognize vectorizable
728 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000729 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000730
731 FunctionPassManager OptimizePM(DebugLogging);
732 OptimizePM.addPass(Float2IntPass());
733 // FIXME: We need to run some loop optimizations to re-rotate loops after
734 // simplify-cfg and others undo their rotation.
735
736 // Optimize the loop execution. These passes operate on entire loop nests
737 // rather than on each loop in an inside-out manner, and so they are actually
738 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000739
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000740 for (auto &C : VectorizerStartEPCallbacks)
741 C(OptimizePM, Level);
742
Chandler Carrutha95ff382017-01-27 00:50:21 +0000743 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000744 OptimizePM.addPass(
745 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000746
747 // Distribute loops to allow partial vectorization. I.e. isolate dependences
748 // into separate loop that would otherwise inhibit vectorization. This is
749 // currently only performed for loops marked with the metadata
750 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000751 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000752
753 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000754 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000755
Chandler Carruthbaabda92017-01-27 01:32:26 +0000756 // Eliminate loads by forwarding stores from the previous iteration to loads
757 // of the current iteration.
758 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000759
760 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000761 OptimizePM.addPass(InstCombinePass());
762
Chandler Carrutha95ff382017-01-27 00:50:21 +0000763 // Now that we've formed fast to execute loop structures, we do further
764 // optimizations. These are run afterward as they might block doing complex
765 // analyses and transforms such as what are needed for loop vectorization.
766
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000767 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
768 // GVN, loop transforms, and others have already run, so it's now better to
769 // convert to more optimized IR using more aggressive simplify CFG options.
770 // The extra sinking transform can create larger basic blocks, so do this
771 // before SLP vectorization.
772 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
773 forwardSwitchCondToPhi(true).
774 convertSwitchToLookupTable(true).
775 needCanonicalLoops(false).
776 sinkCommonInsts(true)));
777
Chandler Carruthe3f50642016-12-22 06:59:15 +0000778 // Optimize parallel scalar instruction chains into SIMD instructions.
779 OptimizePM.addPass(SLPVectorizerPass());
780
Chandler Carruthe3f50642016-12-22 06:59:15 +0000781 OptimizePM.addPass(InstCombinePass());
782
783 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000784 // execution resources of an out-of-order processor. We also then need to
785 // clean up redundancies and loop invariant code.
786 // FIXME: It would be really good to use a loop-integrated instruction
787 // combiner for cleanup here so that the unrolling and LICM can be pipelined
788 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000789 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000790 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000791 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000792 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000793
794 // Now that we've vectorized and unrolled loops, we may have more refined
795 // alignment information, try to re-derive it here.
796 OptimizePM.addPass(AlignmentFromAssumptionsPass());
797
Chandler Carrutha95ff382017-01-27 00:50:21 +0000798 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
799 // canonicalization pass that enables other optimizations. As a result,
800 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
801 // result too early.
802 OptimizePM.addPass(LoopSinkPass());
803
804 // And finally clean up LCSSA form before generating code.
805 OptimizePM.addPass(InstSimplifierPass());
806
Sanjay Patel6fd43912017-09-09 13:38:18 +0000807 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
808 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
809 // flattening of blocks.
810 OptimizePM.addPass(DivRemPairsPass());
811
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000812 // LoopSink (and other loop passes since the last simplifyCFG) might have
813 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
814 OptimizePM.addPass(SimplifyCFGPass());
815
Chandler Carruthc34f7892017-11-28 11:32:31 +0000816 // Optimize PHIs by speculating around them when profitable. Note that this
817 // pass needs to be run after any PRE or similar pass as it is essentially
818 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
819 OptimizePM.addPass(SpeculateAroundPHIsPass());
820
Chandler Carrutha95ff382017-01-27 00:50:21 +0000821 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000822 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
823
824 // Now we need to do some global optimization transforms.
825 // FIXME: It would seem like these should come first in the optimization
826 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
827 // ordering here.
828 MPM.addPass(GlobalDCEPass());
829 MPM.addPass(ConstantMergePass());
830
831 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000832}
833
Chandler Carruthe3f50642016-12-22 06:59:15 +0000834ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000835PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
836 bool DebugLogging) {
837 assert(Level != O0 && "Must request optimizations for the default pipeline!");
838
839 ModulePassManager MPM(DebugLogging);
840
841 // Force any function attributes we want the rest of the pipeline to observe.
842 MPM.addPass(ForceFunctionAttrsPass());
843
David Blaikie0c64f5a2018-01-23 01:25:20 +0000844 // Apply module pipeline start EP callback.
845 for (auto &C : PipelineStartEPCallbacks)
846 C(MPM);
847
Dehao Chen08f88312017-08-07 20:23:20 +0000848 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000849 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
850
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000851 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000852 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
853 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000854
855 // Now add the optimization pipeline.
856 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
857
858 return MPM;
859}
860
861ModulePassManager
862PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
863 bool DebugLogging) {
864 assert(Level != O0 && "Must request optimizations for the default pipeline!");
865
866 ModulePassManager MPM(DebugLogging);
867
868 // Force any function attributes we want the rest of the pipeline to observe.
869 MPM.addPass(ForceFunctionAttrsPass());
870
Dehao Chen08f88312017-08-07 20:23:20 +0000871 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000872 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
873
David Blaikie0c64f5a2018-01-23 01:25:20 +0000874 // Apply module pipeline start EP callback.
875 for (auto &C : PipelineStartEPCallbacks)
876 C(MPM);
877
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000878 // If we are planning to perform ThinLTO later, we don't bloat the code with
879 // unrolling/vectorization/... now. Just simplify the module as much as we
880 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000881 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
882 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000883
884 // Run partial inlining pass to partially inline functions that have
885 // large bodies.
886 // FIXME: It isn't clear whether this is really the right place to run this
887 // in ThinLTO. Because there is another canonicalization and simplification
888 // phase that will run after the thin link, running this here ends up with
889 // less information than will be available later and it may grow functions in
890 // ways that aren't beneficial.
891 if (RunPartialInlining)
892 MPM.addPass(PartialInlinerPass());
893
894 // Reduce the size of the IR as much as possible.
895 MPM.addPass(GlobalOptPass());
896
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000897 return MPM;
898}
899
900ModulePassManager
901PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
902 bool DebugLogging) {
903 // FIXME: The summary index is not hooked in the new pass manager yet.
904 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
905 // here.
906
907 ModulePassManager MPM(DebugLogging);
908
909 // Force any function attributes we want the rest of the pipeline to observe.
910 MPM.addPass(ForceFunctionAttrsPass());
911
912 // During the ThinLTO backend phase we perform early indirect call promotion
913 // here, before globalopt. Otherwise imported available_externally functions
914 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000915 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
916 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000917 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000918 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
919 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000920
921 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000922 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
923 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000924
925 // Now add the optimization pipeline.
926 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
927
928 return MPM;
929}
930
931ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000932PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
933 bool DebugLogging) {
934 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000935 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000936 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000937}
938
Chandler Carruthe3f50642016-12-22 06:59:15 +0000939ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
940 bool DebugLogging) {
941 assert(Level != O0 && "Must request optimizations for the default pipeline!");
942 ModulePassManager MPM(DebugLogging);
943
Davide Italiano089a9122017-01-24 00:57:39 +0000944 // Remove unused virtual tables to improve the quality of code generated by
945 // whole-program devirtualization and bitset lowering.
946 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000947
Davide Italiano089a9122017-01-24 00:57:39 +0000948 // Force any function attributes we want the rest of the pipeline to observe.
949 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000950
Davide Italiano089a9122017-01-24 00:57:39 +0000951 // Do basic inference of function attributes from known properties of system
952 // libraries and other oracles.
953 MPM.addPass(InferFunctionAttrsPass());
954
955 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000956 FunctionPassManager EarlyFPM(DebugLogging);
957 EarlyFPM.addPass(CallSiteSplittingPass());
958 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
959
Davide Italiano089a9122017-01-24 00:57:39 +0000960 // Indirect call promotion. This should promote all the targets that are
961 // left by the earlier promotion pass that promotes intra-module targets.
962 // This two-step promotion is to save the compile time. For LTO, it should
963 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000964 MPM.addPass(PGOIndirectCallPromotion(
965 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000966 // Propagate constants at call sites into the functions they call. This
967 // opens opportunities for globalopt (and inlining) by substituting function
968 // pointers passed as arguments to direct uses of functions.
969 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000970
971 // Attach metadata to indirect call sites indicating the set of functions
972 // they may target at run-time. This should follow IPSCCP.
973 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000974 }
975
976 // Now deduce any function attributes based in the current code.
977 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
978 PostOrderFunctionAttrsPass()));
979
980 // Do RPO function attribute inference across the module to forward-propagate
981 // attributes where applicable.
982 // FIXME: Is this really an optimization rather than a canonicalization?
983 MPM.addPass(ReversePostOrderFunctionAttrsPass());
984
985 // Use inragne annotations on GEP indices to split globals where beneficial.
986 MPM.addPass(GlobalSplitPass());
987
988 // Run whole program optimization of virtual call when the list of callees
989 // is fixed.
990 MPM.addPass(WholeProgramDevirtPass());
991
992 // Stop here at -O1.
993 if (Level == 1)
994 return MPM;
995
996 // Optimize globals to try and fold them into constants.
997 MPM.addPass(GlobalOptPass());
998
999 // Promote any localized globals to SSA registers.
1000 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1001
1002 // Linking modules together can lead to duplicate global constant, only
1003 // keep one copy of each constant.
1004 MPM.addPass(ConstantMergePass());
1005
1006 // Remove unused arguments from functions.
1007 MPM.addPass(DeadArgumentEliminationPass());
1008
1009 // Reduce the code after globalopt and ipsccp. Both can open up significant
1010 // simplification opportunities, and both can propagate functions through
1011 // function pointers. When this happens, we often have to resolve varargs
1012 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001013 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001014 if (Level == O3)
1015 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001016 PeepholeFPM.addPass(InstCombinePass());
1017 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1018
1019 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001020
1021 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1022 // generally clean up exception handling overhead. It isn't clear this is
1023 // valuable as the inliner doesn't currently care whether it is inlining an
1024 // invoke or a call.
1025 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001026 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1027 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001028
1029 // Optimize globals again after we ran the inliner.
1030 MPM.addPass(GlobalOptPass());
1031
1032 // Garbage collect dead functions.
1033 // FIXME: Add ArgumentPromotion pass after once it's ported.
1034 MPM.addPass(GlobalDCEPass());
1035
1036 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001037 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001038 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001039 invokePeepholeEPCallbacks(FPM, Level);
1040
Davide Italiano089a9122017-01-24 00:57:39 +00001041 FPM.addPass(JumpThreadingPass());
1042
1043 // Break up allocas
1044 FPM.addPass(SROA());
1045
1046 // Run a few AA driver optimizations here and now to cleanup the code.
1047 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1048
1049 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1050 PostOrderFunctionAttrsPass()));
1051 // FIXME: here we run IP alias analysis in the legacy PM.
1052
1053 FunctionPassManager MainFPM;
1054
1055 // FIXME: once we fix LoopPass Manager, add LICM here.
1056 // FIXME: once we provide support for enabling MLSM, add it here.
1057 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001058 if (RunNewGVN)
1059 MainFPM.addPass(NewGVNPass());
1060 else
1061 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001062
1063 // Remove dead memcpy()'s.
1064 MainFPM.addPass(MemCpyOptPass());
1065
1066 // Nuke dead stores.
1067 MainFPM.addPass(DSEPass());
1068
1069 // FIXME: at this point, we run a bunch of loop passes:
1070 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1071 // loopVectorize. Enable them once the remaining issue with LPM
1072 // are sorted out.
1073
1074 MainFPM.addPass(InstCombinePass());
1075 MainFPM.addPass(SimplifyCFGPass());
1076 MainFPM.addPass(SCCPPass());
1077 MainFPM.addPass(InstCombinePass());
1078 MainFPM.addPass(BDCEPass());
1079
1080 // FIXME: We may want to run SLPVectorizer here.
1081 // After vectorization, assume intrinsics may tell us more
1082 // about pointer alignments.
1083#if 0
1084 MainFPM.add(AlignmentFromAssumptionsPass());
1085#endif
1086
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001087 // FIXME: Conditionally run LoadCombine here, after it's ported
1088 // (in case we still have this pass, given its questionable usefulness).
1089
Davide Italiano089a9122017-01-24 00:57:39 +00001090 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001091 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001092 MainFPM.addPass(JumpThreadingPass());
1093 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1094
1095 // Create a function that performs CFI checks for cross-DSO calls with
1096 // targets in the current module.
1097 MPM.addPass(CrossDSOCFIPass());
1098
1099 // Lower type metadata and the type.test intrinsic. This pass supports
1100 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1101 // to be run at link time if CFI is enabled. This pass does nothing if
1102 // CFI is disabled.
1103 // Enable once we add support for the summary in the new PM.
1104#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001105 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1106 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001107 Summary));
1108#endif
1109
1110 // Add late LTO optimization passes.
1111 // Delete basic blocks, which optimization passes may have killed.
1112 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1113
1114 // Drop bodies of available eternally objects to improve GlobalDCE.
1115 MPM.addPass(EliminateAvailableExternallyPass());
1116
1117 // Now that we have optimized the program, discard unreachable functions.
1118 MPM.addPass(GlobalDCEPass());
1119
1120 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001121 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001122}
1123
Chandler Carruth060ad612016-12-23 20:38:19 +00001124AAManager PassBuilder::buildDefaultAAPipeline() {
1125 AAManager AA;
1126
1127 // The order in which these are registered determines their priority when
1128 // being queried.
1129
1130 // First we register the basic alias analysis that provides the majority of
1131 // per-function local AA logic. This is a stateless, on-demand local set of
1132 // AA techniques.
1133 AA.registerFunctionAnalysis<BasicAA>();
1134
1135 // Next we query fast, specialized alias analyses that wrap IR-embedded
1136 // information about aliasing.
1137 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1138 AA.registerFunctionAnalysis<TypeBasedAA>();
1139
1140 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001141 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1142 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001143 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001144 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001145
1146 return AA;
1147}
1148
Chandler Carruth241bf242016-08-03 07:44:48 +00001149static Optional<int> parseRepeatPassName(StringRef Name) {
1150 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1151 return None;
1152 int Count;
1153 if (Name.getAsInteger(0, Count) || Count <= 0)
1154 return None;
1155 return Count;
1156}
1157
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001158static Optional<int> parseDevirtPassName(StringRef Name) {
1159 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1160 return None;
1161 int Count;
1162 if (Name.getAsInteger(0, Count) || Count <= 0)
1163 return None;
1164 return Count;
1165}
1166
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001167/// Tests whether a pass name starts with a valid prefix for a default pipeline
1168/// alias.
1169static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1170 return Name.startswith("default") || Name.startswith("thinlto") ||
1171 Name.startswith("lto");
1172}
1173
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001174/// Tests whether registered callbacks will accept a given pass name.
1175///
1176/// When parsing a pipeline text, the type of the outermost pipeline may be
1177/// omitted, in which case the type is automatically determined from the first
1178/// pass name in the text. This may be a name that is handled through one of the
1179/// callbacks. We check this through the oridinary parsing callbacks by setting
1180/// up a dummy PassManager in order to not force the client to also handle this
1181/// type of query.
1182template <typename PassManagerT, typename CallbacksT>
1183static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1184 if (!Callbacks.empty()) {
1185 PassManagerT DummyPM;
1186 for (auto &CB : Callbacks)
1187 if (CB(Name, DummyPM, {}))
1188 return true;
1189 }
1190 return false;
1191}
1192
1193template <typename CallbacksT>
1194static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001195 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001196 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001197 return DefaultAliasRegex.match(Name);
1198
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001199 // Explicitly handle pass manager names.
1200 if (Name == "module")
1201 return true;
1202 if (Name == "cgscc")
1203 return true;
1204 if (Name == "function")
1205 return true;
1206
Chandler Carruth241bf242016-08-03 07:44:48 +00001207 // Explicitly handle custom-parsed pass names.
1208 if (parseRepeatPassName(Name))
1209 return true;
1210
Chandler Carruth74a8a222016-06-17 07:15:29 +00001211#define MODULE_PASS(NAME, CREATE_PASS) \
1212 if (Name == NAME) \
1213 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001214#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001215 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001216 return true;
1217#include "PassRegistry.def"
1218
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001219 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001220}
1221
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001222template <typename CallbacksT>
1223static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001224 // Explicitly handle pass manager names.
1225 if (Name == "cgscc")
1226 return true;
1227 if (Name == "function")
1228 return true;
1229
Chandler Carruth241bf242016-08-03 07:44:48 +00001230 // Explicitly handle custom-parsed pass names.
1231 if (parseRepeatPassName(Name))
1232 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001233 if (parseDevirtPassName(Name))
1234 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001235
Chandler Carruth74a8a222016-06-17 07:15:29 +00001236#define CGSCC_PASS(NAME, CREATE_PASS) \
1237 if (Name == NAME) \
1238 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001239#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001240 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001241 return true;
1242#include "PassRegistry.def"
1243
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001244 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001245}
1246
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001247template <typename CallbacksT>
1248static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001249 // Explicitly handle pass manager names.
1250 if (Name == "function")
1251 return true;
1252 if (Name == "loop")
1253 return true;
1254
Chandler Carruth241bf242016-08-03 07:44:48 +00001255 // Explicitly handle custom-parsed pass names.
1256 if (parseRepeatPassName(Name))
1257 return true;
1258
Chandler Carruth74a8a222016-06-17 07:15:29 +00001259#define FUNCTION_PASS(NAME, CREATE_PASS) \
1260 if (Name == NAME) \
1261 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001262#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001263 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001264 return true;
1265#include "PassRegistry.def"
1266
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001267 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001268}
1269
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001270template <typename CallbacksT>
1271static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001272 // Explicitly handle pass manager names.
1273 if (Name == "loop")
1274 return true;
1275
Chandler Carruth241bf242016-08-03 07:44:48 +00001276 // Explicitly handle custom-parsed pass names.
1277 if (parseRepeatPassName(Name))
1278 return true;
1279
Chandler Carruth74a8a222016-06-17 07:15:29 +00001280#define LOOP_PASS(NAME, CREATE_PASS) \
1281 if (Name == NAME) \
1282 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001283#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1284 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1285 return true;
1286#include "PassRegistry.def"
1287
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001288 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001289}
1290
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001291Optional<std::vector<PassBuilder::PipelineElement>>
1292PassBuilder::parsePipelineText(StringRef Text) {
1293 std::vector<PipelineElement> ResultPipeline;
1294
1295 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1296 &ResultPipeline};
1297 for (;;) {
1298 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1299 size_t Pos = Text.find_first_of(",()");
1300 Pipeline.push_back({Text.substr(0, Pos), {}});
1301
1302 // If we have a single terminating name, we're done.
1303 if (Pos == Text.npos)
1304 break;
1305
1306 char Sep = Text[Pos];
1307 Text = Text.substr(Pos + 1);
1308 if (Sep == ',')
1309 // Just a name ending in a comma, continue.
1310 continue;
1311
1312 if (Sep == '(') {
1313 // Push the inner pipeline onto the stack to continue processing.
1314 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1315 continue;
1316 }
1317
1318 assert(Sep == ')' && "Bogus separator!");
1319 // When handling the close parenthesis, we greedily consume them to avoid
1320 // empty strings in the pipeline.
1321 do {
1322 // If we try to pop the outer pipeline we have unbalanced parentheses.
1323 if (PipelineStack.size() == 1)
1324 return None;
1325
1326 PipelineStack.pop_back();
1327 } while (Text.consume_front(")"));
1328
1329 // Check if we've finished parsing.
1330 if (Text.empty())
1331 break;
1332
1333 // Otherwise, the end of an inner pipeline always has to be followed by
1334 // a comma, and then we can continue.
1335 if (!Text.consume_front(","))
1336 return None;
1337 }
1338
1339 if (PipelineStack.size() > 1)
1340 // Unbalanced paretheses.
1341 return None;
1342
1343 assert(PipelineStack.back() == &ResultPipeline &&
1344 "Wrong pipeline at the bottom of the stack!");
1345 return {std::move(ResultPipeline)};
1346}
1347
1348bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1349 const PipelineElement &E, bool VerifyEachPass,
1350 bool DebugLogging) {
1351 auto &Name = E.Name;
1352 auto &InnerPipeline = E.InnerPipeline;
1353
1354 // First handle complex passes like the pass managers which carry pipelines.
1355 if (!InnerPipeline.empty()) {
1356 if (Name == "module") {
1357 ModulePassManager NestedMPM(DebugLogging);
1358 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1359 DebugLogging))
1360 return false;
1361 MPM.addPass(std::move(NestedMPM));
1362 return true;
1363 }
1364 if (Name == "cgscc") {
1365 CGSCCPassManager CGPM(DebugLogging);
1366 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1367 DebugLogging))
1368 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001369 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001370 return true;
1371 }
1372 if (Name == "function") {
1373 FunctionPassManager FPM(DebugLogging);
1374 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1375 DebugLogging))
1376 return false;
1377 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1378 return true;
1379 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001380 if (auto Count = parseRepeatPassName(Name)) {
1381 ModulePassManager NestedMPM(DebugLogging);
1382 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1383 DebugLogging))
1384 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001385 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001386 return true;
1387 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001388
1389 for (auto &C : ModulePipelineParsingCallbacks)
1390 if (C(Name, MPM, InnerPipeline))
1391 return true;
1392
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001393 // Normal passes can't have pipelines.
1394 return false;
1395 }
1396
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001397 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001398 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001399 SmallVector<StringRef, 3> Matches;
1400 if (!DefaultAliasRegex.match(Name, &Matches))
1401 return false;
1402 assert(Matches.size() == 3 && "Must capture two matched strings!");
1403
Jordan Rosef85a95f2016-07-25 18:34:51 +00001404 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001405 .Case("O0", O0)
1406 .Case("O1", O1)
1407 .Case("O2", O2)
1408 .Case("O3", O3)
1409 .Case("Os", Os)
1410 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001411 if (L == O0)
1412 // At O0 we do nothing at all!
1413 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001414
1415 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001416 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001417 } else if (Matches[1] == "thinlto-pre-link") {
1418 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1419 } else if (Matches[1] == "thinlto") {
1420 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001421 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001422 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001423 } else {
1424 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001425 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001426 }
1427 return true;
1428 }
1429
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001430 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001431#define MODULE_PASS(NAME, CREATE_PASS) \
1432 if (Name == NAME) { \
1433 MPM.addPass(CREATE_PASS); \
1434 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001435 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001436#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1437 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001438 MPM.addPass( \
1439 RequireAnalysisPass< \
1440 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001441 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001442 } \
1443 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001444 MPM.addPass(InvalidateAnalysisPass< \
1445 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001446 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001447 }
1448#include "PassRegistry.def"
1449
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001450 for (auto &C : ModulePipelineParsingCallbacks)
1451 if (C(Name, MPM, InnerPipeline))
1452 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001453 return false;
1454}
1455
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001456bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1457 const PipelineElement &E, bool VerifyEachPass,
1458 bool DebugLogging) {
1459 auto &Name = E.Name;
1460 auto &InnerPipeline = E.InnerPipeline;
1461
1462 // First handle complex passes like the pass managers which carry pipelines.
1463 if (!InnerPipeline.empty()) {
1464 if (Name == "cgscc") {
1465 CGSCCPassManager NestedCGPM(DebugLogging);
1466 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1467 DebugLogging))
1468 return false;
1469 // Add the nested pass manager with the appropriate adaptor.
1470 CGPM.addPass(std::move(NestedCGPM));
1471 return true;
1472 }
1473 if (Name == "function") {
1474 FunctionPassManager FPM(DebugLogging);
1475 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1476 DebugLogging))
1477 return false;
1478 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001479 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001480 return true;
1481 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001482 if (auto Count = parseRepeatPassName(Name)) {
1483 CGSCCPassManager NestedCGPM(DebugLogging);
1484 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1485 DebugLogging))
1486 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001487 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001488 return true;
1489 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001490 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1491 CGSCCPassManager NestedCGPM(DebugLogging);
1492 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1493 DebugLogging))
1494 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001495 CGPM.addPass(
1496 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001497 return true;
1498 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001499
1500 for (auto &C : CGSCCPipelineParsingCallbacks)
1501 if (C(Name, CGPM, InnerPipeline))
1502 return true;
1503
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001504 // Normal passes can't have pipelines.
1505 return false;
1506 }
1507
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001508// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001509#define CGSCC_PASS(NAME, CREATE_PASS) \
1510 if (Name == NAME) { \
1511 CGPM.addPass(CREATE_PASS); \
1512 return true; \
1513 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001514#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1515 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001516 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001517 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001518 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1519 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001520 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001521 } \
1522 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001523 CGPM.addPass(InvalidateAnalysisPass< \
1524 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001525 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001526 }
1527#include "PassRegistry.def"
1528
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001529 for (auto &C : CGSCCPipelineParsingCallbacks)
1530 if (C(Name, CGPM, InnerPipeline))
1531 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001532 return false;
1533}
1534
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001535bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1536 const PipelineElement &E,
1537 bool VerifyEachPass, bool DebugLogging) {
1538 auto &Name = E.Name;
1539 auto &InnerPipeline = E.InnerPipeline;
1540
1541 // First handle complex passes like the pass managers which carry pipelines.
1542 if (!InnerPipeline.empty()) {
1543 if (Name == "function") {
1544 FunctionPassManager NestedFPM(DebugLogging);
1545 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1546 DebugLogging))
1547 return false;
1548 // Add the nested pass manager with the appropriate adaptor.
1549 FPM.addPass(std::move(NestedFPM));
1550 return true;
1551 }
1552 if (Name == "loop") {
1553 LoopPassManager LPM(DebugLogging);
1554 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1555 DebugLogging))
1556 return false;
1557 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001558 FPM.addPass(
1559 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001560 return true;
1561 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001562 if (auto Count = parseRepeatPassName(Name)) {
1563 FunctionPassManager NestedFPM(DebugLogging);
1564 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1565 DebugLogging))
1566 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001567 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001568 return true;
1569 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001570
1571 for (auto &C : FunctionPipelineParsingCallbacks)
1572 if (C(Name, FPM, InnerPipeline))
1573 return true;
1574
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001575 // Normal passes can't have pipelines.
1576 return false;
1577 }
1578
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001579// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001580#define FUNCTION_PASS(NAME, CREATE_PASS) \
1581 if (Name == NAME) { \
1582 FPM.addPass(CREATE_PASS); \
1583 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001584 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001585#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1586 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001587 FPM.addPass( \
1588 RequireAnalysisPass< \
1589 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001590 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001591 } \
1592 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001593 FPM.addPass(InvalidateAnalysisPass< \
1594 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001595 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001596 }
1597#include "PassRegistry.def"
1598
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001599 for (auto &C : FunctionPipelineParsingCallbacks)
1600 if (C(Name, FPM, InnerPipeline))
1601 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001602 return false;
1603}
1604
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001605bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001606 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001607 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001608 auto &InnerPipeline = E.InnerPipeline;
1609
1610 // First handle complex passes like the pass managers which carry pipelines.
1611 if (!InnerPipeline.empty()) {
1612 if (Name == "loop") {
1613 LoopPassManager NestedLPM(DebugLogging);
1614 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1615 DebugLogging))
1616 return false;
1617 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001618 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001619 return true;
1620 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001621 if (auto Count = parseRepeatPassName(Name)) {
1622 LoopPassManager NestedLPM(DebugLogging);
1623 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1624 DebugLogging))
1625 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001626 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001627 return true;
1628 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001629
1630 for (auto &C : LoopPipelineParsingCallbacks)
1631 if (C(Name, LPM, InnerPipeline))
1632 return true;
1633
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001634 // Normal passes can't have pipelines.
1635 return false;
1636 }
1637
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001638// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001639#define LOOP_PASS(NAME, CREATE_PASS) \
1640 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001641 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001642 return true; \
1643 }
1644#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1645 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001646 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001647 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1648 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1649 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001650 return true; \
1651 } \
1652 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001653 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001654 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001655 return true; \
1656 }
1657#include "PassRegistry.def"
1658
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001659 for (auto &C : LoopPipelineParsingCallbacks)
1660 if (C(Name, LPM, InnerPipeline))
1661 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001662 return false;
1663}
1664
Chandler Carruthedf59962016-02-18 09:45:17 +00001665bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001666#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1667 if (Name == NAME) { \
1668 AA.registerModuleAnalysis< \
1669 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1670 return true; \
1671 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001672#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1673 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001674 AA.registerFunctionAnalysis< \
1675 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001676 return true; \
1677 }
1678#include "PassRegistry.def"
1679
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001680 for (auto &C : AAParsingCallbacks)
1681 if (C(Name, AA))
1682 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001683 return false;
1684}
1685
Justin Bognereecc3c82016-02-25 07:23:08 +00001686bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001687 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001688 bool VerifyEachPass,
1689 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001690 for (const auto &Element : Pipeline) {
1691 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1692 return false;
1693 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001694 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001695 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001696}
1697
Chandler Carruth1ff77242015-03-07 09:02:36 +00001698bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001699 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001700 bool VerifyEachPass,
1701 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001702 for (const auto &Element : Pipeline) {
1703 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1704 return false;
1705 if (VerifyEachPass)
1706 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001707 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001708 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001709}
1710
Chandler Carruth1ff77242015-03-07 09:02:36 +00001711bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001712 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001713 bool VerifyEachPass,
1714 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001715 for (const auto &Element : Pipeline) {
1716 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1717 return false;
1718 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001719 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001720 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001721}
1722
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001723void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1724 FunctionAnalysisManager &FAM,
1725 CGSCCAnalysisManager &CGAM,
1726 ModuleAnalysisManager &MAM) {
1727 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1728 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001729 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1730 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1731 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1732 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1733 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1734}
1735
Chandler Carruth1ff77242015-03-07 09:02:36 +00001736bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001737 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001738 bool VerifyEachPass,
1739 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001740 for (const auto &Element : Pipeline) {
1741 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1742 return false;
1743 if (VerifyEachPass)
1744 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001745 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001746 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001747}
1748
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001749// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001750// FIXME: Should this routine accept a TargetMachine or require the caller to
1751// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001752bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1753 StringRef PipelineText, bool VerifyEachPass,
1754 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001755 auto Pipeline = parsePipelineText(PipelineText);
1756 if (!Pipeline || Pipeline->empty())
1757 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001758
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001759 // If the first name isn't at the module layer, wrap the pipeline up
1760 // automatically.
1761 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001762
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001763 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1764 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001765 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001766 } else if (isFunctionPassName(FirstName,
1767 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001768 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001769 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001770 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001771 } else {
1772 for (auto &C : TopLevelPipelineParsingCallbacks)
1773 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1774 return true;
1775
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001776 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001777 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001778 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001779 }
1780
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001781 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001782}
Chandler Carruthedf59962016-02-18 09:45:17 +00001783
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001784// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1785bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1786 StringRef PipelineText, bool VerifyEachPass,
1787 bool DebugLogging) {
1788 auto Pipeline = parsePipelineText(PipelineText);
1789 if (!Pipeline || Pipeline->empty())
1790 return false;
1791
1792 StringRef FirstName = Pipeline->front().Name;
1793 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1794 return false;
1795
1796 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1797}
1798
1799// Primary pass pipeline description parsing routine for a \c
1800// FunctionPassManager
1801bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1802 StringRef PipelineText, bool VerifyEachPass,
1803 bool DebugLogging) {
1804 auto Pipeline = parsePipelineText(PipelineText);
1805 if (!Pipeline || Pipeline->empty())
1806 return false;
1807
1808 StringRef FirstName = Pipeline->front().Name;
1809 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1810 return false;
1811
1812 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1813 DebugLogging);
1814}
1815
1816// Primary pass pipeline description parsing routine for a \c LoopPassManager
1817bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1818 StringRef PipelineText, bool VerifyEachPass,
1819 bool DebugLogging) {
1820 auto Pipeline = parsePipelineText(PipelineText);
1821 if (!Pipeline || Pipeline->empty())
1822 return false;
1823
1824 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1825}
1826
Chandler Carruthedf59962016-02-18 09:45:17 +00001827bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001828 // If the pipeline just consists of the word 'default' just replace the AA
1829 // manager with our default one.
1830 if (PipelineText == "default") {
1831 AA = buildDefaultAAPipeline();
1832 return true;
1833 }
1834
Chandler Carruthedf59962016-02-18 09:45:17 +00001835 while (!PipelineText.empty()) {
1836 StringRef Name;
1837 std::tie(Name, PipelineText) = PipelineText.split(',');
1838 if (!parseAAPassName(AA, Name))
1839 return false;
1840 }
1841
1842 return true;
1843}