blob: 8b9962af38c338d4de8743d219d1c25256fe9864 [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000025#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000026#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000027#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
28#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000029#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000030#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000031#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000032#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000033#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000034#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000035#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000036#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000037#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000038#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000039#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000040#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000041#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000043#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000044#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000045#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000046#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000047#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000048#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000049#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000050#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000051#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000052#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000053#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000054#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000055#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000056#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000057#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000058#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000059#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000060#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000061#include "llvm/Target/TargetMachine.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000062#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000063#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000066#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000067#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000068#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000069#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000070#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000071#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000072#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000073#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000074#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000075#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000076#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000077#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000078#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000079#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000080#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000081#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000082#include "llvm/Transforms/IPO/SCCP.h"
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"
David Blaikie4fe1fe12018-03-23 22:11:06 +000088#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000089#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000090#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000091#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000092#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000093#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000094#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000095#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000096#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000097#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000098#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000099#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000100#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000101#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000102#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000103#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000104#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000105#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000106#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Sean Silva46590d52016-06-14 00:51:09 +0000107#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000108#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000109#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000110#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000111#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000112#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000113#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000114#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000115#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000116#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000117#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000118#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000119#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000120#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000121#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000122#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000123#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000124#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000125#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000126#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000127#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000128#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000129#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000130#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000131#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000132#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000133#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000134#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000135#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000136#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000137#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000138#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000139#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000140#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000141#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000142#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000143#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000144#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000145#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000146#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000147#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000148#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000149#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000150#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000151#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000152#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000153#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000154
Chandler Carruth66445382014-01-11 08:16:35 +0000155using namespace llvm;
156
Chandler Carruth719ffe12017-02-12 05:38:04 +0000157static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
158 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000159static cl::opt<bool>
160 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
161 cl::Hidden, cl::ZeroOrMore,
162 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000163
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000164static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000165 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000166 cl::Hidden, cl::ZeroOrMore,
167 cl::desc("Run NewGVN instead of GVN"));
168
Geoff Berry3cca1da2017-06-10 15:20:03 +0000169static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000170 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
171 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000172
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000173static cl::opt<bool> EnableGVNHoist(
174 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
175 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
176
Davide Italianobe1b6a92017-06-03 23:18:29 +0000177static cl::opt<bool> EnableGVNSink(
178 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
179 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
180
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000181static cl::opt<bool> EnableSyntheticCounts(
182 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
183 cl::desc("Run synthetic function entry count generation "
184 "pass"));
185
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000186static Regex DefaultAliasRegex(
187 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000188
Chandler Carruthe3f50642016-12-22 06:59:15 +0000189static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
190 switch (Level) {
191 case PassBuilder::O0:
192 case PassBuilder::O1:
193 case PassBuilder::O2:
194 case PassBuilder::O3:
195 return false;
196
197 case PassBuilder::Os:
198 case PassBuilder::Oz:
199 return true;
200 }
201 llvm_unreachable("Invalid optimization level!");
202}
203
Chandler Carruth66445382014-01-11 08:16:35 +0000204namespace {
205
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000206/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000207struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000208 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000209 return PreservedAnalyses::all();
210 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000211 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000212};
213
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000214/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000215class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
216 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000217 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000218
219public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000220 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000221 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000222 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223};
224
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000225/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000226struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000227 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
228 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000229 return PreservedAnalyses::all();
230 }
231 static StringRef name() { return "NoOpCGSCCPass"; }
232};
233
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000234/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000235class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
236 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000237 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000238
239public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000240 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000241 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000242 return Result();
243 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000244 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000245};
246
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000247/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000248struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000249 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000250 return PreservedAnalyses::all();
251 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000252 static StringRef name() { return "NoOpFunctionPass"; }
253};
254
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000255/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000256class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
257 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000258 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000259
260public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000261 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000262 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000263 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000264};
265
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000266/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000267struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000268 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
269 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000270 return PreservedAnalyses::all();
271 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000272 static StringRef name() { return "NoOpLoopPass"; }
273};
274
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000275/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000276class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
277 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000278 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000279
280public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000281 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000282 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
283 return Result();
284 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000285 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000286};
287
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000288AnalysisKey NoOpModuleAnalysis::Key;
289AnalysisKey NoOpCGSCCAnalysis::Key;
290AnalysisKey NoOpFunctionAnalysis::Key;
291AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000292
Chandler Carruth66445382014-01-11 08:16:35 +0000293} // End anonymous namespace.
294
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000295void PassBuilder::invokePeepholeEPCallbacks(
296 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
297 for (auto &C : PeepholeEPCallbacks)
298 C(FPM, Level);
299}
300
Chandler Carruth1ff77242015-03-07 09:02:36 +0000301void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000302#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000303 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000304#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000305
306 for (auto &C : ModuleAnalysisRegistrationCallbacks)
307 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000308}
309
Chandler Carruth1ff77242015-03-07 09:02:36 +0000310void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000311#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000312 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000313#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000314
315 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
316 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000317}
318
Chandler Carruth1ff77242015-03-07 09:02:36 +0000319void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000320#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000321 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000322#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000323
324 for (auto &C : FunctionAnalysisRegistrationCallbacks)
325 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000326}
327
Justin Bognereecc3c82016-02-25 07:23:08 +0000328void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000329#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000330 LAM.registerPass([&] { return CREATE_PASS; });
331#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000332
333 for (auto &C : LoopAnalysisRegistrationCallbacks)
334 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000335}
336
Chandler Carruthe3f50642016-12-22 06:59:15 +0000337FunctionPassManager
338PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000339 ThinLTOPhase Phase,
340 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000341 assert(Level != O0 && "Must request optimizations!");
342 FunctionPassManager FPM(DebugLogging);
343
344 // Form SSA out of local memory accesses after breaking apart aggregates into
345 // scalars.
346 FPM.addPass(SROA());
347
348 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000349 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000350
Davide Italianoc3688312017-06-01 23:08:14 +0000351 // Hoisting of scalars and load expressions.
352 if (EnableGVNHoist)
353 FPM.addPass(GVNHoistPass());
354
Davide Italianobe1b6a92017-06-03 23:18:29 +0000355 // Global value numbering based sinking.
356 if (EnableGVNSink) {
357 FPM.addPass(GVNSinkPass());
358 FPM.addPass(SimplifyCFGPass());
359 }
360
Chandler Carruthe3f50642016-12-22 06:59:15 +0000361 // Speculative execution if the target has divergent branches; otherwise nop.
362 FPM.addPass(SpeculativeExecutionPass());
363
364 // Optimize based on known information about branches, and cleanup afterward.
365 FPM.addPass(JumpThreadingPass());
366 FPM.addPass(CorrelatedValuePropagationPass());
367 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000368 if (Level == O3)
369 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000370 FPM.addPass(InstCombinePass());
371
372 if (!isOptimizingForSize(Level))
373 FPM.addPass(LibCallsShrinkWrapPass());
374
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000375 invokePeepholeEPCallbacks(FPM, Level);
376
Rong Xue1f42452017-10-23 22:21:29 +0000377 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
378 // using the size value profile. Don't perform this when optimizing for size.
379 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
380 !isOptimizingForSize(Level))
381 FPM.addPass(PGOMemOPSizeOpt());
382
Chandler Carruthe3f50642016-12-22 06:59:15 +0000383 FPM.addPass(TailCallElimPass());
384 FPM.addPass(SimplifyCFGPass());
385
386 // Form canonically associated expression trees, and simplify the trees using
387 // basic mathematical properties. For example, this will form (nearly)
388 // minimal multiplication trees.
389 FPM.addPass(ReassociatePass());
390
391 // Add the primary loop simplification pipeline.
392 // FIXME: Currently this is split into two loop pass pipelines because we run
393 // some function passes in between them. These can and should be replaced by
394 // loop pass equivalenst but those aren't ready yet. Specifically,
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000395 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
396 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
397 // the other we have is `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000398 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
399
400 // Rotate Loop - disable header duplication at -Oz
401 LPM1.addPass(LoopRotatePass(Level != Oz));
402 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000403 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000404 LPM2.addPass(IndVarSimplifyPass());
405 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000406
407 for (auto &C : LateLoopOptimizationsEPCallbacks)
408 C(LPM2, Level);
409
Chandler Carruth79b733b2017-01-26 23:21:17 +0000410 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000411 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000412 // because it changes IR to makes profile annotation in back compile
413 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000414 if (Phase != ThinLTOPhase::PreLink ||
415 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000416 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000417
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000418 for (auto &C : LoopOptimizerEndEPCallbacks)
419 C(LPM2, Level);
420
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000421 // We provide the opt remark emitter pass for LICM to use. We only need to do
422 // this once as it is immutable.
423 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000424 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000425 FPM.addPass(SimplifyCFGPass());
426 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000427 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000428
429 // Eliminate redundancies.
430 if (Level != O1) {
431 // These passes add substantial compile time so skip them at O1.
432 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000433 if (RunNewGVN)
434 FPM.addPass(NewGVNPass());
435 else
436 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000437 }
438
439 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
440 FPM.addPass(MemCpyOptPass());
441
442 // Sparse conditional constant propagation.
443 // FIXME: It isn't clear why we do this *after* loop passes rather than
444 // before...
445 FPM.addPass(SCCPPass());
446
447 // Delete dead bit computations (instcombine runs after to fold away the dead
448 // computations, and then ADCE will run later to exploit any new DCE
449 // opportunities that creates).
450 FPM.addPass(BDCEPass());
451
452 // Run instcombine after redundancy and dead bit elimination to exploit
453 // opportunities opened up by them.
454 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000455 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000456
457 // Re-consider control flow based optimizations after redundancy elimination,
458 // redo DCE, etc.
459 FPM.addPass(JumpThreadingPass());
460 FPM.addPass(CorrelatedValuePropagationPass());
461 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000462 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000463
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000464 for (auto &C : ScalarOptimizerLateEPCallbacks)
465 C(FPM, Level);
466
Chandler Carruthe3f50642016-12-22 06:59:15 +0000467 // Finally, do an expensive DCE pass to catch all the dead code exposed by
468 // the simplifications and basic cleanup after all the simplifications.
469 FPM.addPass(ADCEPass());
470 FPM.addPass(SimplifyCFGPass());
471 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000472 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000473
474 return FPM;
475}
476
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000477void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
478 PassBuilder::OptimizationLevel Level,
479 bool RunProfileGen,
480 std::string ProfileGenFile,
481 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000482 // Generally running simplification passes and the inliner with an high
483 // threshold results in smaller executables, but there may be cases where
484 // the size grows, so let's be conservative here and skip this simplification
485 // at -Os/Oz.
486 if (!isOptimizingForSize(Level)) {
487 InlineParams IP;
488
489 // In the old pass manager, this is a cl::opt. Should still this be one?
490 IP.DefaultThreshold = 75;
491
492 // FIXME: The hint threshold has the same value used by the regular inliner.
493 // This should probably be lowered after performance testing.
494 // FIXME: this comment is cargo culted from the old pass manager, revisit).
495 IP.HintThreshold = 325;
496
497 CGSCCPassManager CGPipeline(DebugLogging);
498
499 CGPipeline.addPass(InlinerPass(IP));
500
501 FunctionPassManager FPM;
502 FPM.addPass(SROA());
503 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
504 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
505 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000506 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000507
Davide Italiano513dfaa2017-02-13 15:26:22 +0000508 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
509
510 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
511 }
512
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000513 // Delete anything that is now dead to make sure that we don't instrument
514 // dead code. Instrumentation can end up keeping dead code around and
515 // dramatically increase code size.
516 MPM.addPass(GlobalDCEPass());
517
Davide Italiano513dfaa2017-02-13 15:26:22 +0000518 if (RunProfileGen) {
519 MPM.addPass(PGOInstrumentationGen());
520
Xinliang David Lib67530e2017-06-25 00:26:43 +0000521 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000522 FPM.addPass(
523 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000524 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
525
Davide Italiano513dfaa2017-02-13 15:26:22 +0000526 // Add the profile lowering pass.
527 InstrProfOptions Options;
528 if (!ProfileGenFile.empty())
529 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000530 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000531 MPM.addPass(InstrProfiling(Options));
532 }
533
534 if (!ProfileUseFile.empty())
535 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
536}
537
Easwaran Raman8249fac2017-06-28 13:33:49 +0000538static InlineParams
539getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
540 auto O3 = PassBuilder::O3;
541 unsigned OptLevel = Level > O3 ? 2 : Level;
542 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
543 return getInlineParams(OptLevel, SizeLevel);
544}
545
Chandler Carruthe3f50642016-12-22 06:59:15 +0000546ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000547PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000548 ThinLTOPhase Phase,
549 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000550 ModulePassManager MPM(DebugLogging);
551
Chandler Carruthe3f50642016-12-22 06:59:15 +0000552 // Do basic inference of function attributes from known properties of system
553 // libraries and other oracles.
554 MPM.addPass(InferFunctionAttrsPass());
555
556 // Create an early function pass manager to cleanup the output of the
557 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000558 FunctionPassManager EarlyFPM(DebugLogging);
559 EarlyFPM.addPass(SimplifyCFGPass());
560 EarlyFPM.addPass(SROA());
561 EarlyFPM.addPass(EarlyCSEPass());
562 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000563 if (Level == O3)
564 EarlyFPM.addPass(CallSiteSplittingPass());
565
Dehao Chen08f88312017-08-07 20:23:20 +0000566 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
567 // to convert bitcast to direct calls so that they can be inlined during the
568 // profile annotation prepration step.
569 // More details about SamplePGO design can be found in:
570 // https://research.google.com/pubs/pub45290.html
571 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
572 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
573 Phase == ThinLTOPhase::PostLink)
574 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000575 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000576
Dehao Chen08f88312017-08-07 20:23:20 +0000577 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
578 // Annotate sample profile right after early FPM to ensure freshness of
579 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000580 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
581 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000582 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
583 // for the profile annotation to be accurate in the ThinLTO backend.
584 if (Phase != ThinLTOPhase::PreLink)
585 // We perform early indirect call promotion here, before globalopt.
586 // This is important for the ThinLTO backend phase because otherwise
587 // imported available_externally functions look unreferenced and are
588 // removed.
589 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
590 true));
591 }
592
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000593 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000594 // and prior to optimizing globals.
595 // FIXME: This position in the pipeline hasn't been carefully considered in
596 // years, it should be re-analyzed.
597 MPM.addPass(IPSCCPPass());
598
Matthew Simpsoncb585582017-10-25 13:40:08 +0000599 // Attach metadata to indirect call sites indicating the set of functions
600 // they may target at run-time. This should follow IPSCCP.
601 MPM.addPass(CalledValuePropagationPass());
602
Chandler Carruthe3f50642016-12-22 06:59:15 +0000603 // Optimize globals to try and fold them into constants.
604 MPM.addPass(GlobalOptPass());
605
606 // Promote any localized globals to SSA registers.
607 // FIXME: Should this instead by a run of SROA?
608 // FIXME: We should probably run instcombine and simplify-cfg afterward to
609 // delete control flows that are dead once globals have been folded to
610 // constants.
611 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
612
613 // Remove any dead arguments exposed by cleanups and constand folding
614 // globals.
615 MPM.addPass(DeadArgumentEliminationPass());
616
617 // Create a small function pass pipeline to cleanup after all the global
618 // optimizations.
619 FunctionPassManager GlobalCleanupPM(DebugLogging);
620 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000621 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
622
Chandler Carruthe3f50642016-12-22 06:59:15 +0000623 GlobalCleanupPM.addPass(SimplifyCFGPass());
624 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
625
Dehao Chen89d32262017-08-02 01:28:31 +0000626 // Add all the requested passes for instrumentation PGO, if requested.
627 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
628 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
629 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
630 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
631 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000632 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000633
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000634 // Synthesize function entry counts for non-PGO compilation.
635 if (EnableSyntheticCounts && !PGOOpt)
636 MPM.addPass(SyntheticCountsPropagation());
637
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000638 // Require the GlobalsAA analysis for the module so we can query it within
639 // the CGSCC pipeline.
640 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000641
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000642 // Require the ProfileSummaryAnalysis for the module so we can query it within
643 // the inliner pass.
644 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
645
Chandler Carruthe3f50642016-12-22 06:59:15 +0000646 // Now begin the main postorder CGSCC pipeline.
647 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
648 // manager and trying to emulate its precise behavior. Much of this doesn't
649 // make a lot of sense and we should revisit the core CGSCC structure.
650 CGSCCPassManager MainCGPipeline(DebugLogging);
651
652 // Note: historically, the PruneEH pass was run first to deduce nounwind and
653 // generally clean up exception handling overhead. It isn't clear this is
654 // valuable as the inliner doesn't currently care whether it is inlining an
655 // invoke or a call.
656
657 // Run the inliner first. The theory is that we are walking bottom-up and so
658 // the callees have already been fully optimized, and we want to inline them
659 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000660 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000661 // because it makes profile annotation in the backend inaccurate.
662 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000663 if (Phase == ThinLTOPhase::PreLink &&
664 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000665 IP.HotCallSiteThreshold = 0;
666 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000667
668 // Now deduce any function attributes based in the current code.
669 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
670
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000671 // When at O3 add argument promotion to the pass pipeline.
672 // FIXME: It isn't at all clear why this should be limited to O3.
673 if (Level == O3)
674 MainCGPipeline.addPass(ArgumentPromotionPass());
675
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676 // Lastly, add the core function simplification pipeline nested inside the
677 // CGSCC walk.
678 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000679 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000680
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000681 for (auto &C : CGSCCOptimizerLateEPCallbacks)
682 C(MainCGPipeline, Level);
683
Chandler Carruth719ffe12017-02-12 05:38:04 +0000684 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
685 // to detect when we devirtualize indirect calls and iterate the SCC passes
686 // in that case to try and catch knock-on inlining or function attrs
687 // opportunities. Then we add it to the module pipeline by walking the SCCs
688 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000689 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000690 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000691 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000692
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000693 return MPM;
694}
695
696ModulePassManager
697PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
698 bool DebugLogging) {
699 ModulePassManager MPM(DebugLogging);
700
701 // Optimize globals now that the module is fully simplified.
702 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000703 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000704
Xinliang David Li126157c2017-05-22 16:41:57 +0000705 // Run partial inlining pass to partially inline functions that have
706 // large bodies.
707 if (RunPartialInlining)
708 MPM.addPass(PartialInlinerPass());
709
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000710 // Remove avail extern fns and globals definitions since we aren't compiling
711 // an object file for later LTO. For LTO we want to preserve these so they
712 // are eligible for inlining at link-time. Note if they are unreferenced they
713 // will be removed by GlobalDCE later, so this only impacts referenced
714 // available externally globals. Eventually they will be suppressed during
715 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
716 // may make globals referenced by available external functions dead and saves
717 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000718 MPM.addPass(EliminateAvailableExternallyPass());
719
720 // Do RPO function attribute inference across the module to forward-propagate
721 // attributes where applicable.
722 // FIXME: Is this really an optimization rather than a canonicalization?
723 MPM.addPass(ReversePostOrderFunctionAttrsPass());
724
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000725 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000726 // useful as the above will have inlined, DCE'ed, and function-attr
727 // propagated everything. We should at this point have a reasonably minimal
728 // and richly annotated call graph. By computing aliasing and mod/ref
729 // information for all local globals here, the late loop passes and notably
730 // the vectorizer will be able to use them to help recognize vectorizable
731 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000732 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000733
734 FunctionPassManager OptimizePM(DebugLogging);
735 OptimizePM.addPass(Float2IntPass());
736 // FIXME: We need to run some loop optimizations to re-rotate loops after
737 // simplify-cfg and others undo their rotation.
738
739 // Optimize the loop execution. These passes operate on entire loop nests
740 // rather than on each loop in an inside-out manner, and so they are actually
741 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000742
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000743 for (auto &C : VectorizerStartEPCallbacks)
744 C(OptimizePM, Level);
745
Chandler Carrutha95ff382017-01-27 00:50:21 +0000746 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000747 OptimizePM.addPass(
748 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000749
750 // Distribute loops to allow partial vectorization. I.e. isolate dependences
751 // into separate loop that would otherwise inhibit vectorization. This is
752 // currently only performed for loops marked with the metadata
753 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000754 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000755
756 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000757 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000758
Chandler Carruthbaabda92017-01-27 01:32:26 +0000759 // Eliminate loads by forwarding stores from the previous iteration to loads
760 // of the current iteration.
761 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000762
763 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000764 OptimizePM.addPass(InstCombinePass());
765
Chandler Carrutha95ff382017-01-27 00:50:21 +0000766 // Now that we've formed fast to execute loop structures, we do further
767 // optimizations. These are run afterward as they might block doing complex
768 // analyses and transforms such as what are needed for loop vectorization.
769
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000770 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
771 // GVN, loop transforms, and others have already run, so it's now better to
772 // convert to more optimized IR using more aggressive simplify CFG options.
773 // The extra sinking transform can create larger basic blocks, so do this
774 // before SLP vectorization.
775 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
776 forwardSwitchCondToPhi(true).
777 convertSwitchToLookupTable(true).
778 needCanonicalLoops(false).
779 sinkCommonInsts(true)));
780
Chandler Carruthe3f50642016-12-22 06:59:15 +0000781 // Optimize parallel scalar instruction chains into SIMD instructions.
782 OptimizePM.addPass(SLPVectorizerPass());
783
Chandler Carruthe3f50642016-12-22 06:59:15 +0000784 OptimizePM.addPass(InstCombinePass());
785
786 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000787 // execution resources of an out-of-order processor. We also then need to
788 // clean up redundancies and loop invariant code.
789 // FIXME: It would be really good to use a loop-integrated instruction
790 // combiner for cleanup here so that the unrolling and LICM can be pipelined
791 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000792 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000793 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000794 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000795 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000796
797 // Now that we've vectorized and unrolled loops, we may have more refined
798 // alignment information, try to re-derive it here.
799 OptimizePM.addPass(AlignmentFromAssumptionsPass());
800
Chandler Carrutha95ff382017-01-27 00:50:21 +0000801 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
802 // canonicalization pass that enables other optimizations. As a result,
803 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
804 // result too early.
805 OptimizePM.addPass(LoopSinkPass());
806
807 // And finally clean up LCSSA form before generating code.
808 OptimizePM.addPass(InstSimplifierPass());
809
Sanjay Patel6fd43912017-09-09 13:38:18 +0000810 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
811 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
812 // flattening of blocks.
813 OptimizePM.addPass(DivRemPairsPass());
814
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000815 // LoopSink (and other loop passes since the last simplifyCFG) might have
816 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
817 OptimizePM.addPass(SimplifyCFGPass());
818
Chandler Carruthc34f7892017-11-28 11:32:31 +0000819 // Optimize PHIs by speculating around them when profitable. Note that this
820 // pass needs to be run after any PRE or similar pass as it is essentially
821 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
822 OptimizePM.addPass(SpeculateAroundPHIsPass());
823
Chandler Carrutha95ff382017-01-27 00:50:21 +0000824 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000825 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
826
827 // Now we need to do some global optimization transforms.
828 // FIXME: It would seem like these should come first in the optimization
829 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
830 // ordering here.
831 MPM.addPass(GlobalDCEPass());
832 MPM.addPass(ConstantMergePass());
833
834 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000835}
836
Chandler Carruthe3f50642016-12-22 06:59:15 +0000837ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000838PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
839 bool DebugLogging) {
840 assert(Level != O0 && "Must request optimizations for the default pipeline!");
841
842 ModulePassManager MPM(DebugLogging);
843
844 // Force any function attributes we want the rest of the pipeline to observe.
845 MPM.addPass(ForceFunctionAttrsPass());
846
David Blaikie0c64f5a2018-01-23 01:25:20 +0000847 // Apply module pipeline start EP callback.
848 for (auto &C : PipelineStartEPCallbacks)
849 C(MPM);
850
Dehao Chen08f88312017-08-07 20:23:20 +0000851 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000852 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
853
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000854 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000855 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
856 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000857
858 // Now add the optimization pipeline.
859 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
860
861 return MPM;
862}
863
864ModulePassManager
865PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
866 bool DebugLogging) {
867 assert(Level != O0 && "Must request optimizations for the default pipeline!");
868
869 ModulePassManager MPM(DebugLogging);
870
871 // Force any function attributes we want the rest of the pipeline to observe.
872 MPM.addPass(ForceFunctionAttrsPass());
873
Dehao Chen08f88312017-08-07 20:23:20 +0000874 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000875 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
876
David Blaikie0c64f5a2018-01-23 01:25:20 +0000877 // Apply module pipeline start EP callback.
878 for (auto &C : PipelineStartEPCallbacks)
879 C(MPM);
880
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000881 // If we are planning to perform ThinLTO later, we don't bloat the code with
882 // unrolling/vectorization/... now. Just simplify the module as much as we
883 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000884 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
885 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000886
887 // Run partial inlining pass to partially inline functions that have
888 // large bodies.
889 // FIXME: It isn't clear whether this is really the right place to run this
890 // in ThinLTO. Because there is another canonicalization and simplification
891 // phase that will run after the thin link, running this here ends up with
892 // less information than will be available later and it may grow functions in
893 // ways that aren't beneficial.
894 if (RunPartialInlining)
895 MPM.addPass(PartialInlinerPass());
896
897 // Reduce the size of the IR as much as possible.
898 MPM.addPass(GlobalOptPass());
899
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000900 return MPM;
901}
902
903ModulePassManager
904PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
905 bool DebugLogging) {
906 // FIXME: The summary index is not hooked in the new pass manager yet.
907 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
908 // here.
909
910 ModulePassManager MPM(DebugLogging);
911
912 // Force any function attributes we want the rest of the pipeline to observe.
913 MPM.addPass(ForceFunctionAttrsPass());
914
915 // During the ThinLTO backend phase we perform early indirect call promotion
916 // here, before globalopt. Otherwise imported available_externally functions
917 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000918 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
919 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000920 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000921 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
922 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000923
924 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000925 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
926 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000927
928 // Now add the optimization pipeline.
929 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
930
931 return MPM;
932}
933
934ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000935PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
936 bool DebugLogging) {
937 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000938 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000939 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000940}
941
Chandler Carruthe3f50642016-12-22 06:59:15 +0000942ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
943 bool DebugLogging) {
944 assert(Level != O0 && "Must request optimizations for the default pipeline!");
945 ModulePassManager MPM(DebugLogging);
946
Davide Italiano089a9122017-01-24 00:57:39 +0000947 // Remove unused virtual tables to improve the quality of code generated by
948 // whole-program devirtualization and bitset lowering.
949 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000950
Davide Italiano089a9122017-01-24 00:57:39 +0000951 // Force any function attributes we want the rest of the pipeline to observe.
952 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000953
Davide Italiano089a9122017-01-24 00:57:39 +0000954 // Do basic inference of function attributes from known properties of system
955 // libraries and other oracles.
956 MPM.addPass(InferFunctionAttrsPass());
957
958 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000959 FunctionPassManager EarlyFPM(DebugLogging);
960 EarlyFPM.addPass(CallSiteSplittingPass());
961 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
962
Davide Italiano089a9122017-01-24 00:57:39 +0000963 // Indirect call promotion. This should promote all the targets that are
964 // left by the earlier promotion pass that promotes intra-module targets.
965 // This two-step promotion is to save the compile time. For LTO, it should
966 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000967 MPM.addPass(PGOIndirectCallPromotion(
968 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000969 // Propagate constants at call sites into the functions they call. This
970 // opens opportunities for globalopt (and inlining) by substituting function
971 // pointers passed as arguments to direct uses of functions.
972 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000973
974 // Attach metadata to indirect call sites indicating the set of functions
975 // they may target at run-time. This should follow IPSCCP.
976 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000977 }
978
979 // Now deduce any function attributes based in the current code.
980 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
981 PostOrderFunctionAttrsPass()));
982
983 // Do RPO function attribute inference across the module to forward-propagate
984 // attributes where applicable.
985 // FIXME: Is this really an optimization rather than a canonicalization?
986 MPM.addPass(ReversePostOrderFunctionAttrsPass());
987
988 // Use inragne annotations on GEP indices to split globals where beneficial.
989 MPM.addPass(GlobalSplitPass());
990
991 // Run whole program optimization of virtual call when the list of callees
992 // is fixed.
993 MPM.addPass(WholeProgramDevirtPass());
994
995 // Stop here at -O1.
996 if (Level == 1)
997 return MPM;
998
999 // Optimize globals to try and fold them into constants.
1000 MPM.addPass(GlobalOptPass());
1001
1002 // Promote any localized globals to SSA registers.
1003 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1004
1005 // Linking modules together can lead to duplicate global constant, only
1006 // keep one copy of each constant.
1007 MPM.addPass(ConstantMergePass());
1008
1009 // Remove unused arguments from functions.
1010 MPM.addPass(DeadArgumentEliminationPass());
1011
1012 // Reduce the code after globalopt and ipsccp. Both can open up significant
1013 // simplification opportunities, and both can propagate functions through
1014 // function pointers. When this happens, we often have to resolve varargs
1015 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001016 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001017 if (Level == O3)
1018 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001019 PeepholeFPM.addPass(InstCombinePass());
1020 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1021
1022 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001023
1024 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1025 // generally clean up exception handling overhead. It isn't clear this is
1026 // valuable as the inliner doesn't currently care whether it is inlining an
1027 // invoke or a call.
1028 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001029 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1030 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001031
1032 // Optimize globals again after we ran the inliner.
1033 MPM.addPass(GlobalOptPass());
1034
1035 // Garbage collect dead functions.
1036 // FIXME: Add ArgumentPromotion pass after once it's ported.
1037 MPM.addPass(GlobalDCEPass());
1038
1039 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001040 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001041 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001042 invokePeepholeEPCallbacks(FPM, Level);
1043
Davide Italiano089a9122017-01-24 00:57:39 +00001044 FPM.addPass(JumpThreadingPass());
1045
1046 // Break up allocas
1047 FPM.addPass(SROA());
1048
1049 // Run a few AA driver optimizations here and now to cleanup the code.
1050 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1051
1052 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1053 PostOrderFunctionAttrsPass()));
1054 // FIXME: here we run IP alias analysis in the legacy PM.
1055
1056 FunctionPassManager MainFPM;
1057
1058 // FIXME: once we fix LoopPass Manager, add LICM here.
1059 // FIXME: once we provide support for enabling MLSM, add it here.
1060 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001061 if (RunNewGVN)
1062 MainFPM.addPass(NewGVNPass());
1063 else
1064 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001065
1066 // Remove dead memcpy()'s.
1067 MainFPM.addPass(MemCpyOptPass());
1068
1069 // Nuke dead stores.
1070 MainFPM.addPass(DSEPass());
1071
1072 // FIXME: at this point, we run a bunch of loop passes:
1073 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1074 // loopVectorize. Enable them once the remaining issue with LPM
1075 // are sorted out.
1076
1077 MainFPM.addPass(InstCombinePass());
1078 MainFPM.addPass(SimplifyCFGPass());
1079 MainFPM.addPass(SCCPPass());
1080 MainFPM.addPass(InstCombinePass());
1081 MainFPM.addPass(BDCEPass());
1082
1083 // FIXME: We may want to run SLPVectorizer here.
1084 // After vectorization, assume intrinsics may tell us more
1085 // about pointer alignments.
1086#if 0
1087 MainFPM.add(AlignmentFromAssumptionsPass());
1088#endif
1089
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001090 // FIXME: Conditionally run LoadCombine here, after it's ported
1091 // (in case we still have this pass, given its questionable usefulness).
1092
Davide Italiano089a9122017-01-24 00:57:39 +00001093 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001094 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001095 MainFPM.addPass(JumpThreadingPass());
1096 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1097
1098 // Create a function that performs CFI checks for cross-DSO calls with
1099 // targets in the current module.
1100 MPM.addPass(CrossDSOCFIPass());
1101
1102 // Lower type metadata and the type.test intrinsic. This pass supports
1103 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1104 // to be run at link time if CFI is enabled. This pass does nothing if
1105 // CFI is disabled.
1106 // Enable once we add support for the summary in the new PM.
1107#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001108 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1109 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001110 Summary));
1111#endif
1112
1113 // Add late LTO optimization passes.
1114 // Delete basic blocks, which optimization passes may have killed.
1115 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1116
1117 // Drop bodies of available eternally objects to improve GlobalDCE.
1118 MPM.addPass(EliminateAvailableExternallyPass());
1119
1120 // Now that we have optimized the program, discard unreachable functions.
1121 MPM.addPass(GlobalDCEPass());
1122
1123 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001124 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001125}
1126
Chandler Carruth060ad612016-12-23 20:38:19 +00001127AAManager PassBuilder::buildDefaultAAPipeline() {
1128 AAManager AA;
1129
1130 // The order in which these are registered determines their priority when
1131 // being queried.
1132
1133 // First we register the basic alias analysis that provides the majority of
1134 // per-function local AA logic. This is a stateless, on-demand local set of
1135 // AA techniques.
1136 AA.registerFunctionAnalysis<BasicAA>();
1137
1138 // Next we query fast, specialized alias analyses that wrap IR-embedded
1139 // information about aliasing.
1140 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1141 AA.registerFunctionAnalysis<TypeBasedAA>();
1142
1143 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001144 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1145 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001146 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001147 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001148
1149 return AA;
1150}
1151
Chandler Carruth241bf242016-08-03 07:44:48 +00001152static Optional<int> parseRepeatPassName(StringRef Name) {
1153 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1154 return None;
1155 int Count;
1156 if (Name.getAsInteger(0, Count) || Count <= 0)
1157 return None;
1158 return Count;
1159}
1160
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001161static Optional<int> parseDevirtPassName(StringRef Name) {
1162 if (!Name.consume_front("devirt<") || !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 Carruth8b3be4e2017-06-01 11:39:39 +00001170/// Tests whether a pass name starts with a valid prefix for a default pipeline
1171/// alias.
1172static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1173 return Name.startswith("default") || Name.startswith("thinlto") ||
1174 Name.startswith("lto");
1175}
1176
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001177/// Tests whether registered callbacks will accept a given pass name.
1178///
1179/// When parsing a pipeline text, the type of the outermost pipeline may be
1180/// omitted, in which case the type is automatically determined from the first
1181/// pass name in the text. This may be a name that is handled through one of the
1182/// callbacks. We check this through the oridinary parsing callbacks by setting
1183/// up a dummy PassManager in order to not force the client to also handle this
1184/// type of query.
1185template <typename PassManagerT, typename CallbacksT>
1186static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1187 if (!Callbacks.empty()) {
1188 PassManagerT DummyPM;
1189 for (auto &CB : Callbacks)
1190 if (CB(Name, DummyPM, {}))
1191 return true;
1192 }
1193 return false;
1194}
1195
1196template <typename CallbacksT>
1197static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001198 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001199 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001200 return DefaultAliasRegex.match(Name);
1201
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001202 // Explicitly handle pass manager names.
1203 if (Name == "module")
1204 return true;
1205 if (Name == "cgscc")
1206 return true;
1207 if (Name == "function")
1208 return true;
1209
Chandler Carruth241bf242016-08-03 07:44:48 +00001210 // Explicitly handle custom-parsed pass names.
1211 if (parseRepeatPassName(Name))
1212 return true;
1213
Chandler Carruth74a8a222016-06-17 07:15:29 +00001214#define MODULE_PASS(NAME, CREATE_PASS) \
1215 if (Name == NAME) \
1216 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001217#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001218 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001219 return true;
1220#include "PassRegistry.def"
1221
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001222 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001223}
1224
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001225template <typename CallbacksT>
1226static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001227 // Explicitly handle pass manager names.
1228 if (Name == "cgscc")
1229 return true;
1230 if (Name == "function")
1231 return true;
1232
Chandler Carruth241bf242016-08-03 07:44:48 +00001233 // Explicitly handle custom-parsed pass names.
1234 if (parseRepeatPassName(Name))
1235 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001236 if (parseDevirtPassName(Name))
1237 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001238
Chandler Carruth74a8a222016-06-17 07:15:29 +00001239#define CGSCC_PASS(NAME, CREATE_PASS) \
1240 if (Name == NAME) \
1241 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001242#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001243 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001244 return true;
1245#include "PassRegistry.def"
1246
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001247 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001248}
1249
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001250template <typename CallbacksT>
1251static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001252 // Explicitly handle pass manager names.
1253 if (Name == "function")
1254 return true;
1255 if (Name == "loop")
1256 return true;
1257
Chandler Carruth241bf242016-08-03 07:44:48 +00001258 // Explicitly handle custom-parsed pass names.
1259 if (parseRepeatPassName(Name))
1260 return true;
1261
Chandler Carruth74a8a222016-06-17 07:15:29 +00001262#define FUNCTION_PASS(NAME, CREATE_PASS) \
1263 if (Name == NAME) \
1264 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001265#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001266 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001267 return true;
1268#include "PassRegistry.def"
1269
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001270 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001271}
1272
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001273template <typename CallbacksT>
1274static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001275 // Explicitly handle pass manager names.
1276 if (Name == "loop")
1277 return true;
1278
Chandler Carruth241bf242016-08-03 07:44:48 +00001279 // Explicitly handle custom-parsed pass names.
1280 if (parseRepeatPassName(Name))
1281 return true;
1282
Chandler Carruth74a8a222016-06-17 07:15:29 +00001283#define LOOP_PASS(NAME, CREATE_PASS) \
1284 if (Name == NAME) \
1285 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001286#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1287 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1288 return true;
1289#include "PassRegistry.def"
1290
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001291 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001292}
1293
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001294Optional<std::vector<PassBuilder::PipelineElement>>
1295PassBuilder::parsePipelineText(StringRef Text) {
1296 std::vector<PipelineElement> ResultPipeline;
1297
1298 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1299 &ResultPipeline};
1300 for (;;) {
1301 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1302 size_t Pos = Text.find_first_of(",()");
1303 Pipeline.push_back({Text.substr(0, Pos), {}});
1304
1305 // If we have a single terminating name, we're done.
1306 if (Pos == Text.npos)
1307 break;
1308
1309 char Sep = Text[Pos];
1310 Text = Text.substr(Pos + 1);
1311 if (Sep == ',')
1312 // Just a name ending in a comma, continue.
1313 continue;
1314
1315 if (Sep == '(') {
1316 // Push the inner pipeline onto the stack to continue processing.
1317 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1318 continue;
1319 }
1320
1321 assert(Sep == ')' && "Bogus separator!");
1322 // When handling the close parenthesis, we greedily consume them to avoid
1323 // empty strings in the pipeline.
1324 do {
1325 // If we try to pop the outer pipeline we have unbalanced parentheses.
1326 if (PipelineStack.size() == 1)
1327 return None;
1328
1329 PipelineStack.pop_back();
1330 } while (Text.consume_front(")"));
1331
1332 // Check if we've finished parsing.
1333 if (Text.empty())
1334 break;
1335
1336 // Otherwise, the end of an inner pipeline always has to be followed by
1337 // a comma, and then we can continue.
1338 if (!Text.consume_front(","))
1339 return None;
1340 }
1341
1342 if (PipelineStack.size() > 1)
1343 // Unbalanced paretheses.
1344 return None;
1345
1346 assert(PipelineStack.back() == &ResultPipeline &&
1347 "Wrong pipeline at the bottom of the stack!");
1348 return {std::move(ResultPipeline)};
1349}
1350
1351bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1352 const PipelineElement &E, bool VerifyEachPass,
1353 bool DebugLogging) {
1354 auto &Name = E.Name;
1355 auto &InnerPipeline = E.InnerPipeline;
1356
1357 // First handle complex passes like the pass managers which carry pipelines.
1358 if (!InnerPipeline.empty()) {
1359 if (Name == "module") {
1360 ModulePassManager NestedMPM(DebugLogging);
1361 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1362 DebugLogging))
1363 return false;
1364 MPM.addPass(std::move(NestedMPM));
1365 return true;
1366 }
1367 if (Name == "cgscc") {
1368 CGSCCPassManager CGPM(DebugLogging);
1369 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1370 DebugLogging))
1371 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001372 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001373 return true;
1374 }
1375 if (Name == "function") {
1376 FunctionPassManager FPM(DebugLogging);
1377 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1378 DebugLogging))
1379 return false;
1380 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1381 return true;
1382 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001383 if (auto Count = parseRepeatPassName(Name)) {
1384 ModulePassManager NestedMPM(DebugLogging);
1385 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1386 DebugLogging))
1387 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001388 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001389 return true;
1390 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001391
1392 for (auto &C : ModulePipelineParsingCallbacks)
1393 if (C(Name, MPM, InnerPipeline))
1394 return true;
1395
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001396 // Normal passes can't have pipelines.
1397 return false;
1398 }
1399
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001400 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001401 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001402 SmallVector<StringRef, 3> Matches;
1403 if (!DefaultAliasRegex.match(Name, &Matches))
1404 return false;
1405 assert(Matches.size() == 3 && "Must capture two matched strings!");
1406
Jordan Rosef85a95f2016-07-25 18:34:51 +00001407 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001408 .Case("O0", O0)
1409 .Case("O1", O1)
1410 .Case("O2", O2)
1411 .Case("O3", O3)
1412 .Case("Os", Os)
1413 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001414 if (L == O0)
1415 // At O0 we do nothing at all!
1416 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001417
1418 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001419 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001420 } else if (Matches[1] == "thinlto-pre-link") {
1421 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1422 } else if (Matches[1] == "thinlto") {
1423 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001424 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001425 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001426 } else {
1427 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001428 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001429 }
1430 return true;
1431 }
1432
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001433 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001434#define MODULE_PASS(NAME, CREATE_PASS) \
1435 if (Name == NAME) { \
1436 MPM.addPass(CREATE_PASS); \
1437 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001438 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001439#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1440 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001441 MPM.addPass( \
1442 RequireAnalysisPass< \
1443 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001444 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001445 } \
1446 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001447 MPM.addPass(InvalidateAnalysisPass< \
1448 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001449 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001450 }
1451#include "PassRegistry.def"
1452
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001453 for (auto &C : ModulePipelineParsingCallbacks)
1454 if (C(Name, MPM, InnerPipeline))
1455 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001456 return false;
1457}
1458
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001459bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1460 const PipelineElement &E, bool VerifyEachPass,
1461 bool DebugLogging) {
1462 auto &Name = E.Name;
1463 auto &InnerPipeline = E.InnerPipeline;
1464
1465 // First handle complex passes like the pass managers which carry pipelines.
1466 if (!InnerPipeline.empty()) {
1467 if (Name == "cgscc") {
1468 CGSCCPassManager NestedCGPM(DebugLogging);
1469 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1470 DebugLogging))
1471 return false;
1472 // Add the nested pass manager with the appropriate adaptor.
1473 CGPM.addPass(std::move(NestedCGPM));
1474 return true;
1475 }
1476 if (Name == "function") {
1477 FunctionPassManager FPM(DebugLogging);
1478 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1479 DebugLogging))
1480 return false;
1481 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001482 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001483 return true;
1484 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001485 if (auto Count = parseRepeatPassName(Name)) {
1486 CGSCCPassManager NestedCGPM(DebugLogging);
1487 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1488 DebugLogging))
1489 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001490 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001491 return true;
1492 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001493 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1494 CGSCCPassManager NestedCGPM(DebugLogging);
1495 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1496 DebugLogging))
1497 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001498 CGPM.addPass(
1499 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001500 return true;
1501 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001502
1503 for (auto &C : CGSCCPipelineParsingCallbacks)
1504 if (C(Name, CGPM, InnerPipeline))
1505 return true;
1506
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001507 // Normal passes can't have pipelines.
1508 return false;
1509 }
1510
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001511// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001512#define CGSCC_PASS(NAME, CREATE_PASS) \
1513 if (Name == NAME) { \
1514 CGPM.addPass(CREATE_PASS); \
1515 return true; \
1516 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001517#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1518 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001519 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001520 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001521 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1522 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001523 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001524 } \
1525 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001526 CGPM.addPass(InvalidateAnalysisPass< \
1527 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001528 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001529 }
1530#include "PassRegistry.def"
1531
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001532 for (auto &C : CGSCCPipelineParsingCallbacks)
1533 if (C(Name, CGPM, InnerPipeline))
1534 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001535 return false;
1536}
1537
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001538bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1539 const PipelineElement &E,
1540 bool VerifyEachPass, bool DebugLogging) {
1541 auto &Name = E.Name;
1542 auto &InnerPipeline = E.InnerPipeline;
1543
1544 // First handle complex passes like the pass managers which carry pipelines.
1545 if (!InnerPipeline.empty()) {
1546 if (Name == "function") {
1547 FunctionPassManager NestedFPM(DebugLogging);
1548 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1549 DebugLogging))
1550 return false;
1551 // Add the nested pass manager with the appropriate adaptor.
1552 FPM.addPass(std::move(NestedFPM));
1553 return true;
1554 }
1555 if (Name == "loop") {
1556 LoopPassManager LPM(DebugLogging);
1557 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1558 DebugLogging))
1559 return false;
1560 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001561 FPM.addPass(
1562 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001563 return true;
1564 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001565 if (auto Count = parseRepeatPassName(Name)) {
1566 FunctionPassManager NestedFPM(DebugLogging);
1567 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1568 DebugLogging))
1569 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001570 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001571 return true;
1572 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001573
1574 for (auto &C : FunctionPipelineParsingCallbacks)
1575 if (C(Name, FPM, InnerPipeline))
1576 return true;
1577
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001578 // Normal passes can't have pipelines.
1579 return false;
1580 }
1581
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001582// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001583#define FUNCTION_PASS(NAME, CREATE_PASS) \
1584 if (Name == NAME) { \
1585 FPM.addPass(CREATE_PASS); \
1586 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001587 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001588#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1589 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001590 FPM.addPass( \
1591 RequireAnalysisPass< \
1592 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001593 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001594 } \
1595 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001596 FPM.addPass(InvalidateAnalysisPass< \
1597 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001598 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001599 }
1600#include "PassRegistry.def"
1601
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001602 for (auto &C : FunctionPipelineParsingCallbacks)
1603 if (C(Name, FPM, InnerPipeline))
1604 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001605 return false;
1606}
1607
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001608bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001609 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001610 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001611 auto &InnerPipeline = E.InnerPipeline;
1612
1613 // First handle complex passes like the pass managers which carry pipelines.
1614 if (!InnerPipeline.empty()) {
1615 if (Name == "loop") {
1616 LoopPassManager NestedLPM(DebugLogging);
1617 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1618 DebugLogging))
1619 return false;
1620 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001621 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001622 return true;
1623 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001624 if (auto Count = parseRepeatPassName(Name)) {
1625 LoopPassManager NestedLPM(DebugLogging);
1626 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1627 DebugLogging))
1628 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001629 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001630 return true;
1631 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001632
1633 for (auto &C : LoopPipelineParsingCallbacks)
1634 if (C(Name, LPM, InnerPipeline))
1635 return true;
1636
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001637 // Normal passes can't have pipelines.
1638 return false;
1639 }
1640
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001641// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001642#define LOOP_PASS(NAME, CREATE_PASS) \
1643 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001644 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001645 return true; \
1646 }
1647#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1648 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001649 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001650 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1651 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1652 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001653 return true; \
1654 } \
1655 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001656 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001657 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001658 return true; \
1659 }
1660#include "PassRegistry.def"
1661
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001662 for (auto &C : LoopPipelineParsingCallbacks)
1663 if (C(Name, LPM, InnerPipeline))
1664 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001665 return false;
1666}
1667
Chandler Carruthedf59962016-02-18 09:45:17 +00001668bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001669#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1670 if (Name == NAME) { \
1671 AA.registerModuleAnalysis< \
1672 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1673 return true; \
1674 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001675#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1676 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001677 AA.registerFunctionAnalysis< \
1678 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001679 return true; \
1680 }
1681#include "PassRegistry.def"
1682
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001683 for (auto &C : AAParsingCallbacks)
1684 if (C(Name, AA))
1685 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001686 return false;
1687}
1688
Justin Bognereecc3c82016-02-25 07:23:08 +00001689bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001690 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001691 bool VerifyEachPass,
1692 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001693 for (const auto &Element : Pipeline) {
1694 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1695 return false;
1696 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001697 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001698 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001699}
1700
Chandler Carruth1ff77242015-03-07 09:02:36 +00001701bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001702 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001703 bool VerifyEachPass,
1704 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 for (const auto &Element : Pipeline) {
1706 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1707 return false;
1708 if (VerifyEachPass)
1709 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001710 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001711 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001712}
1713
Chandler Carruth1ff77242015-03-07 09:02:36 +00001714bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001715 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001716 bool VerifyEachPass,
1717 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001718 for (const auto &Element : Pipeline) {
1719 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1720 return false;
1721 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001722 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001723 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001724}
1725
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001726void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1727 FunctionAnalysisManager &FAM,
1728 CGSCCAnalysisManager &CGAM,
1729 ModuleAnalysisManager &MAM) {
1730 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1731 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001732 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1733 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1734 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1735 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1736 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1737}
1738
Chandler Carruth1ff77242015-03-07 09:02:36 +00001739bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001740 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001741 bool VerifyEachPass,
1742 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001743 for (const auto &Element : Pipeline) {
1744 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1745 return false;
1746 if (VerifyEachPass)
1747 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001748 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001749 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001750}
1751
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001752// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001753// FIXME: Should this routine accept a TargetMachine or require the caller to
1754// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001755bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1756 StringRef PipelineText, bool VerifyEachPass,
1757 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001758 auto Pipeline = parsePipelineText(PipelineText);
1759 if (!Pipeline || Pipeline->empty())
1760 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001761
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001762 // If the first name isn't at the module layer, wrap the pipeline up
1763 // automatically.
1764 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001765
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001766 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1767 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001768 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001769 } else if (isFunctionPassName(FirstName,
1770 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001771 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001772 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001773 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001774 } else {
1775 for (auto &C : TopLevelPipelineParsingCallbacks)
1776 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1777 return true;
1778
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001779 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001780 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001781 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001782 }
1783
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001784 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001785}
Chandler Carruthedf59962016-02-18 09:45:17 +00001786
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001787// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1788bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1789 StringRef PipelineText, bool VerifyEachPass,
1790 bool DebugLogging) {
1791 auto Pipeline = parsePipelineText(PipelineText);
1792 if (!Pipeline || Pipeline->empty())
1793 return false;
1794
1795 StringRef FirstName = Pipeline->front().Name;
1796 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1797 return false;
1798
1799 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1800}
1801
1802// Primary pass pipeline description parsing routine for a \c
1803// FunctionPassManager
1804bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1805 StringRef PipelineText, bool VerifyEachPass,
1806 bool DebugLogging) {
1807 auto Pipeline = parsePipelineText(PipelineText);
1808 if (!Pipeline || Pipeline->empty())
1809 return false;
1810
1811 StringRef FirstName = Pipeline->front().Name;
1812 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1813 return false;
1814
1815 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1816 DebugLogging);
1817}
1818
1819// Primary pass pipeline description parsing routine for a \c LoopPassManager
1820bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1821 StringRef PipelineText, bool VerifyEachPass,
1822 bool DebugLogging) {
1823 auto Pipeline = parsePipelineText(PipelineText);
1824 if (!Pipeline || Pipeline->empty())
1825 return false;
1826
1827 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1828}
1829
Chandler Carruthedf59962016-02-18 09:45:17 +00001830bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001831 // If the pipeline just consists of the word 'default' just replace the AA
1832 // manager with our default one.
1833 if (PipelineText == "default") {
1834 AA = buildDefaultAAPipeline();
1835 return true;
1836 }
1837
Chandler Carruthedf59962016-02-18 09:45:17 +00001838 while (!PipelineText.empty()) {
1839 StringRef Name;
1840 std::tie(Name, PipelineText) = PipelineText.split(',');
1841 if (!parseAAPassName(AA, Name))
1842 return false;
1843 }
1844
1845 return true;
1846}