blob: eb04dcc8b6ef206c49e0519e263d8b1ac8d421f3 [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000025#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000026#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000027#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
28#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000029#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000030#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000031#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000032#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000033#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000034#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000035#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000036#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000037#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000038#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000039#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000040#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000041#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000043#include "llvm/Analysis/OptimizationRemarkEmitter.h"
John Brawnbdbbd832018-06-28 14:13:06 +000044#include "llvm/Analysis/PhiValues.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000063#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
Michael J. Spencer7bb27672018-07-16 00:28:24 +000064#include "llvm/Transforms/Instrumentation/CGProfile.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000065#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000066#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000067#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000068#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000069#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000070#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000071#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000072#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000073#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000074#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000075#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000076#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000077#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000078#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000079#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000080#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000081#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000082#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000083#include "llvm/Transforms/IPO/SCCP.h"
David Blaikie301627f2018-03-22 22:42:44 +000084#include "llvm/Transforms/IPO/SampleProfile.h"
Justin Bogner21e15372015-10-30 23:28:12 +000085#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000086#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000087#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000088#include "llvm/Transforms/InstCombine/InstCombine.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000089#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +000090#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
91#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000092#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000093#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000094#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000095#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000096#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000097#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000098#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000099#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +0000100#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +0000101#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000102#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000103#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000104#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000105#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000106#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000107#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000108#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +0000109#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
Sean Silva46590d52016-06-14 00:51:09 +0000110#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000111#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000112#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000113#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000114#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000115#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000116#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000117#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000118#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000119#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000120#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000121#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000122#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000123#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000124#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
David Green963401d2018-07-01 12:47:30 +0000125#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000126#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000127#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000128#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000129#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000130#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000131#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000132#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000133#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000134#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000135#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000136#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000137#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000138#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000139#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000140#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000141#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000142#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000143#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000144#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000145#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000146#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000147#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000148#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000149#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000150#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000151#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000152#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000153#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000154#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000155#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000156#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000157
Chandler Carruth66445382014-01-11 08:16:35 +0000158using namespace llvm;
159
Chandler Carruth719ffe12017-02-12 05:38:04 +0000160static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
161 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000162static cl::opt<bool>
163 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
164 cl::Hidden, cl::ZeroOrMore,
165 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000166
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000167static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000168 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000169 cl::Hidden, cl::ZeroOrMore,
170 cl::desc("Run NewGVN instead of GVN"));
171
Geoff Berry3cca1da2017-06-10 15:20:03 +0000172static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000173 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
174 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000175
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000176static cl::opt<bool> EnableGVNHoist(
177 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
178 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
179
Davide Italianobe1b6a92017-06-03 23:18:29 +0000180static cl::opt<bool> EnableGVNSink(
181 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
182 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
183
David Green963401d2018-07-01 12:47:30 +0000184static cl::opt<bool> EnableUnrollAndJam(
185 "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
186 cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
187
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000188static cl::opt<bool> EnableSyntheticCounts(
189 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
190 cl::desc("Run synthetic function entry count generation "
191 "pass"));
192
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000193static Regex DefaultAliasRegex(
194 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000195
Chandler Carruthe3f50642016-12-22 06:59:15 +0000196static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
197 switch (Level) {
198 case PassBuilder::O0:
199 case PassBuilder::O1:
200 case PassBuilder::O2:
201 case PassBuilder::O3:
202 return false;
203
204 case PassBuilder::Os:
205 case PassBuilder::Oz:
206 return true;
207 }
208 llvm_unreachable("Invalid optimization level!");
209}
210
Chandler Carruth66445382014-01-11 08:16:35 +0000211namespace {
212
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000213/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000214struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000215 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000216 return PreservedAnalyses::all();
217 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000218 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000219};
220
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000221/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000222class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
223 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000224 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000225
226public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000227 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000228 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000229 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000230};
231
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000232/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000233struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000234 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
235 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000236 return PreservedAnalyses::all();
237 }
238 static StringRef name() { return "NoOpCGSCCPass"; }
239};
240
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000241/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000242class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
243 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000244 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000245
246public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000247 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000248 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000249 return Result();
250 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000251 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000252};
253
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000254/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000255struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000256 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000257 return PreservedAnalyses::all();
258 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000259 static StringRef name() { return "NoOpFunctionPass"; }
260};
261
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000262/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000263class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
264 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000265 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000266
267public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000268 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000269 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000270 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000271};
272
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000273/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000274struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000275 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
276 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000277 return PreservedAnalyses::all();
278 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000279 static StringRef name() { return "NoOpLoopPass"; }
280};
281
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000282/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000283class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
284 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000285 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000286
287public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000288 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000289 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
290 return Result();
291 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000292 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000293};
294
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000295AnalysisKey NoOpModuleAnalysis::Key;
296AnalysisKey NoOpCGSCCAnalysis::Key;
297AnalysisKey NoOpFunctionAnalysis::Key;
298AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000299
Chandler Carruth66445382014-01-11 08:16:35 +0000300} // End anonymous namespace.
301
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000302void PassBuilder::invokePeepholeEPCallbacks(
303 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
304 for (auto &C : PeepholeEPCallbacks)
305 C(FPM, Level);
306}
307
Chandler Carruth1ff77242015-03-07 09:02:36 +0000308void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000309#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000310 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000311#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000312
313 for (auto &C : ModuleAnalysisRegistrationCallbacks)
314 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000315}
316
Chandler Carruth1ff77242015-03-07 09:02:36 +0000317void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000318#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000319 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000320#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000321
322 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
323 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000324}
325
Chandler Carruth1ff77242015-03-07 09:02:36 +0000326void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000327#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000328 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000329#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000330
331 for (auto &C : FunctionAnalysisRegistrationCallbacks)
332 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000333}
334
Justin Bognereecc3c82016-02-25 07:23:08 +0000335void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000336#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000337 LAM.registerPass([&] { return CREATE_PASS; });
338#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000339
340 for (auto &C : LoopAnalysisRegistrationCallbacks)
341 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000342}
343
Chandler Carruthe3f50642016-12-22 06:59:15 +0000344FunctionPassManager
345PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000346 ThinLTOPhase Phase,
347 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000348 assert(Level != O0 && "Must request optimizations!");
349 FunctionPassManager FPM(DebugLogging);
350
351 // Form SSA out of local memory accesses after breaking apart aggregates into
352 // scalars.
353 FPM.addPass(SROA());
354
355 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000356 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000357
Davide Italianoc3688312017-06-01 23:08:14 +0000358 // Hoisting of scalars and load expressions.
359 if (EnableGVNHoist)
360 FPM.addPass(GVNHoistPass());
361
Davide Italianobe1b6a92017-06-03 23:18:29 +0000362 // Global value numbering based sinking.
363 if (EnableGVNSink) {
364 FPM.addPass(GVNSinkPass());
365 FPM.addPass(SimplifyCFGPass());
366 }
367
Chandler Carruthe3f50642016-12-22 06:59:15 +0000368 // Speculative execution if the target has divergent branches; otherwise nop.
369 FPM.addPass(SpeculativeExecutionPass());
370
371 // Optimize based on known information about branches, and cleanup afterward.
372 FPM.addPass(JumpThreadingPass());
373 FPM.addPass(CorrelatedValuePropagationPass());
374 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000375 if (Level == O3)
376 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000377 FPM.addPass(InstCombinePass());
378
379 if (!isOptimizingForSize(Level))
380 FPM.addPass(LibCallsShrinkWrapPass());
381
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000382 invokePeepholeEPCallbacks(FPM, Level);
383
Rong Xue1f42452017-10-23 22:21:29 +0000384 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
385 // using the size value profile. Don't perform this when optimizing for size.
386 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
387 !isOptimizingForSize(Level))
388 FPM.addPass(PGOMemOPSizeOpt());
389
Chandler Carruthe3f50642016-12-22 06:59:15 +0000390 FPM.addPass(TailCallElimPass());
391 FPM.addPass(SimplifyCFGPass());
392
393 // Form canonically associated expression trees, and simplify the trees using
394 // basic mathematical properties. For example, this will form (nearly)
395 // minimal multiplication trees.
396 FPM.addPass(ReassociatePass());
397
398 // Add the primary loop simplification pipeline.
399 // FIXME: Currently this is split into two loop pass pipelines because we run
Chandler Carruth71fd2702018-05-30 02:46:45 +0000400 // some function passes in between them. These can and should be removed
401 // and/or replaced by scheduling the loop pass equivalents in the correct
402 // positions. But those equivalent passes aren't powerful enough yet.
403 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
404 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
405 // fully replace `SimplifyCFGPass`, and the closest to the other we have is
406 // `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000407 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
408
Chandler Carruth71fd2702018-05-30 02:46:45 +0000409 // Simplify the loop body. We do this initially to clean up after other loop
410 // passes run, either when iterating on a loop or on inner loops with
411 // implications on the outer loop.
412 LPM1.addPass(LoopInstSimplifyPass());
413 LPM1.addPass(LoopSimplifyCFGPass());
414
Chandler Carruthe3f50642016-12-22 06:59:15 +0000415 // Rotate Loop - disable header duplication at -Oz
416 LPM1.addPass(LoopRotatePass(Level != Oz));
417 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000418 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000419 LPM2.addPass(IndVarSimplifyPass());
420 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000421
422 for (auto &C : LateLoopOptimizationsEPCallbacks)
423 C(LPM2, Level);
424
Chandler Carruth79b733b2017-01-26 23:21:17 +0000425 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000426 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000427 // because it changes IR to makes profile annotation in back compile
428 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000429 if (Phase != ThinLTOPhase::PreLink ||
430 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000431 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000432
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000433 for (auto &C : LoopOptimizerEndEPCallbacks)
434 C(LPM2, Level);
435
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000436 // We provide the opt remark emitter pass for LICM to use. We only need to do
437 // this once as it is immutable.
438 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000439 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000440 FPM.addPass(SimplifyCFGPass());
441 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000442 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000443
444 // Eliminate redundancies.
445 if (Level != O1) {
446 // These passes add substantial compile time so skip them at O1.
447 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000448 if (RunNewGVN)
449 FPM.addPass(NewGVNPass());
450 else
451 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000452 }
453
454 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
455 FPM.addPass(MemCpyOptPass());
456
457 // Sparse conditional constant propagation.
458 // FIXME: It isn't clear why we do this *after* loop passes rather than
459 // before...
460 FPM.addPass(SCCPPass());
461
462 // Delete dead bit computations (instcombine runs after to fold away the dead
463 // computations, and then ADCE will run later to exploit any new DCE
464 // opportunities that creates).
465 FPM.addPass(BDCEPass());
466
467 // Run instcombine after redundancy and dead bit elimination to exploit
468 // opportunities opened up by them.
469 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000470 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000471
472 // Re-consider control flow based optimizations after redundancy elimination,
473 // redo DCE, etc.
474 FPM.addPass(JumpThreadingPass());
475 FPM.addPass(CorrelatedValuePropagationPass());
476 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000477 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000478
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000479 for (auto &C : ScalarOptimizerLateEPCallbacks)
480 C(FPM, Level);
481
Chandler Carruthe3f50642016-12-22 06:59:15 +0000482 // Finally, do an expensive DCE pass to catch all the dead code exposed by
483 // the simplifications and basic cleanup after all the simplifications.
484 FPM.addPass(ADCEPass());
485 FPM.addPass(SimplifyCFGPass());
486 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000487 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000488
489 return FPM;
490}
491
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000492void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
493 PassBuilder::OptimizationLevel Level,
494 bool RunProfileGen,
495 std::string ProfileGenFile,
496 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000497 // Generally running simplification passes and the inliner with an high
498 // threshold results in smaller executables, but there may be cases where
499 // the size grows, so let's be conservative here and skip this simplification
500 // at -Os/Oz.
501 if (!isOptimizingForSize(Level)) {
502 InlineParams IP;
503
504 // In the old pass manager, this is a cl::opt. Should still this be one?
505 IP.DefaultThreshold = 75;
506
507 // FIXME: The hint threshold has the same value used by the regular inliner.
508 // This should probably be lowered after performance testing.
509 // FIXME: this comment is cargo culted from the old pass manager, revisit).
510 IP.HintThreshold = 325;
511
512 CGSCCPassManager CGPipeline(DebugLogging);
513
514 CGPipeline.addPass(InlinerPass(IP));
515
516 FunctionPassManager FPM;
517 FPM.addPass(SROA());
518 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
519 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
520 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000521 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000522
Davide Italiano513dfaa2017-02-13 15:26:22 +0000523 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
524
525 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
526 }
527
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000528 // Delete anything that is now dead to make sure that we don't instrument
529 // dead code. Instrumentation can end up keeping dead code around and
530 // dramatically increase code size.
531 MPM.addPass(GlobalDCEPass());
532
Davide Italiano513dfaa2017-02-13 15:26:22 +0000533 if (RunProfileGen) {
534 MPM.addPass(PGOInstrumentationGen());
535
Xinliang David Lib67530e2017-06-25 00:26:43 +0000536 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000537 FPM.addPass(
538 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000539 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
540
Davide Italiano513dfaa2017-02-13 15:26:22 +0000541 // Add the profile lowering pass.
542 InstrProfOptions Options;
543 if (!ProfileGenFile.empty())
544 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000545 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000546 MPM.addPass(InstrProfiling(Options));
547 }
548
549 if (!ProfileUseFile.empty())
550 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
551}
552
Easwaran Raman8249fac2017-06-28 13:33:49 +0000553static InlineParams
554getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
555 auto O3 = PassBuilder::O3;
556 unsigned OptLevel = Level > O3 ? 2 : Level;
557 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
558 return getInlineParams(OptLevel, SizeLevel);
559}
560
Chandler Carruthe3f50642016-12-22 06:59:15 +0000561ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000562PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000563 ThinLTOPhase Phase,
564 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000565 ModulePassManager MPM(DebugLogging);
566
Chandler Carruthe3f50642016-12-22 06:59:15 +0000567 // Do basic inference of function attributes from known properties of system
568 // libraries and other oracles.
569 MPM.addPass(InferFunctionAttrsPass());
570
571 // Create an early function pass manager to cleanup the output of the
572 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000573 FunctionPassManager EarlyFPM(DebugLogging);
574 EarlyFPM.addPass(SimplifyCFGPass());
575 EarlyFPM.addPass(SROA());
576 EarlyFPM.addPass(EarlyCSEPass());
577 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000578 if (Level == O3)
579 EarlyFPM.addPass(CallSiteSplittingPass());
580
Dehao Chen08f88312017-08-07 20:23:20 +0000581 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
582 // to convert bitcast to direct calls so that they can be inlined during the
583 // profile annotation prepration step.
584 // More details about SamplePGO design can be found in:
585 // https://research.google.com/pubs/pub45290.html
586 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
587 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
588 Phase == ThinLTOPhase::PostLink)
589 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000590 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000591
Dehao Chen08f88312017-08-07 20:23:20 +0000592 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
593 // Annotate sample profile right after early FPM to ensure freshness of
594 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000595 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
596 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000597 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
598 // for the profile annotation to be accurate in the ThinLTO backend.
599 if (Phase != ThinLTOPhase::PreLink)
600 // We perform early indirect call promotion here, before globalopt.
601 // This is important for the ThinLTO backend phase because otherwise
602 // imported available_externally functions look unreferenced and are
603 // removed.
604 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
605 true));
606 }
607
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000608 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000609 // and prior to optimizing globals.
610 // FIXME: This position in the pipeline hasn't been carefully considered in
611 // years, it should be re-analyzed.
612 MPM.addPass(IPSCCPPass());
613
Matthew Simpsoncb585582017-10-25 13:40:08 +0000614 // Attach metadata to indirect call sites indicating the set of functions
615 // they may target at run-time. This should follow IPSCCP.
616 MPM.addPass(CalledValuePropagationPass());
617
Chandler Carruthe3f50642016-12-22 06:59:15 +0000618 // Optimize globals to try and fold them into constants.
619 MPM.addPass(GlobalOptPass());
620
621 // Promote any localized globals to SSA registers.
622 // FIXME: Should this instead by a run of SROA?
623 // FIXME: We should probably run instcombine and simplify-cfg afterward to
624 // delete control flows that are dead once globals have been folded to
625 // constants.
626 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
627
628 // Remove any dead arguments exposed by cleanups and constand folding
629 // globals.
630 MPM.addPass(DeadArgumentEliminationPass());
631
632 // Create a small function pass pipeline to cleanup after all the global
633 // optimizations.
634 FunctionPassManager GlobalCleanupPM(DebugLogging);
635 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000636 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
637
Chandler Carruthe3f50642016-12-22 06:59:15 +0000638 GlobalCleanupPM.addPass(SimplifyCFGPass());
639 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
640
Dehao Chen89d32262017-08-02 01:28:31 +0000641 // Add all the requested passes for instrumentation PGO, if requested.
642 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
643 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
644 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
645 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
646 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000647 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000648
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000649 // Synthesize function entry counts for non-PGO compilation.
650 if (EnableSyntheticCounts && !PGOOpt)
651 MPM.addPass(SyntheticCountsPropagation());
652
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000653 // Require the GlobalsAA analysis for the module so we can query it within
654 // the CGSCC pipeline.
655 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000656
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000657 // Require the ProfileSummaryAnalysis for the module so we can query it within
658 // the inliner pass.
659 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
660
Chandler Carruthe3f50642016-12-22 06:59:15 +0000661 // Now begin the main postorder CGSCC pipeline.
662 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
663 // manager and trying to emulate its precise behavior. Much of this doesn't
664 // make a lot of sense and we should revisit the core CGSCC structure.
665 CGSCCPassManager MainCGPipeline(DebugLogging);
666
667 // Note: historically, the PruneEH pass was run first to deduce nounwind and
668 // generally clean up exception handling overhead. It isn't clear this is
669 // valuable as the inliner doesn't currently care whether it is inlining an
670 // invoke or a call.
671
672 // Run the inliner first. The theory is that we are walking bottom-up and so
673 // the callees have already been fully optimized, and we want to inline them
674 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000675 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000676 // because it makes profile annotation in the backend inaccurate.
677 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000678 if (Phase == ThinLTOPhase::PreLink &&
679 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000680 IP.HotCallSiteThreshold = 0;
681 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000682
683 // Now deduce any function attributes based in the current code.
684 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
685
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000686 // When at O3 add argument promotion to the pass pipeline.
687 // FIXME: It isn't at all clear why this should be limited to O3.
688 if (Level == O3)
689 MainCGPipeline.addPass(ArgumentPromotionPass());
690
Chandler Carruthe3f50642016-12-22 06:59:15 +0000691 // Lastly, add the core function simplification pipeline nested inside the
692 // CGSCC walk.
693 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000694 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000695
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000696 for (auto &C : CGSCCOptimizerLateEPCallbacks)
697 C(MainCGPipeline, Level);
698
Chandler Carruth719ffe12017-02-12 05:38:04 +0000699 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
700 // to detect when we devirtualize indirect calls and iterate the SCC passes
701 // in that case to try and catch knock-on inlining or function attrs
702 // opportunities. Then we add it to the module pipeline by walking the SCCs
703 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000704 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000705 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000706 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000707
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000708 return MPM;
709}
710
711ModulePassManager
712PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
713 bool DebugLogging) {
714 ModulePassManager MPM(DebugLogging);
715
716 // Optimize globals now that the module is fully simplified.
717 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000718 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000719
Xinliang David Li126157c2017-05-22 16:41:57 +0000720 // Run partial inlining pass to partially inline functions that have
721 // large bodies.
722 if (RunPartialInlining)
723 MPM.addPass(PartialInlinerPass());
724
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000725 // Remove avail extern fns and globals definitions since we aren't compiling
726 // an object file for later LTO. For LTO we want to preserve these so they
727 // are eligible for inlining at link-time. Note if they are unreferenced they
728 // will be removed by GlobalDCE later, so this only impacts referenced
729 // available externally globals. Eventually they will be suppressed during
730 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
731 // may make globals referenced by available external functions dead and saves
732 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000733 MPM.addPass(EliminateAvailableExternallyPass());
734
735 // Do RPO function attribute inference across the module to forward-propagate
736 // attributes where applicable.
737 // FIXME: Is this really an optimization rather than a canonicalization?
738 MPM.addPass(ReversePostOrderFunctionAttrsPass());
739
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000740 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000741 // useful as the above will have inlined, DCE'ed, and function-attr
742 // propagated everything. We should at this point have a reasonably minimal
743 // and richly annotated call graph. By computing aliasing and mod/ref
744 // information for all local globals here, the late loop passes and notably
745 // the vectorizer will be able to use them to help recognize vectorizable
746 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000747 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000748
749 FunctionPassManager OptimizePM(DebugLogging);
750 OptimizePM.addPass(Float2IntPass());
751 // FIXME: We need to run some loop optimizations to re-rotate loops after
752 // simplify-cfg and others undo their rotation.
753
754 // Optimize the loop execution. These passes operate on entire loop nests
755 // rather than on each loop in an inside-out manner, and so they are actually
756 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000757
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000758 for (auto &C : VectorizerStartEPCallbacks)
759 C(OptimizePM, Level);
760
Chandler Carrutha95ff382017-01-27 00:50:21 +0000761 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000762 OptimizePM.addPass(
763 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000764
765 // Distribute loops to allow partial vectorization. I.e. isolate dependences
766 // into separate loop that would otherwise inhibit vectorization. This is
767 // currently only performed for loops marked with the metadata
768 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000769 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000770
771 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000772 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000773
Chandler Carruthbaabda92017-01-27 01:32:26 +0000774 // Eliminate loads by forwarding stores from the previous iteration to loads
775 // of the current iteration.
776 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000777
778 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000779 OptimizePM.addPass(InstCombinePass());
780
Chandler Carrutha95ff382017-01-27 00:50:21 +0000781 // Now that we've formed fast to execute loop structures, we do further
782 // optimizations. These are run afterward as they might block doing complex
783 // analyses and transforms such as what are needed for loop vectorization.
784
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000785 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
786 // GVN, loop transforms, and others have already run, so it's now better to
787 // convert to more optimized IR using more aggressive simplify CFG options.
788 // The extra sinking transform can create larger basic blocks, so do this
789 // before SLP vectorization.
790 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
791 forwardSwitchCondToPhi(true).
792 convertSwitchToLookupTable(true).
793 needCanonicalLoops(false).
794 sinkCommonInsts(true)));
795
Chandler Carruthe3f50642016-12-22 06:59:15 +0000796 // Optimize parallel scalar instruction chains into SIMD instructions.
797 OptimizePM.addPass(SLPVectorizerPass());
798
Chandler Carruthe3f50642016-12-22 06:59:15 +0000799 OptimizePM.addPass(InstCombinePass());
800
801 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000802 // execution resources of an out-of-order processor. We also then need to
803 // clean up redundancies and loop invariant code.
804 // FIXME: It would be really good to use a loop-integrated instruction
805 // combiner for cleanup here so that the unrolling and LICM can be pipelined
806 // across the loop nests.
David Green963401d2018-07-01 12:47:30 +0000807 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
808 if (EnableUnrollAndJam) {
809 OptimizePM.addPass(
810 createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
811 }
Teresa Johnsonecd90132017-08-02 20:35:29 +0000812 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000813 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000814 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000815 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000816
817 // Now that we've vectorized and unrolled loops, we may have more refined
818 // alignment information, try to re-derive it here.
819 OptimizePM.addPass(AlignmentFromAssumptionsPass());
820
Chandler Carrutha95ff382017-01-27 00:50:21 +0000821 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
822 // canonicalization pass that enables other optimizations. As a result,
823 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
824 // result too early.
825 OptimizePM.addPass(LoopSinkPass());
826
827 // And finally clean up LCSSA form before generating code.
Chandler Carruth7c557f82018-06-29 23:36:03 +0000828 OptimizePM.addPass(InstSimplifyPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000829
Sanjay Patel6fd43912017-09-09 13:38:18 +0000830 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
831 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
832 // flattening of blocks.
833 OptimizePM.addPass(DivRemPairsPass());
834
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000835 // LoopSink (and other loop passes since the last simplifyCFG) might have
836 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
837 OptimizePM.addPass(SimplifyCFGPass());
838
Chandler Carruthc34f7892017-11-28 11:32:31 +0000839 // Optimize PHIs by speculating around them when profitable. Note that this
840 // pass needs to be run after any PRE or similar pass as it is essentially
841 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
842 OptimizePM.addPass(SpeculateAroundPHIsPass());
843
Chandler Carrutha95ff382017-01-27 00:50:21 +0000844 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000845 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
846
Michael J. Spencer7bb27672018-07-16 00:28:24 +0000847 MPM.addPass(CGProfilePass());
848
Chandler Carruthe3f50642016-12-22 06:59:15 +0000849 // Now we need to do some global optimization transforms.
850 // FIXME: It would seem like these should come first in the optimization
851 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
852 // ordering here.
853 MPM.addPass(GlobalDCEPass());
854 MPM.addPass(ConstantMergePass());
855
856 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000857}
858
Chandler Carruthe3f50642016-12-22 06:59:15 +0000859ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000860PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
861 bool DebugLogging) {
862 assert(Level != O0 && "Must request optimizations for the default pipeline!");
863
864 ModulePassManager MPM(DebugLogging);
865
866 // Force any function attributes we want the rest of the pipeline to observe.
867 MPM.addPass(ForceFunctionAttrsPass());
868
David Blaikie0c64f5a2018-01-23 01:25:20 +0000869 // Apply module pipeline start EP callback.
870 for (auto &C : PipelineStartEPCallbacks)
871 C(MPM);
872
Dehao Chen08f88312017-08-07 20:23:20 +0000873 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000874 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
875
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000876 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000877 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
878 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000879
880 // Now add the optimization pipeline.
881 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
882
883 return MPM;
884}
885
886ModulePassManager
887PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
888 bool DebugLogging) {
889 assert(Level != O0 && "Must request optimizations for the default pipeline!");
890
891 ModulePassManager MPM(DebugLogging);
892
893 // Force any function attributes we want the rest of the pipeline to observe.
894 MPM.addPass(ForceFunctionAttrsPass());
895
Dehao Chen08f88312017-08-07 20:23:20 +0000896 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000897 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
898
David Blaikie0c64f5a2018-01-23 01:25:20 +0000899 // Apply module pipeline start EP callback.
900 for (auto &C : PipelineStartEPCallbacks)
901 C(MPM);
902
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000903 // If we are planning to perform ThinLTO later, we don't bloat the code with
904 // unrolling/vectorization/... now. Just simplify the module as much as we
905 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000906 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
907 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000908
909 // Run partial inlining pass to partially inline functions that have
910 // large bodies.
911 // FIXME: It isn't clear whether this is really the right place to run this
912 // in ThinLTO. Because there is another canonicalization and simplification
913 // phase that will run after the thin link, running this here ends up with
914 // less information than will be available later and it may grow functions in
915 // ways that aren't beneficial.
916 if (RunPartialInlining)
917 MPM.addPass(PartialInlinerPass());
918
919 // Reduce the size of the IR as much as possible.
920 MPM.addPass(GlobalOptPass());
921
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000922 return MPM;
923}
924
Teresa Johnson28023db2018-07-19 14:51:32 +0000925ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
926 OptimizationLevel Level, bool DebugLogging,
927 const ModuleSummaryIndex *ImportSummary) {
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000928 ModulePassManager MPM(DebugLogging);
929
Teresa Johnson28023db2018-07-19 14:51:32 +0000930 if (ImportSummary) {
931 // These passes import type identifier resolutions for whole-program
932 // devirtualization and CFI. They must run early because other passes may
933 // disturb the specific instruction patterns that these passes look for,
934 // creating dependencies on resolutions that may not appear in the summary.
935 //
936 // For example, GVN may transform the pattern assume(type.test) appearing in
937 // two basic blocks into assume(phi(type.test, type.test)), which would
938 // transform a dependency on a WPD resolution into a dependency on a type
939 // identifier resolution for CFI.
940 //
941 // Also, WPD has access to more precise information than ICP and can
942 // devirtualize more effectively, so it should operate on the IR first.
943 MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
944 MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
945 }
946
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000947 // Force any function attributes we want the rest of the pipeline to observe.
948 MPM.addPass(ForceFunctionAttrsPass());
949
950 // During the ThinLTO backend phase we perform early indirect call promotion
951 // here, before globalopt. Otherwise imported available_externally functions
952 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000953 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
954 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000955 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000956 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
957 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000958
959 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000960 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
961 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000962
963 // Now add the optimization pipeline.
964 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
965
966 return MPM;
967}
968
969ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000970PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
971 bool DebugLogging) {
972 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000973 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000974 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000975}
976
Teresa Johnson28023db2018-07-19 14:51:32 +0000977ModulePassManager
978PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
979 ModuleSummaryIndex *ExportSummary) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000980 assert(Level != O0 && "Must request optimizations for the default pipeline!");
981 ModulePassManager MPM(DebugLogging);
982
Davide Italiano089a9122017-01-24 00:57:39 +0000983 // Remove unused virtual tables to improve the quality of code generated by
984 // whole-program devirtualization and bitset lowering.
985 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000986
Davide Italiano089a9122017-01-24 00:57:39 +0000987 // Force any function attributes we want the rest of the pipeline to observe.
988 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000989
Davide Italiano089a9122017-01-24 00:57:39 +0000990 // Do basic inference of function attributes from known properties of system
991 // libraries and other oracles.
992 MPM.addPass(InferFunctionAttrsPass());
993
994 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000995 FunctionPassManager EarlyFPM(DebugLogging);
996 EarlyFPM.addPass(CallSiteSplittingPass());
997 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
998
Davide Italiano089a9122017-01-24 00:57:39 +0000999 // Indirect call promotion. This should promote all the targets that are
1000 // left by the earlier promotion pass that promotes intra-module targets.
1001 // This two-step promotion is to save the compile time. For LTO, it should
1002 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +00001003 MPM.addPass(PGOIndirectCallPromotion(
1004 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +00001005 // Propagate constants at call sites into the functions they call. This
1006 // opens opportunities for globalopt (and inlining) by substituting function
1007 // pointers passed as arguments to direct uses of functions.
1008 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +00001009
1010 // Attach metadata to indirect call sites indicating the set of functions
1011 // they may target at run-time. This should follow IPSCCP.
1012 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +00001013 }
1014
1015 // Now deduce any function attributes based in the current code.
1016 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1017 PostOrderFunctionAttrsPass()));
1018
1019 // Do RPO function attribute inference across the module to forward-propagate
1020 // attributes where applicable.
1021 // FIXME: Is this really an optimization rather than a canonicalization?
1022 MPM.addPass(ReversePostOrderFunctionAttrsPass());
1023
1024 // Use inragne annotations on GEP indices to split globals where beneficial.
1025 MPM.addPass(GlobalSplitPass());
1026
1027 // Run whole program optimization of virtual call when the list of callees
1028 // is fixed.
Teresa Johnson28023db2018-07-19 14:51:32 +00001029 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001030
1031 // Stop here at -O1.
Teresa Johnson28023db2018-07-19 14:51:32 +00001032 if (Level == 1) {
1033 // The LowerTypeTestsPass needs to run to lower type metadata and the
1034 // type.test intrinsics. The pass does nothing if CFI is disabled.
1035 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001036 return MPM;
Teresa Johnson28023db2018-07-19 14:51:32 +00001037 }
Davide Italiano089a9122017-01-24 00:57:39 +00001038
1039 // Optimize globals to try and fold them into constants.
1040 MPM.addPass(GlobalOptPass());
1041
1042 // Promote any localized globals to SSA registers.
1043 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1044
1045 // Linking modules together can lead to duplicate global constant, only
1046 // keep one copy of each constant.
1047 MPM.addPass(ConstantMergePass());
1048
1049 // Remove unused arguments from functions.
1050 MPM.addPass(DeadArgumentEliminationPass());
1051
1052 // Reduce the code after globalopt and ipsccp. Both can open up significant
1053 // simplification opportunities, and both can propagate functions through
1054 // function pointers. When this happens, we often have to resolve varargs
1055 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001056 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001057 if (Level == O3)
1058 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001059 PeepholeFPM.addPass(InstCombinePass());
1060 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1061
1062 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001063
1064 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1065 // generally clean up exception handling overhead. It isn't clear this is
1066 // valuable as the inliner doesn't currently care whether it is inlining an
1067 // invoke or a call.
1068 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001069 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1070 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001071
1072 // Optimize globals again after we ran the inliner.
1073 MPM.addPass(GlobalOptPass());
1074
1075 // Garbage collect dead functions.
1076 // FIXME: Add ArgumentPromotion pass after once it's ported.
1077 MPM.addPass(GlobalDCEPass());
1078
1079 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001080 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001081 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001082 invokePeepholeEPCallbacks(FPM, Level);
1083
Davide Italiano089a9122017-01-24 00:57:39 +00001084 FPM.addPass(JumpThreadingPass());
1085
1086 // Break up allocas
1087 FPM.addPass(SROA());
1088
1089 // Run a few AA driver optimizations here and now to cleanup the code.
1090 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1091
1092 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1093 PostOrderFunctionAttrsPass()));
1094 // FIXME: here we run IP alias analysis in the legacy PM.
1095
1096 FunctionPassManager MainFPM;
1097
1098 // FIXME: once we fix LoopPass Manager, add LICM here.
1099 // FIXME: once we provide support for enabling MLSM, add it here.
1100 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001101 if (RunNewGVN)
1102 MainFPM.addPass(NewGVNPass());
1103 else
1104 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001105
1106 // Remove dead memcpy()'s.
1107 MainFPM.addPass(MemCpyOptPass());
1108
1109 // Nuke dead stores.
1110 MainFPM.addPass(DSEPass());
1111
1112 // FIXME: at this point, we run a bunch of loop passes:
1113 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1114 // loopVectorize. Enable them once the remaining issue with LPM
1115 // are sorted out.
1116
1117 MainFPM.addPass(InstCombinePass());
1118 MainFPM.addPass(SimplifyCFGPass());
1119 MainFPM.addPass(SCCPPass());
1120 MainFPM.addPass(InstCombinePass());
1121 MainFPM.addPass(BDCEPass());
1122
1123 // FIXME: We may want to run SLPVectorizer here.
1124 // After vectorization, assume intrinsics may tell us more
1125 // about pointer alignments.
1126#if 0
1127 MainFPM.add(AlignmentFromAssumptionsPass());
1128#endif
1129
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001130 // FIXME: Conditionally run LoadCombine here, after it's ported
1131 // (in case we still have this pass, given its questionable usefulness).
1132
Davide Italiano089a9122017-01-24 00:57:39 +00001133 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001134 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001135 MainFPM.addPass(JumpThreadingPass());
1136 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1137
1138 // Create a function that performs CFI checks for cross-DSO calls with
1139 // targets in the current module.
1140 MPM.addPass(CrossDSOCFIPass());
1141
1142 // Lower type metadata and the type.test intrinsic. This pass supports
1143 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1144 // to be run at link time if CFI is enabled. This pass does nothing if
1145 // CFI is disabled.
Teresa Johnson28023db2018-07-19 14:51:32 +00001146 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001147
1148 // Add late LTO optimization passes.
1149 // Delete basic blocks, which optimization passes may have killed.
1150 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1151
1152 // Drop bodies of available eternally objects to improve GlobalDCE.
1153 MPM.addPass(EliminateAvailableExternallyPass());
1154
1155 // Now that we have optimized the program, discard unreachable functions.
1156 MPM.addPass(GlobalDCEPass());
1157
1158 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001159 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001160}
1161
Chandler Carruth060ad612016-12-23 20:38:19 +00001162AAManager PassBuilder::buildDefaultAAPipeline() {
1163 AAManager AA;
1164
1165 // The order in which these are registered determines their priority when
1166 // being queried.
1167
1168 // First we register the basic alias analysis that provides the majority of
1169 // per-function local AA logic. This is a stateless, on-demand local set of
1170 // AA techniques.
1171 AA.registerFunctionAnalysis<BasicAA>();
1172
1173 // Next we query fast, specialized alias analyses that wrap IR-embedded
1174 // information about aliasing.
1175 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1176 AA.registerFunctionAnalysis<TypeBasedAA>();
1177
1178 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001179 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1180 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001181 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001182 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001183
1184 return AA;
1185}
1186
Chandler Carruth241bf242016-08-03 07:44:48 +00001187static Optional<int> parseRepeatPassName(StringRef Name) {
1188 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1189 return None;
1190 int Count;
1191 if (Name.getAsInteger(0, Count) || Count <= 0)
1192 return None;
1193 return Count;
1194}
1195
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001196static Optional<int> parseDevirtPassName(StringRef Name) {
1197 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1198 return None;
1199 int Count;
1200 if (Name.getAsInteger(0, Count) || Count <= 0)
1201 return None;
1202 return Count;
1203}
1204
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001205/// Tests whether a pass name starts with a valid prefix for a default pipeline
1206/// alias.
1207static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1208 return Name.startswith("default") || Name.startswith("thinlto") ||
1209 Name.startswith("lto");
1210}
1211
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001212/// Tests whether registered callbacks will accept a given pass name.
1213///
1214/// When parsing a pipeline text, the type of the outermost pipeline may be
1215/// omitted, in which case the type is automatically determined from the first
1216/// pass name in the text. This may be a name that is handled through one of the
1217/// callbacks. We check this through the oridinary parsing callbacks by setting
1218/// up a dummy PassManager in order to not force the client to also handle this
1219/// type of query.
1220template <typename PassManagerT, typename CallbacksT>
1221static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1222 if (!Callbacks.empty()) {
1223 PassManagerT DummyPM;
1224 for (auto &CB : Callbacks)
1225 if (CB(Name, DummyPM, {}))
1226 return true;
1227 }
1228 return false;
1229}
1230
1231template <typename CallbacksT>
1232static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001233 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001234 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001235 return DefaultAliasRegex.match(Name);
1236
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001237 // Explicitly handle pass manager names.
1238 if (Name == "module")
1239 return true;
1240 if (Name == "cgscc")
1241 return true;
1242 if (Name == "function")
1243 return true;
1244
Chandler Carruth241bf242016-08-03 07:44:48 +00001245 // Explicitly handle custom-parsed pass names.
1246 if (parseRepeatPassName(Name))
1247 return true;
1248
Chandler Carruth74a8a222016-06-17 07:15:29 +00001249#define MODULE_PASS(NAME, CREATE_PASS) \
1250 if (Name == NAME) \
1251 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001252#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001253 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001254 return true;
1255#include "PassRegistry.def"
1256
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001257 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001258}
1259
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001260template <typename CallbacksT>
1261static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001262 // Explicitly handle pass manager names.
1263 if (Name == "cgscc")
1264 return true;
1265 if (Name == "function")
1266 return true;
1267
Chandler Carruth241bf242016-08-03 07:44:48 +00001268 // Explicitly handle custom-parsed pass names.
1269 if (parseRepeatPassName(Name))
1270 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001271 if (parseDevirtPassName(Name))
1272 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001273
Chandler Carruth74a8a222016-06-17 07:15:29 +00001274#define CGSCC_PASS(NAME, CREATE_PASS) \
1275 if (Name == NAME) \
1276 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001277#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001278 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001279 return true;
1280#include "PassRegistry.def"
1281
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001282 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001283}
1284
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001285template <typename CallbacksT>
1286static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001287 // Explicitly handle pass manager names.
1288 if (Name == "function")
1289 return true;
1290 if (Name == "loop")
1291 return true;
1292
Chandler Carruth241bf242016-08-03 07:44:48 +00001293 // Explicitly handle custom-parsed pass names.
1294 if (parseRepeatPassName(Name))
1295 return true;
1296
Chandler Carruth74a8a222016-06-17 07:15:29 +00001297#define FUNCTION_PASS(NAME, CREATE_PASS) \
1298 if (Name == NAME) \
1299 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001300#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001301 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001302 return true;
1303#include "PassRegistry.def"
1304
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001305 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001306}
1307
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001308template <typename CallbacksT>
1309static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001310 // Explicitly handle pass manager names.
1311 if (Name == "loop")
1312 return true;
1313
Chandler Carruth241bf242016-08-03 07:44:48 +00001314 // Explicitly handle custom-parsed pass names.
1315 if (parseRepeatPassName(Name))
1316 return true;
1317
Chandler Carruth74a8a222016-06-17 07:15:29 +00001318#define LOOP_PASS(NAME, CREATE_PASS) \
1319 if (Name == NAME) \
1320 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001321#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1322 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1323 return true;
1324#include "PassRegistry.def"
1325
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001326 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001327}
1328
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001329Optional<std::vector<PassBuilder::PipelineElement>>
1330PassBuilder::parsePipelineText(StringRef Text) {
1331 std::vector<PipelineElement> ResultPipeline;
1332
1333 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1334 &ResultPipeline};
1335 for (;;) {
1336 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1337 size_t Pos = Text.find_first_of(",()");
1338 Pipeline.push_back({Text.substr(0, Pos), {}});
1339
1340 // If we have a single terminating name, we're done.
1341 if (Pos == Text.npos)
1342 break;
1343
1344 char Sep = Text[Pos];
1345 Text = Text.substr(Pos + 1);
1346 if (Sep == ',')
1347 // Just a name ending in a comma, continue.
1348 continue;
1349
1350 if (Sep == '(') {
1351 // Push the inner pipeline onto the stack to continue processing.
1352 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1353 continue;
1354 }
1355
1356 assert(Sep == ')' && "Bogus separator!");
1357 // When handling the close parenthesis, we greedily consume them to avoid
1358 // empty strings in the pipeline.
1359 do {
1360 // If we try to pop the outer pipeline we have unbalanced parentheses.
1361 if (PipelineStack.size() == 1)
1362 return None;
1363
1364 PipelineStack.pop_back();
1365 } while (Text.consume_front(")"));
1366
1367 // Check if we've finished parsing.
1368 if (Text.empty())
1369 break;
1370
1371 // Otherwise, the end of an inner pipeline always has to be followed by
1372 // a comma, and then we can continue.
1373 if (!Text.consume_front(","))
1374 return None;
1375 }
1376
1377 if (PipelineStack.size() > 1)
1378 // Unbalanced paretheses.
1379 return None;
1380
1381 assert(PipelineStack.back() == &ResultPipeline &&
1382 "Wrong pipeline at the bottom of the stack!");
1383 return {std::move(ResultPipeline)};
1384}
1385
1386bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1387 const PipelineElement &E, bool VerifyEachPass,
1388 bool DebugLogging) {
1389 auto &Name = E.Name;
1390 auto &InnerPipeline = E.InnerPipeline;
1391
1392 // First handle complex passes like the pass managers which carry pipelines.
1393 if (!InnerPipeline.empty()) {
1394 if (Name == "module") {
1395 ModulePassManager NestedMPM(DebugLogging);
1396 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1397 DebugLogging))
1398 return false;
1399 MPM.addPass(std::move(NestedMPM));
1400 return true;
1401 }
1402 if (Name == "cgscc") {
1403 CGSCCPassManager CGPM(DebugLogging);
1404 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1405 DebugLogging))
1406 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001407 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001408 return true;
1409 }
1410 if (Name == "function") {
1411 FunctionPassManager FPM(DebugLogging);
1412 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1413 DebugLogging))
1414 return false;
1415 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1416 return true;
1417 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001418 if (auto Count = parseRepeatPassName(Name)) {
1419 ModulePassManager NestedMPM(DebugLogging);
1420 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1421 DebugLogging))
1422 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001423 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001424 return true;
1425 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001426
1427 for (auto &C : ModulePipelineParsingCallbacks)
1428 if (C(Name, MPM, InnerPipeline))
1429 return true;
1430
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001431 // Normal passes can't have pipelines.
1432 return false;
1433 }
1434
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001435 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001436 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001437 SmallVector<StringRef, 3> Matches;
1438 if (!DefaultAliasRegex.match(Name, &Matches))
1439 return false;
1440 assert(Matches.size() == 3 && "Must capture two matched strings!");
1441
Jordan Rosef85a95f2016-07-25 18:34:51 +00001442 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001443 .Case("O0", O0)
1444 .Case("O1", O1)
1445 .Case("O2", O2)
1446 .Case("O3", O3)
1447 .Case("Os", Os)
1448 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001449 if (L == O0)
1450 // At O0 we do nothing at all!
1451 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001452
1453 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001454 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001455 } else if (Matches[1] == "thinlto-pre-link") {
1456 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1457 } else if (Matches[1] == "thinlto") {
Teresa Johnson28023db2018-07-19 14:51:32 +00001458 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001459 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001460 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001461 } else {
1462 assert(Matches[1] == "lto" && "Not one of the matched options!");
Teresa Johnson28023db2018-07-19 14:51:32 +00001463 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001464 }
1465 return true;
1466 }
1467
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001468 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001469#define MODULE_PASS(NAME, CREATE_PASS) \
1470 if (Name == NAME) { \
1471 MPM.addPass(CREATE_PASS); \
1472 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001473 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001474#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1475 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001476 MPM.addPass( \
1477 RequireAnalysisPass< \
1478 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001479 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001480 } \
1481 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001482 MPM.addPass(InvalidateAnalysisPass< \
1483 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001484 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001485 }
1486#include "PassRegistry.def"
1487
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001488 for (auto &C : ModulePipelineParsingCallbacks)
1489 if (C(Name, MPM, InnerPipeline))
1490 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001491 return false;
1492}
1493
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001494bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1495 const PipelineElement &E, bool VerifyEachPass,
1496 bool DebugLogging) {
1497 auto &Name = E.Name;
1498 auto &InnerPipeline = E.InnerPipeline;
1499
1500 // First handle complex passes like the pass managers which carry pipelines.
1501 if (!InnerPipeline.empty()) {
1502 if (Name == "cgscc") {
1503 CGSCCPassManager NestedCGPM(DebugLogging);
1504 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1505 DebugLogging))
1506 return false;
1507 // Add the nested pass manager with the appropriate adaptor.
1508 CGPM.addPass(std::move(NestedCGPM));
1509 return true;
1510 }
1511 if (Name == "function") {
1512 FunctionPassManager FPM(DebugLogging);
1513 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1514 DebugLogging))
1515 return false;
1516 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001517 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001518 return true;
1519 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001520 if (auto Count = parseRepeatPassName(Name)) {
1521 CGSCCPassManager NestedCGPM(DebugLogging);
1522 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1523 DebugLogging))
1524 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001525 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001526 return true;
1527 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001528 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1529 CGSCCPassManager NestedCGPM(DebugLogging);
1530 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1531 DebugLogging))
1532 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001533 CGPM.addPass(
1534 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001535 return true;
1536 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001537
1538 for (auto &C : CGSCCPipelineParsingCallbacks)
1539 if (C(Name, CGPM, InnerPipeline))
1540 return true;
1541
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001542 // Normal passes can't have pipelines.
1543 return false;
1544 }
1545
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001546// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001547#define CGSCC_PASS(NAME, CREATE_PASS) \
1548 if (Name == NAME) { \
1549 CGPM.addPass(CREATE_PASS); \
1550 return true; \
1551 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001552#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1553 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001554 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001555 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001556 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1557 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001558 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001559 } \
1560 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001561 CGPM.addPass(InvalidateAnalysisPass< \
1562 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001563 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001564 }
1565#include "PassRegistry.def"
1566
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001567 for (auto &C : CGSCCPipelineParsingCallbacks)
1568 if (C(Name, CGPM, InnerPipeline))
1569 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001570 return false;
1571}
1572
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001573bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1574 const PipelineElement &E,
1575 bool VerifyEachPass, bool DebugLogging) {
1576 auto &Name = E.Name;
1577 auto &InnerPipeline = E.InnerPipeline;
1578
1579 // First handle complex passes like the pass managers which carry pipelines.
1580 if (!InnerPipeline.empty()) {
1581 if (Name == "function") {
1582 FunctionPassManager NestedFPM(DebugLogging);
1583 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1584 DebugLogging))
1585 return false;
1586 // Add the nested pass manager with the appropriate adaptor.
1587 FPM.addPass(std::move(NestedFPM));
1588 return true;
1589 }
1590 if (Name == "loop") {
1591 LoopPassManager LPM(DebugLogging);
1592 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1593 DebugLogging))
1594 return false;
1595 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001596 FPM.addPass(
1597 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001598 return true;
1599 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001600 if (auto Count = parseRepeatPassName(Name)) {
1601 FunctionPassManager NestedFPM(DebugLogging);
1602 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1603 DebugLogging))
1604 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001605 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001606 return true;
1607 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001608
1609 for (auto &C : FunctionPipelineParsingCallbacks)
1610 if (C(Name, FPM, InnerPipeline))
1611 return true;
1612
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001613 // Normal passes can't have pipelines.
1614 return false;
1615 }
1616
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001617// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001618#define FUNCTION_PASS(NAME, CREATE_PASS) \
1619 if (Name == NAME) { \
1620 FPM.addPass(CREATE_PASS); \
1621 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001622 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001623#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1624 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001625 FPM.addPass( \
1626 RequireAnalysisPass< \
1627 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001628 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001629 } \
1630 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001631 FPM.addPass(InvalidateAnalysisPass< \
1632 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001633 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001634 }
1635#include "PassRegistry.def"
1636
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001637 for (auto &C : FunctionPipelineParsingCallbacks)
1638 if (C(Name, FPM, InnerPipeline))
1639 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001640 return false;
1641}
1642
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001643bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001644 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001645 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001646 auto &InnerPipeline = E.InnerPipeline;
1647
1648 // First handle complex passes like the pass managers which carry pipelines.
1649 if (!InnerPipeline.empty()) {
1650 if (Name == "loop") {
1651 LoopPassManager NestedLPM(DebugLogging);
1652 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1653 DebugLogging))
1654 return false;
1655 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001656 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001657 return true;
1658 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001659 if (auto Count = parseRepeatPassName(Name)) {
1660 LoopPassManager NestedLPM(DebugLogging);
1661 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1662 DebugLogging))
1663 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001664 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001665 return true;
1666 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001667
1668 for (auto &C : LoopPipelineParsingCallbacks)
1669 if (C(Name, LPM, InnerPipeline))
1670 return true;
1671
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001672 // Normal passes can't have pipelines.
1673 return false;
1674 }
1675
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001676// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001677#define LOOP_PASS(NAME, CREATE_PASS) \
1678 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001679 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001680 return true; \
1681 }
1682#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1683 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001684 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001685 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1686 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1687 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001688 return true; \
1689 } \
1690 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001691 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001692 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001693 return true; \
1694 }
1695#include "PassRegistry.def"
1696
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001697 for (auto &C : LoopPipelineParsingCallbacks)
1698 if (C(Name, LPM, InnerPipeline))
1699 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001700 return false;
1701}
1702
Chandler Carruthedf59962016-02-18 09:45:17 +00001703bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001704#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1705 if (Name == NAME) { \
1706 AA.registerModuleAnalysis< \
1707 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1708 return true; \
1709 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001710#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1711 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001712 AA.registerFunctionAnalysis< \
1713 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001714 return true; \
1715 }
1716#include "PassRegistry.def"
1717
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001718 for (auto &C : AAParsingCallbacks)
1719 if (C(Name, AA))
1720 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001721 return false;
1722}
1723
Justin Bognereecc3c82016-02-25 07:23:08 +00001724bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001725 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001726 bool VerifyEachPass,
1727 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001728 for (const auto &Element : Pipeline) {
1729 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1730 return false;
1731 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001732 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001733 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001734}
1735
Chandler Carruth1ff77242015-03-07 09:02:36 +00001736bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001737 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001738 bool VerifyEachPass,
1739 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001740 for (const auto &Element : Pipeline) {
1741 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1742 return false;
1743 if (VerifyEachPass)
1744 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001745 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001746 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001747}
1748
Chandler Carruth1ff77242015-03-07 09:02:36 +00001749bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001750 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001751 bool VerifyEachPass,
1752 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001753 for (const auto &Element : Pipeline) {
1754 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1755 return false;
1756 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001757 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001758 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001759}
1760
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001761void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1762 FunctionAnalysisManager &FAM,
1763 CGSCCAnalysisManager &CGAM,
1764 ModuleAnalysisManager &MAM) {
1765 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1766 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001767 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1768 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1769 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1770 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1771 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1772}
1773
Chandler Carruth1ff77242015-03-07 09:02:36 +00001774bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001775 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001776 bool VerifyEachPass,
1777 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001778 for (const auto &Element : Pipeline) {
1779 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1780 return false;
1781 if (VerifyEachPass)
1782 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001783 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001784 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001785}
1786
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001787// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001788// FIXME: Should this routine accept a TargetMachine or require the caller to
1789// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001790bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1791 StringRef PipelineText, bool VerifyEachPass,
1792 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001793 auto Pipeline = parsePipelineText(PipelineText);
1794 if (!Pipeline || Pipeline->empty())
1795 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001796
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001797 // If the first name isn't at the module layer, wrap the pipeline up
1798 // automatically.
1799 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001800
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001801 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1802 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001803 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001804 } else if (isFunctionPassName(FirstName,
1805 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001806 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001807 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001808 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001809 } else {
1810 for (auto &C : TopLevelPipelineParsingCallbacks)
1811 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1812 return true;
1813
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001814 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001815 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001816 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001817 }
1818
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001819 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001820}
Chandler Carruthedf59962016-02-18 09:45:17 +00001821
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001822// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1823bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1824 StringRef PipelineText, bool VerifyEachPass,
1825 bool DebugLogging) {
1826 auto Pipeline = parsePipelineText(PipelineText);
1827 if (!Pipeline || Pipeline->empty())
1828 return false;
1829
1830 StringRef FirstName = Pipeline->front().Name;
1831 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1832 return false;
1833
1834 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1835}
1836
1837// Primary pass pipeline description parsing routine for a \c
1838// FunctionPassManager
1839bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1840 StringRef PipelineText, bool VerifyEachPass,
1841 bool DebugLogging) {
1842 auto Pipeline = parsePipelineText(PipelineText);
1843 if (!Pipeline || Pipeline->empty())
1844 return false;
1845
1846 StringRef FirstName = Pipeline->front().Name;
1847 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1848 return false;
1849
1850 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1851 DebugLogging);
1852}
1853
1854// Primary pass pipeline description parsing routine for a \c LoopPassManager
1855bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1856 StringRef PipelineText, bool VerifyEachPass,
1857 bool DebugLogging) {
1858 auto Pipeline = parsePipelineText(PipelineText);
1859 if (!Pipeline || Pipeline->empty())
1860 return false;
1861
1862 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1863}
1864
Chandler Carruthedf59962016-02-18 09:45:17 +00001865bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001866 // If the pipeline just consists of the word 'default' just replace the AA
1867 // manager with our default one.
1868 if (PipelineText == "default") {
1869 AA = buildDefaultAAPipeline();
1870 return true;
1871 }
1872
Chandler Carruthedf59962016-02-18 09:45:17 +00001873 while (!PipelineText.empty()) {
1874 StringRef Name;
1875 std::tie(Name, PipelineText) = PipelineText.split(',');
1876 if (!parseAAPassName(AA, Name))
1877 return false;
1878 }
1879
1880 return true;
1881}