blob: 8da493f69ec3d04705b5bbf268cd8283e1ec42e2 [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"
John Brawnbdbbd832018-06-28 14:13:06 +000044#include "llvm/Analysis/PhiValues.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000063#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.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"
David Blaikie301627f2018-03-22 22:42:44 +000083#include "llvm/Transforms/IPO/SampleProfile.h"
Justin Bogner21e15372015-10-30 23:28:12 +000084#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000085#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000086#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000087#include "llvm/Transforms/InstCombine/InstCombine.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000088#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +000089#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
90#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000091#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000092#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000093#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000094#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000095#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000096#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000097#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000098#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000099#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +0000100#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000101#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000102#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000103#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000104#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000105#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000106#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000107#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +0000108#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
Sean Silva46590d52016-06-14 00:51:09 +0000109#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000110#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000111#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000112#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000113#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000114#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000115#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000116#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000117#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000118#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000119#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000120#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000121#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000122#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000123#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000124#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000125#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000126#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000127#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000128#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000129#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000130#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000131#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000132#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000133#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000134#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000135#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000136#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000137#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000138#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000139#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000140#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000141#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000142#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000143#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000144#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000145#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000146#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000147#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000148#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000149#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000150#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000151#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000152#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000153#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000154#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000155
Chandler Carruth66445382014-01-11 08:16:35 +0000156using namespace llvm;
157
Chandler Carruth719ffe12017-02-12 05:38:04 +0000158static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
159 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000160static cl::opt<bool>
161 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
162 cl::Hidden, cl::ZeroOrMore,
163 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000164
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000165static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000166 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000167 cl::Hidden, cl::ZeroOrMore,
168 cl::desc("Run NewGVN instead of GVN"));
169
Geoff Berry3cca1da2017-06-10 15:20:03 +0000170static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000171 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
172 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000173
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000174static cl::opt<bool> EnableGVNHoist(
175 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
176 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
177
Davide Italianobe1b6a92017-06-03 23:18:29 +0000178static cl::opt<bool> EnableGVNSink(
179 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
180 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
181
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000182static cl::opt<bool> EnableSyntheticCounts(
183 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
184 cl::desc("Run synthetic function entry count generation "
185 "pass"));
186
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000187static Regex DefaultAliasRegex(
188 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000189
Chandler Carruthe3f50642016-12-22 06:59:15 +0000190static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
191 switch (Level) {
192 case PassBuilder::O0:
193 case PassBuilder::O1:
194 case PassBuilder::O2:
195 case PassBuilder::O3:
196 return false;
197
198 case PassBuilder::Os:
199 case PassBuilder::Oz:
200 return true;
201 }
202 llvm_unreachable("Invalid optimization level!");
203}
204
Chandler Carruth66445382014-01-11 08:16:35 +0000205namespace {
206
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000207/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000208struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000209 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000210 return PreservedAnalyses::all();
211 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000212 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000213};
214
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000215/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000216class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
217 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000218 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000219
220public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000221 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000222 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000224};
225
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000226/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000227struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000228 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
229 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000230 return PreservedAnalyses::all();
231 }
232 static StringRef name() { return "NoOpCGSCCPass"; }
233};
234
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000235/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000236class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
237 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000238 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000239
240public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000241 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000242 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000243 return Result();
244 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000245 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000246};
247
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000248/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000249struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000250 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000251 return PreservedAnalyses::all();
252 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000253 static StringRef name() { return "NoOpFunctionPass"; }
254};
255
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000256/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000257class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
258 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000259 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000260
261public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000262 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000263 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000264 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000265};
266
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000267/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000268struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000269 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
270 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000271 return PreservedAnalyses::all();
272 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000273 static StringRef name() { return "NoOpLoopPass"; }
274};
275
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000276/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000277class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
278 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000279 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000280
281public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000282 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000283 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
284 return Result();
285 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000286 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000287};
288
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000289AnalysisKey NoOpModuleAnalysis::Key;
290AnalysisKey NoOpCGSCCAnalysis::Key;
291AnalysisKey NoOpFunctionAnalysis::Key;
292AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000293
Chandler Carruth66445382014-01-11 08:16:35 +0000294} // End anonymous namespace.
295
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000296void PassBuilder::invokePeepholeEPCallbacks(
297 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
298 for (auto &C : PeepholeEPCallbacks)
299 C(FPM, Level);
300}
301
Chandler Carruth1ff77242015-03-07 09:02:36 +0000302void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000303#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000304 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000305#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000306
307 for (auto &C : ModuleAnalysisRegistrationCallbacks)
308 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000309}
310
Chandler Carruth1ff77242015-03-07 09:02:36 +0000311void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000312#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000313 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000314#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000315
316 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
317 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000318}
319
Chandler Carruth1ff77242015-03-07 09:02:36 +0000320void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000321#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000322 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000323#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000324
325 for (auto &C : FunctionAnalysisRegistrationCallbacks)
326 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000327}
328
Justin Bognereecc3c82016-02-25 07:23:08 +0000329void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000330#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000331 LAM.registerPass([&] { return CREATE_PASS; });
332#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000333
334 for (auto &C : LoopAnalysisRegistrationCallbacks)
335 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000336}
337
Chandler Carruthe3f50642016-12-22 06:59:15 +0000338FunctionPassManager
339PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000340 ThinLTOPhase Phase,
341 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000342 assert(Level != O0 && "Must request optimizations!");
343 FunctionPassManager FPM(DebugLogging);
344
345 // Form SSA out of local memory accesses after breaking apart aggregates into
346 // scalars.
347 FPM.addPass(SROA());
348
349 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000350 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000351
Davide Italianoc3688312017-06-01 23:08:14 +0000352 // Hoisting of scalars and load expressions.
353 if (EnableGVNHoist)
354 FPM.addPass(GVNHoistPass());
355
Davide Italianobe1b6a92017-06-03 23:18:29 +0000356 // Global value numbering based sinking.
357 if (EnableGVNSink) {
358 FPM.addPass(GVNSinkPass());
359 FPM.addPass(SimplifyCFGPass());
360 }
361
Chandler Carruthe3f50642016-12-22 06:59:15 +0000362 // Speculative execution if the target has divergent branches; otherwise nop.
363 FPM.addPass(SpeculativeExecutionPass());
364
365 // Optimize based on known information about branches, and cleanup afterward.
366 FPM.addPass(JumpThreadingPass());
367 FPM.addPass(CorrelatedValuePropagationPass());
368 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000369 if (Level == O3)
370 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000371 FPM.addPass(InstCombinePass());
372
373 if (!isOptimizingForSize(Level))
374 FPM.addPass(LibCallsShrinkWrapPass());
375
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000376 invokePeepholeEPCallbacks(FPM, Level);
377
Rong Xue1f42452017-10-23 22:21:29 +0000378 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
379 // using the size value profile. Don't perform this when optimizing for size.
380 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
381 !isOptimizingForSize(Level))
382 FPM.addPass(PGOMemOPSizeOpt());
383
Chandler Carruthe3f50642016-12-22 06:59:15 +0000384 FPM.addPass(TailCallElimPass());
385 FPM.addPass(SimplifyCFGPass());
386
387 // Form canonically associated expression trees, and simplify the trees using
388 // basic mathematical properties. For example, this will form (nearly)
389 // minimal multiplication trees.
390 FPM.addPass(ReassociatePass());
391
392 // Add the primary loop simplification pipeline.
393 // FIXME: Currently this is split into two loop pass pipelines because we run
Chandler Carruth71fd2702018-05-30 02:46:45 +0000394 // some function passes in between them. These can and should be removed
395 // and/or replaced by scheduling the loop pass equivalents in the correct
396 // positions. But those equivalent passes aren't powerful enough yet.
397 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
398 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
399 // fully replace `SimplifyCFGPass`, and the closest to the other we have is
400 // `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000401 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
402
Chandler Carruth71fd2702018-05-30 02:46:45 +0000403 // Simplify the loop body. We do this initially to clean up after other loop
404 // passes run, either when iterating on a loop or on inner loops with
405 // implications on the outer loop.
406 LPM1.addPass(LoopInstSimplifyPass());
407 LPM1.addPass(LoopSimplifyCFGPass());
408
Chandler Carruthe3f50642016-12-22 06:59:15 +0000409 // Rotate Loop - disable header duplication at -Oz
410 LPM1.addPass(LoopRotatePass(Level != Oz));
411 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000412 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000413 LPM2.addPass(IndVarSimplifyPass());
414 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000415
416 for (auto &C : LateLoopOptimizationsEPCallbacks)
417 C(LPM2, Level);
418
Chandler Carruth79b733b2017-01-26 23:21:17 +0000419 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000420 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000421 // because it changes IR to makes profile annotation in back compile
422 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000423 if (Phase != ThinLTOPhase::PreLink ||
424 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000425 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000426
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000427 for (auto &C : LoopOptimizerEndEPCallbacks)
428 C(LPM2, Level);
429
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000430 // We provide the opt remark emitter pass for LICM to use. We only need to do
431 // this once as it is immutable.
432 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000433 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000434 FPM.addPass(SimplifyCFGPass());
435 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000436 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000437
438 // Eliminate redundancies.
439 if (Level != O1) {
440 // These passes add substantial compile time so skip them at O1.
441 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000442 if (RunNewGVN)
443 FPM.addPass(NewGVNPass());
444 else
445 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000446 }
447
448 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
449 FPM.addPass(MemCpyOptPass());
450
451 // Sparse conditional constant propagation.
452 // FIXME: It isn't clear why we do this *after* loop passes rather than
453 // before...
454 FPM.addPass(SCCPPass());
455
456 // Delete dead bit computations (instcombine runs after to fold away the dead
457 // computations, and then ADCE will run later to exploit any new DCE
458 // opportunities that creates).
459 FPM.addPass(BDCEPass());
460
461 // Run instcombine after redundancy and dead bit elimination to exploit
462 // opportunities opened up by them.
463 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000464 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000465
466 // Re-consider control flow based optimizations after redundancy elimination,
467 // redo DCE, etc.
468 FPM.addPass(JumpThreadingPass());
469 FPM.addPass(CorrelatedValuePropagationPass());
470 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000471 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000472
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000473 for (auto &C : ScalarOptimizerLateEPCallbacks)
474 C(FPM, Level);
475
Chandler Carruthe3f50642016-12-22 06:59:15 +0000476 // Finally, do an expensive DCE pass to catch all the dead code exposed by
477 // the simplifications and basic cleanup after all the simplifications.
478 FPM.addPass(ADCEPass());
479 FPM.addPass(SimplifyCFGPass());
480 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000481 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000482
483 return FPM;
484}
485
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000486void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
487 PassBuilder::OptimizationLevel Level,
488 bool RunProfileGen,
489 std::string ProfileGenFile,
490 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000491 // Generally running simplification passes and the inliner with an high
492 // threshold results in smaller executables, but there may be cases where
493 // the size grows, so let's be conservative here and skip this simplification
494 // at -Os/Oz.
495 if (!isOptimizingForSize(Level)) {
496 InlineParams IP;
497
498 // In the old pass manager, this is a cl::opt. Should still this be one?
499 IP.DefaultThreshold = 75;
500
501 // FIXME: The hint threshold has the same value used by the regular inliner.
502 // This should probably be lowered after performance testing.
503 // FIXME: this comment is cargo culted from the old pass manager, revisit).
504 IP.HintThreshold = 325;
505
506 CGSCCPassManager CGPipeline(DebugLogging);
507
508 CGPipeline.addPass(InlinerPass(IP));
509
510 FunctionPassManager FPM;
511 FPM.addPass(SROA());
512 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
513 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
514 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000515 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000516
Davide Italiano513dfaa2017-02-13 15:26:22 +0000517 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
518
519 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
520 }
521
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000522 // Delete anything that is now dead to make sure that we don't instrument
523 // dead code. Instrumentation can end up keeping dead code around and
524 // dramatically increase code size.
525 MPM.addPass(GlobalDCEPass());
526
Davide Italiano513dfaa2017-02-13 15:26:22 +0000527 if (RunProfileGen) {
528 MPM.addPass(PGOInstrumentationGen());
529
Xinliang David Lib67530e2017-06-25 00:26:43 +0000530 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000531 FPM.addPass(
532 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000533 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
534
Davide Italiano513dfaa2017-02-13 15:26:22 +0000535 // Add the profile lowering pass.
536 InstrProfOptions Options;
537 if (!ProfileGenFile.empty())
538 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000539 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000540 MPM.addPass(InstrProfiling(Options));
541 }
542
543 if (!ProfileUseFile.empty())
544 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
545}
546
Easwaran Raman8249fac2017-06-28 13:33:49 +0000547static InlineParams
548getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
549 auto O3 = PassBuilder::O3;
550 unsigned OptLevel = Level > O3 ? 2 : Level;
551 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
552 return getInlineParams(OptLevel, SizeLevel);
553}
554
Chandler Carruthe3f50642016-12-22 06:59:15 +0000555ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000556PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000557 ThinLTOPhase Phase,
558 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000559 ModulePassManager MPM(DebugLogging);
560
Chandler Carruthe3f50642016-12-22 06:59:15 +0000561 // Do basic inference of function attributes from known properties of system
562 // libraries and other oracles.
563 MPM.addPass(InferFunctionAttrsPass());
564
565 // Create an early function pass manager to cleanup the output of the
566 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000567 FunctionPassManager EarlyFPM(DebugLogging);
568 EarlyFPM.addPass(SimplifyCFGPass());
569 EarlyFPM.addPass(SROA());
570 EarlyFPM.addPass(EarlyCSEPass());
571 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000572 if (Level == O3)
573 EarlyFPM.addPass(CallSiteSplittingPass());
574
Dehao Chen08f88312017-08-07 20:23:20 +0000575 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
576 // to convert bitcast to direct calls so that they can be inlined during the
577 // profile annotation prepration step.
578 // More details about SamplePGO design can be found in:
579 // https://research.google.com/pubs/pub45290.html
580 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
581 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
582 Phase == ThinLTOPhase::PostLink)
583 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000584 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000585
Dehao Chen08f88312017-08-07 20:23:20 +0000586 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
587 // Annotate sample profile right after early FPM to ensure freshness of
588 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000589 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
590 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000591 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
592 // for the profile annotation to be accurate in the ThinLTO backend.
593 if (Phase != ThinLTOPhase::PreLink)
594 // We perform early indirect call promotion here, before globalopt.
595 // This is important for the ThinLTO backend phase because otherwise
596 // imported available_externally functions look unreferenced and are
597 // removed.
598 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
599 true));
600 }
601
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000602 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000603 // and prior to optimizing globals.
604 // FIXME: This position in the pipeline hasn't been carefully considered in
605 // years, it should be re-analyzed.
606 MPM.addPass(IPSCCPPass());
607
Matthew Simpsoncb585582017-10-25 13:40:08 +0000608 // Attach metadata to indirect call sites indicating the set of functions
609 // they may target at run-time. This should follow IPSCCP.
610 MPM.addPass(CalledValuePropagationPass());
611
Chandler Carruthe3f50642016-12-22 06:59:15 +0000612 // Optimize globals to try and fold them into constants.
613 MPM.addPass(GlobalOptPass());
614
615 // Promote any localized globals to SSA registers.
616 // FIXME: Should this instead by a run of SROA?
617 // FIXME: We should probably run instcombine and simplify-cfg afterward to
618 // delete control flows that are dead once globals have been folded to
619 // constants.
620 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
621
622 // Remove any dead arguments exposed by cleanups and constand folding
623 // globals.
624 MPM.addPass(DeadArgumentEliminationPass());
625
626 // Create a small function pass pipeline to cleanup after all the global
627 // optimizations.
628 FunctionPassManager GlobalCleanupPM(DebugLogging);
629 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000630 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
631
Chandler Carruthe3f50642016-12-22 06:59:15 +0000632 GlobalCleanupPM.addPass(SimplifyCFGPass());
633 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
634
Dehao Chen89d32262017-08-02 01:28:31 +0000635 // Add all the requested passes for instrumentation PGO, if requested.
636 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
637 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
638 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
639 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
640 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000641 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000642
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000643 // Synthesize function entry counts for non-PGO compilation.
644 if (EnableSyntheticCounts && !PGOOpt)
645 MPM.addPass(SyntheticCountsPropagation());
646
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000647 // Require the GlobalsAA analysis for the module so we can query it within
648 // the CGSCC pipeline.
649 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000650
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000651 // Require the ProfileSummaryAnalysis for the module so we can query it within
652 // the inliner pass.
653 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
654
Chandler Carruthe3f50642016-12-22 06:59:15 +0000655 // Now begin the main postorder CGSCC pipeline.
656 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
657 // manager and trying to emulate its precise behavior. Much of this doesn't
658 // make a lot of sense and we should revisit the core CGSCC structure.
659 CGSCCPassManager MainCGPipeline(DebugLogging);
660
661 // Note: historically, the PruneEH pass was run first to deduce nounwind and
662 // generally clean up exception handling overhead. It isn't clear this is
663 // valuable as the inliner doesn't currently care whether it is inlining an
664 // invoke or a call.
665
666 // Run the inliner first. The theory is that we are walking bottom-up and so
667 // the callees have already been fully optimized, and we want to inline them
668 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000669 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000670 // because it makes profile annotation in the backend inaccurate.
671 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000672 if (Phase == ThinLTOPhase::PreLink &&
673 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000674 IP.HotCallSiteThreshold = 0;
675 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676
677 // Now deduce any function attributes based in the current code.
678 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
679
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000680 // When at O3 add argument promotion to the pass pipeline.
681 // FIXME: It isn't at all clear why this should be limited to O3.
682 if (Level == O3)
683 MainCGPipeline.addPass(ArgumentPromotionPass());
684
Chandler Carruthe3f50642016-12-22 06:59:15 +0000685 // Lastly, add the core function simplification pipeline nested inside the
686 // CGSCC walk.
687 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000688 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000689
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000690 for (auto &C : CGSCCOptimizerLateEPCallbacks)
691 C(MainCGPipeline, Level);
692
Chandler Carruth719ffe12017-02-12 05:38:04 +0000693 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
694 // to detect when we devirtualize indirect calls and iterate the SCC passes
695 // in that case to try and catch knock-on inlining or function attrs
696 // opportunities. Then we add it to the module pipeline by walking the SCCs
697 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000698 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000699 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000700 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000701
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000702 return MPM;
703}
704
705ModulePassManager
706PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
707 bool DebugLogging) {
708 ModulePassManager MPM(DebugLogging);
709
710 // Optimize globals now that the module is fully simplified.
711 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000712 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000713
Xinliang David Li126157c2017-05-22 16:41:57 +0000714 // Run partial inlining pass to partially inline functions that have
715 // large bodies.
716 if (RunPartialInlining)
717 MPM.addPass(PartialInlinerPass());
718
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000719 // Remove avail extern fns and globals definitions since we aren't compiling
720 // an object file for later LTO. For LTO we want to preserve these so they
721 // are eligible for inlining at link-time. Note if they are unreferenced they
722 // will be removed by GlobalDCE later, so this only impacts referenced
723 // available externally globals. Eventually they will be suppressed during
724 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
725 // may make globals referenced by available external functions dead and saves
726 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000727 MPM.addPass(EliminateAvailableExternallyPass());
728
729 // Do RPO function attribute inference across the module to forward-propagate
730 // attributes where applicable.
731 // FIXME: Is this really an optimization rather than a canonicalization?
732 MPM.addPass(ReversePostOrderFunctionAttrsPass());
733
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000734 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000735 // useful as the above will have inlined, DCE'ed, and function-attr
736 // propagated everything. We should at this point have a reasonably minimal
737 // and richly annotated call graph. By computing aliasing and mod/ref
738 // information for all local globals here, the late loop passes and notably
739 // the vectorizer will be able to use them to help recognize vectorizable
740 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000741 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000742
743 FunctionPassManager OptimizePM(DebugLogging);
744 OptimizePM.addPass(Float2IntPass());
745 // FIXME: We need to run some loop optimizations to re-rotate loops after
746 // simplify-cfg and others undo their rotation.
747
748 // Optimize the loop execution. These passes operate on entire loop nests
749 // rather than on each loop in an inside-out manner, and so they are actually
750 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000751
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000752 for (auto &C : VectorizerStartEPCallbacks)
753 C(OptimizePM, Level);
754
Chandler Carrutha95ff382017-01-27 00:50:21 +0000755 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000756 OptimizePM.addPass(
757 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000758
759 // Distribute loops to allow partial vectorization. I.e. isolate dependences
760 // into separate loop that would otherwise inhibit vectorization. This is
761 // currently only performed for loops marked with the metadata
762 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000763 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000764
765 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000766 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000767
Chandler Carruthbaabda92017-01-27 01:32:26 +0000768 // Eliminate loads by forwarding stores from the previous iteration to loads
769 // of the current iteration.
770 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000771
772 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000773 OptimizePM.addPass(InstCombinePass());
774
Chandler Carrutha95ff382017-01-27 00:50:21 +0000775 // Now that we've formed fast to execute loop structures, we do further
776 // optimizations. These are run afterward as they might block doing complex
777 // analyses and transforms such as what are needed for loop vectorization.
778
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000779 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
780 // GVN, loop transforms, and others have already run, so it's now better to
781 // convert to more optimized IR using more aggressive simplify CFG options.
782 // The extra sinking transform can create larger basic blocks, so do this
783 // before SLP vectorization.
784 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
785 forwardSwitchCondToPhi(true).
786 convertSwitchToLookupTable(true).
787 needCanonicalLoops(false).
788 sinkCommonInsts(true)));
789
Chandler Carruthe3f50642016-12-22 06:59:15 +0000790 // Optimize parallel scalar instruction chains into SIMD instructions.
791 OptimizePM.addPass(SLPVectorizerPass());
792
Chandler Carruthe3f50642016-12-22 06:59:15 +0000793 OptimizePM.addPass(InstCombinePass());
794
795 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000796 // execution resources of an out-of-order processor. We also then need to
797 // clean up redundancies and loop invariant code.
798 // FIXME: It would be really good to use a loop-integrated instruction
799 // combiner for cleanup here so that the unrolling and LICM can be pipelined
800 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000801 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000802 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000803 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000804 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000805
806 // Now that we've vectorized and unrolled loops, we may have more refined
807 // alignment information, try to re-derive it here.
808 OptimizePM.addPass(AlignmentFromAssumptionsPass());
809
Chandler Carrutha95ff382017-01-27 00:50:21 +0000810 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
811 // canonicalization pass that enables other optimizations. As a result,
812 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
813 // result too early.
814 OptimizePM.addPass(LoopSinkPass());
815
816 // And finally clean up LCSSA form before generating code.
Chandler Carruth7c557f82018-06-29 23:36:03 +0000817 OptimizePM.addPass(InstSimplifyPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000818
Sanjay Patel6fd43912017-09-09 13:38:18 +0000819 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
820 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
821 // flattening of blocks.
822 OptimizePM.addPass(DivRemPairsPass());
823
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000824 // LoopSink (and other loop passes since the last simplifyCFG) might have
825 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
826 OptimizePM.addPass(SimplifyCFGPass());
827
Chandler Carruthc34f7892017-11-28 11:32:31 +0000828 // Optimize PHIs by speculating around them when profitable. Note that this
829 // pass needs to be run after any PRE or similar pass as it is essentially
830 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
831 OptimizePM.addPass(SpeculateAroundPHIsPass());
832
Chandler Carrutha95ff382017-01-27 00:50:21 +0000833 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000834 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
835
836 // Now we need to do some global optimization transforms.
837 // FIXME: It would seem like these should come first in the optimization
838 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
839 // ordering here.
840 MPM.addPass(GlobalDCEPass());
841 MPM.addPass(ConstantMergePass());
842
843 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000844}
845
Chandler Carruthe3f50642016-12-22 06:59:15 +0000846ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000847PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
848 bool DebugLogging) {
849 assert(Level != O0 && "Must request optimizations for the default pipeline!");
850
851 ModulePassManager MPM(DebugLogging);
852
853 // Force any function attributes we want the rest of the pipeline to observe.
854 MPM.addPass(ForceFunctionAttrsPass());
855
David Blaikie0c64f5a2018-01-23 01:25:20 +0000856 // Apply module pipeline start EP callback.
857 for (auto &C : PipelineStartEPCallbacks)
858 C(MPM);
859
Dehao Chen08f88312017-08-07 20:23:20 +0000860 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000861 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
862
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000863 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000864 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
865 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000866
867 // Now add the optimization pipeline.
868 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
869
870 return MPM;
871}
872
873ModulePassManager
874PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
875 bool DebugLogging) {
876 assert(Level != O0 && "Must request optimizations for the default pipeline!");
877
878 ModulePassManager MPM(DebugLogging);
879
880 // Force any function attributes we want the rest of the pipeline to observe.
881 MPM.addPass(ForceFunctionAttrsPass());
882
Dehao Chen08f88312017-08-07 20:23:20 +0000883 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000884 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
885
David Blaikie0c64f5a2018-01-23 01:25:20 +0000886 // Apply module pipeline start EP callback.
887 for (auto &C : PipelineStartEPCallbacks)
888 C(MPM);
889
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000890 // If we are planning to perform ThinLTO later, we don't bloat the code with
891 // unrolling/vectorization/... now. Just simplify the module as much as we
892 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000893 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
894 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000895
896 // Run partial inlining pass to partially inline functions that have
897 // large bodies.
898 // FIXME: It isn't clear whether this is really the right place to run this
899 // in ThinLTO. Because there is another canonicalization and simplification
900 // phase that will run after the thin link, running this here ends up with
901 // less information than will be available later and it may grow functions in
902 // ways that aren't beneficial.
903 if (RunPartialInlining)
904 MPM.addPass(PartialInlinerPass());
905
906 // Reduce the size of the IR as much as possible.
907 MPM.addPass(GlobalOptPass());
908
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000909 return MPM;
910}
911
912ModulePassManager
913PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
914 bool DebugLogging) {
915 // FIXME: The summary index is not hooked in the new pass manager yet.
916 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
917 // here.
918
919 ModulePassManager MPM(DebugLogging);
920
921 // Force any function attributes we want the rest of the pipeline to observe.
922 MPM.addPass(ForceFunctionAttrsPass());
923
924 // During the ThinLTO backend phase we perform early indirect call promotion
925 // here, before globalopt. Otherwise imported available_externally functions
926 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000927 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
928 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000929 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000930 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
931 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000932
933 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000934 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
935 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000936
937 // Now add the optimization pipeline.
938 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
939
940 return MPM;
941}
942
943ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000944PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
945 bool DebugLogging) {
946 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000947 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000948 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000949}
950
Chandler Carruthe3f50642016-12-22 06:59:15 +0000951ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
952 bool DebugLogging) {
953 assert(Level != O0 && "Must request optimizations for the default pipeline!");
954 ModulePassManager MPM(DebugLogging);
955
Davide Italiano089a9122017-01-24 00:57:39 +0000956 // Remove unused virtual tables to improve the quality of code generated by
957 // whole-program devirtualization and bitset lowering.
958 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000959
Davide Italiano089a9122017-01-24 00:57:39 +0000960 // Force any function attributes we want the rest of the pipeline to observe.
961 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000962
Davide Italiano089a9122017-01-24 00:57:39 +0000963 // Do basic inference of function attributes from known properties of system
964 // libraries and other oracles.
965 MPM.addPass(InferFunctionAttrsPass());
966
967 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000968 FunctionPassManager EarlyFPM(DebugLogging);
969 EarlyFPM.addPass(CallSiteSplittingPass());
970 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
971
Davide Italiano089a9122017-01-24 00:57:39 +0000972 // Indirect call promotion. This should promote all the targets that are
973 // left by the earlier promotion pass that promotes intra-module targets.
974 // This two-step promotion is to save the compile time. For LTO, it should
975 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000976 MPM.addPass(PGOIndirectCallPromotion(
977 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000978 // Propagate constants at call sites into the functions they call. This
979 // opens opportunities for globalopt (and inlining) by substituting function
980 // pointers passed as arguments to direct uses of functions.
981 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000982
983 // Attach metadata to indirect call sites indicating the set of functions
984 // they may target at run-time. This should follow IPSCCP.
985 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000986 }
987
988 // Now deduce any function attributes based in the current code.
989 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
990 PostOrderFunctionAttrsPass()));
991
992 // Do RPO function attribute inference across the module to forward-propagate
993 // attributes where applicable.
994 // FIXME: Is this really an optimization rather than a canonicalization?
995 MPM.addPass(ReversePostOrderFunctionAttrsPass());
996
997 // Use inragne annotations on GEP indices to split globals where beneficial.
998 MPM.addPass(GlobalSplitPass());
999
1000 // Run whole program optimization of virtual call when the list of callees
1001 // is fixed.
1002 MPM.addPass(WholeProgramDevirtPass());
1003
1004 // Stop here at -O1.
1005 if (Level == 1)
1006 return MPM;
1007
1008 // Optimize globals to try and fold them into constants.
1009 MPM.addPass(GlobalOptPass());
1010
1011 // Promote any localized globals to SSA registers.
1012 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1013
1014 // Linking modules together can lead to duplicate global constant, only
1015 // keep one copy of each constant.
1016 MPM.addPass(ConstantMergePass());
1017
1018 // Remove unused arguments from functions.
1019 MPM.addPass(DeadArgumentEliminationPass());
1020
1021 // Reduce the code after globalopt and ipsccp. Both can open up significant
1022 // simplification opportunities, and both can propagate functions through
1023 // function pointers. When this happens, we often have to resolve varargs
1024 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001025 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001026 if (Level == O3)
1027 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001028 PeepholeFPM.addPass(InstCombinePass());
1029 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1030
1031 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001032
1033 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1034 // generally clean up exception handling overhead. It isn't clear this is
1035 // valuable as the inliner doesn't currently care whether it is inlining an
1036 // invoke or a call.
1037 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001038 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1039 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001040
1041 // Optimize globals again after we ran the inliner.
1042 MPM.addPass(GlobalOptPass());
1043
1044 // Garbage collect dead functions.
1045 // FIXME: Add ArgumentPromotion pass after once it's ported.
1046 MPM.addPass(GlobalDCEPass());
1047
1048 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001049 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001050 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001051 invokePeepholeEPCallbacks(FPM, Level);
1052
Davide Italiano089a9122017-01-24 00:57:39 +00001053 FPM.addPass(JumpThreadingPass());
1054
1055 // Break up allocas
1056 FPM.addPass(SROA());
1057
1058 // Run a few AA driver optimizations here and now to cleanup the code.
1059 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1060
1061 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1062 PostOrderFunctionAttrsPass()));
1063 // FIXME: here we run IP alias analysis in the legacy PM.
1064
1065 FunctionPassManager MainFPM;
1066
1067 // FIXME: once we fix LoopPass Manager, add LICM here.
1068 // FIXME: once we provide support for enabling MLSM, add it here.
1069 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001070 if (RunNewGVN)
1071 MainFPM.addPass(NewGVNPass());
1072 else
1073 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001074
1075 // Remove dead memcpy()'s.
1076 MainFPM.addPass(MemCpyOptPass());
1077
1078 // Nuke dead stores.
1079 MainFPM.addPass(DSEPass());
1080
1081 // FIXME: at this point, we run a bunch of loop passes:
1082 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1083 // loopVectorize. Enable them once the remaining issue with LPM
1084 // are sorted out.
1085
1086 MainFPM.addPass(InstCombinePass());
1087 MainFPM.addPass(SimplifyCFGPass());
1088 MainFPM.addPass(SCCPPass());
1089 MainFPM.addPass(InstCombinePass());
1090 MainFPM.addPass(BDCEPass());
1091
1092 // FIXME: We may want to run SLPVectorizer here.
1093 // After vectorization, assume intrinsics may tell us more
1094 // about pointer alignments.
1095#if 0
1096 MainFPM.add(AlignmentFromAssumptionsPass());
1097#endif
1098
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001099 // FIXME: Conditionally run LoadCombine here, after it's ported
1100 // (in case we still have this pass, given its questionable usefulness).
1101
Davide Italiano089a9122017-01-24 00:57:39 +00001102 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001103 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001104 MainFPM.addPass(JumpThreadingPass());
1105 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1106
1107 // Create a function that performs CFI checks for cross-DSO calls with
1108 // targets in the current module.
1109 MPM.addPass(CrossDSOCFIPass());
1110
1111 // Lower type metadata and the type.test intrinsic. This pass supports
1112 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1113 // to be run at link time if CFI is enabled. This pass does nothing if
1114 // CFI is disabled.
1115 // Enable once we add support for the summary in the new PM.
1116#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001117 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1118 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001119 Summary));
1120#endif
1121
1122 // Add late LTO optimization passes.
1123 // Delete basic blocks, which optimization passes may have killed.
1124 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1125
1126 // Drop bodies of available eternally objects to improve GlobalDCE.
1127 MPM.addPass(EliminateAvailableExternallyPass());
1128
1129 // Now that we have optimized the program, discard unreachable functions.
1130 MPM.addPass(GlobalDCEPass());
1131
1132 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001133 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001134}
1135
Chandler Carruth060ad612016-12-23 20:38:19 +00001136AAManager PassBuilder::buildDefaultAAPipeline() {
1137 AAManager AA;
1138
1139 // The order in which these are registered determines their priority when
1140 // being queried.
1141
1142 // First we register the basic alias analysis that provides the majority of
1143 // per-function local AA logic. This is a stateless, on-demand local set of
1144 // AA techniques.
1145 AA.registerFunctionAnalysis<BasicAA>();
1146
1147 // Next we query fast, specialized alias analyses that wrap IR-embedded
1148 // information about aliasing.
1149 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1150 AA.registerFunctionAnalysis<TypeBasedAA>();
1151
1152 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001153 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1154 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001155 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001156 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001157
1158 return AA;
1159}
1160
Chandler Carruth241bf242016-08-03 07:44:48 +00001161static Optional<int> parseRepeatPassName(StringRef Name) {
1162 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1163 return None;
1164 int Count;
1165 if (Name.getAsInteger(0, Count) || Count <= 0)
1166 return None;
1167 return Count;
1168}
1169
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001170static Optional<int> parseDevirtPassName(StringRef Name) {
1171 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1172 return None;
1173 int Count;
1174 if (Name.getAsInteger(0, Count) || Count <= 0)
1175 return None;
1176 return Count;
1177}
1178
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001179/// Tests whether a pass name starts with a valid prefix for a default pipeline
1180/// alias.
1181static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1182 return Name.startswith("default") || Name.startswith("thinlto") ||
1183 Name.startswith("lto");
1184}
1185
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001186/// Tests whether registered callbacks will accept a given pass name.
1187///
1188/// When parsing a pipeline text, the type of the outermost pipeline may be
1189/// omitted, in which case the type is automatically determined from the first
1190/// pass name in the text. This may be a name that is handled through one of the
1191/// callbacks. We check this through the oridinary parsing callbacks by setting
1192/// up a dummy PassManager in order to not force the client to also handle this
1193/// type of query.
1194template <typename PassManagerT, typename CallbacksT>
1195static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1196 if (!Callbacks.empty()) {
1197 PassManagerT DummyPM;
1198 for (auto &CB : Callbacks)
1199 if (CB(Name, DummyPM, {}))
1200 return true;
1201 }
1202 return false;
1203}
1204
1205template <typename CallbacksT>
1206static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001207 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001208 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001209 return DefaultAliasRegex.match(Name);
1210
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001211 // Explicitly handle pass manager names.
1212 if (Name == "module")
1213 return true;
1214 if (Name == "cgscc")
1215 return true;
1216 if (Name == "function")
1217 return true;
1218
Chandler Carruth241bf242016-08-03 07:44:48 +00001219 // Explicitly handle custom-parsed pass names.
1220 if (parseRepeatPassName(Name))
1221 return true;
1222
Chandler Carruth74a8a222016-06-17 07:15:29 +00001223#define MODULE_PASS(NAME, CREATE_PASS) \
1224 if (Name == NAME) \
1225 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001226#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001227 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001228 return true;
1229#include "PassRegistry.def"
1230
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001231 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001232}
1233
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001234template <typename CallbacksT>
1235static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001236 // Explicitly handle pass manager names.
1237 if (Name == "cgscc")
1238 return true;
1239 if (Name == "function")
1240 return true;
1241
Chandler Carruth241bf242016-08-03 07:44:48 +00001242 // Explicitly handle custom-parsed pass names.
1243 if (parseRepeatPassName(Name))
1244 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001245 if (parseDevirtPassName(Name))
1246 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001247
Chandler Carruth74a8a222016-06-17 07:15:29 +00001248#define CGSCC_PASS(NAME, CREATE_PASS) \
1249 if (Name == NAME) \
1250 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001251#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001252 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001253 return true;
1254#include "PassRegistry.def"
1255
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001256 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001257}
1258
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001259template <typename CallbacksT>
1260static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001261 // Explicitly handle pass manager names.
1262 if (Name == "function")
1263 return true;
1264 if (Name == "loop")
1265 return true;
1266
Chandler Carruth241bf242016-08-03 07:44:48 +00001267 // Explicitly handle custom-parsed pass names.
1268 if (parseRepeatPassName(Name))
1269 return true;
1270
Chandler Carruth74a8a222016-06-17 07:15:29 +00001271#define FUNCTION_PASS(NAME, CREATE_PASS) \
1272 if (Name == NAME) \
1273 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001274#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001275 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001276 return true;
1277#include "PassRegistry.def"
1278
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001279 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001280}
1281
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001282template <typename CallbacksT>
1283static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001284 // Explicitly handle pass manager names.
1285 if (Name == "loop")
1286 return true;
1287
Chandler Carruth241bf242016-08-03 07:44:48 +00001288 // Explicitly handle custom-parsed pass names.
1289 if (parseRepeatPassName(Name))
1290 return true;
1291
Chandler Carruth74a8a222016-06-17 07:15:29 +00001292#define LOOP_PASS(NAME, CREATE_PASS) \
1293 if (Name == NAME) \
1294 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001295#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1296 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1297 return true;
1298#include "PassRegistry.def"
1299
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001300 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001301}
1302
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001303Optional<std::vector<PassBuilder::PipelineElement>>
1304PassBuilder::parsePipelineText(StringRef Text) {
1305 std::vector<PipelineElement> ResultPipeline;
1306
1307 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1308 &ResultPipeline};
1309 for (;;) {
1310 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1311 size_t Pos = Text.find_first_of(",()");
1312 Pipeline.push_back({Text.substr(0, Pos), {}});
1313
1314 // If we have a single terminating name, we're done.
1315 if (Pos == Text.npos)
1316 break;
1317
1318 char Sep = Text[Pos];
1319 Text = Text.substr(Pos + 1);
1320 if (Sep == ',')
1321 // Just a name ending in a comma, continue.
1322 continue;
1323
1324 if (Sep == '(') {
1325 // Push the inner pipeline onto the stack to continue processing.
1326 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1327 continue;
1328 }
1329
1330 assert(Sep == ')' && "Bogus separator!");
1331 // When handling the close parenthesis, we greedily consume them to avoid
1332 // empty strings in the pipeline.
1333 do {
1334 // If we try to pop the outer pipeline we have unbalanced parentheses.
1335 if (PipelineStack.size() == 1)
1336 return None;
1337
1338 PipelineStack.pop_back();
1339 } while (Text.consume_front(")"));
1340
1341 // Check if we've finished parsing.
1342 if (Text.empty())
1343 break;
1344
1345 // Otherwise, the end of an inner pipeline always has to be followed by
1346 // a comma, and then we can continue.
1347 if (!Text.consume_front(","))
1348 return None;
1349 }
1350
1351 if (PipelineStack.size() > 1)
1352 // Unbalanced paretheses.
1353 return None;
1354
1355 assert(PipelineStack.back() == &ResultPipeline &&
1356 "Wrong pipeline at the bottom of the stack!");
1357 return {std::move(ResultPipeline)};
1358}
1359
1360bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1361 const PipelineElement &E, bool VerifyEachPass,
1362 bool DebugLogging) {
1363 auto &Name = E.Name;
1364 auto &InnerPipeline = E.InnerPipeline;
1365
1366 // First handle complex passes like the pass managers which carry pipelines.
1367 if (!InnerPipeline.empty()) {
1368 if (Name == "module") {
1369 ModulePassManager NestedMPM(DebugLogging);
1370 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1371 DebugLogging))
1372 return false;
1373 MPM.addPass(std::move(NestedMPM));
1374 return true;
1375 }
1376 if (Name == "cgscc") {
1377 CGSCCPassManager CGPM(DebugLogging);
1378 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1379 DebugLogging))
1380 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001381 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001382 return true;
1383 }
1384 if (Name == "function") {
1385 FunctionPassManager FPM(DebugLogging);
1386 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1387 DebugLogging))
1388 return false;
1389 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1390 return true;
1391 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001392 if (auto Count = parseRepeatPassName(Name)) {
1393 ModulePassManager NestedMPM(DebugLogging);
1394 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1395 DebugLogging))
1396 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001397 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001398 return true;
1399 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001400
1401 for (auto &C : ModulePipelineParsingCallbacks)
1402 if (C(Name, MPM, InnerPipeline))
1403 return true;
1404
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001405 // Normal passes can't have pipelines.
1406 return false;
1407 }
1408
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001409 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001410 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001411 SmallVector<StringRef, 3> Matches;
1412 if (!DefaultAliasRegex.match(Name, &Matches))
1413 return false;
1414 assert(Matches.size() == 3 && "Must capture two matched strings!");
1415
Jordan Rosef85a95f2016-07-25 18:34:51 +00001416 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001417 .Case("O0", O0)
1418 .Case("O1", O1)
1419 .Case("O2", O2)
1420 .Case("O3", O3)
1421 .Case("Os", Os)
1422 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001423 if (L == O0)
1424 // At O0 we do nothing at all!
1425 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001426
1427 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001428 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001429 } else if (Matches[1] == "thinlto-pre-link") {
1430 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1431 } else if (Matches[1] == "thinlto") {
1432 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001433 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001434 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001435 } else {
1436 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001437 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001438 }
1439 return true;
1440 }
1441
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001442 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001443#define MODULE_PASS(NAME, CREATE_PASS) \
1444 if (Name == NAME) { \
1445 MPM.addPass(CREATE_PASS); \
1446 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001447 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001448#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1449 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001450 MPM.addPass( \
1451 RequireAnalysisPass< \
1452 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001453 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001454 } \
1455 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001456 MPM.addPass(InvalidateAnalysisPass< \
1457 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001458 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001459 }
1460#include "PassRegistry.def"
1461
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001462 for (auto &C : ModulePipelineParsingCallbacks)
1463 if (C(Name, MPM, InnerPipeline))
1464 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001465 return false;
1466}
1467
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001468bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1469 const PipelineElement &E, bool VerifyEachPass,
1470 bool DebugLogging) {
1471 auto &Name = E.Name;
1472 auto &InnerPipeline = E.InnerPipeline;
1473
1474 // First handle complex passes like the pass managers which carry pipelines.
1475 if (!InnerPipeline.empty()) {
1476 if (Name == "cgscc") {
1477 CGSCCPassManager NestedCGPM(DebugLogging);
1478 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1479 DebugLogging))
1480 return false;
1481 // Add the nested pass manager with the appropriate adaptor.
1482 CGPM.addPass(std::move(NestedCGPM));
1483 return true;
1484 }
1485 if (Name == "function") {
1486 FunctionPassManager FPM(DebugLogging);
1487 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1488 DebugLogging))
1489 return false;
1490 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001491 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001492 return true;
1493 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001494 if (auto Count = parseRepeatPassName(Name)) {
1495 CGSCCPassManager NestedCGPM(DebugLogging);
1496 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1497 DebugLogging))
1498 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001499 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001500 return true;
1501 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001502 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1503 CGSCCPassManager NestedCGPM(DebugLogging);
1504 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1505 DebugLogging))
1506 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001507 CGPM.addPass(
1508 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001509 return true;
1510 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001511
1512 for (auto &C : CGSCCPipelineParsingCallbacks)
1513 if (C(Name, CGPM, InnerPipeline))
1514 return true;
1515
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001516 // Normal passes can't have pipelines.
1517 return false;
1518 }
1519
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001520// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001521#define CGSCC_PASS(NAME, CREATE_PASS) \
1522 if (Name == NAME) { \
1523 CGPM.addPass(CREATE_PASS); \
1524 return true; \
1525 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001526#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1527 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001528 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001529 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001530 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1531 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001532 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001533 } \
1534 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001535 CGPM.addPass(InvalidateAnalysisPass< \
1536 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001537 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001538 }
1539#include "PassRegistry.def"
1540
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001541 for (auto &C : CGSCCPipelineParsingCallbacks)
1542 if (C(Name, CGPM, InnerPipeline))
1543 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001544 return false;
1545}
1546
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001547bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1548 const PipelineElement &E,
1549 bool VerifyEachPass, bool DebugLogging) {
1550 auto &Name = E.Name;
1551 auto &InnerPipeline = E.InnerPipeline;
1552
1553 // First handle complex passes like the pass managers which carry pipelines.
1554 if (!InnerPipeline.empty()) {
1555 if (Name == "function") {
1556 FunctionPassManager NestedFPM(DebugLogging);
1557 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1558 DebugLogging))
1559 return false;
1560 // Add the nested pass manager with the appropriate adaptor.
1561 FPM.addPass(std::move(NestedFPM));
1562 return true;
1563 }
1564 if (Name == "loop") {
1565 LoopPassManager LPM(DebugLogging);
1566 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1567 DebugLogging))
1568 return false;
1569 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001570 FPM.addPass(
1571 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001572 return true;
1573 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001574 if (auto Count = parseRepeatPassName(Name)) {
1575 FunctionPassManager NestedFPM(DebugLogging);
1576 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1577 DebugLogging))
1578 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001579 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001580 return true;
1581 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001582
1583 for (auto &C : FunctionPipelineParsingCallbacks)
1584 if (C(Name, FPM, InnerPipeline))
1585 return true;
1586
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001587 // Normal passes can't have pipelines.
1588 return false;
1589 }
1590
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001591// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001592#define FUNCTION_PASS(NAME, CREATE_PASS) \
1593 if (Name == NAME) { \
1594 FPM.addPass(CREATE_PASS); \
1595 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001596 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001597#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1598 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001599 FPM.addPass( \
1600 RequireAnalysisPass< \
1601 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001602 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001603 } \
1604 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001605 FPM.addPass(InvalidateAnalysisPass< \
1606 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001607 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001608 }
1609#include "PassRegistry.def"
1610
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001611 for (auto &C : FunctionPipelineParsingCallbacks)
1612 if (C(Name, FPM, InnerPipeline))
1613 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001614 return false;
1615}
1616
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001617bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001618 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001619 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001620 auto &InnerPipeline = E.InnerPipeline;
1621
1622 // First handle complex passes like the pass managers which carry pipelines.
1623 if (!InnerPipeline.empty()) {
1624 if (Name == "loop") {
1625 LoopPassManager NestedLPM(DebugLogging);
1626 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1627 DebugLogging))
1628 return false;
1629 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001630 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001631 return true;
1632 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001633 if (auto Count = parseRepeatPassName(Name)) {
1634 LoopPassManager NestedLPM(DebugLogging);
1635 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1636 DebugLogging))
1637 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001638 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001639 return true;
1640 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001641
1642 for (auto &C : LoopPipelineParsingCallbacks)
1643 if (C(Name, LPM, InnerPipeline))
1644 return true;
1645
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001646 // Normal passes can't have pipelines.
1647 return false;
1648 }
1649
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001650// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001651#define LOOP_PASS(NAME, CREATE_PASS) \
1652 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001653 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001654 return true; \
1655 }
1656#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1657 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001658 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001659 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1660 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1661 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001662 return true; \
1663 } \
1664 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001665 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001666 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001667 return true; \
1668 }
1669#include "PassRegistry.def"
1670
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001671 for (auto &C : LoopPipelineParsingCallbacks)
1672 if (C(Name, LPM, InnerPipeline))
1673 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001674 return false;
1675}
1676
Chandler Carruthedf59962016-02-18 09:45:17 +00001677bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001678#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1679 if (Name == NAME) { \
1680 AA.registerModuleAnalysis< \
1681 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1682 return true; \
1683 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001684#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1685 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001686 AA.registerFunctionAnalysis< \
1687 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001688 return true; \
1689 }
1690#include "PassRegistry.def"
1691
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001692 for (auto &C : AAParsingCallbacks)
1693 if (C(Name, AA))
1694 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001695 return false;
1696}
1697
Justin Bognereecc3c82016-02-25 07:23:08 +00001698bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001699 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001700 bool VerifyEachPass,
1701 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001702 for (const auto &Element : Pipeline) {
1703 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1704 return false;
1705 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001706 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001707 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001708}
1709
Chandler Carruth1ff77242015-03-07 09:02:36 +00001710bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001711 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001712 bool VerifyEachPass,
1713 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001714 for (const auto &Element : Pipeline) {
1715 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1716 return false;
1717 if (VerifyEachPass)
1718 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001719 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001720 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001721}
1722
Chandler Carruth1ff77242015-03-07 09:02:36 +00001723bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001724 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001725 bool VerifyEachPass,
1726 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001727 for (const auto &Element : Pipeline) {
1728 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1729 return false;
1730 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001731 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001732 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001733}
1734
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001735void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1736 FunctionAnalysisManager &FAM,
1737 CGSCCAnalysisManager &CGAM,
1738 ModuleAnalysisManager &MAM) {
1739 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1740 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001741 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1742 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1743 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1744 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1745 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1746}
1747
Chandler Carruth1ff77242015-03-07 09:02:36 +00001748bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001749 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001750 bool VerifyEachPass,
1751 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001752 for (const auto &Element : Pipeline) {
1753 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1754 return false;
1755 if (VerifyEachPass)
1756 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001757 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001758 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001759}
1760
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001761// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001762// FIXME: Should this routine accept a TargetMachine or require the caller to
1763// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001764bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1765 StringRef PipelineText, bool VerifyEachPass,
1766 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001767 auto Pipeline = parsePipelineText(PipelineText);
1768 if (!Pipeline || Pipeline->empty())
1769 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001770
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001771 // If the first name isn't at the module layer, wrap the pipeline up
1772 // automatically.
1773 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001774
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001775 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1776 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001777 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001778 } else if (isFunctionPassName(FirstName,
1779 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001780 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001781 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001782 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001783 } else {
1784 for (auto &C : TopLevelPipelineParsingCallbacks)
1785 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1786 return true;
1787
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001788 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001789 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001790 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001791 }
1792
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001793 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001794}
Chandler Carruthedf59962016-02-18 09:45:17 +00001795
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001796// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1797bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1798 StringRef PipelineText, bool VerifyEachPass,
1799 bool DebugLogging) {
1800 auto Pipeline = parsePipelineText(PipelineText);
1801 if (!Pipeline || Pipeline->empty())
1802 return false;
1803
1804 StringRef FirstName = Pipeline->front().Name;
1805 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1806 return false;
1807
1808 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1809}
1810
1811// Primary pass pipeline description parsing routine for a \c
1812// FunctionPassManager
1813bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1814 StringRef PipelineText, bool VerifyEachPass,
1815 bool DebugLogging) {
1816 auto Pipeline = parsePipelineText(PipelineText);
1817 if (!Pipeline || Pipeline->empty())
1818 return false;
1819
1820 StringRef FirstName = Pipeline->front().Name;
1821 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1822 return false;
1823
1824 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1825 DebugLogging);
1826}
1827
1828// Primary pass pipeline description parsing routine for a \c LoopPassManager
1829bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1830 StringRef PipelineText, bool VerifyEachPass,
1831 bool DebugLogging) {
1832 auto Pipeline = parsePipelineText(PipelineText);
1833 if (!Pipeline || Pipeline->empty())
1834 return false;
1835
1836 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1837}
1838
Chandler Carruthedf59962016-02-18 09:45:17 +00001839bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001840 // If the pipeline just consists of the word 'default' just replace the AA
1841 // manager with our default one.
1842 if (PipelineText == "default") {
1843 AA = buildDefaultAAPipeline();
1844 return true;
1845 }
1846
Chandler Carruthedf59962016-02-18 09:45:17 +00001847 while (!PipelineText.empty()) {
1848 StringRef Name;
1849 std::tie(Name, PipelineText) = PipelineText.split(',');
1850 if (!parseAAPassName(AA, Name))
1851 return false;
1852 }
1853
1854 return true;
1855}