blob: a880befc0d50b786602a31a7a829141186a2f0a1 [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"
Aditya Kumar9e20ade2018-10-03 05:55:20 +000078#include "llvm/Transforms/IPO/HotColdSplitting.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000079#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000080#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000081#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000082#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000083#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000084#include "llvm/Transforms/IPO/SCCP.h"
David Blaikie301627f2018-03-22 22:42:44 +000085#include "llvm/Transforms/IPO/SampleProfile.h"
Justin Bogner21e15372015-10-30 23:28:12 +000086#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000087#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000088#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000089#include "llvm/Transforms/InstCombine/InstCombine.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000090#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +000091#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +000092#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
93#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000094#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000095#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000096#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000097#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000098#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000099#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +0000100#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +0000101#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +0000102#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +0000103#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000104#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000105#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000106#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000107#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000108#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000109#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000110#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +0000111#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
Sean Silva46590d52016-06-14 00:51:09 +0000112#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000113#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000114#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000115#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000116#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000117#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000118#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000119#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000120#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000121#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000122#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000123#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000124#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000125#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000126#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
David Green963401d2018-07-01 12:47:30 +0000127#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000128#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000129#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000130#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000131#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000132#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000133#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000134#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000135#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000136#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000137#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000138#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000139#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000140#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000141#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000142#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000143#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000144#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000145#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000146#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000147#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000148#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000149#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000150#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000151#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000152#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000153#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000154#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000155#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000156#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000157#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000158#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000159
Chandler Carruth66445382014-01-11 08:16:35 +0000160using namespace llvm;
161
Chandler Carruth719ffe12017-02-12 05:38:04 +0000162static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
163 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000164static cl::opt<bool>
165 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
166 cl::Hidden, cl::ZeroOrMore,
167 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000168
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000169static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000170 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000171 cl::Hidden, cl::ZeroOrMore,
172 cl::desc("Run NewGVN instead of GVN"));
173
Geoff Berry3cca1da2017-06-10 15:20:03 +0000174static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000175 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
176 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000177
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000178static cl::opt<bool> EnableGVNHoist(
Eric Christopherdcf1d972018-10-01 18:57:08 +0000179 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
180 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000181
Davide Italianobe1b6a92017-06-03 23:18:29 +0000182static cl::opt<bool> EnableGVNSink(
183 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
184 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
185
David Green963401d2018-07-01 12:47:30 +0000186static cl::opt<bool> EnableUnrollAndJam(
187 "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
188 cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
189
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000190static cl::opt<bool> EnableSyntheticCounts(
191 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
192 cl::desc("Run synthetic function entry count generation "
193 "pass"));
194
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000195static Regex DefaultAliasRegex(
196 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000197
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000198static cl::opt<bool>
199 EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden,
200 cl::desc("Enable control height reduction optimization (CHR)"));
201
Aditya Kumar9e20ade2018-10-03 05:55:20 +0000202extern cl::opt<bool> EnableHotColdSplit;
203
Chandler Carruthe3f50642016-12-22 06:59:15 +0000204static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
205 switch (Level) {
206 case PassBuilder::O0:
207 case PassBuilder::O1:
208 case PassBuilder::O2:
209 case PassBuilder::O3:
210 return false;
211
212 case PassBuilder::Os:
213 case PassBuilder::Oz:
214 return true;
215 }
216 llvm_unreachable("Invalid optimization level!");
217}
218
Chandler Carruth66445382014-01-11 08:16:35 +0000219namespace {
220
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000221/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000222struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000223 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000224 return PreservedAnalyses::all();
225 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000226 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000227};
228
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000229/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000230class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
231 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000232 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000233
234public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000236 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000237 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000238};
239
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000240/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000241struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000242 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
243 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000244 return PreservedAnalyses::all();
245 }
246 static StringRef name() { return "NoOpCGSCCPass"; }
247};
248
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000249/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000250class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
251 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000252 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000253
254public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000255 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000256 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000257 return Result();
258 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000259 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000260};
261
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000262/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000263struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000264 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000265 return PreservedAnalyses::all();
266 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000267 static StringRef name() { return "NoOpFunctionPass"; }
268};
269
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000270/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000271class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
272 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000273 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000274
275public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000276 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000277 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000278 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000279};
280
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000281/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000282struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000283 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
284 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000285 return PreservedAnalyses::all();
286 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000287 static StringRef name() { return "NoOpLoopPass"; }
288};
289
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000290/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000291class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
292 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000293 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000294
295public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000296 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000297 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
298 return Result();
299 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000300 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000301};
302
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000303AnalysisKey NoOpModuleAnalysis::Key;
304AnalysisKey NoOpCGSCCAnalysis::Key;
305AnalysisKey NoOpFunctionAnalysis::Key;
306AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000307
Chandler Carruth66445382014-01-11 08:16:35 +0000308} // End anonymous namespace.
309
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000310void PassBuilder::invokePeepholeEPCallbacks(
311 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
312 for (auto &C : PeepholeEPCallbacks)
313 C(FPM, Level);
314}
315
Chandler Carruth1ff77242015-03-07 09:02:36 +0000316void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000317#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000318 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000319#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000320
321 for (auto &C : ModuleAnalysisRegistrationCallbacks)
322 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000323}
324
Chandler Carruth1ff77242015-03-07 09:02:36 +0000325void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000326#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000327 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000328#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000329
330 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
331 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000332}
333
Chandler Carruth1ff77242015-03-07 09:02:36 +0000334void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000335#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000336 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000337#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000338
339 for (auto &C : FunctionAnalysisRegistrationCallbacks)
340 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000341}
342
Justin Bognereecc3c82016-02-25 07:23:08 +0000343void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000344#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000345 LAM.registerPass([&] { return CREATE_PASS; });
346#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000347
348 for (auto &C : LoopAnalysisRegistrationCallbacks)
349 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000350}
351
Chandler Carruthe3f50642016-12-22 06:59:15 +0000352FunctionPassManager
353PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000354 ThinLTOPhase Phase,
355 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000356 assert(Level != O0 && "Must request optimizations!");
357 FunctionPassManager FPM(DebugLogging);
358
359 // Form SSA out of local memory accesses after breaking apart aggregates into
360 // scalars.
361 FPM.addPass(SROA());
362
363 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000364 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000365
Davide Italianoc3688312017-06-01 23:08:14 +0000366 // Hoisting of scalars and load expressions.
367 if (EnableGVNHoist)
368 FPM.addPass(GVNHoistPass());
369
Davide Italianobe1b6a92017-06-03 23:18:29 +0000370 // Global value numbering based sinking.
371 if (EnableGVNSink) {
372 FPM.addPass(GVNSinkPass());
373 FPM.addPass(SimplifyCFGPass());
374 }
375
Chandler Carruthe3f50642016-12-22 06:59:15 +0000376 // Speculative execution if the target has divergent branches; otherwise nop.
377 FPM.addPass(SpeculativeExecutionPass());
378
379 // Optimize based on known information about branches, and cleanup afterward.
380 FPM.addPass(JumpThreadingPass());
381 FPM.addPass(CorrelatedValuePropagationPass());
382 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000383 if (Level == O3)
384 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000385 FPM.addPass(InstCombinePass());
386
387 if (!isOptimizingForSize(Level))
388 FPM.addPass(LibCallsShrinkWrapPass());
389
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000390 invokePeepholeEPCallbacks(FPM, Level);
391
Rong Xue1f42452017-10-23 22:21:29 +0000392 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
393 // using the size value profile. Don't perform this when optimizing for size.
394 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
395 !isOptimizingForSize(Level))
396 FPM.addPass(PGOMemOPSizeOpt());
397
Chandler Carruthe3f50642016-12-22 06:59:15 +0000398 FPM.addPass(TailCallElimPass());
399 FPM.addPass(SimplifyCFGPass());
400
401 // Form canonically associated expression trees, and simplify the trees using
402 // basic mathematical properties. For example, this will form (nearly)
403 // minimal multiplication trees.
404 FPM.addPass(ReassociatePass());
405
406 // Add the primary loop simplification pipeline.
407 // FIXME: Currently this is split into two loop pass pipelines because we run
Chandler Carruth71fd2702018-05-30 02:46:45 +0000408 // some function passes in between them. These can and should be removed
409 // and/or replaced by scheduling the loop pass equivalents in the correct
410 // positions. But those equivalent passes aren't powerful enough yet.
411 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
412 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
413 // fully replace `SimplifyCFGPass`, and the closest to the other we have is
414 // `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000415 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
416
Chandler Carruth71fd2702018-05-30 02:46:45 +0000417 // Simplify the loop body. We do this initially to clean up after other loop
418 // passes run, either when iterating on a loop or on inner loops with
419 // implications on the outer loop.
420 LPM1.addPass(LoopInstSimplifyPass());
421 LPM1.addPass(LoopSimplifyCFGPass());
422
Chandler Carruthe3f50642016-12-22 06:59:15 +0000423 // Rotate Loop - disable header duplication at -Oz
424 LPM1.addPass(LoopRotatePass(Level != Oz));
425 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000426 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000427 LPM2.addPass(IndVarSimplifyPass());
428 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000429
430 for (auto &C : LateLoopOptimizationsEPCallbacks)
431 C(LPM2, Level);
432
Chandler Carruth79b733b2017-01-26 23:21:17 +0000433 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000434 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000435 // because it changes IR to makes profile annotation in back compile
436 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000437 if (Phase != ThinLTOPhase::PreLink ||
438 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000439 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000440
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000441 for (auto &C : LoopOptimizerEndEPCallbacks)
442 C(LPM2, Level);
443
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000444 // We provide the opt remark emitter pass for LICM to use. We only need to do
445 // this once as it is immutable.
446 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000447 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000448 FPM.addPass(SimplifyCFGPass());
449 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000450 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000451
452 // Eliminate redundancies.
453 if (Level != O1) {
454 // These passes add substantial compile time so skip them at O1.
455 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000456 if (RunNewGVN)
457 FPM.addPass(NewGVNPass());
458 else
459 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000460 }
461
462 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
463 FPM.addPass(MemCpyOptPass());
464
465 // Sparse conditional constant propagation.
466 // FIXME: It isn't clear why we do this *after* loop passes rather than
467 // before...
468 FPM.addPass(SCCPPass());
469
470 // Delete dead bit computations (instcombine runs after to fold away the dead
471 // computations, and then ADCE will run later to exploit any new DCE
472 // opportunities that creates).
473 FPM.addPass(BDCEPass());
474
475 // Run instcombine after redundancy and dead bit elimination to exploit
476 // opportunities opened up by them.
477 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000478 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000479
480 // Re-consider control flow based optimizations after redundancy elimination,
481 // redo DCE, etc.
482 FPM.addPass(JumpThreadingPass());
483 FPM.addPass(CorrelatedValuePropagationPass());
484 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000485 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000486
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000487 for (auto &C : ScalarOptimizerLateEPCallbacks)
488 C(FPM, Level);
489
Chandler Carruthe3f50642016-12-22 06:59:15 +0000490 // Finally, do an expensive DCE pass to catch all the dead code exposed by
491 // the simplifications and basic cleanup after all the simplifications.
492 FPM.addPass(ADCEPass());
493 FPM.addPass(SimplifyCFGPass());
494 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000495 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000496
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000497 if (EnableCHR && Level == O3 && PGOOpt &&
498 (!PGOOpt->ProfileUseFile.empty() || !PGOOpt->SampleProfileFile.empty()))
499 FPM.addPass(ControlHeightReductionPass());
500
Chandler Carruthe3f50642016-12-22 06:59:15 +0000501 return FPM;
502}
503
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000504void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
505 PassBuilder::OptimizationLevel Level,
506 bool RunProfileGen,
507 std::string ProfileGenFile,
508 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000509 // Generally running simplification passes and the inliner with an high
510 // threshold results in smaller executables, but there may be cases where
511 // the size grows, so let's be conservative here and skip this simplification
512 // at -Os/Oz.
513 if (!isOptimizingForSize(Level)) {
514 InlineParams IP;
515
516 // In the old pass manager, this is a cl::opt. Should still this be one?
517 IP.DefaultThreshold = 75;
518
519 // FIXME: The hint threshold has the same value used by the regular inliner.
520 // This should probably be lowered after performance testing.
521 // FIXME: this comment is cargo culted from the old pass manager, revisit).
522 IP.HintThreshold = 325;
523
524 CGSCCPassManager CGPipeline(DebugLogging);
525
526 CGPipeline.addPass(InlinerPass(IP));
527
528 FunctionPassManager FPM;
529 FPM.addPass(SROA());
530 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
531 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
532 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000533 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000534
Davide Italiano513dfaa2017-02-13 15:26:22 +0000535 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
536
537 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
538 }
539
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000540 // Delete anything that is now dead to make sure that we don't instrument
541 // dead code. Instrumentation can end up keeping dead code around and
542 // dramatically increase code size.
543 MPM.addPass(GlobalDCEPass());
544
Davide Italiano513dfaa2017-02-13 15:26:22 +0000545 if (RunProfileGen) {
546 MPM.addPass(PGOInstrumentationGen());
547
Xinliang David Lib67530e2017-06-25 00:26:43 +0000548 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000549 FPM.addPass(
550 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000551 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
552
Davide Italiano513dfaa2017-02-13 15:26:22 +0000553 // Add the profile lowering pass.
554 InstrProfOptions Options;
555 if (!ProfileGenFile.empty())
556 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000557 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000558 MPM.addPass(InstrProfiling(Options));
559 }
560
561 if (!ProfileUseFile.empty())
562 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
563}
564
Easwaran Raman8249fac2017-06-28 13:33:49 +0000565static InlineParams
566getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
567 auto O3 = PassBuilder::O3;
568 unsigned OptLevel = Level > O3 ? 2 : Level;
569 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
570 return getInlineParams(OptLevel, SizeLevel);
571}
572
Chandler Carruthe3f50642016-12-22 06:59:15 +0000573ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000574PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000575 ThinLTOPhase Phase,
576 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000577 ModulePassManager MPM(DebugLogging);
578
Chandler Carruthe3f50642016-12-22 06:59:15 +0000579 // Do basic inference of function attributes from known properties of system
580 // libraries and other oracles.
581 MPM.addPass(InferFunctionAttrsPass());
582
583 // Create an early function pass manager to cleanup the output of the
584 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000585 FunctionPassManager EarlyFPM(DebugLogging);
586 EarlyFPM.addPass(SimplifyCFGPass());
587 EarlyFPM.addPass(SROA());
588 EarlyFPM.addPass(EarlyCSEPass());
589 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000590 if (Level == O3)
591 EarlyFPM.addPass(CallSiteSplittingPass());
592
Dehao Chen08f88312017-08-07 20:23:20 +0000593 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
594 // to convert bitcast to direct calls so that they can be inlined during the
595 // profile annotation prepration step.
596 // More details about SamplePGO design can be found in:
597 // https://research.google.com/pubs/pub45290.html
598 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
599 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
600 Phase == ThinLTOPhase::PostLink)
601 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000602 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000603
Dehao Chen08f88312017-08-07 20:23:20 +0000604 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
605 // Annotate sample profile right after early FPM to ensure freshness of
606 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000607 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
608 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000609 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
610 // for the profile annotation to be accurate in the ThinLTO backend.
611 if (Phase != ThinLTOPhase::PreLink)
612 // We perform early indirect call promotion here, before globalopt.
613 // This is important for the ThinLTO backend phase because otherwise
614 // imported available_externally functions look unreferenced and are
615 // removed.
616 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
617 true));
618 }
619
Aditya Kumar9e20ade2018-10-03 05:55:20 +0000620 if (EnableHotColdSplit)
621 MPM.addPass(HotColdSplittingPass());
622
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000623 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000624 // and prior to optimizing globals.
625 // FIXME: This position in the pipeline hasn't been carefully considered in
626 // years, it should be re-analyzed.
627 MPM.addPass(IPSCCPPass());
628
Matthew Simpsoncb585582017-10-25 13:40:08 +0000629 // Attach metadata to indirect call sites indicating the set of functions
630 // they may target at run-time. This should follow IPSCCP.
631 MPM.addPass(CalledValuePropagationPass());
632
Chandler Carruthe3f50642016-12-22 06:59:15 +0000633 // Optimize globals to try and fold them into constants.
634 MPM.addPass(GlobalOptPass());
635
636 // Promote any localized globals to SSA registers.
637 // FIXME: Should this instead by a run of SROA?
638 // FIXME: We should probably run instcombine and simplify-cfg afterward to
639 // delete control flows that are dead once globals have been folded to
640 // constants.
641 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
642
643 // Remove any dead arguments exposed by cleanups and constand folding
644 // globals.
645 MPM.addPass(DeadArgumentEliminationPass());
646
647 // Create a small function pass pipeline to cleanup after all the global
648 // optimizations.
649 FunctionPassManager GlobalCleanupPM(DebugLogging);
650 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000651 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
652
Chandler Carruthe3f50642016-12-22 06:59:15 +0000653 GlobalCleanupPM.addPass(SimplifyCFGPass());
654 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
655
Dehao Chen89d32262017-08-02 01:28:31 +0000656 // Add all the requested passes for instrumentation PGO, if requested.
657 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
658 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
659 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
660 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
661 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000662 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000663
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000664 // Synthesize function entry counts for non-PGO compilation.
665 if (EnableSyntheticCounts && !PGOOpt)
666 MPM.addPass(SyntheticCountsPropagation());
667
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000668 // Require the GlobalsAA analysis for the module so we can query it within
669 // the CGSCC pipeline.
670 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000671
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000672 // Require the ProfileSummaryAnalysis for the module so we can query it within
673 // the inliner pass.
674 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
675
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676 // Now begin the main postorder CGSCC pipeline.
677 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
678 // manager and trying to emulate its precise behavior. Much of this doesn't
679 // make a lot of sense and we should revisit the core CGSCC structure.
680 CGSCCPassManager MainCGPipeline(DebugLogging);
681
682 // Note: historically, the PruneEH pass was run first to deduce nounwind and
683 // generally clean up exception handling overhead. It isn't clear this is
684 // valuable as the inliner doesn't currently care whether it is inlining an
685 // invoke or a call.
686
687 // Run the inliner first. The theory is that we are walking bottom-up and so
688 // the callees have already been fully optimized, and we want to inline them
689 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000690 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000691 // because it makes profile annotation in the backend inaccurate.
692 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000693 if (Phase == ThinLTOPhase::PreLink &&
694 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000695 IP.HotCallSiteThreshold = 0;
696 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000697
698 // Now deduce any function attributes based in the current code.
699 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
700
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000701 // When at O3 add argument promotion to the pass pipeline.
702 // FIXME: It isn't at all clear why this should be limited to O3.
703 if (Level == O3)
704 MainCGPipeline.addPass(ArgumentPromotionPass());
705
Chandler Carruthe3f50642016-12-22 06:59:15 +0000706 // Lastly, add the core function simplification pipeline nested inside the
707 // CGSCC walk.
708 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000709 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000710
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000711 for (auto &C : CGSCCOptimizerLateEPCallbacks)
712 C(MainCGPipeline, Level);
713
Chandler Carruth719ffe12017-02-12 05:38:04 +0000714 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
715 // to detect when we devirtualize indirect calls and iterate the SCC passes
716 // in that case to try and catch knock-on inlining or function attrs
717 // opportunities. Then we add it to the module pipeline by walking the SCCs
718 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000719 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000720 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000721 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000722
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000723 return MPM;
724}
725
726ModulePassManager
727PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
728 bool DebugLogging) {
729 ModulePassManager MPM(DebugLogging);
730
731 // Optimize globals now that the module is fully simplified.
732 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000733 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000734
Xinliang David Li126157c2017-05-22 16:41:57 +0000735 // Run partial inlining pass to partially inline functions that have
736 // large bodies.
737 if (RunPartialInlining)
738 MPM.addPass(PartialInlinerPass());
739
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000740 // Remove avail extern fns and globals definitions since we aren't compiling
741 // an object file for later LTO. For LTO we want to preserve these so they
742 // are eligible for inlining at link-time. Note if they are unreferenced they
743 // will be removed by GlobalDCE later, so this only impacts referenced
744 // available externally globals. Eventually they will be suppressed during
745 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
746 // may make globals referenced by available external functions dead and saves
747 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000748 MPM.addPass(EliminateAvailableExternallyPass());
749
750 // Do RPO function attribute inference across the module to forward-propagate
751 // attributes where applicable.
752 // FIXME: Is this really an optimization rather than a canonicalization?
753 MPM.addPass(ReversePostOrderFunctionAttrsPass());
754
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000755 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000756 // useful as the above will have inlined, DCE'ed, and function-attr
757 // propagated everything. We should at this point have a reasonably minimal
758 // and richly annotated call graph. By computing aliasing and mod/ref
759 // information for all local globals here, the late loop passes and notably
760 // the vectorizer will be able to use them to help recognize vectorizable
761 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000762 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000763
764 FunctionPassManager OptimizePM(DebugLogging);
765 OptimizePM.addPass(Float2IntPass());
766 // FIXME: We need to run some loop optimizations to re-rotate loops after
767 // simplify-cfg and others undo their rotation.
768
769 // Optimize the loop execution. These passes operate on entire loop nests
770 // rather than on each loop in an inside-out manner, and so they are actually
771 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000772
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000773 for (auto &C : VectorizerStartEPCallbacks)
774 C(OptimizePM, Level);
775
Chandler Carrutha95ff382017-01-27 00:50:21 +0000776 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000777 OptimizePM.addPass(
778 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000779
780 // Distribute loops to allow partial vectorization. I.e. isolate dependences
781 // into separate loop that would otherwise inhibit vectorization. This is
782 // currently only performed for loops marked with the metadata
783 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000784 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000785
786 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000787 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000788
Chandler Carruthbaabda92017-01-27 01:32:26 +0000789 // Eliminate loads by forwarding stores from the previous iteration to loads
790 // of the current iteration.
791 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000792
793 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000794 OptimizePM.addPass(InstCombinePass());
795
Chandler Carrutha95ff382017-01-27 00:50:21 +0000796 // Now that we've formed fast to execute loop structures, we do further
797 // optimizations. These are run afterward as they might block doing complex
798 // analyses and transforms such as what are needed for loop vectorization.
799
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000800 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
801 // GVN, loop transforms, and others have already run, so it's now better to
802 // convert to more optimized IR using more aggressive simplify CFG options.
803 // The extra sinking transform can create larger basic blocks, so do this
804 // before SLP vectorization.
805 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
806 forwardSwitchCondToPhi(true).
807 convertSwitchToLookupTable(true).
808 needCanonicalLoops(false).
809 sinkCommonInsts(true)));
810
Chandler Carruthe3f50642016-12-22 06:59:15 +0000811 // Optimize parallel scalar instruction chains into SIMD instructions.
812 OptimizePM.addPass(SLPVectorizerPass());
813
Chandler Carruthe3f50642016-12-22 06:59:15 +0000814 OptimizePM.addPass(InstCombinePass());
815
816 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000817 // execution resources of an out-of-order processor. We also then need to
818 // clean up redundancies and loop invariant code.
819 // FIXME: It would be really good to use a loop-integrated instruction
820 // combiner for cleanup here so that the unrolling and LICM can be pipelined
821 // across the loop nests.
David Green963401d2018-07-01 12:47:30 +0000822 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
823 if (EnableUnrollAndJam) {
824 OptimizePM.addPass(
825 createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
826 }
Teresa Johnsonecd90132017-08-02 20:35:29 +0000827 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000828 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000829 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000830 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000831
832 // Now that we've vectorized and unrolled loops, we may have more refined
833 // alignment information, try to re-derive it here.
834 OptimizePM.addPass(AlignmentFromAssumptionsPass());
835
Chandler Carrutha95ff382017-01-27 00:50:21 +0000836 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
837 // canonicalization pass that enables other optimizations. As a result,
838 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
839 // result too early.
840 OptimizePM.addPass(LoopSinkPass());
841
842 // And finally clean up LCSSA form before generating code.
Chandler Carruth7c557f82018-06-29 23:36:03 +0000843 OptimizePM.addPass(InstSimplifyPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000844
Sanjay Patel6fd43912017-09-09 13:38:18 +0000845 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
846 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
847 // flattening of blocks.
848 OptimizePM.addPass(DivRemPairsPass());
849
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000850 // LoopSink (and other loop passes since the last simplifyCFG) might have
851 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
852 OptimizePM.addPass(SimplifyCFGPass());
853
Chandler Carruthc34f7892017-11-28 11:32:31 +0000854 // Optimize PHIs by speculating around them when profitable. Note that this
855 // pass needs to be run after any PRE or similar pass as it is essentially
856 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
857 OptimizePM.addPass(SpeculateAroundPHIsPass());
858
Chandler Carrutha95ff382017-01-27 00:50:21 +0000859 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000860 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
861
Michael J. Spencer7bb27672018-07-16 00:28:24 +0000862 MPM.addPass(CGProfilePass());
863
Chandler Carruthe3f50642016-12-22 06:59:15 +0000864 // Now we need to do some global optimization transforms.
865 // FIXME: It would seem like these should come first in the optimization
866 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
867 // ordering here.
868 MPM.addPass(GlobalDCEPass());
869 MPM.addPass(ConstantMergePass());
870
871 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000872}
873
Chandler Carruthe3f50642016-12-22 06:59:15 +0000874ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000875PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
876 bool DebugLogging) {
877 assert(Level != O0 && "Must request optimizations for the default pipeline!");
878
879 ModulePassManager MPM(DebugLogging);
880
881 // Force any function attributes we want the rest of the pipeline to observe.
882 MPM.addPass(ForceFunctionAttrsPass());
883
David Blaikie0c64f5a2018-01-23 01:25:20 +0000884 // Apply module pipeline start EP callback.
885 for (auto &C : PipelineStartEPCallbacks)
886 C(MPM);
887
Dehao Chen08f88312017-08-07 20:23:20 +0000888 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000889 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
890
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000891 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000892 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
893 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000894
895 // Now add the optimization pipeline.
896 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
897
898 return MPM;
899}
900
901ModulePassManager
902PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
903 bool DebugLogging) {
904 assert(Level != O0 && "Must request optimizations for the default pipeline!");
905
906 ModulePassManager MPM(DebugLogging);
907
908 // Force any function attributes we want the rest of the pipeline to observe.
909 MPM.addPass(ForceFunctionAttrsPass());
910
Dehao Chen08f88312017-08-07 20:23:20 +0000911 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000912 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
913
David Blaikie0c64f5a2018-01-23 01:25:20 +0000914 // Apply module pipeline start EP callback.
915 for (auto &C : PipelineStartEPCallbacks)
916 C(MPM);
917
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000918 // If we are planning to perform ThinLTO later, we don't bloat the code with
919 // unrolling/vectorization/... now. Just simplify the module as much as we
920 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000921 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
922 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000923
924 // Run partial inlining pass to partially inline functions that have
925 // large bodies.
926 // FIXME: It isn't clear whether this is really the right place to run this
927 // in ThinLTO. Because there is another canonicalization and simplification
928 // phase that will run after the thin link, running this here ends up with
929 // less information than will be available later and it may grow functions in
930 // ways that aren't beneficial.
931 if (RunPartialInlining)
932 MPM.addPass(PartialInlinerPass());
933
934 // Reduce the size of the IR as much as possible.
935 MPM.addPass(GlobalOptPass());
936
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000937 return MPM;
938}
939
Teresa Johnson28023db2018-07-19 14:51:32 +0000940ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
941 OptimizationLevel Level, bool DebugLogging,
942 const ModuleSummaryIndex *ImportSummary) {
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000943 ModulePassManager MPM(DebugLogging);
944
Teresa Johnson28023db2018-07-19 14:51:32 +0000945 if (ImportSummary) {
946 // These passes import type identifier resolutions for whole-program
947 // devirtualization and CFI. They must run early because other passes may
948 // disturb the specific instruction patterns that these passes look for,
949 // creating dependencies on resolutions that may not appear in the summary.
950 //
951 // For example, GVN may transform the pattern assume(type.test) appearing in
952 // two basic blocks into assume(phi(type.test, type.test)), which would
953 // transform a dependency on a WPD resolution into a dependency on a type
954 // identifier resolution for CFI.
955 //
956 // Also, WPD has access to more precise information than ICP and can
957 // devirtualize more effectively, so it should operate on the IR first.
958 MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
959 MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
960 }
961
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000962 // Force any function attributes we want the rest of the pipeline to observe.
963 MPM.addPass(ForceFunctionAttrsPass());
964
965 // During the ThinLTO backend phase we perform early indirect call promotion
966 // here, before globalopt. Otherwise imported available_externally functions
967 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000968 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
969 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000970 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000971 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
972 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000973
974 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000975 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
976 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000977
978 // Now add the optimization pipeline.
979 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
980
981 return MPM;
982}
983
984ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000985PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
986 bool DebugLogging) {
987 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000988 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000989 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000990}
991
Teresa Johnson28023db2018-07-19 14:51:32 +0000992ModulePassManager
993PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
994 ModuleSummaryIndex *ExportSummary) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000995 assert(Level != O0 && "Must request optimizations for the default pipeline!");
996 ModulePassManager MPM(DebugLogging);
997
Davide Italiano089a9122017-01-24 00:57:39 +0000998 // Remove unused virtual tables to improve the quality of code generated by
999 // whole-program devirtualization and bitset lowering.
1000 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001001
Davide Italiano089a9122017-01-24 00:57:39 +00001002 // Force any function attributes we want the rest of the pipeline to observe.
1003 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +00001004
Davide Italiano089a9122017-01-24 00:57:39 +00001005 // Do basic inference of function attributes from known properties of system
1006 // libraries and other oracles.
1007 MPM.addPass(InferFunctionAttrsPass());
1008
1009 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +00001010 FunctionPassManager EarlyFPM(DebugLogging);
1011 EarlyFPM.addPass(CallSiteSplittingPass());
1012 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
1013
Davide Italiano089a9122017-01-24 00:57:39 +00001014 // Indirect call promotion. This should promote all the targets that are
1015 // left by the earlier promotion pass that promotes intra-module targets.
1016 // This two-step promotion is to save the compile time. For LTO, it should
1017 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +00001018 MPM.addPass(PGOIndirectCallPromotion(
1019 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +00001020 // Propagate constants at call sites into the functions they call. This
1021 // opens opportunities for globalopt (and inlining) by substituting function
1022 // pointers passed as arguments to direct uses of functions.
1023 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +00001024
1025 // Attach metadata to indirect call sites indicating the set of functions
1026 // they may target at run-time. This should follow IPSCCP.
1027 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +00001028 }
1029
1030 // Now deduce any function attributes based in the current code.
1031 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1032 PostOrderFunctionAttrsPass()));
1033
1034 // Do RPO function attribute inference across the module to forward-propagate
1035 // attributes where applicable.
1036 // FIXME: Is this really an optimization rather than a canonicalization?
1037 MPM.addPass(ReversePostOrderFunctionAttrsPass());
1038
1039 // Use inragne annotations on GEP indices to split globals where beneficial.
1040 MPM.addPass(GlobalSplitPass());
1041
1042 // Run whole program optimization of virtual call when the list of callees
1043 // is fixed.
Teresa Johnson28023db2018-07-19 14:51:32 +00001044 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001045
1046 // Stop here at -O1.
Teresa Johnson28023db2018-07-19 14:51:32 +00001047 if (Level == 1) {
1048 // The LowerTypeTestsPass needs to run to lower type metadata and the
1049 // type.test intrinsics. The pass does nothing if CFI is disabled.
1050 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001051 return MPM;
Teresa Johnson28023db2018-07-19 14:51:32 +00001052 }
Davide Italiano089a9122017-01-24 00:57:39 +00001053
1054 // Optimize globals to try and fold them into constants.
1055 MPM.addPass(GlobalOptPass());
1056
1057 // Promote any localized globals to SSA registers.
1058 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1059
1060 // Linking modules together can lead to duplicate global constant, only
1061 // keep one copy of each constant.
1062 MPM.addPass(ConstantMergePass());
1063
1064 // Remove unused arguments from functions.
1065 MPM.addPass(DeadArgumentEliminationPass());
1066
1067 // Reduce the code after globalopt and ipsccp. Both can open up significant
1068 // simplification opportunities, and both can propagate functions through
1069 // function pointers. When this happens, we often have to resolve varargs
1070 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001071 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001072 if (Level == O3)
1073 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001074 PeepholeFPM.addPass(InstCombinePass());
1075 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1076
1077 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001078
1079 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1080 // generally clean up exception handling overhead. It isn't clear this is
1081 // valuable as the inliner doesn't currently care whether it is inlining an
1082 // invoke or a call.
1083 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001084 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1085 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001086
1087 // Optimize globals again after we ran the inliner.
1088 MPM.addPass(GlobalOptPass());
1089
1090 // Garbage collect dead functions.
1091 // FIXME: Add ArgumentPromotion pass after once it's ported.
1092 MPM.addPass(GlobalDCEPass());
1093
1094 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001095 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001096 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001097 invokePeepholeEPCallbacks(FPM, Level);
1098
Davide Italiano089a9122017-01-24 00:57:39 +00001099 FPM.addPass(JumpThreadingPass());
1100
1101 // Break up allocas
1102 FPM.addPass(SROA());
1103
1104 // Run a few AA driver optimizations here and now to cleanup the code.
1105 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1106
1107 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1108 PostOrderFunctionAttrsPass()));
1109 // FIXME: here we run IP alias analysis in the legacy PM.
1110
1111 FunctionPassManager MainFPM;
1112
1113 // FIXME: once we fix LoopPass Manager, add LICM here.
1114 // FIXME: once we provide support for enabling MLSM, add it here.
1115 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001116 if (RunNewGVN)
1117 MainFPM.addPass(NewGVNPass());
1118 else
1119 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001120
1121 // Remove dead memcpy()'s.
1122 MainFPM.addPass(MemCpyOptPass());
1123
1124 // Nuke dead stores.
1125 MainFPM.addPass(DSEPass());
1126
1127 // FIXME: at this point, we run a bunch of loop passes:
1128 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1129 // loopVectorize. Enable them once the remaining issue with LPM
1130 // are sorted out.
1131
1132 MainFPM.addPass(InstCombinePass());
1133 MainFPM.addPass(SimplifyCFGPass());
1134 MainFPM.addPass(SCCPPass());
1135 MainFPM.addPass(InstCombinePass());
1136 MainFPM.addPass(BDCEPass());
1137
1138 // FIXME: We may want to run SLPVectorizer here.
1139 // After vectorization, assume intrinsics may tell us more
1140 // about pointer alignments.
1141#if 0
1142 MainFPM.add(AlignmentFromAssumptionsPass());
1143#endif
1144
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001145 // FIXME: Conditionally run LoadCombine here, after it's ported
1146 // (in case we still have this pass, given its questionable usefulness).
1147
Davide Italiano089a9122017-01-24 00:57:39 +00001148 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001149 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001150 MainFPM.addPass(JumpThreadingPass());
1151 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1152
1153 // Create a function that performs CFI checks for cross-DSO calls with
1154 // targets in the current module.
1155 MPM.addPass(CrossDSOCFIPass());
1156
1157 // Lower type metadata and the type.test intrinsic. This pass supports
1158 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1159 // to be run at link time if CFI is enabled. This pass does nothing if
1160 // CFI is disabled.
Teresa Johnson28023db2018-07-19 14:51:32 +00001161 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001162
1163 // Add late LTO optimization passes.
1164 // Delete basic blocks, which optimization passes may have killed.
1165 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1166
1167 // Drop bodies of available eternally objects to improve GlobalDCE.
1168 MPM.addPass(EliminateAvailableExternallyPass());
1169
1170 // Now that we have optimized the program, discard unreachable functions.
1171 MPM.addPass(GlobalDCEPass());
1172
1173 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001174 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001175}
1176
Chandler Carruth060ad612016-12-23 20:38:19 +00001177AAManager PassBuilder::buildDefaultAAPipeline() {
1178 AAManager AA;
1179
1180 // The order in which these are registered determines their priority when
1181 // being queried.
1182
1183 // First we register the basic alias analysis that provides the majority of
1184 // per-function local AA logic. This is a stateless, on-demand local set of
1185 // AA techniques.
1186 AA.registerFunctionAnalysis<BasicAA>();
1187
1188 // Next we query fast, specialized alias analyses that wrap IR-embedded
1189 // information about aliasing.
1190 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1191 AA.registerFunctionAnalysis<TypeBasedAA>();
1192
1193 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001194 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1195 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001196 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001197 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001198
1199 return AA;
1200}
1201
Chandler Carruth241bf242016-08-03 07:44:48 +00001202static Optional<int> parseRepeatPassName(StringRef Name) {
1203 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1204 return None;
1205 int Count;
1206 if (Name.getAsInteger(0, Count) || Count <= 0)
1207 return None;
1208 return Count;
1209}
1210
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001211static Optional<int> parseDevirtPassName(StringRef Name) {
1212 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1213 return None;
1214 int Count;
1215 if (Name.getAsInteger(0, Count) || Count <= 0)
1216 return None;
1217 return Count;
1218}
1219
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001220/// Tests whether a pass name starts with a valid prefix for a default pipeline
1221/// alias.
1222static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1223 return Name.startswith("default") || Name.startswith("thinlto") ||
1224 Name.startswith("lto");
1225}
1226
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001227/// Tests whether registered callbacks will accept a given pass name.
1228///
1229/// When parsing a pipeline text, the type of the outermost pipeline may be
1230/// omitted, in which case the type is automatically determined from the first
1231/// pass name in the text. This may be a name that is handled through one of the
1232/// callbacks. We check this through the oridinary parsing callbacks by setting
1233/// up a dummy PassManager in order to not force the client to also handle this
1234/// type of query.
1235template <typename PassManagerT, typename CallbacksT>
1236static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1237 if (!Callbacks.empty()) {
1238 PassManagerT DummyPM;
1239 for (auto &CB : Callbacks)
1240 if (CB(Name, DummyPM, {}))
1241 return true;
1242 }
1243 return false;
1244}
1245
1246template <typename CallbacksT>
1247static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001248 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001249 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001250 return DefaultAliasRegex.match(Name);
1251
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001252 // Explicitly handle pass manager names.
1253 if (Name == "module")
1254 return true;
1255 if (Name == "cgscc")
1256 return true;
1257 if (Name == "function")
1258 return true;
1259
Chandler Carruth241bf242016-08-03 07:44:48 +00001260 // Explicitly handle custom-parsed pass names.
1261 if (parseRepeatPassName(Name))
1262 return true;
1263
Chandler Carruth74a8a222016-06-17 07:15:29 +00001264#define MODULE_PASS(NAME, CREATE_PASS) \
1265 if (Name == NAME) \
1266 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001267#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001268 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001269 return true;
1270#include "PassRegistry.def"
1271
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001272 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001273}
1274
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001275template <typename CallbacksT>
1276static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001277 // Explicitly handle pass manager names.
1278 if (Name == "cgscc")
1279 return true;
1280 if (Name == "function")
1281 return true;
1282
Chandler Carruth241bf242016-08-03 07:44:48 +00001283 // Explicitly handle custom-parsed pass names.
1284 if (parseRepeatPassName(Name))
1285 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001286 if (parseDevirtPassName(Name))
1287 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001288
Chandler Carruth74a8a222016-06-17 07:15:29 +00001289#define CGSCC_PASS(NAME, CREATE_PASS) \
1290 if (Name == NAME) \
1291 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001292#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001293 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001294 return true;
1295#include "PassRegistry.def"
1296
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001297 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001298}
1299
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001300template <typename CallbacksT>
1301static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001302 // Explicitly handle pass manager names.
1303 if (Name == "function")
1304 return true;
1305 if (Name == "loop")
1306 return true;
1307
Chandler Carruth241bf242016-08-03 07:44:48 +00001308 // Explicitly handle custom-parsed pass names.
1309 if (parseRepeatPassName(Name))
1310 return true;
1311
Chandler Carruth74a8a222016-06-17 07:15:29 +00001312#define FUNCTION_PASS(NAME, CREATE_PASS) \
1313 if (Name == NAME) \
1314 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001315#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001316 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001317 return true;
1318#include "PassRegistry.def"
1319
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001320 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001321}
1322
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001323template <typename CallbacksT>
1324static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001325 // Explicitly handle pass manager names.
1326 if (Name == "loop")
1327 return true;
1328
Chandler Carruth241bf242016-08-03 07:44:48 +00001329 // Explicitly handle custom-parsed pass names.
1330 if (parseRepeatPassName(Name))
1331 return true;
1332
Chandler Carruth74a8a222016-06-17 07:15:29 +00001333#define LOOP_PASS(NAME, CREATE_PASS) \
1334 if (Name == NAME) \
1335 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001336#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1337 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1338 return true;
1339#include "PassRegistry.def"
1340
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001341 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001342}
1343
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001344Optional<std::vector<PassBuilder::PipelineElement>>
1345PassBuilder::parsePipelineText(StringRef Text) {
1346 std::vector<PipelineElement> ResultPipeline;
1347
1348 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1349 &ResultPipeline};
1350 for (;;) {
1351 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1352 size_t Pos = Text.find_first_of(",()");
1353 Pipeline.push_back({Text.substr(0, Pos), {}});
1354
1355 // If we have a single terminating name, we're done.
1356 if (Pos == Text.npos)
1357 break;
1358
1359 char Sep = Text[Pos];
1360 Text = Text.substr(Pos + 1);
1361 if (Sep == ',')
1362 // Just a name ending in a comma, continue.
1363 continue;
1364
1365 if (Sep == '(') {
1366 // Push the inner pipeline onto the stack to continue processing.
1367 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1368 continue;
1369 }
1370
1371 assert(Sep == ')' && "Bogus separator!");
1372 // When handling the close parenthesis, we greedily consume them to avoid
1373 // empty strings in the pipeline.
1374 do {
1375 // If we try to pop the outer pipeline we have unbalanced parentheses.
1376 if (PipelineStack.size() == 1)
1377 return None;
1378
1379 PipelineStack.pop_back();
1380 } while (Text.consume_front(")"));
1381
1382 // Check if we've finished parsing.
1383 if (Text.empty())
1384 break;
1385
1386 // Otherwise, the end of an inner pipeline always has to be followed by
1387 // a comma, and then we can continue.
1388 if (!Text.consume_front(","))
1389 return None;
1390 }
1391
1392 if (PipelineStack.size() > 1)
1393 // Unbalanced paretheses.
1394 return None;
1395
1396 assert(PipelineStack.back() == &ResultPipeline &&
1397 "Wrong pipeline at the bottom of the stack!");
1398 return {std::move(ResultPipeline)};
1399}
1400
1401bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1402 const PipelineElement &E, bool VerifyEachPass,
1403 bool DebugLogging) {
1404 auto &Name = E.Name;
1405 auto &InnerPipeline = E.InnerPipeline;
1406
1407 // First handle complex passes like the pass managers which carry pipelines.
1408 if (!InnerPipeline.empty()) {
1409 if (Name == "module") {
1410 ModulePassManager NestedMPM(DebugLogging);
1411 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1412 DebugLogging))
1413 return false;
1414 MPM.addPass(std::move(NestedMPM));
1415 return true;
1416 }
1417 if (Name == "cgscc") {
1418 CGSCCPassManager CGPM(DebugLogging);
1419 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1420 DebugLogging))
1421 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001422 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001423 return true;
1424 }
1425 if (Name == "function") {
1426 FunctionPassManager FPM(DebugLogging);
1427 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1428 DebugLogging))
1429 return false;
1430 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1431 return true;
1432 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001433 if (auto Count = parseRepeatPassName(Name)) {
1434 ModulePassManager NestedMPM(DebugLogging);
1435 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1436 DebugLogging))
1437 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001438 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001439 return true;
1440 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001441
1442 for (auto &C : ModulePipelineParsingCallbacks)
1443 if (C(Name, MPM, InnerPipeline))
1444 return true;
1445
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001446 // Normal passes can't have pipelines.
1447 return false;
1448 }
1449
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001450 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001451 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001452 SmallVector<StringRef, 3> Matches;
1453 if (!DefaultAliasRegex.match(Name, &Matches))
1454 return false;
1455 assert(Matches.size() == 3 && "Must capture two matched strings!");
1456
Jordan Rosef85a95f2016-07-25 18:34:51 +00001457 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001458 .Case("O0", O0)
1459 .Case("O1", O1)
1460 .Case("O2", O2)
1461 .Case("O3", O3)
1462 .Case("Os", Os)
1463 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001464 if (L == O0)
1465 // At O0 we do nothing at all!
1466 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001467
1468 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001469 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001470 } else if (Matches[1] == "thinlto-pre-link") {
1471 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1472 } else if (Matches[1] == "thinlto") {
Teresa Johnson28023db2018-07-19 14:51:32 +00001473 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001474 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001475 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001476 } else {
1477 assert(Matches[1] == "lto" && "Not one of the matched options!");
Teresa Johnson28023db2018-07-19 14:51:32 +00001478 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001479 }
1480 return true;
1481 }
1482
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001483 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001484#define MODULE_PASS(NAME, CREATE_PASS) \
1485 if (Name == NAME) { \
1486 MPM.addPass(CREATE_PASS); \
1487 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001488 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001489#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1490 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001491 MPM.addPass( \
1492 RequireAnalysisPass< \
1493 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001494 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001495 } \
1496 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001497 MPM.addPass(InvalidateAnalysisPass< \
1498 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001499 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001500 }
1501#include "PassRegistry.def"
1502
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001503 for (auto &C : ModulePipelineParsingCallbacks)
1504 if (C(Name, MPM, InnerPipeline))
1505 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001506 return false;
1507}
1508
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001509bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1510 const PipelineElement &E, bool VerifyEachPass,
1511 bool DebugLogging) {
1512 auto &Name = E.Name;
1513 auto &InnerPipeline = E.InnerPipeline;
1514
1515 // First handle complex passes like the pass managers which carry pipelines.
1516 if (!InnerPipeline.empty()) {
1517 if (Name == "cgscc") {
1518 CGSCCPassManager NestedCGPM(DebugLogging);
1519 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1520 DebugLogging))
1521 return false;
1522 // Add the nested pass manager with the appropriate adaptor.
1523 CGPM.addPass(std::move(NestedCGPM));
1524 return true;
1525 }
1526 if (Name == "function") {
1527 FunctionPassManager FPM(DebugLogging);
1528 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1529 DebugLogging))
1530 return false;
1531 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001532 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001533 return true;
1534 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001535 if (auto Count = parseRepeatPassName(Name)) {
1536 CGSCCPassManager NestedCGPM(DebugLogging);
1537 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1538 DebugLogging))
1539 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001540 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001541 return true;
1542 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001543 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1544 CGSCCPassManager NestedCGPM(DebugLogging);
1545 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1546 DebugLogging))
1547 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001548 CGPM.addPass(
1549 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001550 return true;
1551 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001552
1553 for (auto &C : CGSCCPipelineParsingCallbacks)
1554 if (C(Name, CGPM, InnerPipeline))
1555 return true;
1556
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001557 // Normal passes can't have pipelines.
1558 return false;
1559 }
1560
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001561// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001562#define CGSCC_PASS(NAME, CREATE_PASS) \
1563 if (Name == NAME) { \
1564 CGPM.addPass(CREATE_PASS); \
1565 return true; \
1566 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001567#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1568 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001569 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001570 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001571 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1572 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001573 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001574 } \
1575 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001576 CGPM.addPass(InvalidateAnalysisPass< \
1577 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001578 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001579 }
1580#include "PassRegistry.def"
1581
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001582 for (auto &C : CGSCCPipelineParsingCallbacks)
1583 if (C(Name, CGPM, InnerPipeline))
1584 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001585 return false;
1586}
1587
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001588bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1589 const PipelineElement &E,
1590 bool VerifyEachPass, bool DebugLogging) {
1591 auto &Name = E.Name;
1592 auto &InnerPipeline = E.InnerPipeline;
1593
1594 // First handle complex passes like the pass managers which carry pipelines.
1595 if (!InnerPipeline.empty()) {
1596 if (Name == "function") {
1597 FunctionPassManager NestedFPM(DebugLogging);
1598 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1599 DebugLogging))
1600 return false;
1601 // Add the nested pass manager with the appropriate adaptor.
1602 FPM.addPass(std::move(NestedFPM));
1603 return true;
1604 }
1605 if (Name == "loop") {
1606 LoopPassManager LPM(DebugLogging);
1607 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1608 DebugLogging))
1609 return false;
1610 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001611 FPM.addPass(
1612 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001613 return true;
1614 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001615 if (auto Count = parseRepeatPassName(Name)) {
1616 FunctionPassManager NestedFPM(DebugLogging);
1617 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1618 DebugLogging))
1619 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001620 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001621 return true;
1622 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001623
1624 for (auto &C : FunctionPipelineParsingCallbacks)
1625 if (C(Name, FPM, InnerPipeline))
1626 return true;
1627
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001628 // Normal passes can't have pipelines.
1629 return false;
1630 }
1631
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001632// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001633#define FUNCTION_PASS(NAME, CREATE_PASS) \
1634 if (Name == NAME) { \
1635 FPM.addPass(CREATE_PASS); \
1636 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001637 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001638#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1639 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001640 FPM.addPass( \
1641 RequireAnalysisPass< \
1642 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001643 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001644 } \
1645 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001646 FPM.addPass(InvalidateAnalysisPass< \
1647 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001648 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001649 }
1650#include "PassRegistry.def"
1651
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001652 for (auto &C : FunctionPipelineParsingCallbacks)
1653 if (C(Name, FPM, InnerPipeline))
1654 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001655 return false;
1656}
1657
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001658bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001659 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001660 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001661 auto &InnerPipeline = E.InnerPipeline;
1662
1663 // First handle complex passes like the pass managers which carry pipelines.
1664 if (!InnerPipeline.empty()) {
1665 if (Name == "loop") {
1666 LoopPassManager NestedLPM(DebugLogging);
1667 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1668 DebugLogging))
1669 return false;
1670 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001671 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001672 return true;
1673 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001674 if (auto Count = parseRepeatPassName(Name)) {
1675 LoopPassManager NestedLPM(DebugLogging);
1676 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1677 DebugLogging))
1678 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001679 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001680 return true;
1681 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001682
1683 for (auto &C : LoopPipelineParsingCallbacks)
1684 if (C(Name, LPM, InnerPipeline))
1685 return true;
1686
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001687 // Normal passes can't have pipelines.
1688 return false;
1689 }
1690
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001691// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001692#define LOOP_PASS(NAME, CREATE_PASS) \
1693 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001694 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001695 return true; \
1696 }
1697#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1698 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001699 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001700 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1701 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1702 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001703 return true; \
1704 } \
1705 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001706 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001707 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001708 return true; \
1709 }
1710#include "PassRegistry.def"
1711
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001712 for (auto &C : LoopPipelineParsingCallbacks)
1713 if (C(Name, LPM, InnerPipeline))
1714 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001715 return false;
1716}
1717
Chandler Carruthedf59962016-02-18 09:45:17 +00001718bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001719#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1720 if (Name == NAME) { \
1721 AA.registerModuleAnalysis< \
1722 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1723 return true; \
1724 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001725#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1726 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001727 AA.registerFunctionAnalysis< \
1728 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001729 return true; \
1730 }
1731#include "PassRegistry.def"
1732
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001733 for (auto &C : AAParsingCallbacks)
1734 if (C(Name, AA))
1735 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001736 return false;
1737}
1738
Justin Bognereecc3c82016-02-25 07:23:08 +00001739bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001740 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001741 bool VerifyEachPass,
1742 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001743 for (const auto &Element : Pipeline) {
1744 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1745 return false;
1746 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001747 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001748 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001749}
1750
Chandler Carruth1ff77242015-03-07 09:02:36 +00001751bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001752 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001753 bool VerifyEachPass,
1754 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001755 for (const auto &Element : Pipeline) {
1756 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1757 return false;
1758 if (VerifyEachPass)
1759 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001760 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001761 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001762}
1763
Chandler Carruth1ff77242015-03-07 09:02:36 +00001764bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001765 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001766 bool VerifyEachPass,
1767 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001768 for (const auto &Element : Pipeline) {
1769 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1770 return false;
1771 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001772 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001773 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001774}
1775
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001776void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1777 FunctionAnalysisManager &FAM,
1778 CGSCCAnalysisManager &CGAM,
1779 ModuleAnalysisManager &MAM) {
1780 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1781 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001782 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1783 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1784 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1785 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1786 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1787}
1788
Chandler Carruth1ff77242015-03-07 09:02:36 +00001789bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001790 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001791 bool VerifyEachPass,
1792 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001793 for (const auto &Element : Pipeline) {
1794 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1795 return false;
1796 if (VerifyEachPass)
1797 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001798 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001799 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001800}
1801
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001802// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001803// FIXME: Should this routine accept a TargetMachine or require the caller to
1804// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001805bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1806 StringRef PipelineText, bool VerifyEachPass,
1807 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001808 auto Pipeline = parsePipelineText(PipelineText);
1809 if (!Pipeline || Pipeline->empty())
1810 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001811
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001812 // If the first name isn't at the module layer, wrap the pipeline up
1813 // automatically.
1814 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001815
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001816 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1817 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001818 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001819 } else if (isFunctionPassName(FirstName,
1820 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001821 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001822 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001823 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001824 } else {
1825 for (auto &C : TopLevelPipelineParsingCallbacks)
1826 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1827 return true;
1828
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001829 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001830 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001831 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001832 }
1833
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001834 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001835}
Chandler Carruthedf59962016-02-18 09:45:17 +00001836
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001837// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1838bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1839 StringRef PipelineText, bool VerifyEachPass,
1840 bool DebugLogging) {
1841 auto Pipeline = parsePipelineText(PipelineText);
1842 if (!Pipeline || Pipeline->empty())
1843 return false;
1844
1845 StringRef FirstName = Pipeline->front().Name;
1846 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1847 return false;
1848
1849 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1850}
1851
1852// Primary pass pipeline description parsing routine for a \c
1853// FunctionPassManager
1854bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1855 StringRef PipelineText, bool VerifyEachPass,
1856 bool DebugLogging) {
1857 auto Pipeline = parsePipelineText(PipelineText);
1858 if (!Pipeline || Pipeline->empty())
1859 return false;
1860
1861 StringRef FirstName = Pipeline->front().Name;
1862 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1863 return false;
1864
1865 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1866 DebugLogging);
1867}
1868
1869// Primary pass pipeline description parsing routine for a \c LoopPassManager
1870bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1871 StringRef PipelineText, bool VerifyEachPass,
1872 bool DebugLogging) {
1873 auto Pipeline = parsePipelineText(PipelineText);
1874 if (!Pipeline || Pipeline->empty())
1875 return false;
1876
1877 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1878}
1879
Chandler Carruthedf59962016-02-18 09:45:17 +00001880bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001881 // If the pipeline just consists of the word 'default' just replace the AA
1882 // manager with our default one.
1883 if (PipelineText == "default") {
1884 AA = buildDefaultAAPipeline();
1885 return true;
1886 }
1887
Chandler Carruthedf59962016-02-18 09:45:17 +00001888 while (!PipelineText.empty()) {
1889 StringRef Name;
1890 std::tie(Name, PipelineText) = PipelineText.split(',');
1891 if (!parseAAPassName(AA, Name))
1892 return false;
1893 }
1894
1895 return true;
1896}