blob: 89570f7d153ad76290033c3b3973047466d70557 [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"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000113#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000114#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000115#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000116#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000117#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000118#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000119#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000120#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000121#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000122#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000123#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000124#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000125#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000126#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000127#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000128#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000129#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000130#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000131#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000132#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000133#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000134#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000135#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000136#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000137#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000138#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000139#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000140#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000141#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000142#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000143#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000144#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000145#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000146#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000147#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000148#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000149#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000150#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000151#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000152#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000153
Chandler Carruth66445382014-01-11 08:16:35 +0000154using namespace llvm;
155
Chandler Carruth719ffe12017-02-12 05:38:04 +0000156static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
157 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000158static cl::opt<bool>
159 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
160 cl::Hidden, cl::ZeroOrMore,
161 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000162
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000163static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000164 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000165 cl::Hidden, cl::ZeroOrMore,
166 cl::desc("Run NewGVN instead of GVN"));
167
Geoff Berry3cca1da2017-06-10 15:20:03 +0000168static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000169 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
170 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000171
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000172static cl::opt<bool> EnableGVNHoist(
173 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
174 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
175
Davide Italianobe1b6a92017-06-03 23:18:29 +0000176static cl::opt<bool> EnableGVNSink(
177 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
178 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
179
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000180static cl::opt<bool> EnableSyntheticCounts(
181 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
182 cl::desc("Run synthetic function entry count generation "
183 "pass"));
184
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000185static Regex DefaultAliasRegex(
186 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000187
Chandler Carruthe3f50642016-12-22 06:59:15 +0000188static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
189 switch (Level) {
190 case PassBuilder::O0:
191 case PassBuilder::O1:
192 case PassBuilder::O2:
193 case PassBuilder::O3:
194 return false;
195
196 case PassBuilder::Os:
197 case PassBuilder::Oz:
198 return true;
199 }
200 llvm_unreachable("Invalid optimization level!");
201}
202
Chandler Carruth66445382014-01-11 08:16:35 +0000203namespace {
204
Chandler Carruthd8330982014-01-12 09:34:22 +0000205/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000206struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000207 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000208 return PreservedAnalyses::all();
209 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000210 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000211};
212
Chandler Carruth0b576b32015-01-06 02:50:06 +0000213/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000214class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
215 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000216 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000217
218public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000219 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000220 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000221 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000222};
223
Chandler Carruth572e3402014-04-21 11:12:00 +0000224/// \brief No-op CGSCC pass which does nothing.
225struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000226 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
227 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000228 return PreservedAnalyses::all();
229 }
230 static StringRef name() { return "NoOpCGSCCPass"; }
231};
232
Chandler Carruth0b576b32015-01-06 02:50:06 +0000233/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000234class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
235 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000236 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000237
238public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000239 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000240 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000241 return Result();
242 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000243 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000244};
245
Chandler Carruthd8330982014-01-12 09:34:22 +0000246/// \brief No-op function pass which does nothing.
247struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000248 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000249 return PreservedAnalyses::all();
250 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000251 static StringRef name() { return "NoOpFunctionPass"; }
252};
253
Chandler Carruth0b576b32015-01-06 02:50:06 +0000254/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000255class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
256 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000257 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000258
259public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000260 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000261 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000262 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000263};
264
Justin Bognereecc3c82016-02-25 07:23:08 +0000265/// \brief No-op loop pass which does nothing.
266struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000267 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
268 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000269 return PreservedAnalyses::all();
270 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000271 static StringRef name() { return "NoOpLoopPass"; }
272};
273
274/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000275class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
276 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000277 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000278
279public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000280 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000281 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
282 return Result();
283 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000284 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000285};
286
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000287AnalysisKey NoOpModuleAnalysis::Key;
288AnalysisKey NoOpCGSCCAnalysis::Key;
289AnalysisKey NoOpFunctionAnalysis::Key;
290AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000291
Chandler Carruth66445382014-01-11 08:16:35 +0000292} // End anonymous namespace.
293
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000294void PassBuilder::invokePeepholeEPCallbacks(
295 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
296 for (auto &C : PeepholeEPCallbacks)
297 C(FPM, Level);
298}
299
Chandler Carruth1ff77242015-03-07 09:02:36 +0000300void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000301#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000302 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000303#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000304
305 for (auto &C : ModuleAnalysisRegistrationCallbacks)
306 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000307}
308
Chandler Carruth1ff77242015-03-07 09:02:36 +0000309void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000310#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000311 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000312#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000313
314 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
315 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000316}
317
Chandler Carruth1ff77242015-03-07 09:02:36 +0000318void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000319#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000320 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000321#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000322
323 for (auto &C : FunctionAnalysisRegistrationCallbacks)
324 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000325}
326
Justin Bognereecc3c82016-02-25 07:23:08 +0000327void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000328#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000329 LAM.registerPass([&] { return CREATE_PASS; });
330#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000331
332 for (auto &C : LoopAnalysisRegistrationCallbacks)
333 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000334}
335
Chandler Carruthe3f50642016-12-22 06:59:15 +0000336FunctionPassManager
337PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000338 ThinLTOPhase Phase,
339 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000340 assert(Level != O0 && "Must request optimizations!");
341 FunctionPassManager FPM(DebugLogging);
342
343 // Form SSA out of local memory accesses after breaking apart aggregates into
344 // scalars.
345 FPM.addPass(SROA());
346
347 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000348 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000349
Davide Italianoc3688312017-06-01 23:08:14 +0000350 // Hoisting of scalars and load expressions.
351 if (EnableGVNHoist)
352 FPM.addPass(GVNHoistPass());
353
Davide Italianobe1b6a92017-06-03 23:18:29 +0000354 // Global value numbering based sinking.
355 if (EnableGVNSink) {
356 FPM.addPass(GVNSinkPass());
357 FPM.addPass(SimplifyCFGPass());
358 }
359
Chandler Carruthe3f50642016-12-22 06:59:15 +0000360 // Speculative execution if the target has divergent branches; otherwise nop.
361 FPM.addPass(SpeculativeExecutionPass());
362
363 // Optimize based on known information about branches, and cleanup afterward.
364 FPM.addPass(JumpThreadingPass());
365 FPM.addPass(CorrelatedValuePropagationPass());
366 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000367 if (Level == O3)
368 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000369 FPM.addPass(InstCombinePass());
370
371 if (!isOptimizingForSize(Level))
372 FPM.addPass(LibCallsShrinkWrapPass());
373
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000374 invokePeepholeEPCallbacks(FPM, Level);
375
Rong Xue1f42452017-10-23 22:21:29 +0000376 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
377 // using the size value profile. Don't perform this when optimizing for size.
378 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
379 !isOptimizingForSize(Level))
380 FPM.addPass(PGOMemOPSizeOpt());
381
Chandler Carruthe3f50642016-12-22 06:59:15 +0000382 FPM.addPass(TailCallElimPass());
383 FPM.addPass(SimplifyCFGPass());
384
385 // Form canonically associated expression trees, and simplify the trees using
386 // basic mathematical properties. For example, this will form (nearly)
387 // minimal multiplication trees.
388 FPM.addPass(ReassociatePass());
389
390 // Add the primary loop simplification pipeline.
391 // FIXME: Currently this is split into two loop pass pipelines because we run
392 // some function passes in between them. These can and should be replaced by
393 // loop pass equivalenst but those aren't ready yet. Specifically,
394 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
395 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
396 // the other we have is `LoopInstSimplify`.
397 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
398
399 // Rotate Loop - disable header duplication at -Oz
400 LPM1.addPass(LoopRotatePass(Level != Oz));
401 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000402 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000403 LPM2.addPass(IndVarSimplifyPass());
404 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000405
406 for (auto &C : LateLoopOptimizationsEPCallbacks)
407 C(LPM2, Level);
408
Chandler Carruth79b733b2017-01-26 23:21:17 +0000409 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000410 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000411 // because it changes IR to makes profile annotation in back compile
412 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000413 if (Phase != ThinLTOPhase::PreLink ||
414 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000415 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000416
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000417 for (auto &C : LoopOptimizerEndEPCallbacks)
418 C(LPM2, Level);
419
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000420 // We provide the opt remark emitter pass for LICM to use. We only need to do
421 // this once as it is immutable.
422 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000423 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000424 FPM.addPass(SimplifyCFGPass());
425 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000426 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000427
428 // Eliminate redundancies.
429 if (Level != O1) {
430 // These passes add substantial compile time so skip them at O1.
431 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000432 if (RunNewGVN)
433 FPM.addPass(NewGVNPass());
434 else
435 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000436 }
437
438 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
439 FPM.addPass(MemCpyOptPass());
440
441 // Sparse conditional constant propagation.
442 // FIXME: It isn't clear why we do this *after* loop passes rather than
443 // before...
444 FPM.addPass(SCCPPass());
445
446 // Delete dead bit computations (instcombine runs after to fold away the dead
447 // computations, and then ADCE will run later to exploit any new DCE
448 // opportunities that creates).
449 FPM.addPass(BDCEPass());
450
451 // Run instcombine after redundancy and dead bit elimination to exploit
452 // opportunities opened up by them.
453 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000454 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000455
456 // Re-consider control flow based optimizations after redundancy elimination,
457 // redo DCE, etc.
458 FPM.addPass(JumpThreadingPass());
459 FPM.addPass(CorrelatedValuePropagationPass());
460 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000461 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000462
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000463 for (auto &C : ScalarOptimizerLateEPCallbacks)
464 C(FPM, Level);
465
Chandler Carruthe3f50642016-12-22 06:59:15 +0000466 // Finally, do an expensive DCE pass to catch all the dead code exposed by
467 // the simplifications and basic cleanup after all the simplifications.
468 FPM.addPass(ADCEPass());
469 FPM.addPass(SimplifyCFGPass());
470 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000471 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000472
473 return FPM;
474}
475
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000476void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
477 PassBuilder::OptimizationLevel Level,
478 bool RunProfileGen,
479 std::string ProfileGenFile,
480 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000481 // Generally running simplification passes and the inliner with an high
482 // threshold results in smaller executables, but there may be cases where
483 // the size grows, so let's be conservative here and skip this simplification
484 // at -Os/Oz.
485 if (!isOptimizingForSize(Level)) {
486 InlineParams IP;
487
488 // In the old pass manager, this is a cl::opt. Should still this be one?
489 IP.DefaultThreshold = 75;
490
491 // FIXME: The hint threshold has the same value used by the regular inliner.
492 // This should probably be lowered after performance testing.
493 // FIXME: this comment is cargo culted from the old pass manager, revisit).
494 IP.HintThreshold = 325;
495
496 CGSCCPassManager CGPipeline(DebugLogging);
497
498 CGPipeline.addPass(InlinerPass(IP));
499
500 FunctionPassManager FPM;
501 FPM.addPass(SROA());
502 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
503 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
504 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000505 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000506
Davide Italiano513dfaa2017-02-13 15:26:22 +0000507 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
508
509 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
510 }
511
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000512 // Delete anything that is now dead to make sure that we don't instrument
513 // dead code. Instrumentation can end up keeping dead code around and
514 // dramatically increase code size.
515 MPM.addPass(GlobalDCEPass());
516
Davide Italiano513dfaa2017-02-13 15:26:22 +0000517 if (RunProfileGen) {
518 MPM.addPass(PGOInstrumentationGen());
519
Xinliang David Lib67530e2017-06-25 00:26:43 +0000520 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000521 FPM.addPass(
522 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000523 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
524
Davide Italiano513dfaa2017-02-13 15:26:22 +0000525 // Add the profile lowering pass.
526 InstrProfOptions Options;
527 if (!ProfileGenFile.empty())
528 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000529 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000530 MPM.addPass(InstrProfiling(Options));
531 }
532
533 if (!ProfileUseFile.empty())
534 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
535}
536
Easwaran Raman8249fac2017-06-28 13:33:49 +0000537static InlineParams
538getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
539 auto O3 = PassBuilder::O3;
540 unsigned OptLevel = Level > O3 ? 2 : Level;
541 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
542 return getInlineParams(OptLevel, SizeLevel);
543}
544
Chandler Carruthe3f50642016-12-22 06:59:15 +0000545ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000546PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000547 ThinLTOPhase Phase,
548 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000549 ModulePassManager MPM(DebugLogging);
550
Chandler Carruthe3f50642016-12-22 06:59:15 +0000551 // Do basic inference of function attributes from known properties of system
552 // libraries and other oracles.
553 MPM.addPass(InferFunctionAttrsPass());
554
555 // Create an early function pass manager to cleanup the output of the
556 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000557 FunctionPassManager EarlyFPM(DebugLogging);
558 EarlyFPM.addPass(SimplifyCFGPass());
559 EarlyFPM.addPass(SROA());
560 EarlyFPM.addPass(EarlyCSEPass());
561 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000562 if (Level == O3)
563 EarlyFPM.addPass(CallSiteSplittingPass());
564
Dehao Chen08f88312017-08-07 20:23:20 +0000565 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
566 // to convert bitcast to direct calls so that they can be inlined during the
567 // profile annotation prepration step.
568 // More details about SamplePGO design can be found in:
569 // https://research.google.com/pubs/pub45290.html
570 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
571 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
572 Phase == ThinLTOPhase::PostLink)
573 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000574 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000575
Dehao Chen08f88312017-08-07 20:23:20 +0000576 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
577 // Annotate sample profile right after early FPM to ensure freshness of
578 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000579 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
580 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000581 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
582 // for the profile annotation to be accurate in the ThinLTO backend.
583 if (Phase != ThinLTOPhase::PreLink)
584 // We perform early indirect call promotion here, before globalopt.
585 // This is important for the ThinLTO backend phase because otherwise
586 // imported available_externally functions look unreferenced and are
587 // removed.
588 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
589 true));
590 }
591
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000592 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000593 // and prior to optimizing globals.
594 // FIXME: This position in the pipeline hasn't been carefully considered in
595 // years, it should be re-analyzed.
596 MPM.addPass(IPSCCPPass());
597
Matthew Simpsoncb585582017-10-25 13:40:08 +0000598 // Attach metadata to indirect call sites indicating the set of functions
599 // they may target at run-time. This should follow IPSCCP.
600 MPM.addPass(CalledValuePropagationPass());
601
Chandler Carruthe3f50642016-12-22 06:59:15 +0000602 // Optimize globals to try and fold them into constants.
603 MPM.addPass(GlobalOptPass());
604
605 // Promote any localized globals to SSA registers.
606 // FIXME: Should this instead by a run of SROA?
607 // FIXME: We should probably run instcombine and simplify-cfg afterward to
608 // delete control flows that are dead once globals have been folded to
609 // constants.
610 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
611
612 // Remove any dead arguments exposed by cleanups and constand folding
613 // globals.
614 MPM.addPass(DeadArgumentEliminationPass());
615
616 // Create a small function pass pipeline to cleanup after all the global
617 // optimizations.
618 FunctionPassManager GlobalCleanupPM(DebugLogging);
619 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000620 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
621
Chandler Carruthe3f50642016-12-22 06:59:15 +0000622 GlobalCleanupPM.addPass(SimplifyCFGPass());
623 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
624
Dehao Chen89d32262017-08-02 01:28:31 +0000625 // Add all the requested passes for instrumentation PGO, if requested.
626 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
627 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
628 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
629 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
630 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000631 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000632
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000633 // Synthesize function entry counts for non-PGO compilation.
634 if (EnableSyntheticCounts && !PGOOpt)
635 MPM.addPass(SyntheticCountsPropagation());
636
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000637 // Require the GlobalsAA analysis for the module so we can query it within
638 // the CGSCC pipeline.
639 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000640
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000641 // Require the ProfileSummaryAnalysis for the module so we can query it within
642 // the inliner pass.
643 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
644
Chandler Carruthe3f50642016-12-22 06:59:15 +0000645 // Now begin the main postorder CGSCC pipeline.
646 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
647 // manager and trying to emulate its precise behavior. Much of this doesn't
648 // make a lot of sense and we should revisit the core CGSCC structure.
649 CGSCCPassManager MainCGPipeline(DebugLogging);
650
651 // Note: historically, the PruneEH pass was run first to deduce nounwind and
652 // generally clean up exception handling overhead. It isn't clear this is
653 // valuable as the inliner doesn't currently care whether it is inlining an
654 // invoke or a call.
655
656 // Run the inliner first. The theory is that we are walking bottom-up and so
657 // the callees have already been fully optimized, and we want to inline them
658 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000659 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000660 // because it makes profile annotation in the backend inaccurate.
661 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000662 if (Phase == ThinLTOPhase::PreLink &&
663 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000664 IP.HotCallSiteThreshold = 0;
665 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000666
667 // Now deduce any function attributes based in the current code.
668 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
669
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000670 // When at O3 add argument promotion to the pass pipeline.
671 // FIXME: It isn't at all clear why this should be limited to O3.
672 if (Level == O3)
673 MainCGPipeline.addPass(ArgumentPromotionPass());
674
Chandler Carruthe3f50642016-12-22 06:59:15 +0000675 // Lastly, add the core function simplification pipeline nested inside the
676 // CGSCC walk.
677 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000678 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000679
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000680 for (auto &C : CGSCCOptimizerLateEPCallbacks)
681 C(MainCGPipeline, Level);
682
Chandler Carruth719ffe12017-02-12 05:38:04 +0000683 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
684 // to detect when we devirtualize indirect calls and iterate the SCC passes
685 // in that case to try and catch knock-on inlining or function attrs
686 // opportunities. Then we add it to the module pipeline by walking the SCCs
687 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000688 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000689 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000690 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000691
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000692 return MPM;
693}
694
695ModulePassManager
696PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
697 bool DebugLogging) {
698 ModulePassManager MPM(DebugLogging);
699
700 // Optimize globals now that the module is fully simplified.
701 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000702 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000703
Xinliang David Li126157c2017-05-22 16:41:57 +0000704 // Run partial inlining pass to partially inline functions that have
705 // large bodies.
706 if (RunPartialInlining)
707 MPM.addPass(PartialInlinerPass());
708
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000709 // Remove avail extern fns and globals definitions since we aren't compiling
710 // an object file for later LTO. For LTO we want to preserve these so they
711 // are eligible for inlining at link-time. Note if they are unreferenced they
712 // will be removed by GlobalDCE later, so this only impacts referenced
713 // available externally globals. Eventually they will be suppressed during
714 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
715 // may make globals referenced by available external functions dead and saves
716 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000717 MPM.addPass(EliminateAvailableExternallyPass());
718
719 // Do RPO function attribute inference across the module to forward-propagate
720 // attributes where applicable.
721 // FIXME: Is this really an optimization rather than a canonicalization?
722 MPM.addPass(ReversePostOrderFunctionAttrsPass());
723
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000724 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000725 // useful as the above will have inlined, DCE'ed, and function-attr
726 // propagated everything. We should at this point have a reasonably minimal
727 // and richly annotated call graph. By computing aliasing and mod/ref
728 // information for all local globals here, the late loop passes and notably
729 // the vectorizer will be able to use them to help recognize vectorizable
730 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000731 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000732
733 FunctionPassManager OptimizePM(DebugLogging);
734 OptimizePM.addPass(Float2IntPass());
735 // FIXME: We need to run some loop optimizations to re-rotate loops after
736 // simplify-cfg and others undo their rotation.
737
738 // Optimize the loop execution. These passes operate on entire loop nests
739 // rather than on each loop in an inside-out manner, and so they are actually
740 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000741
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000742 for (auto &C : VectorizerStartEPCallbacks)
743 C(OptimizePM, Level);
744
Chandler Carrutha95ff382017-01-27 00:50:21 +0000745 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000746 OptimizePM.addPass(
747 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000748
749 // Distribute loops to allow partial vectorization. I.e. isolate dependences
750 // into separate loop that would otherwise inhibit vectorization. This is
751 // currently only performed for loops marked with the metadata
752 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000753 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000754
755 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000756 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000757
Chandler Carruthbaabda92017-01-27 01:32:26 +0000758 // Eliminate loads by forwarding stores from the previous iteration to loads
759 // of the current iteration.
760 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000761
762 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000763 OptimizePM.addPass(InstCombinePass());
764
Chandler Carrutha95ff382017-01-27 00:50:21 +0000765 // Now that we've formed fast to execute loop structures, we do further
766 // optimizations. These are run afterward as they might block doing complex
767 // analyses and transforms such as what are needed for loop vectorization.
768
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000769 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
770 // GVN, loop transforms, and others have already run, so it's now better to
771 // convert to more optimized IR using more aggressive simplify CFG options.
772 // The extra sinking transform can create larger basic blocks, so do this
773 // before SLP vectorization.
774 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
775 forwardSwitchCondToPhi(true).
776 convertSwitchToLookupTable(true).
777 needCanonicalLoops(false).
778 sinkCommonInsts(true)));
779
Chandler Carruthe3f50642016-12-22 06:59:15 +0000780 // Optimize parallel scalar instruction chains into SIMD instructions.
781 OptimizePM.addPass(SLPVectorizerPass());
782
Chandler Carruthe3f50642016-12-22 06:59:15 +0000783 OptimizePM.addPass(InstCombinePass());
784
785 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000786 // execution resources of an out-of-order processor. We also then need to
787 // clean up redundancies and loop invariant code.
788 // FIXME: It would be really good to use a loop-integrated instruction
789 // combiner for cleanup here so that the unrolling and LICM can be pipelined
790 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000791 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000792 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000793 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000794 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000795
796 // Now that we've vectorized and unrolled loops, we may have more refined
797 // alignment information, try to re-derive it here.
798 OptimizePM.addPass(AlignmentFromAssumptionsPass());
799
Chandler Carrutha95ff382017-01-27 00:50:21 +0000800 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
801 // canonicalization pass that enables other optimizations. As a result,
802 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
803 // result too early.
804 OptimizePM.addPass(LoopSinkPass());
805
806 // And finally clean up LCSSA form before generating code.
807 OptimizePM.addPass(InstSimplifierPass());
808
Sanjay Patel6fd43912017-09-09 13:38:18 +0000809 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
810 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
811 // flattening of blocks.
812 OptimizePM.addPass(DivRemPairsPass());
813
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000814 // LoopSink (and other loop passes since the last simplifyCFG) might have
815 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
816 OptimizePM.addPass(SimplifyCFGPass());
817
Chandler Carruthc34f7892017-11-28 11:32:31 +0000818 // Optimize PHIs by speculating around them when profitable. Note that this
819 // pass needs to be run after any PRE or similar pass as it is essentially
820 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
821 OptimizePM.addPass(SpeculateAroundPHIsPass());
822
Chandler Carrutha95ff382017-01-27 00:50:21 +0000823 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000824 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
825
826 // Now we need to do some global optimization transforms.
827 // FIXME: It would seem like these should come first in the optimization
828 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
829 // ordering here.
830 MPM.addPass(GlobalDCEPass());
831 MPM.addPass(ConstantMergePass());
832
833 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000834}
835
Chandler Carruthe3f50642016-12-22 06:59:15 +0000836ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000837PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
838 bool DebugLogging) {
839 assert(Level != O0 && "Must request optimizations for the default pipeline!");
840
841 ModulePassManager MPM(DebugLogging);
842
843 // Force any function attributes we want the rest of the pipeline to observe.
844 MPM.addPass(ForceFunctionAttrsPass());
845
David Blaikie0c64f5a2018-01-23 01:25:20 +0000846 // Apply module pipeline start EP callback.
847 for (auto &C : PipelineStartEPCallbacks)
848 C(MPM);
849
Dehao Chen08f88312017-08-07 20:23:20 +0000850 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000851 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
852
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000853 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000854 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
855 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000856
857 // Now add the optimization pipeline.
858 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
859
860 return MPM;
861}
862
863ModulePassManager
864PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
865 bool DebugLogging) {
866 assert(Level != O0 && "Must request optimizations for the default pipeline!");
867
868 ModulePassManager MPM(DebugLogging);
869
870 // Force any function attributes we want the rest of the pipeline to observe.
871 MPM.addPass(ForceFunctionAttrsPass());
872
Dehao Chen08f88312017-08-07 20:23:20 +0000873 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000874 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
875
David Blaikie0c64f5a2018-01-23 01:25:20 +0000876 // Apply module pipeline start EP callback.
877 for (auto &C : PipelineStartEPCallbacks)
878 C(MPM);
879
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000880 // If we are planning to perform ThinLTO later, we don't bloat the code with
881 // unrolling/vectorization/... now. Just simplify the module as much as we
882 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000883 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
884 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000885
886 // Run partial inlining pass to partially inline functions that have
887 // large bodies.
888 // FIXME: It isn't clear whether this is really the right place to run this
889 // in ThinLTO. Because there is another canonicalization and simplification
890 // phase that will run after the thin link, running this here ends up with
891 // less information than will be available later and it may grow functions in
892 // ways that aren't beneficial.
893 if (RunPartialInlining)
894 MPM.addPass(PartialInlinerPass());
895
896 // Reduce the size of the IR as much as possible.
897 MPM.addPass(GlobalOptPass());
898
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000899 return MPM;
900}
901
902ModulePassManager
903PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
904 bool DebugLogging) {
905 // FIXME: The summary index is not hooked in the new pass manager yet.
906 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
907 // here.
908
909 ModulePassManager MPM(DebugLogging);
910
911 // Force any function attributes we want the rest of the pipeline to observe.
912 MPM.addPass(ForceFunctionAttrsPass());
913
914 // During the ThinLTO backend phase we perform early indirect call promotion
915 // here, before globalopt. Otherwise imported available_externally functions
916 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000917 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
918 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000919 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000920 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
921 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000922
923 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000924 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
925 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000926
927 // Now add the optimization pipeline.
928 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
929
930 return MPM;
931}
932
933ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000934PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
935 bool DebugLogging) {
936 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000937 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000938 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000939}
940
Chandler Carruthe3f50642016-12-22 06:59:15 +0000941ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
942 bool DebugLogging) {
943 assert(Level != O0 && "Must request optimizations for the default pipeline!");
944 ModulePassManager MPM(DebugLogging);
945
Davide Italiano089a9122017-01-24 00:57:39 +0000946 // Remove unused virtual tables to improve the quality of code generated by
947 // whole-program devirtualization and bitset lowering.
948 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000949
Davide Italiano089a9122017-01-24 00:57:39 +0000950 // Force any function attributes we want the rest of the pipeline to observe.
951 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000952
Davide Italiano089a9122017-01-24 00:57:39 +0000953 // Do basic inference of function attributes from known properties of system
954 // libraries and other oracles.
955 MPM.addPass(InferFunctionAttrsPass());
956
957 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000958 FunctionPassManager EarlyFPM(DebugLogging);
959 EarlyFPM.addPass(CallSiteSplittingPass());
960 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
961
Davide Italiano089a9122017-01-24 00:57:39 +0000962 // Indirect call promotion. This should promote all the targets that are
963 // left by the earlier promotion pass that promotes intra-module targets.
964 // This two-step promotion is to save the compile time. For LTO, it should
965 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000966 MPM.addPass(PGOIndirectCallPromotion(
967 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000968 // Propagate constants at call sites into the functions they call. This
969 // opens opportunities for globalopt (and inlining) by substituting function
970 // pointers passed as arguments to direct uses of functions.
971 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000972
973 // Attach metadata to indirect call sites indicating the set of functions
974 // they may target at run-time. This should follow IPSCCP.
975 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000976 }
977
978 // Now deduce any function attributes based in the current code.
979 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
980 PostOrderFunctionAttrsPass()));
981
982 // Do RPO function attribute inference across the module to forward-propagate
983 // attributes where applicable.
984 // FIXME: Is this really an optimization rather than a canonicalization?
985 MPM.addPass(ReversePostOrderFunctionAttrsPass());
986
987 // Use inragne annotations on GEP indices to split globals where beneficial.
988 MPM.addPass(GlobalSplitPass());
989
990 // Run whole program optimization of virtual call when the list of callees
991 // is fixed.
992 MPM.addPass(WholeProgramDevirtPass());
993
994 // Stop here at -O1.
995 if (Level == 1)
996 return MPM;
997
998 // Optimize globals to try and fold them into constants.
999 MPM.addPass(GlobalOptPass());
1000
1001 // Promote any localized globals to SSA registers.
1002 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1003
1004 // Linking modules together can lead to duplicate global constant, only
1005 // keep one copy of each constant.
1006 MPM.addPass(ConstantMergePass());
1007
1008 // Remove unused arguments from functions.
1009 MPM.addPass(DeadArgumentEliminationPass());
1010
1011 // Reduce the code after globalopt and ipsccp. Both can open up significant
1012 // simplification opportunities, and both can propagate functions through
1013 // function pointers. When this happens, we often have to resolve varargs
1014 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001015 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001016 if (Level == O3)
1017 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001018 PeepholeFPM.addPass(InstCombinePass());
1019 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1020
1021 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001022
1023 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1024 // generally clean up exception handling overhead. It isn't clear this is
1025 // valuable as the inliner doesn't currently care whether it is inlining an
1026 // invoke or a call.
1027 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001028 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1029 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001030
1031 // Optimize globals again after we ran the inliner.
1032 MPM.addPass(GlobalOptPass());
1033
1034 // Garbage collect dead functions.
1035 // FIXME: Add ArgumentPromotion pass after once it's ported.
1036 MPM.addPass(GlobalDCEPass());
1037
1038 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001039 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001040 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001041 invokePeepholeEPCallbacks(FPM, Level);
1042
Davide Italiano089a9122017-01-24 00:57:39 +00001043 FPM.addPass(JumpThreadingPass());
1044
1045 // Break up allocas
1046 FPM.addPass(SROA());
1047
1048 // Run a few AA driver optimizations here and now to cleanup the code.
1049 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1050
1051 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1052 PostOrderFunctionAttrsPass()));
1053 // FIXME: here we run IP alias analysis in the legacy PM.
1054
1055 FunctionPassManager MainFPM;
1056
1057 // FIXME: once we fix LoopPass Manager, add LICM here.
1058 // FIXME: once we provide support for enabling MLSM, add it here.
1059 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001060 if (RunNewGVN)
1061 MainFPM.addPass(NewGVNPass());
1062 else
1063 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001064
1065 // Remove dead memcpy()'s.
1066 MainFPM.addPass(MemCpyOptPass());
1067
1068 // Nuke dead stores.
1069 MainFPM.addPass(DSEPass());
1070
1071 // FIXME: at this point, we run a bunch of loop passes:
1072 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1073 // loopVectorize. Enable them once the remaining issue with LPM
1074 // are sorted out.
1075
1076 MainFPM.addPass(InstCombinePass());
1077 MainFPM.addPass(SimplifyCFGPass());
1078 MainFPM.addPass(SCCPPass());
1079 MainFPM.addPass(InstCombinePass());
1080 MainFPM.addPass(BDCEPass());
1081
1082 // FIXME: We may want to run SLPVectorizer here.
1083 // After vectorization, assume intrinsics may tell us more
1084 // about pointer alignments.
1085#if 0
1086 MainFPM.add(AlignmentFromAssumptionsPass());
1087#endif
1088
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001089 // FIXME: Conditionally run LoadCombine here, after it's ported
1090 // (in case we still have this pass, given its questionable usefulness).
1091
Davide Italiano089a9122017-01-24 00:57:39 +00001092 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001093 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001094 MainFPM.addPass(JumpThreadingPass());
1095 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1096
1097 // Create a function that performs CFI checks for cross-DSO calls with
1098 // targets in the current module.
1099 MPM.addPass(CrossDSOCFIPass());
1100
1101 // Lower type metadata and the type.test intrinsic. This pass supports
1102 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1103 // to be run at link time if CFI is enabled. This pass does nothing if
1104 // CFI is disabled.
1105 // Enable once we add support for the summary in the new PM.
1106#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001107 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1108 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001109 Summary));
1110#endif
1111
1112 // Add late LTO optimization passes.
1113 // Delete basic blocks, which optimization passes may have killed.
1114 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1115
1116 // Drop bodies of available eternally objects to improve GlobalDCE.
1117 MPM.addPass(EliminateAvailableExternallyPass());
1118
1119 // Now that we have optimized the program, discard unreachable functions.
1120 MPM.addPass(GlobalDCEPass());
1121
1122 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001123 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001124}
1125
Chandler Carruth060ad612016-12-23 20:38:19 +00001126AAManager PassBuilder::buildDefaultAAPipeline() {
1127 AAManager AA;
1128
1129 // The order in which these are registered determines their priority when
1130 // being queried.
1131
1132 // First we register the basic alias analysis that provides the majority of
1133 // per-function local AA logic. This is a stateless, on-demand local set of
1134 // AA techniques.
1135 AA.registerFunctionAnalysis<BasicAA>();
1136
1137 // Next we query fast, specialized alias analyses that wrap IR-embedded
1138 // information about aliasing.
1139 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1140 AA.registerFunctionAnalysis<TypeBasedAA>();
1141
1142 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001143 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1144 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001145 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001146 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001147
1148 return AA;
1149}
1150
Chandler Carruth241bf242016-08-03 07:44:48 +00001151static Optional<int> parseRepeatPassName(StringRef Name) {
1152 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1153 return None;
1154 int Count;
1155 if (Name.getAsInteger(0, Count) || Count <= 0)
1156 return None;
1157 return Count;
1158}
1159
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001160static Optional<int> parseDevirtPassName(StringRef Name) {
1161 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1162 return None;
1163 int Count;
1164 if (Name.getAsInteger(0, Count) || Count <= 0)
1165 return None;
1166 return Count;
1167}
1168
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001169/// Tests whether a pass name starts with a valid prefix for a default pipeline
1170/// alias.
1171static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1172 return Name.startswith("default") || Name.startswith("thinlto") ||
1173 Name.startswith("lto");
1174}
1175
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001176/// Tests whether registered callbacks will accept a given pass name.
1177///
1178/// When parsing a pipeline text, the type of the outermost pipeline may be
1179/// omitted, in which case the type is automatically determined from the first
1180/// pass name in the text. This may be a name that is handled through one of the
1181/// callbacks. We check this through the oridinary parsing callbacks by setting
1182/// up a dummy PassManager in order to not force the client to also handle this
1183/// type of query.
1184template <typename PassManagerT, typename CallbacksT>
1185static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1186 if (!Callbacks.empty()) {
1187 PassManagerT DummyPM;
1188 for (auto &CB : Callbacks)
1189 if (CB(Name, DummyPM, {}))
1190 return true;
1191 }
1192 return false;
1193}
1194
1195template <typename CallbacksT>
1196static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001197 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001198 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001199 return DefaultAliasRegex.match(Name);
1200
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001201 // Explicitly handle pass manager names.
1202 if (Name == "module")
1203 return true;
1204 if (Name == "cgscc")
1205 return true;
1206 if (Name == "function")
1207 return true;
1208
Chandler Carruth241bf242016-08-03 07:44:48 +00001209 // Explicitly handle custom-parsed pass names.
1210 if (parseRepeatPassName(Name))
1211 return true;
1212
Chandler Carruth74a8a222016-06-17 07:15:29 +00001213#define MODULE_PASS(NAME, CREATE_PASS) \
1214 if (Name == NAME) \
1215 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001216#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001217 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001218 return true;
1219#include "PassRegistry.def"
1220
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001221 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001222}
1223
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001224template <typename CallbacksT>
1225static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001226 // Explicitly handle pass manager names.
1227 if (Name == "cgscc")
1228 return true;
1229 if (Name == "function")
1230 return true;
1231
Chandler Carruth241bf242016-08-03 07:44:48 +00001232 // Explicitly handle custom-parsed pass names.
1233 if (parseRepeatPassName(Name))
1234 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001235 if (parseDevirtPassName(Name))
1236 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001237
Chandler Carruth74a8a222016-06-17 07:15:29 +00001238#define CGSCC_PASS(NAME, CREATE_PASS) \
1239 if (Name == NAME) \
1240 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001241#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001242 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001243 return true;
1244#include "PassRegistry.def"
1245
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001246 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001247}
1248
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001249template <typename CallbacksT>
1250static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001251 // Explicitly handle pass manager names.
1252 if (Name == "function")
1253 return true;
1254 if (Name == "loop")
1255 return true;
1256
Chandler Carruth241bf242016-08-03 07:44:48 +00001257 // Explicitly handle custom-parsed pass names.
1258 if (parseRepeatPassName(Name))
1259 return true;
1260
Chandler Carruth74a8a222016-06-17 07:15:29 +00001261#define FUNCTION_PASS(NAME, CREATE_PASS) \
1262 if (Name == NAME) \
1263 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001264#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001265 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001266 return true;
1267#include "PassRegistry.def"
1268
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001269 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001270}
1271
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001272template <typename CallbacksT>
1273static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001274 // Explicitly handle pass manager names.
1275 if (Name == "loop")
1276 return true;
1277
Chandler Carruth241bf242016-08-03 07:44:48 +00001278 // Explicitly handle custom-parsed pass names.
1279 if (parseRepeatPassName(Name))
1280 return true;
1281
Chandler Carruth74a8a222016-06-17 07:15:29 +00001282#define LOOP_PASS(NAME, CREATE_PASS) \
1283 if (Name == NAME) \
1284 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001285#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1286 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1287 return true;
1288#include "PassRegistry.def"
1289
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001290 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001291}
1292
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001293Optional<std::vector<PassBuilder::PipelineElement>>
1294PassBuilder::parsePipelineText(StringRef Text) {
1295 std::vector<PipelineElement> ResultPipeline;
1296
1297 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1298 &ResultPipeline};
1299 for (;;) {
1300 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1301 size_t Pos = Text.find_first_of(",()");
1302 Pipeline.push_back({Text.substr(0, Pos), {}});
1303
1304 // If we have a single terminating name, we're done.
1305 if (Pos == Text.npos)
1306 break;
1307
1308 char Sep = Text[Pos];
1309 Text = Text.substr(Pos + 1);
1310 if (Sep == ',')
1311 // Just a name ending in a comma, continue.
1312 continue;
1313
1314 if (Sep == '(') {
1315 // Push the inner pipeline onto the stack to continue processing.
1316 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1317 continue;
1318 }
1319
1320 assert(Sep == ')' && "Bogus separator!");
1321 // When handling the close parenthesis, we greedily consume them to avoid
1322 // empty strings in the pipeline.
1323 do {
1324 // If we try to pop the outer pipeline we have unbalanced parentheses.
1325 if (PipelineStack.size() == 1)
1326 return None;
1327
1328 PipelineStack.pop_back();
1329 } while (Text.consume_front(")"));
1330
1331 // Check if we've finished parsing.
1332 if (Text.empty())
1333 break;
1334
1335 // Otherwise, the end of an inner pipeline always has to be followed by
1336 // a comma, and then we can continue.
1337 if (!Text.consume_front(","))
1338 return None;
1339 }
1340
1341 if (PipelineStack.size() > 1)
1342 // Unbalanced paretheses.
1343 return None;
1344
1345 assert(PipelineStack.back() == &ResultPipeline &&
1346 "Wrong pipeline at the bottom of the stack!");
1347 return {std::move(ResultPipeline)};
1348}
1349
1350bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1351 const PipelineElement &E, bool VerifyEachPass,
1352 bool DebugLogging) {
1353 auto &Name = E.Name;
1354 auto &InnerPipeline = E.InnerPipeline;
1355
1356 // First handle complex passes like the pass managers which carry pipelines.
1357 if (!InnerPipeline.empty()) {
1358 if (Name == "module") {
1359 ModulePassManager NestedMPM(DebugLogging);
1360 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1361 DebugLogging))
1362 return false;
1363 MPM.addPass(std::move(NestedMPM));
1364 return true;
1365 }
1366 if (Name == "cgscc") {
1367 CGSCCPassManager CGPM(DebugLogging);
1368 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1369 DebugLogging))
1370 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001371 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001372 return true;
1373 }
1374 if (Name == "function") {
1375 FunctionPassManager FPM(DebugLogging);
1376 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1377 DebugLogging))
1378 return false;
1379 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1380 return true;
1381 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001382 if (auto Count = parseRepeatPassName(Name)) {
1383 ModulePassManager NestedMPM(DebugLogging);
1384 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1385 DebugLogging))
1386 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001387 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001388 return true;
1389 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001390
1391 for (auto &C : ModulePipelineParsingCallbacks)
1392 if (C(Name, MPM, InnerPipeline))
1393 return true;
1394
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001395 // Normal passes can't have pipelines.
1396 return false;
1397 }
1398
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001399 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001400 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001401 SmallVector<StringRef, 3> Matches;
1402 if (!DefaultAliasRegex.match(Name, &Matches))
1403 return false;
1404 assert(Matches.size() == 3 && "Must capture two matched strings!");
1405
Jordan Rosef85a95f2016-07-25 18:34:51 +00001406 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001407 .Case("O0", O0)
1408 .Case("O1", O1)
1409 .Case("O2", O2)
1410 .Case("O3", O3)
1411 .Case("Os", Os)
1412 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001413 if (L == O0)
1414 // At O0 we do nothing at all!
1415 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001416
1417 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001418 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001419 } else if (Matches[1] == "thinlto-pre-link") {
1420 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1421 } else if (Matches[1] == "thinlto") {
1422 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001423 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001424 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001425 } else {
1426 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001427 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001428 }
1429 return true;
1430 }
1431
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001432 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001433#define MODULE_PASS(NAME, CREATE_PASS) \
1434 if (Name == NAME) { \
1435 MPM.addPass(CREATE_PASS); \
1436 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001437 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001438#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1439 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001440 MPM.addPass( \
1441 RequireAnalysisPass< \
1442 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001443 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001444 } \
1445 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001446 MPM.addPass(InvalidateAnalysisPass< \
1447 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001448 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001449 }
1450#include "PassRegistry.def"
1451
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001452 for (auto &C : ModulePipelineParsingCallbacks)
1453 if (C(Name, MPM, InnerPipeline))
1454 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001455 return false;
1456}
1457
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001458bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1459 const PipelineElement &E, bool VerifyEachPass,
1460 bool DebugLogging) {
1461 auto &Name = E.Name;
1462 auto &InnerPipeline = E.InnerPipeline;
1463
1464 // First handle complex passes like the pass managers which carry pipelines.
1465 if (!InnerPipeline.empty()) {
1466 if (Name == "cgscc") {
1467 CGSCCPassManager NestedCGPM(DebugLogging);
1468 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1469 DebugLogging))
1470 return false;
1471 // Add the nested pass manager with the appropriate adaptor.
1472 CGPM.addPass(std::move(NestedCGPM));
1473 return true;
1474 }
1475 if (Name == "function") {
1476 FunctionPassManager FPM(DebugLogging);
1477 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1478 DebugLogging))
1479 return false;
1480 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001481 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001482 return true;
1483 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001484 if (auto Count = parseRepeatPassName(Name)) {
1485 CGSCCPassManager NestedCGPM(DebugLogging);
1486 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1487 DebugLogging))
1488 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001489 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001490 return true;
1491 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001492 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1493 CGSCCPassManager NestedCGPM(DebugLogging);
1494 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1495 DebugLogging))
1496 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001497 CGPM.addPass(
1498 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001499 return true;
1500 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001501
1502 for (auto &C : CGSCCPipelineParsingCallbacks)
1503 if (C(Name, CGPM, InnerPipeline))
1504 return true;
1505
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001506 // Normal passes can't have pipelines.
1507 return false;
1508 }
1509
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001510// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001511#define CGSCC_PASS(NAME, CREATE_PASS) \
1512 if (Name == NAME) { \
1513 CGPM.addPass(CREATE_PASS); \
1514 return true; \
1515 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001516#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1517 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001518 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001519 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001520 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1521 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001522 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001523 } \
1524 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001525 CGPM.addPass(InvalidateAnalysisPass< \
1526 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001527 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001528 }
1529#include "PassRegistry.def"
1530
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001531 for (auto &C : CGSCCPipelineParsingCallbacks)
1532 if (C(Name, CGPM, InnerPipeline))
1533 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001534 return false;
1535}
1536
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001537bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1538 const PipelineElement &E,
1539 bool VerifyEachPass, bool DebugLogging) {
1540 auto &Name = E.Name;
1541 auto &InnerPipeline = E.InnerPipeline;
1542
1543 // First handle complex passes like the pass managers which carry pipelines.
1544 if (!InnerPipeline.empty()) {
1545 if (Name == "function") {
1546 FunctionPassManager NestedFPM(DebugLogging);
1547 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1548 DebugLogging))
1549 return false;
1550 // Add the nested pass manager with the appropriate adaptor.
1551 FPM.addPass(std::move(NestedFPM));
1552 return true;
1553 }
1554 if (Name == "loop") {
1555 LoopPassManager LPM(DebugLogging);
1556 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1557 DebugLogging))
1558 return false;
1559 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001560 FPM.addPass(
1561 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001562 return true;
1563 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001564 if (auto Count = parseRepeatPassName(Name)) {
1565 FunctionPassManager NestedFPM(DebugLogging);
1566 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1567 DebugLogging))
1568 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001569 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001570 return true;
1571 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001572
1573 for (auto &C : FunctionPipelineParsingCallbacks)
1574 if (C(Name, FPM, InnerPipeline))
1575 return true;
1576
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001577 // Normal passes can't have pipelines.
1578 return false;
1579 }
1580
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001581// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001582#define FUNCTION_PASS(NAME, CREATE_PASS) \
1583 if (Name == NAME) { \
1584 FPM.addPass(CREATE_PASS); \
1585 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001586 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001587#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1588 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001589 FPM.addPass( \
1590 RequireAnalysisPass< \
1591 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001592 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001593 } \
1594 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001595 FPM.addPass(InvalidateAnalysisPass< \
1596 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001597 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001598 }
1599#include "PassRegistry.def"
1600
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001601 for (auto &C : FunctionPipelineParsingCallbacks)
1602 if (C(Name, FPM, InnerPipeline))
1603 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001604 return false;
1605}
1606
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001607bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001608 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001609 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001610 auto &InnerPipeline = E.InnerPipeline;
1611
1612 // First handle complex passes like the pass managers which carry pipelines.
1613 if (!InnerPipeline.empty()) {
1614 if (Name == "loop") {
1615 LoopPassManager NestedLPM(DebugLogging);
1616 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1617 DebugLogging))
1618 return false;
1619 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001620 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001621 return true;
1622 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001623 if (auto Count = parseRepeatPassName(Name)) {
1624 LoopPassManager NestedLPM(DebugLogging);
1625 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1626 DebugLogging))
1627 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001628 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001629 return true;
1630 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001631
1632 for (auto &C : LoopPipelineParsingCallbacks)
1633 if (C(Name, LPM, InnerPipeline))
1634 return true;
1635
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001636 // Normal passes can't have pipelines.
1637 return false;
1638 }
1639
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001640// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001641#define LOOP_PASS(NAME, CREATE_PASS) \
1642 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001643 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001644 return true; \
1645 }
1646#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1647 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001648 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001649 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1650 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1651 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001652 return true; \
1653 } \
1654 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001655 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001656 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001657 return true; \
1658 }
1659#include "PassRegistry.def"
1660
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001661 for (auto &C : LoopPipelineParsingCallbacks)
1662 if (C(Name, LPM, InnerPipeline))
1663 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001664 return false;
1665}
1666
Chandler Carruthedf59962016-02-18 09:45:17 +00001667bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001668#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1669 if (Name == NAME) { \
1670 AA.registerModuleAnalysis< \
1671 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1672 return true; \
1673 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001674#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1675 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001676 AA.registerFunctionAnalysis< \
1677 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001678 return true; \
1679 }
1680#include "PassRegistry.def"
1681
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001682 for (auto &C : AAParsingCallbacks)
1683 if (C(Name, AA))
1684 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001685 return false;
1686}
1687
Justin Bognereecc3c82016-02-25 07:23:08 +00001688bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001689 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001690 bool VerifyEachPass,
1691 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001692 for (const auto &Element : Pipeline) {
1693 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1694 return false;
1695 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001696 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001697 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001698}
1699
Chandler Carruth1ff77242015-03-07 09:02:36 +00001700bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001701 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001702 bool VerifyEachPass,
1703 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001704 for (const auto &Element : Pipeline) {
1705 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1706 return false;
1707 if (VerifyEachPass)
1708 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001709 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001710 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001711}
1712
Chandler Carruth1ff77242015-03-07 09:02:36 +00001713bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001714 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001715 bool VerifyEachPass,
1716 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001717 for (const auto &Element : Pipeline) {
1718 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1719 return false;
1720 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001721 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001722 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001723}
1724
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001725void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1726 FunctionAnalysisManager &FAM,
1727 CGSCCAnalysisManager &CGAM,
1728 ModuleAnalysisManager &MAM) {
1729 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1730 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001731 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1732 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1733 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1734 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1735 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1736}
1737
Chandler Carruth1ff77242015-03-07 09:02:36 +00001738bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001739 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001740 bool VerifyEachPass,
1741 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001742 for (const auto &Element : Pipeline) {
1743 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1744 return false;
1745 if (VerifyEachPass)
1746 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001747 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001748 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001749}
1750
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001751// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001752// FIXME: Should this routine accept a TargetMachine or require the caller to
1753// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001754bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1755 StringRef PipelineText, bool VerifyEachPass,
1756 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001757 auto Pipeline = parsePipelineText(PipelineText);
1758 if (!Pipeline || Pipeline->empty())
1759 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001760
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001761 // If the first name isn't at the module layer, wrap the pipeline up
1762 // automatically.
1763 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001764
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001765 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1766 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001767 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001768 } else if (isFunctionPassName(FirstName,
1769 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001770 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001771 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001772 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001773 } else {
1774 for (auto &C : TopLevelPipelineParsingCallbacks)
1775 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1776 return true;
1777
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001778 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001779 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001780 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001781 }
1782
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001783 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001784}
Chandler Carruthedf59962016-02-18 09:45:17 +00001785
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001786// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1787bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1788 StringRef PipelineText, bool VerifyEachPass,
1789 bool DebugLogging) {
1790 auto Pipeline = parsePipelineText(PipelineText);
1791 if (!Pipeline || Pipeline->empty())
1792 return false;
1793
1794 StringRef FirstName = Pipeline->front().Name;
1795 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1796 return false;
1797
1798 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1799}
1800
1801// Primary pass pipeline description parsing routine for a \c
1802// FunctionPassManager
1803bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1804 StringRef PipelineText, bool VerifyEachPass,
1805 bool DebugLogging) {
1806 auto Pipeline = parsePipelineText(PipelineText);
1807 if (!Pipeline || Pipeline->empty())
1808 return false;
1809
1810 StringRef FirstName = Pipeline->front().Name;
1811 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1812 return false;
1813
1814 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1815 DebugLogging);
1816}
1817
1818// Primary pass pipeline description parsing routine for a \c LoopPassManager
1819bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1820 StringRef PipelineText, bool VerifyEachPass,
1821 bool DebugLogging) {
1822 auto Pipeline = parsePipelineText(PipelineText);
1823 if (!Pipeline || Pipeline->empty())
1824 return false;
1825
1826 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1827}
1828
Chandler Carruthedf59962016-02-18 09:45:17 +00001829bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001830 // If the pipeline just consists of the word 'default' just replace the AA
1831 // manager with our default one.
1832 if (PipelineText == "default") {
1833 AA = buildDefaultAAPipeline();
1834 return true;
1835 }
1836
Chandler Carruthedf59962016-02-18 09:45:17 +00001837 while (!PipelineText.empty()) {
1838 StringRef Name;
1839 std::tie(Name, PipelineText) = PipelineText.split(',');
1840 if (!parseAAPassName(AA, Name))
1841 return false;
1842 }
1843
1844 return true;
1845}