blob: d7b9dfca34a964d508b33e9c20520a59e0289667 [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"
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +000090#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +000091#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
92#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000093#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000094#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000095#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000096#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000097#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000098#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000099#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +0000100#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +0000101#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +0000102#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000103#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000104#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000105#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000106#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000107#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000108#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000109#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +0000110#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
Sean Silva46590d52016-06-14 00:51:09 +0000111#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000112#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000113#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000114#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000115#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000116#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000117#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000118#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000119#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000120#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000121#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000122#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000123#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000124#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000125#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
David Green963401d2018-07-01 12:47:30 +0000126#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000127#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000128#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000129#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000130#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000131#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000132#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000133#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000134#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000135#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000136#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000137#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000138#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000139#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000140#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000141#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000142#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000143#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000144#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000145#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000146#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000147#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000148#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000149#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000150#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000151#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000152#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000153#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000154#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000155#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000156#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000157#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000158
Chandler Carruth66445382014-01-11 08:16:35 +0000159using namespace llvm;
160
Chandler Carruth719ffe12017-02-12 05:38:04 +0000161static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
162 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000163static cl::opt<bool>
164 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
165 cl::Hidden, cl::ZeroOrMore,
166 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000167
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000168static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000169 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000170 cl::Hidden, cl::ZeroOrMore,
171 cl::desc("Run NewGVN instead of GVN"));
172
Geoff Berry3cca1da2017-06-10 15:20:03 +0000173static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000174 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
175 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000176
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000177static cl::opt<bool> EnableGVNHoist(
Eric Christopherdcf1d972018-10-01 18:57:08 +0000178 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
179 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000180
Davide Italianobe1b6a92017-06-03 23:18:29 +0000181static cl::opt<bool> EnableGVNSink(
182 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
183 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
184
David Green963401d2018-07-01 12:47:30 +0000185static cl::opt<bool> EnableUnrollAndJam(
186 "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
187 cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
188
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000189static cl::opt<bool> EnableSyntheticCounts(
190 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
191 cl::desc("Run synthetic function entry count generation "
192 "pass"));
193
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000194static Regex DefaultAliasRegex(
195 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000196
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000197static cl::opt<bool>
198 EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden,
199 cl::desc("Enable control height reduction optimization (CHR)"));
200
Chandler Carruthe3f50642016-12-22 06:59:15 +0000201static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
202 switch (Level) {
203 case PassBuilder::O0:
204 case PassBuilder::O1:
205 case PassBuilder::O2:
206 case PassBuilder::O3:
207 return false;
208
209 case PassBuilder::Os:
210 case PassBuilder::Oz:
211 return true;
212 }
213 llvm_unreachable("Invalid optimization level!");
214}
215
Chandler Carruth66445382014-01-11 08:16:35 +0000216namespace {
217
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000218/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000219struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000220 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000221 return PreservedAnalyses::all();
222 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000223 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000224};
225
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000226/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000227class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
228 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000229 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000230
231public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000232 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000233 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235};
236
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000237/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000238struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000239 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
240 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000241 return PreservedAnalyses::all();
242 }
243 static StringRef name() { return "NoOpCGSCCPass"; }
244};
245
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000246/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000247class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
248 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000249 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000250
251public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000252 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000253 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000254 return Result();
255 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000256 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000257};
258
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000259/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000260struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000261 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000262 return PreservedAnalyses::all();
263 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000264 static StringRef name() { return "NoOpFunctionPass"; }
265};
266
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000267/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000268class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
269 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000270 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000271
272public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000273 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000274 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000275 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000276};
277
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000278/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000279struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000280 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
281 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000282 return PreservedAnalyses::all();
283 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000284 static StringRef name() { return "NoOpLoopPass"; }
285};
286
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000287/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000288class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
289 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000290 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000291
292public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000293 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000294 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
295 return Result();
296 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000297 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000298};
299
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000300AnalysisKey NoOpModuleAnalysis::Key;
301AnalysisKey NoOpCGSCCAnalysis::Key;
302AnalysisKey NoOpFunctionAnalysis::Key;
303AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000304
Chandler Carruth66445382014-01-11 08:16:35 +0000305} // End anonymous namespace.
306
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000307void PassBuilder::invokePeepholeEPCallbacks(
308 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
309 for (auto &C : PeepholeEPCallbacks)
310 C(FPM, Level);
311}
312
Chandler Carruth1ff77242015-03-07 09:02:36 +0000313void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000314#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000315 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000316#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000317
318 for (auto &C : ModuleAnalysisRegistrationCallbacks)
319 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000320}
321
Chandler Carruth1ff77242015-03-07 09:02:36 +0000322void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000323#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000324 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000325#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000326
327 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
328 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000329}
330
Chandler Carruth1ff77242015-03-07 09:02:36 +0000331void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000332#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000333 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000334#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000335
336 for (auto &C : FunctionAnalysisRegistrationCallbacks)
337 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000338}
339
Justin Bognereecc3c82016-02-25 07:23:08 +0000340void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000341#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000342 LAM.registerPass([&] { return CREATE_PASS; });
343#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000344
345 for (auto &C : LoopAnalysisRegistrationCallbacks)
346 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000347}
348
Chandler Carruthe3f50642016-12-22 06:59:15 +0000349FunctionPassManager
350PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000351 ThinLTOPhase Phase,
352 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000353 assert(Level != O0 && "Must request optimizations!");
354 FunctionPassManager FPM(DebugLogging);
355
356 // Form SSA out of local memory accesses after breaking apart aggregates into
357 // scalars.
358 FPM.addPass(SROA());
359
360 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000361 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000362
Davide Italianoc3688312017-06-01 23:08:14 +0000363 // Hoisting of scalars and load expressions.
364 if (EnableGVNHoist)
365 FPM.addPass(GVNHoistPass());
366
Davide Italianobe1b6a92017-06-03 23:18:29 +0000367 // Global value numbering based sinking.
368 if (EnableGVNSink) {
369 FPM.addPass(GVNSinkPass());
370 FPM.addPass(SimplifyCFGPass());
371 }
372
Chandler Carruthe3f50642016-12-22 06:59:15 +0000373 // Speculative execution if the target has divergent branches; otherwise nop.
374 FPM.addPass(SpeculativeExecutionPass());
375
376 // Optimize based on known information about branches, and cleanup afterward.
377 FPM.addPass(JumpThreadingPass());
378 FPM.addPass(CorrelatedValuePropagationPass());
379 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000380 if (Level == O3)
381 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000382 FPM.addPass(InstCombinePass());
383
384 if (!isOptimizingForSize(Level))
385 FPM.addPass(LibCallsShrinkWrapPass());
386
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000387 invokePeepholeEPCallbacks(FPM, Level);
388
Rong Xue1f42452017-10-23 22:21:29 +0000389 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
390 // using the size value profile. Don't perform this when optimizing for size.
391 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
392 !isOptimizingForSize(Level))
393 FPM.addPass(PGOMemOPSizeOpt());
394
Chandler Carruthe3f50642016-12-22 06:59:15 +0000395 FPM.addPass(TailCallElimPass());
396 FPM.addPass(SimplifyCFGPass());
397
398 // Form canonically associated expression trees, and simplify the trees using
399 // basic mathematical properties. For example, this will form (nearly)
400 // minimal multiplication trees.
401 FPM.addPass(ReassociatePass());
402
403 // Add the primary loop simplification pipeline.
404 // FIXME: Currently this is split into two loop pass pipelines because we run
Chandler Carruth71fd2702018-05-30 02:46:45 +0000405 // some function passes in between them. These can and should be removed
406 // and/or replaced by scheduling the loop pass equivalents in the correct
407 // positions. But those equivalent passes aren't powerful enough yet.
408 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
409 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
410 // fully replace `SimplifyCFGPass`, and the closest to the other we have is
411 // `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000412 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
413
Chandler Carruth71fd2702018-05-30 02:46:45 +0000414 // Simplify the loop body. We do this initially to clean up after other loop
415 // passes run, either when iterating on a loop or on inner loops with
416 // implications on the outer loop.
417 LPM1.addPass(LoopInstSimplifyPass());
418 LPM1.addPass(LoopSimplifyCFGPass());
419
Chandler Carruthe3f50642016-12-22 06:59:15 +0000420 // Rotate Loop - disable header duplication at -Oz
421 LPM1.addPass(LoopRotatePass(Level != Oz));
422 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000423 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000424 LPM2.addPass(IndVarSimplifyPass());
425 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000426
427 for (auto &C : LateLoopOptimizationsEPCallbacks)
428 C(LPM2, Level);
429
Chandler Carruth79b733b2017-01-26 23:21:17 +0000430 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000431 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000432 // because it changes IR to makes profile annotation in back compile
433 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000434 if (Phase != ThinLTOPhase::PreLink ||
435 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000436 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000437
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000438 for (auto &C : LoopOptimizerEndEPCallbacks)
439 C(LPM2, Level);
440
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000441 // We provide the opt remark emitter pass for LICM to use. We only need to do
442 // this once as it is immutable.
443 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000444 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000445 FPM.addPass(SimplifyCFGPass());
446 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000447 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000448
449 // Eliminate redundancies.
450 if (Level != O1) {
451 // These passes add substantial compile time so skip them at O1.
452 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000453 if (RunNewGVN)
454 FPM.addPass(NewGVNPass());
455 else
456 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000457 }
458
459 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
460 FPM.addPass(MemCpyOptPass());
461
462 // Sparse conditional constant propagation.
463 // FIXME: It isn't clear why we do this *after* loop passes rather than
464 // before...
465 FPM.addPass(SCCPPass());
466
467 // Delete dead bit computations (instcombine runs after to fold away the dead
468 // computations, and then ADCE will run later to exploit any new DCE
469 // opportunities that creates).
470 FPM.addPass(BDCEPass());
471
472 // Run instcombine after redundancy and dead bit elimination to exploit
473 // opportunities opened up by them.
474 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000475 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000476
477 // Re-consider control flow based optimizations after redundancy elimination,
478 // redo DCE, etc.
479 FPM.addPass(JumpThreadingPass());
480 FPM.addPass(CorrelatedValuePropagationPass());
481 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000482 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000483
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000484 for (auto &C : ScalarOptimizerLateEPCallbacks)
485 C(FPM, Level);
486
Chandler Carruthe3f50642016-12-22 06:59:15 +0000487 // Finally, do an expensive DCE pass to catch all the dead code exposed by
488 // the simplifications and basic cleanup after all the simplifications.
489 FPM.addPass(ADCEPass());
490 FPM.addPass(SimplifyCFGPass());
491 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000492 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000493
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000494 if (EnableCHR && Level == O3 && PGOOpt &&
495 (!PGOOpt->ProfileUseFile.empty() || !PGOOpt->SampleProfileFile.empty()))
496 FPM.addPass(ControlHeightReductionPass());
497
Chandler Carruthe3f50642016-12-22 06:59:15 +0000498 return FPM;
499}
500
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000501void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
502 PassBuilder::OptimizationLevel Level,
503 bool RunProfileGen,
504 std::string ProfileGenFile,
505 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000506 // Generally running simplification passes and the inliner with an high
507 // threshold results in smaller executables, but there may be cases where
508 // the size grows, so let's be conservative here and skip this simplification
509 // at -Os/Oz.
510 if (!isOptimizingForSize(Level)) {
511 InlineParams IP;
512
513 // In the old pass manager, this is a cl::opt. Should still this be one?
514 IP.DefaultThreshold = 75;
515
516 // FIXME: The hint threshold has the same value used by the regular inliner.
517 // This should probably be lowered after performance testing.
518 // FIXME: this comment is cargo culted from the old pass manager, revisit).
519 IP.HintThreshold = 325;
520
521 CGSCCPassManager CGPipeline(DebugLogging);
522
523 CGPipeline.addPass(InlinerPass(IP));
524
525 FunctionPassManager FPM;
526 FPM.addPass(SROA());
527 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
528 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
529 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000530 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000531
Davide Italiano513dfaa2017-02-13 15:26:22 +0000532 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
533
534 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
535 }
536
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000537 // Delete anything that is now dead to make sure that we don't instrument
538 // dead code. Instrumentation can end up keeping dead code around and
539 // dramatically increase code size.
540 MPM.addPass(GlobalDCEPass());
541
Davide Italiano513dfaa2017-02-13 15:26:22 +0000542 if (RunProfileGen) {
543 MPM.addPass(PGOInstrumentationGen());
544
Xinliang David Lib67530e2017-06-25 00:26:43 +0000545 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000546 FPM.addPass(
547 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000548 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
549
Davide Italiano513dfaa2017-02-13 15:26:22 +0000550 // Add the profile lowering pass.
551 InstrProfOptions Options;
552 if (!ProfileGenFile.empty())
553 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000554 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000555 MPM.addPass(InstrProfiling(Options));
556 }
557
558 if (!ProfileUseFile.empty())
559 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
560}
561
Easwaran Raman8249fac2017-06-28 13:33:49 +0000562static InlineParams
563getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
564 auto O3 = PassBuilder::O3;
565 unsigned OptLevel = Level > O3 ? 2 : Level;
566 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
567 return getInlineParams(OptLevel, SizeLevel);
568}
569
Chandler Carruthe3f50642016-12-22 06:59:15 +0000570ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000571PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000572 ThinLTOPhase Phase,
573 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000574 ModulePassManager MPM(DebugLogging);
575
Chandler Carruthe3f50642016-12-22 06:59:15 +0000576 // Do basic inference of function attributes from known properties of system
577 // libraries and other oracles.
578 MPM.addPass(InferFunctionAttrsPass());
579
580 // Create an early function pass manager to cleanup the output of the
581 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000582 FunctionPassManager EarlyFPM(DebugLogging);
583 EarlyFPM.addPass(SimplifyCFGPass());
584 EarlyFPM.addPass(SROA());
585 EarlyFPM.addPass(EarlyCSEPass());
586 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000587 if (Level == O3)
588 EarlyFPM.addPass(CallSiteSplittingPass());
589
Dehao Chen08f88312017-08-07 20:23:20 +0000590 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
591 // to convert bitcast to direct calls so that they can be inlined during the
592 // profile annotation prepration step.
593 // More details about SamplePGO design can be found in:
594 // https://research.google.com/pubs/pub45290.html
595 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
596 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
597 Phase == ThinLTOPhase::PostLink)
598 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000599 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000600
Dehao Chen08f88312017-08-07 20:23:20 +0000601 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
602 // Annotate sample profile right after early FPM to ensure freshness of
603 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000604 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
605 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000606 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
607 // for the profile annotation to be accurate in the ThinLTO backend.
608 if (Phase != ThinLTOPhase::PreLink)
609 // We perform early indirect call promotion here, before globalopt.
610 // This is important for the ThinLTO backend phase because otherwise
611 // imported available_externally functions look unreferenced and are
612 // removed.
613 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
614 true));
615 }
616
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000617 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000618 // and prior to optimizing globals.
619 // FIXME: This position in the pipeline hasn't been carefully considered in
620 // years, it should be re-analyzed.
621 MPM.addPass(IPSCCPPass());
622
Matthew Simpsoncb585582017-10-25 13:40:08 +0000623 // Attach metadata to indirect call sites indicating the set of functions
624 // they may target at run-time. This should follow IPSCCP.
625 MPM.addPass(CalledValuePropagationPass());
626
Chandler Carruthe3f50642016-12-22 06:59:15 +0000627 // Optimize globals to try and fold them into constants.
628 MPM.addPass(GlobalOptPass());
629
630 // Promote any localized globals to SSA registers.
631 // FIXME: Should this instead by a run of SROA?
632 // FIXME: We should probably run instcombine and simplify-cfg afterward to
633 // delete control flows that are dead once globals have been folded to
634 // constants.
635 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
636
637 // Remove any dead arguments exposed by cleanups and constand folding
638 // globals.
639 MPM.addPass(DeadArgumentEliminationPass());
640
641 // Create a small function pass pipeline to cleanup after all the global
642 // optimizations.
643 FunctionPassManager GlobalCleanupPM(DebugLogging);
644 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000645 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
646
Chandler Carruthe3f50642016-12-22 06:59:15 +0000647 GlobalCleanupPM.addPass(SimplifyCFGPass());
648 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
649
Dehao Chen89d32262017-08-02 01:28:31 +0000650 // Add all the requested passes for instrumentation PGO, if requested.
651 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
652 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
653 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
654 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
655 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000656 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000657
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000658 // Synthesize function entry counts for non-PGO compilation.
659 if (EnableSyntheticCounts && !PGOOpt)
660 MPM.addPass(SyntheticCountsPropagation());
661
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000662 // Require the GlobalsAA analysis for the module so we can query it within
663 // the CGSCC pipeline.
664 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000665
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000666 // Require the ProfileSummaryAnalysis for the module so we can query it within
667 // the inliner pass.
668 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
669
Chandler Carruthe3f50642016-12-22 06:59:15 +0000670 // Now begin the main postorder CGSCC pipeline.
671 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
672 // manager and trying to emulate its precise behavior. Much of this doesn't
673 // make a lot of sense and we should revisit the core CGSCC structure.
674 CGSCCPassManager MainCGPipeline(DebugLogging);
675
676 // Note: historically, the PruneEH pass was run first to deduce nounwind and
677 // generally clean up exception handling overhead. It isn't clear this is
678 // valuable as the inliner doesn't currently care whether it is inlining an
679 // invoke or a call.
680
681 // Run the inliner first. The theory is that we are walking bottom-up and so
682 // the callees have already been fully optimized, and we want to inline them
683 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000684 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000685 // because it makes profile annotation in the backend inaccurate.
686 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000687 if (Phase == ThinLTOPhase::PreLink &&
688 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000689 IP.HotCallSiteThreshold = 0;
690 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000691
692 // Now deduce any function attributes based in the current code.
693 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
694
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000695 // When at O3 add argument promotion to the pass pipeline.
696 // FIXME: It isn't at all clear why this should be limited to O3.
697 if (Level == O3)
698 MainCGPipeline.addPass(ArgumentPromotionPass());
699
Chandler Carruthe3f50642016-12-22 06:59:15 +0000700 // Lastly, add the core function simplification pipeline nested inside the
701 // CGSCC walk.
702 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000703 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000704
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000705 for (auto &C : CGSCCOptimizerLateEPCallbacks)
706 C(MainCGPipeline, Level);
707
Chandler Carruth719ffe12017-02-12 05:38:04 +0000708 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
709 // to detect when we devirtualize indirect calls and iterate the SCC passes
710 // in that case to try and catch knock-on inlining or function attrs
711 // opportunities. Then we add it to the module pipeline by walking the SCCs
712 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000713 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000714 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000715 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000716
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000717 return MPM;
718}
719
720ModulePassManager
721PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
722 bool DebugLogging) {
723 ModulePassManager MPM(DebugLogging);
724
725 // Optimize globals now that the module is fully simplified.
726 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000727 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000728
Xinliang David Li126157c2017-05-22 16:41:57 +0000729 // Run partial inlining pass to partially inline functions that have
730 // large bodies.
731 if (RunPartialInlining)
732 MPM.addPass(PartialInlinerPass());
733
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000734 // Remove avail extern fns and globals definitions since we aren't compiling
735 // an object file for later LTO. For LTO we want to preserve these so they
736 // are eligible for inlining at link-time. Note if they are unreferenced they
737 // will be removed by GlobalDCE later, so this only impacts referenced
738 // available externally globals. Eventually they will be suppressed during
739 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
740 // may make globals referenced by available external functions dead and saves
741 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000742 MPM.addPass(EliminateAvailableExternallyPass());
743
744 // Do RPO function attribute inference across the module to forward-propagate
745 // attributes where applicable.
746 // FIXME: Is this really an optimization rather than a canonicalization?
747 MPM.addPass(ReversePostOrderFunctionAttrsPass());
748
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000749 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000750 // useful as the above will have inlined, DCE'ed, and function-attr
751 // propagated everything. We should at this point have a reasonably minimal
752 // and richly annotated call graph. By computing aliasing and mod/ref
753 // information for all local globals here, the late loop passes and notably
754 // the vectorizer will be able to use them to help recognize vectorizable
755 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000756 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000757
758 FunctionPassManager OptimizePM(DebugLogging);
759 OptimizePM.addPass(Float2IntPass());
760 // FIXME: We need to run some loop optimizations to re-rotate loops after
761 // simplify-cfg and others undo their rotation.
762
763 // Optimize the loop execution. These passes operate on entire loop nests
764 // rather than on each loop in an inside-out manner, and so they are actually
765 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000766
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000767 for (auto &C : VectorizerStartEPCallbacks)
768 C(OptimizePM, Level);
769
Chandler Carrutha95ff382017-01-27 00:50:21 +0000770 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000771 OptimizePM.addPass(
772 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000773
774 // Distribute loops to allow partial vectorization. I.e. isolate dependences
775 // into separate loop that would otherwise inhibit vectorization. This is
776 // currently only performed for loops marked with the metadata
777 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000778 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000779
780 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000781 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000782
Chandler Carruthbaabda92017-01-27 01:32:26 +0000783 // Eliminate loads by forwarding stores from the previous iteration to loads
784 // of the current iteration.
785 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000786
787 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000788 OptimizePM.addPass(InstCombinePass());
789
Chandler Carrutha95ff382017-01-27 00:50:21 +0000790 // Now that we've formed fast to execute loop structures, we do further
791 // optimizations. These are run afterward as they might block doing complex
792 // analyses and transforms such as what are needed for loop vectorization.
793
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000794 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
795 // GVN, loop transforms, and others have already run, so it's now better to
796 // convert to more optimized IR using more aggressive simplify CFG options.
797 // The extra sinking transform can create larger basic blocks, so do this
798 // before SLP vectorization.
799 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
800 forwardSwitchCondToPhi(true).
801 convertSwitchToLookupTable(true).
802 needCanonicalLoops(false).
803 sinkCommonInsts(true)));
804
Chandler Carruthe3f50642016-12-22 06:59:15 +0000805 // Optimize parallel scalar instruction chains into SIMD instructions.
806 OptimizePM.addPass(SLPVectorizerPass());
807
Chandler Carruthe3f50642016-12-22 06:59:15 +0000808 OptimizePM.addPass(InstCombinePass());
809
810 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000811 // execution resources of an out-of-order processor. We also then need to
812 // clean up redundancies and loop invariant code.
813 // FIXME: It would be really good to use a loop-integrated instruction
814 // combiner for cleanup here so that the unrolling and LICM can be pipelined
815 // across the loop nests.
David Green963401d2018-07-01 12:47:30 +0000816 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
817 if (EnableUnrollAndJam) {
818 OptimizePM.addPass(
819 createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
820 }
Teresa Johnsonecd90132017-08-02 20:35:29 +0000821 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000822 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000823 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000824 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000825
826 // Now that we've vectorized and unrolled loops, we may have more refined
827 // alignment information, try to re-derive it here.
828 OptimizePM.addPass(AlignmentFromAssumptionsPass());
829
Chandler Carrutha95ff382017-01-27 00:50:21 +0000830 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
831 // canonicalization pass that enables other optimizations. As a result,
832 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
833 // result too early.
834 OptimizePM.addPass(LoopSinkPass());
835
836 // And finally clean up LCSSA form before generating code.
Chandler Carruth7c557f82018-06-29 23:36:03 +0000837 OptimizePM.addPass(InstSimplifyPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000838
Sanjay Patel6fd43912017-09-09 13:38:18 +0000839 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
840 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
841 // flattening of blocks.
842 OptimizePM.addPass(DivRemPairsPass());
843
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000844 // LoopSink (and other loop passes since the last simplifyCFG) might have
845 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
846 OptimizePM.addPass(SimplifyCFGPass());
847
Chandler Carruthc34f7892017-11-28 11:32:31 +0000848 // Optimize PHIs by speculating around them when profitable. Note that this
849 // pass needs to be run after any PRE or similar pass as it is essentially
850 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
851 OptimizePM.addPass(SpeculateAroundPHIsPass());
852
Chandler Carrutha95ff382017-01-27 00:50:21 +0000853 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000854 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
855
Michael J. Spencer7bb27672018-07-16 00:28:24 +0000856 MPM.addPass(CGProfilePass());
857
Chandler Carruthe3f50642016-12-22 06:59:15 +0000858 // Now we need to do some global optimization transforms.
859 // FIXME: It would seem like these should come first in the optimization
860 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
861 // ordering here.
862 MPM.addPass(GlobalDCEPass());
863 MPM.addPass(ConstantMergePass());
864
865 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000866}
867
Chandler Carruthe3f50642016-12-22 06:59:15 +0000868ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000869PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
870 bool DebugLogging) {
871 assert(Level != O0 && "Must request optimizations for the default pipeline!");
872
873 ModulePassManager MPM(DebugLogging);
874
875 // Force any function attributes we want the rest of the pipeline to observe.
876 MPM.addPass(ForceFunctionAttrsPass());
877
David Blaikie0c64f5a2018-01-23 01:25:20 +0000878 // Apply module pipeline start EP callback.
879 for (auto &C : PipelineStartEPCallbacks)
880 C(MPM);
881
Dehao Chen08f88312017-08-07 20:23:20 +0000882 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000883 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
884
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000885 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000886 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
887 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000888
889 // Now add the optimization pipeline.
890 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
891
892 return MPM;
893}
894
895ModulePassManager
896PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
897 bool DebugLogging) {
898 assert(Level != O0 && "Must request optimizations for the default pipeline!");
899
900 ModulePassManager MPM(DebugLogging);
901
902 // Force any function attributes we want the rest of the pipeline to observe.
903 MPM.addPass(ForceFunctionAttrsPass());
904
Dehao Chen08f88312017-08-07 20:23:20 +0000905 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000906 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
907
David Blaikie0c64f5a2018-01-23 01:25:20 +0000908 // Apply module pipeline start EP callback.
909 for (auto &C : PipelineStartEPCallbacks)
910 C(MPM);
911
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000912 // If we are planning to perform ThinLTO later, we don't bloat the code with
913 // unrolling/vectorization/... now. Just simplify the module as much as we
914 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000915 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
916 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000917
918 // Run partial inlining pass to partially inline functions that have
919 // large bodies.
920 // FIXME: It isn't clear whether this is really the right place to run this
921 // in ThinLTO. Because there is another canonicalization and simplification
922 // phase that will run after the thin link, running this here ends up with
923 // less information than will be available later and it may grow functions in
924 // ways that aren't beneficial.
925 if (RunPartialInlining)
926 MPM.addPass(PartialInlinerPass());
927
928 // Reduce the size of the IR as much as possible.
929 MPM.addPass(GlobalOptPass());
930
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000931 return MPM;
932}
933
Teresa Johnson28023db2018-07-19 14:51:32 +0000934ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
935 OptimizationLevel Level, bool DebugLogging,
936 const ModuleSummaryIndex *ImportSummary) {
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000937 ModulePassManager MPM(DebugLogging);
938
Teresa Johnson28023db2018-07-19 14:51:32 +0000939 if (ImportSummary) {
940 // These passes import type identifier resolutions for whole-program
941 // devirtualization and CFI. They must run early because other passes may
942 // disturb the specific instruction patterns that these passes look for,
943 // creating dependencies on resolutions that may not appear in the summary.
944 //
945 // For example, GVN may transform the pattern assume(type.test) appearing in
946 // two basic blocks into assume(phi(type.test, type.test)), which would
947 // transform a dependency on a WPD resolution into a dependency on a type
948 // identifier resolution for CFI.
949 //
950 // Also, WPD has access to more precise information than ICP and can
951 // devirtualize more effectively, so it should operate on the IR first.
952 MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
953 MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
954 }
955
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000956 // Force any function attributes we want the rest of the pipeline to observe.
957 MPM.addPass(ForceFunctionAttrsPass());
958
959 // During the ThinLTO backend phase we perform early indirect call promotion
960 // here, before globalopt. Otherwise imported available_externally functions
961 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000962 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
963 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000964 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000965 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
966 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000967
968 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000969 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
970 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000971
972 // Now add the optimization pipeline.
973 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
974
975 return MPM;
976}
977
978ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000979PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
980 bool DebugLogging) {
981 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000982 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000983 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000984}
985
Teresa Johnson28023db2018-07-19 14:51:32 +0000986ModulePassManager
987PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
988 ModuleSummaryIndex *ExportSummary) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000989 assert(Level != O0 && "Must request optimizations for the default pipeline!");
990 ModulePassManager MPM(DebugLogging);
991
Davide Italiano089a9122017-01-24 00:57:39 +0000992 // Remove unused virtual tables to improve the quality of code generated by
993 // whole-program devirtualization and bitset lowering.
994 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000995
Davide Italiano089a9122017-01-24 00:57:39 +0000996 // Force any function attributes we want the rest of the pipeline to observe.
997 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000998
Davide Italiano089a9122017-01-24 00:57:39 +0000999 // Do basic inference of function attributes from known properties of system
1000 // libraries and other oracles.
1001 MPM.addPass(InferFunctionAttrsPass());
1002
1003 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +00001004 FunctionPassManager EarlyFPM(DebugLogging);
1005 EarlyFPM.addPass(CallSiteSplittingPass());
1006 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
1007
Davide Italiano089a9122017-01-24 00:57:39 +00001008 // Indirect call promotion. This should promote all the targets that are
1009 // left by the earlier promotion pass that promotes intra-module targets.
1010 // This two-step promotion is to save the compile time. For LTO, it should
1011 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +00001012 MPM.addPass(PGOIndirectCallPromotion(
1013 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +00001014 // Propagate constants at call sites into the functions they call. This
1015 // opens opportunities for globalopt (and inlining) by substituting function
1016 // pointers passed as arguments to direct uses of functions.
1017 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +00001018
1019 // Attach metadata to indirect call sites indicating the set of functions
1020 // they may target at run-time. This should follow IPSCCP.
1021 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +00001022 }
1023
1024 // Now deduce any function attributes based in the current code.
1025 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1026 PostOrderFunctionAttrsPass()));
1027
1028 // Do RPO function attribute inference across the module to forward-propagate
1029 // attributes where applicable.
1030 // FIXME: Is this really an optimization rather than a canonicalization?
1031 MPM.addPass(ReversePostOrderFunctionAttrsPass());
1032
1033 // Use inragne annotations on GEP indices to split globals where beneficial.
1034 MPM.addPass(GlobalSplitPass());
1035
1036 // Run whole program optimization of virtual call when the list of callees
1037 // is fixed.
Teresa Johnson28023db2018-07-19 14:51:32 +00001038 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001039
1040 // Stop here at -O1.
Teresa Johnson28023db2018-07-19 14:51:32 +00001041 if (Level == 1) {
1042 // The LowerTypeTestsPass needs to run to lower type metadata and the
1043 // type.test intrinsics. The pass does nothing if CFI is disabled.
1044 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001045 return MPM;
Teresa Johnson28023db2018-07-19 14:51:32 +00001046 }
Davide Italiano089a9122017-01-24 00:57:39 +00001047
1048 // Optimize globals to try and fold them into constants.
1049 MPM.addPass(GlobalOptPass());
1050
1051 // Promote any localized globals to SSA registers.
1052 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1053
1054 // Linking modules together can lead to duplicate global constant, only
1055 // keep one copy of each constant.
1056 MPM.addPass(ConstantMergePass());
1057
1058 // Remove unused arguments from functions.
1059 MPM.addPass(DeadArgumentEliminationPass());
1060
1061 // Reduce the code after globalopt and ipsccp. Both can open up significant
1062 // simplification opportunities, and both can propagate functions through
1063 // function pointers. When this happens, we often have to resolve varargs
1064 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001065 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001066 if (Level == O3)
1067 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001068 PeepholeFPM.addPass(InstCombinePass());
1069 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1070
1071 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001072
1073 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1074 // generally clean up exception handling overhead. It isn't clear this is
1075 // valuable as the inliner doesn't currently care whether it is inlining an
1076 // invoke or a call.
1077 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001078 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1079 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001080
1081 // Optimize globals again after we ran the inliner.
1082 MPM.addPass(GlobalOptPass());
1083
1084 // Garbage collect dead functions.
1085 // FIXME: Add ArgumentPromotion pass after once it's ported.
1086 MPM.addPass(GlobalDCEPass());
1087
1088 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001089 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001090 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001091 invokePeepholeEPCallbacks(FPM, Level);
1092
Davide Italiano089a9122017-01-24 00:57:39 +00001093 FPM.addPass(JumpThreadingPass());
1094
1095 // Break up allocas
1096 FPM.addPass(SROA());
1097
1098 // Run a few AA driver optimizations here and now to cleanup the code.
1099 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1100
1101 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1102 PostOrderFunctionAttrsPass()));
1103 // FIXME: here we run IP alias analysis in the legacy PM.
1104
1105 FunctionPassManager MainFPM;
1106
1107 // FIXME: once we fix LoopPass Manager, add LICM here.
1108 // FIXME: once we provide support for enabling MLSM, add it here.
1109 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001110 if (RunNewGVN)
1111 MainFPM.addPass(NewGVNPass());
1112 else
1113 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001114
1115 // Remove dead memcpy()'s.
1116 MainFPM.addPass(MemCpyOptPass());
1117
1118 // Nuke dead stores.
1119 MainFPM.addPass(DSEPass());
1120
1121 // FIXME: at this point, we run a bunch of loop passes:
1122 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1123 // loopVectorize. Enable them once the remaining issue with LPM
1124 // are sorted out.
1125
1126 MainFPM.addPass(InstCombinePass());
1127 MainFPM.addPass(SimplifyCFGPass());
1128 MainFPM.addPass(SCCPPass());
1129 MainFPM.addPass(InstCombinePass());
1130 MainFPM.addPass(BDCEPass());
1131
1132 // FIXME: We may want to run SLPVectorizer here.
1133 // After vectorization, assume intrinsics may tell us more
1134 // about pointer alignments.
1135#if 0
1136 MainFPM.add(AlignmentFromAssumptionsPass());
1137#endif
1138
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001139 // FIXME: Conditionally run LoadCombine here, after it's ported
1140 // (in case we still have this pass, given its questionable usefulness).
1141
Davide Italiano089a9122017-01-24 00:57:39 +00001142 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001143 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001144 MainFPM.addPass(JumpThreadingPass());
1145 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1146
1147 // Create a function that performs CFI checks for cross-DSO calls with
1148 // targets in the current module.
1149 MPM.addPass(CrossDSOCFIPass());
1150
1151 // Lower type metadata and the type.test intrinsic. This pass supports
1152 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1153 // to be run at link time if CFI is enabled. This pass does nothing if
1154 // CFI is disabled.
Teresa Johnson28023db2018-07-19 14:51:32 +00001155 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001156
1157 // Add late LTO optimization passes.
1158 // Delete basic blocks, which optimization passes may have killed.
1159 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1160
1161 // Drop bodies of available eternally objects to improve GlobalDCE.
1162 MPM.addPass(EliminateAvailableExternallyPass());
1163
1164 // Now that we have optimized the program, discard unreachable functions.
1165 MPM.addPass(GlobalDCEPass());
1166
1167 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001168 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001169}
1170
Chandler Carruth060ad612016-12-23 20:38:19 +00001171AAManager PassBuilder::buildDefaultAAPipeline() {
1172 AAManager AA;
1173
1174 // The order in which these are registered determines their priority when
1175 // being queried.
1176
1177 // First we register the basic alias analysis that provides the majority of
1178 // per-function local AA logic. This is a stateless, on-demand local set of
1179 // AA techniques.
1180 AA.registerFunctionAnalysis<BasicAA>();
1181
1182 // Next we query fast, specialized alias analyses that wrap IR-embedded
1183 // information about aliasing.
1184 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1185 AA.registerFunctionAnalysis<TypeBasedAA>();
1186
1187 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001188 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1189 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001190 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001191 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001192
1193 return AA;
1194}
1195
Chandler Carruth241bf242016-08-03 07:44:48 +00001196static Optional<int> parseRepeatPassName(StringRef Name) {
1197 if (!Name.consume_front("repeat<") || !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 Carruth05ca5ac2016-12-28 11:07:33 +00001205static Optional<int> parseDevirtPassName(StringRef Name) {
1206 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1207 return None;
1208 int Count;
1209 if (Name.getAsInteger(0, Count) || Count <= 0)
1210 return None;
1211 return Count;
1212}
1213
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001214/// Tests whether a pass name starts with a valid prefix for a default pipeline
1215/// alias.
1216static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1217 return Name.startswith("default") || Name.startswith("thinlto") ||
1218 Name.startswith("lto");
1219}
1220
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001221/// Tests whether registered callbacks will accept a given pass name.
1222///
1223/// When parsing a pipeline text, the type of the outermost pipeline may be
1224/// omitted, in which case the type is automatically determined from the first
1225/// pass name in the text. This may be a name that is handled through one of the
1226/// callbacks. We check this through the oridinary parsing callbacks by setting
1227/// up a dummy PassManager in order to not force the client to also handle this
1228/// type of query.
1229template <typename PassManagerT, typename CallbacksT>
1230static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1231 if (!Callbacks.empty()) {
1232 PassManagerT DummyPM;
1233 for (auto &CB : Callbacks)
1234 if (CB(Name, DummyPM, {}))
1235 return true;
1236 }
1237 return false;
1238}
1239
1240template <typename CallbacksT>
1241static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001242 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001243 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001244 return DefaultAliasRegex.match(Name);
1245
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001246 // Explicitly handle pass manager names.
1247 if (Name == "module")
1248 return true;
1249 if (Name == "cgscc")
1250 return true;
1251 if (Name == "function")
1252 return true;
1253
Chandler Carruth241bf242016-08-03 07:44:48 +00001254 // Explicitly handle custom-parsed pass names.
1255 if (parseRepeatPassName(Name))
1256 return true;
1257
Chandler Carruth74a8a222016-06-17 07:15:29 +00001258#define MODULE_PASS(NAME, CREATE_PASS) \
1259 if (Name == NAME) \
1260 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001261#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001262 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001263 return true;
1264#include "PassRegistry.def"
1265
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001266 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001267}
1268
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001269template <typename CallbacksT>
1270static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001271 // Explicitly handle pass manager names.
1272 if (Name == "cgscc")
1273 return true;
1274 if (Name == "function")
1275 return true;
1276
Chandler Carruth241bf242016-08-03 07:44:48 +00001277 // Explicitly handle custom-parsed pass names.
1278 if (parseRepeatPassName(Name))
1279 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001280 if (parseDevirtPassName(Name))
1281 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001282
Chandler Carruth74a8a222016-06-17 07:15:29 +00001283#define CGSCC_PASS(NAME, CREATE_PASS) \
1284 if (Name == NAME) \
1285 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001286#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001287 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001288 return true;
1289#include "PassRegistry.def"
1290
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001291 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001292}
1293
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001294template <typename CallbacksT>
1295static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001296 // Explicitly handle pass manager names.
1297 if (Name == "function")
1298 return true;
1299 if (Name == "loop")
1300 return true;
1301
Chandler Carruth241bf242016-08-03 07:44:48 +00001302 // Explicitly handle custom-parsed pass names.
1303 if (parseRepeatPassName(Name))
1304 return true;
1305
Chandler Carruth74a8a222016-06-17 07:15:29 +00001306#define FUNCTION_PASS(NAME, CREATE_PASS) \
1307 if (Name == NAME) \
1308 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001309#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001310 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001311 return true;
1312#include "PassRegistry.def"
1313
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001314 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001315}
1316
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001317template <typename CallbacksT>
1318static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001319 // Explicitly handle pass manager names.
1320 if (Name == "loop")
1321 return true;
1322
Chandler Carruth241bf242016-08-03 07:44:48 +00001323 // Explicitly handle custom-parsed pass names.
1324 if (parseRepeatPassName(Name))
1325 return true;
1326
Chandler Carruth74a8a222016-06-17 07:15:29 +00001327#define LOOP_PASS(NAME, CREATE_PASS) \
1328 if (Name == NAME) \
1329 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001330#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1331 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1332 return true;
1333#include "PassRegistry.def"
1334
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001335 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001336}
1337
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001338Optional<std::vector<PassBuilder::PipelineElement>>
1339PassBuilder::parsePipelineText(StringRef Text) {
1340 std::vector<PipelineElement> ResultPipeline;
1341
1342 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1343 &ResultPipeline};
1344 for (;;) {
1345 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1346 size_t Pos = Text.find_first_of(",()");
1347 Pipeline.push_back({Text.substr(0, Pos), {}});
1348
1349 // If we have a single terminating name, we're done.
1350 if (Pos == Text.npos)
1351 break;
1352
1353 char Sep = Text[Pos];
1354 Text = Text.substr(Pos + 1);
1355 if (Sep == ',')
1356 // Just a name ending in a comma, continue.
1357 continue;
1358
1359 if (Sep == '(') {
1360 // Push the inner pipeline onto the stack to continue processing.
1361 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1362 continue;
1363 }
1364
1365 assert(Sep == ')' && "Bogus separator!");
1366 // When handling the close parenthesis, we greedily consume them to avoid
1367 // empty strings in the pipeline.
1368 do {
1369 // If we try to pop the outer pipeline we have unbalanced parentheses.
1370 if (PipelineStack.size() == 1)
1371 return None;
1372
1373 PipelineStack.pop_back();
1374 } while (Text.consume_front(")"));
1375
1376 // Check if we've finished parsing.
1377 if (Text.empty())
1378 break;
1379
1380 // Otherwise, the end of an inner pipeline always has to be followed by
1381 // a comma, and then we can continue.
1382 if (!Text.consume_front(","))
1383 return None;
1384 }
1385
1386 if (PipelineStack.size() > 1)
1387 // Unbalanced paretheses.
1388 return None;
1389
1390 assert(PipelineStack.back() == &ResultPipeline &&
1391 "Wrong pipeline at the bottom of the stack!");
1392 return {std::move(ResultPipeline)};
1393}
1394
1395bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1396 const PipelineElement &E, bool VerifyEachPass,
1397 bool DebugLogging) {
1398 auto &Name = E.Name;
1399 auto &InnerPipeline = E.InnerPipeline;
1400
1401 // First handle complex passes like the pass managers which carry pipelines.
1402 if (!InnerPipeline.empty()) {
1403 if (Name == "module") {
1404 ModulePassManager NestedMPM(DebugLogging);
1405 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1406 DebugLogging))
1407 return false;
1408 MPM.addPass(std::move(NestedMPM));
1409 return true;
1410 }
1411 if (Name == "cgscc") {
1412 CGSCCPassManager CGPM(DebugLogging);
1413 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1414 DebugLogging))
1415 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001416 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001417 return true;
1418 }
1419 if (Name == "function") {
1420 FunctionPassManager FPM(DebugLogging);
1421 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1422 DebugLogging))
1423 return false;
1424 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1425 return true;
1426 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001427 if (auto Count = parseRepeatPassName(Name)) {
1428 ModulePassManager NestedMPM(DebugLogging);
1429 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1430 DebugLogging))
1431 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001432 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001433 return true;
1434 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001435
1436 for (auto &C : ModulePipelineParsingCallbacks)
1437 if (C(Name, MPM, InnerPipeline))
1438 return true;
1439
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001440 // Normal passes can't have pipelines.
1441 return false;
1442 }
1443
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001444 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001445 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001446 SmallVector<StringRef, 3> Matches;
1447 if (!DefaultAliasRegex.match(Name, &Matches))
1448 return false;
1449 assert(Matches.size() == 3 && "Must capture two matched strings!");
1450
Jordan Rosef85a95f2016-07-25 18:34:51 +00001451 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001452 .Case("O0", O0)
1453 .Case("O1", O1)
1454 .Case("O2", O2)
1455 .Case("O3", O3)
1456 .Case("Os", Os)
1457 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001458 if (L == O0)
1459 // At O0 we do nothing at all!
1460 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001461
1462 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001463 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001464 } else if (Matches[1] == "thinlto-pre-link") {
1465 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1466 } else if (Matches[1] == "thinlto") {
Teresa Johnson28023db2018-07-19 14:51:32 +00001467 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001468 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001469 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001470 } else {
1471 assert(Matches[1] == "lto" && "Not one of the matched options!");
Teresa Johnson28023db2018-07-19 14:51:32 +00001472 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001473 }
1474 return true;
1475 }
1476
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001477 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001478#define MODULE_PASS(NAME, CREATE_PASS) \
1479 if (Name == NAME) { \
1480 MPM.addPass(CREATE_PASS); \
1481 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001482 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001483#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1484 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001485 MPM.addPass( \
1486 RequireAnalysisPass< \
1487 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001488 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001489 } \
1490 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001491 MPM.addPass(InvalidateAnalysisPass< \
1492 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001493 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001494 }
1495#include "PassRegistry.def"
1496
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001497 for (auto &C : ModulePipelineParsingCallbacks)
1498 if (C(Name, MPM, InnerPipeline))
1499 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001500 return false;
1501}
1502
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001503bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1504 const PipelineElement &E, bool VerifyEachPass,
1505 bool DebugLogging) {
1506 auto &Name = E.Name;
1507 auto &InnerPipeline = E.InnerPipeline;
1508
1509 // First handle complex passes like the pass managers which carry pipelines.
1510 if (!InnerPipeline.empty()) {
1511 if (Name == "cgscc") {
1512 CGSCCPassManager NestedCGPM(DebugLogging);
1513 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1514 DebugLogging))
1515 return false;
1516 // Add the nested pass manager with the appropriate adaptor.
1517 CGPM.addPass(std::move(NestedCGPM));
1518 return true;
1519 }
1520 if (Name == "function") {
1521 FunctionPassManager FPM(DebugLogging);
1522 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1523 DebugLogging))
1524 return false;
1525 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001526 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001527 return true;
1528 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001529 if (auto Count = parseRepeatPassName(Name)) {
1530 CGSCCPassManager NestedCGPM(DebugLogging);
1531 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1532 DebugLogging))
1533 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001534 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001535 return true;
1536 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001537 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1538 CGSCCPassManager NestedCGPM(DebugLogging);
1539 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1540 DebugLogging))
1541 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001542 CGPM.addPass(
1543 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001544 return true;
1545 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001546
1547 for (auto &C : CGSCCPipelineParsingCallbacks)
1548 if (C(Name, CGPM, InnerPipeline))
1549 return true;
1550
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001551 // Normal passes can't have pipelines.
1552 return false;
1553 }
1554
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001555// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001556#define CGSCC_PASS(NAME, CREATE_PASS) \
1557 if (Name == NAME) { \
1558 CGPM.addPass(CREATE_PASS); \
1559 return true; \
1560 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001561#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1562 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001563 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001564 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001565 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1566 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001567 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001568 } \
1569 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001570 CGPM.addPass(InvalidateAnalysisPass< \
1571 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001572 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001573 }
1574#include "PassRegistry.def"
1575
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001576 for (auto &C : CGSCCPipelineParsingCallbacks)
1577 if (C(Name, CGPM, InnerPipeline))
1578 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001579 return false;
1580}
1581
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001582bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1583 const PipelineElement &E,
1584 bool VerifyEachPass, bool DebugLogging) {
1585 auto &Name = E.Name;
1586 auto &InnerPipeline = E.InnerPipeline;
1587
1588 // First handle complex passes like the pass managers which carry pipelines.
1589 if (!InnerPipeline.empty()) {
1590 if (Name == "function") {
1591 FunctionPassManager NestedFPM(DebugLogging);
1592 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1593 DebugLogging))
1594 return false;
1595 // Add the nested pass manager with the appropriate adaptor.
1596 FPM.addPass(std::move(NestedFPM));
1597 return true;
1598 }
1599 if (Name == "loop") {
1600 LoopPassManager LPM(DebugLogging);
1601 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1602 DebugLogging))
1603 return false;
1604 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001605 FPM.addPass(
1606 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001607 return true;
1608 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001609 if (auto Count = parseRepeatPassName(Name)) {
1610 FunctionPassManager NestedFPM(DebugLogging);
1611 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1612 DebugLogging))
1613 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001614 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001615 return true;
1616 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001617
1618 for (auto &C : FunctionPipelineParsingCallbacks)
1619 if (C(Name, FPM, InnerPipeline))
1620 return true;
1621
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001622 // Normal passes can't have pipelines.
1623 return false;
1624 }
1625
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001626// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001627#define FUNCTION_PASS(NAME, CREATE_PASS) \
1628 if (Name == NAME) { \
1629 FPM.addPass(CREATE_PASS); \
1630 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001631 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001632#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1633 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001634 FPM.addPass( \
1635 RequireAnalysisPass< \
1636 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001637 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001638 } \
1639 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001640 FPM.addPass(InvalidateAnalysisPass< \
1641 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001642 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001643 }
1644#include "PassRegistry.def"
1645
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001646 for (auto &C : FunctionPipelineParsingCallbacks)
1647 if (C(Name, FPM, InnerPipeline))
1648 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001649 return false;
1650}
1651
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001652bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001653 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001654 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001655 auto &InnerPipeline = E.InnerPipeline;
1656
1657 // First handle complex passes like the pass managers which carry pipelines.
1658 if (!InnerPipeline.empty()) {
1659 if (Name == "loop") {
1660 LoopPassManager NestedLPM(DebugLogging);
1661 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1662 DebugLogging))
1663 return false;
1664 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001665 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001666 return true;
1667 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001668 if (auto Count = parseRepeatPassName(Name)) {
1669 LoopPassManager NestedLPM(DebugLogging);
1670 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1671 DebugLogging))
1672 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001673 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001674 return true;
1675 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001676
1677 for (auto &C : LoopPipelineParsingCallbacks)
1678 if (C(Name, LPM, InnerPipeline))
1679 return true;
1680
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001681 // Normal passes can't have pipelines.
1682 return false;
1683 }
1684
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001685// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001686#define LOOP_PASS(NAME, CREATE_PASS) \
1687 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001688 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001689 return true; \
1690 }
1691#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1692 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001693 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001694 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1695 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1696 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001697 return true; \
1698 } \
1699 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001700 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001701 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001702 return true; \
1703 }
1704#include "PassRegistry.def"
1705
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001706 for (auto &C : LoopPipelineParsingCallbacks)
1707 if (C(Name, LPM, InnerPipeline))
1708 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001709 return false;
1710}
1711
Chandler Carruthedf59962016-02-18 09:45:17 +00001712bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001713#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1714 if (Name == NAME) { \
1715 AA.registerModuleAnalysis< \
1716 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1717 return true; \
1718 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001719#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1720 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001721 AA.registerFunctionAnalysis< \
1722 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001723 return true; \
1724 }
1725#include "PassRegistry.def"
1726
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001727 for (auto &C : AAParsingCallbacks)
1728 if (C(Name, AA))
1729 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001730 return false;
1731}
1732
Justin Bognereecc3c82016-02-25 07:23:08 +00001733bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001734 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001735 bool VerifyEachPass,
1736 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001737 for (const auto &Element : Pipeline) {
1738 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1739 return false;
1740 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001741 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001742 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001743}
1744
Chandler Carruth1ff77242015-03-07 09:02:36 +00001745bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001746 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001747 bool VerifyEachPass,
1748 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001749 for (const auto &Element : Pipeline) {
1750 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1751 return false;
1752 if (VerifyEachPass)
1753 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001754 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001755 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001756}
1757
Chandler Carruth1ff77242015-03-07 09:02:36 +00001758bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001759 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001760 bool VerifyEachPass,
1761 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001762 for (const auto &Element : Pipeline) {
1763 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1764 return false;
1765 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001766 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001767 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001768}
1769
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001770void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1771 FunctionAnalysisManager &FAM,
1772 CGSCCAnalysisManager &CGAM,
1773 ModuleAnalysisManager &MAM) {
1774 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1775 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001776 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1777 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1778 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1779 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1780 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1781}
1782
Chandler Carruth1ff77242015-03-07 09:02:36 +00001783bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001784 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001785 bool VerifyEachPass,
1786 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001787 for (const auto &Element : Pipeline) {
1788 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1789 return false;
1790 if (VerifyEachPass)
1791 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001792 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001793 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001794}
1795
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001796// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001797// FIXME: Should this routine accept a TargetMachine or require the caller to
1798// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001799bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1800 StringRef PipelineText, bool VerifyEachPass,
1801 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001802 auto Pipeline = parsePipelineText(PipelineText);
1803 if (!Pipeline || Pipeline->empty())
1804 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001805
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001806 // If the first name isn't at the module layer, wrap the pipeline up
1807 // automatically.
1808 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001809
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001810 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1811 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001812 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001813 } else if (isFunctionPassName(FirstName,
1814 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001815 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001816 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001817 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001818 } else {
1819 for (auto &C : TopLevelPipelineParsingCallbacks)
1820 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1821 return true;
1822
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001823 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001824 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001825 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001826 }
1827
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001828 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001829}
Chandler Carruthedf59962016-02-18 09:45:17 +00001830
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001831// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1832bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1833 StringRef PipelineText, bool VerifyEachPass,
1834 bool DebugLogging) {
1835 auto Pipeline = parsePipelineText(PipelineText);
1836 if (!Pipeline || Pipeline->empty())
1837 return false;
1838
1839 StringRef FirstName = Pipeline->front().Name;
1840 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1841 return false;
1842
1843 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1844}
1845
1846// Primary pass pipeline description parsing routine for a \c
1847// FunctionPassManager
1848bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1849 StringRef PipelineText, bool VerifyEachPass,
1850 bool DebugLogging) {
1851 auto Pipeline = parsePipelineText(PipelineText);
1852 if (!Pipeline || Pipeline->empty())
1853 return false;
1854
1855 StringRef FirstName = Pipeline->front().Name;
1856 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1857 return false;
1858
1859 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1860 DebugLogging);
1861}
1862
1863// Primary pass pipeline description parsing routine for a \c LoopPassManager
1864bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1865 StringRef PipelineText, bool VerifyEachPass,
1866 bool DebugLogging) {
1867 auto Pipeline = parsePipelineText(PipelineText);
1868 if (!Pipeline || Pipeline->empty())
1869 return false;
1870
1871 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1872}
1873
Chandler Carruthedf59962016-02-18 09:45:17 +00001874bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001875 // If the pipeline just consists of the word 'default' just replace the AA
1876 // manager with our default one.
1877 if (PipelineText == "default") {
1878 AA = buildDefaultAAPipeline();
1879 return true;
1880 }
1881
Chandler Carruthedf59962016-02-18 09:45:17 +00001882 while (!PipelineText.empty()) {
1883 StringRef Name;
1884 std::tie(Name, PipelineText) = PipelineText.split(',');
1885 if (!parseAAPassName(AA, Name))
1886 return false;
1887 }
1888
1889 return true;
1890}