blob: 09758dc565182d4998bf22695ca15d0e63d138ea [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000025#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000026#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000027#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
28#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000029#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000030#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000031#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000032#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000033#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000034#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000035#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000036#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000037#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000038#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000039#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000040#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000041#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000043#include "llvm/Analysis/OptimizationRemarkEmitter.h"
John Brawnbdbbd832018-06-28 14:13:06 +000044#include "llvm/Analysis/PhiValues.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000063#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000066#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000067#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000068#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000069#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000070#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000071#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000072#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000073#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000074#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000075#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000076#include "llvm/Transforms/IPO/GlobalSplit.h"
Aditya Kumar9e20ade2018-10-03 05:55:20 +000077#include "llvm/Transforms/IPO/HotColdSplitting.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000078#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000079#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000080#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000081#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000082#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000083#include "llvm/Transforms/IPO/SCCP.h"
David Blaikie301627f2018-03-22 22:42:44 +000084#include "llvm/Transforms/IPO/SampleProfile.h"
Justin Bogner21e15372015-10-30 23:28:12 +000085#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000086#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000087#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000088#include "llvm/Transforms/InstCombine/InstCombine.h"
Leonard Chan64e21b52018-10-11 18:31:51 +000089#include "llvm/Transforms/Instrumentation/AddressSanitizerPass.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000090#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Leonard Chan64e21b52018-10-11 18:31:51 +000091#include "llvm/Transforms/Instrumentation/CGProfile.h"
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +000092#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +000093#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
94#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000095#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000096#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000097#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000098#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000099#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +0000100#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +0000101#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +0000102#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +0000103#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +0000104#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000105#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000106#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000107#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000108#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000109#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000110#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000111#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +0000112#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
Sean Silva46590d52016-06-14 00:51:09 +0000113#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000114#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000115#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000116#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000117#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000118#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000119#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000120#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000121#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000122#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000123#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000124#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000125#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000126#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000127#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
David Green963401d2018-07-01 12:47:30 +0000128#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000129#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000130#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000131#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000132#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000133#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000134#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000135#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000136#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000137#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000138#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000139#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000140#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000141#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000142#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000143#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000144#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000145#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000146#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000147#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000148#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000149#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000150#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000151#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000152#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000153#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000154#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000155#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000156#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000157#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000158#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000159#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000160
Chandler Carruth66445382014-01-11 08:16:35 +0000161using namespace llvm;
162
Chandler Carruth719ffe12017-02-12 05:38:04 +0000163static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
164 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000165static cl::opt<bool>
166 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
167 cl::Hidden, cl::ZeroOrMore,
168 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000169
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000170static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000171 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000172 cl::Hidden, cl::ZeroOrMore,
173 cl::desc("Run NewGVN instead of GVN"));
174
Geoff Berry3cca1da2017-06-10 15:20:03 +0000175static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000176 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
177 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000178
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000179static cl::opt<bool> EnableGVNHoist(
Eric Christopherdcf1d972018-10-01 18:57:08 +0000180 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
181 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000182
Davide Italianobe1b6a92017-06-03 23:18:29 +0000183static cl::opt<bool> EnableGVNSink(
184 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
185 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
186
David Green963401d2018-07-01 12:47:30 +0000187static cl::opt<bool> EnableUnrollAndJam(
188 "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
189 cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
190
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000191static cl::opt<bool> EnableSyntheticCounts(
192 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
193 cl::desc("Run synthetic function entry count generation "
194 "pass"));
195
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000196static Regex DefaultAliasRegex(
197 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000198
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000199static cl::opt<bool>
200 EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden,
201 cl::desc("Enable control height reduction optimization (CHR)"));
202
Aditya Kumar9e20ade2018-10-03 05:55:20 +0000203extern cl::opt<bool> EnableHotColdSplit;
204
Chandler Carruthe3f50642016-12-22 06:59:15 +0000205static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
206 switch (Level) {
207 case PassBuilder::O0:
208 case PassBuilder::O1:
209 case PassBuilder::O2:
210 case PassBuilder::O3:
211 return false;
212
213 case PassBuilder::Os:
214 case PassBuilder::Oz:
215 return true;
216 }
217 llvm_unreachable("Invalid optimization level!");
218}
219
Chandler Carruth66445382014-01-11 08:16:35 +0000220namespace {
221
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000222/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000223struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000224 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000225 return PreservedAnalyses::all();
226 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000227 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000228};
229
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000230/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000231class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
232 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000233 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000234
235public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000236 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000237 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000238 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000239};
240
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000241/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000242struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000243 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
244 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000245 return PreservedAnalyses::all();
246 }
247 static StringRef name() { return "NoOpCGSCCPass"; }
248};
249
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000250/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000251class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
252 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000253 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000254
255public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000256 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000257 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000258 return Result();
259 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000260 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000261};
262
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000263/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000264struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000265 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000266 return PreservedAnalyses::all();
267 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000268 static StringRef name() { return "NoOpFunctionPass"; }
269};
270
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000271/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000272class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
273 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000274 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000275
276public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000277 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000278 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000279 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000280};
281
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000282/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000283struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000284 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
285 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000286 return PreservedAnalyses::all();
287 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000288 static StringRef name() { return "NoOpLoopPass"; }
289};
290
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000291/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000292class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
293 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000294 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000295
296public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000297 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000298 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
299 return Result();
300 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000301 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000302};
303
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000304AnalysisKey NoOpModuleAnalysis::Key;
305AnalysisKey NoOpCGSCCAnalysis::Key;
306AnalysisKey NoOpFunctionAnalysis::Key;
307AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000308
Chandler Carruth66445382014-01-11 08:16:35 +0000309} // End anonymous namespace.
310
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000311void PassBuilder::invokePeepholeEPCallbacks(
312 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
313 for (auto &C : PeepholeEPCallbacks)
314 C(FPM, Level);
315}
316
Chandler Carruth1ff77242015-03-07 09:02:36 +0000317void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000318#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000319 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000320#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000321
322 for (auto &C : ModuleAnalysisRegistrationCallbacks)
323 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000324}
325
Chandler Carruth1ff77242015-03-07 09:02:36 +0000326void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000327#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000328 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000329#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000330
331 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
332 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000333}
334
Chandler Carruth1ff77242015-03-07 09:02:36 +0000335void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000336#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000337 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000338#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000339
340 for (auto &C : FunctionAnalysisRegistrationCallbacks)
341 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000342}
343
Justin Bognereecc3c82016-02-25 07:23:08 +0000344void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000345#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000346 LAM.registerPass([&] { return CREATE_PASS; });
347#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000348
349 for (auto &C : LoopAnalysisRegistrationCallbacks)
350 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000351}
352
Chandler Carruthe3f50642016-12-22 06:59:15 +0000353FunctionPassManager
354PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000355 ThinLTOPhase Phase,
356 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000357 assert(Level != O0 && "Must request optimizations!");
358 FunctionPassManager FPM(DebugLogging);
359
360 // Form SSA out of local memory accesses after breaking apart aggregates into
361 // scalars.
362 FPM.addPass(SROA());
363
364 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000365 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000366
Davide Italianoc3688312017-06-01 23:08:14 +0000367 // Hoisting of scalars and load expressions.
368 if (EnableGVNHoist)
369 FPM.addPass(GVNHoistPass());
370
Davide Italianobe1b6a92017-06-03 23:18:29 +0000371 // Global value numbering based sinking.
372 if (EnableGVNSink) {
373 FPM.addPass(GVNSinkPass());
374 FPM.addPass(SimplifyCFGPass());
375 }
376
Chandler Carruthe3f50642016-12-22 06:59:15 +0000377 // Speculative execution if the target has divergent branches; otherwise nop.
378 FPM.addPass(SpeculativeExecutionPass());
379
380 // Optimize based on known information about branches, and cleanup afterward.
381 FPM.addPass(JumpThreadingPass());
382 FPM.addPass(CorrelatedValuePropagationPass());
383 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000384 if (Level == O3)
385 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000386 FPM.addPass(InstCombinePass());
387
388 if (!isOptimizingForSize(Level))
389 FPM.addPass(LibCallsShrinkWrapPass());
390
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000391 invokePeepholeEPCallbacks(FPM, Level);
392
Rong Xue1f42452017-10-23 22:21:29 +0000393 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
394 // using the size value profile. Don't perform this when optimizing for size.
395 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
396 !isOptimizingForSize(Level))
397 FPM.addPass(PGOMemOPSizeOpt());
398
Chandler Carruthe3f50642016-12-22 06:59:15 +0000399 FPM.addPass(TailCallElimPass());
400 FPM.addPass(SimplifyCFGPass());
401
402 // Form canonically associated expression trees, and simplify the trees using
403 // basic mathematical properties. For example, this will form (nearly)
404 // minimal multiplication trees.
405 FPM.addPass(ReassociatePass());
406
407 // Add the primary loop simplification pipeline.
408 // FIXME: Currently this is split into two loop pass pipelines because we run
Chandler Carruth71fd2702018-05-30 02:46:45 +0000409 // some function passes in between them. These can and should be removed
410 // and/or replaced by scheduling the loop pass equivalents in the correct
411 // positions. But those equivalent passes aren't powerful enough yet.
412 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
413 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
414 // fully replace `SimplifyCFGPass`, and the closest to the other we have is
415 // `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000416 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
417
Chandler Carruth71fd2702018-05-30 02:46:45 +0000418 // Simplify the loop body. We do this initially to clean up after other loop
419 // passes run, either when iterating on a loop or on inner loops with
420 // implications on the outer loop.
421 LPM1.addPass(LoopInstSimplifyPass());
422 LPM1.addPass(LoopSimplifyCFGPass());
423
Chandler Carruthe3f50642016-12-22 06:59:15 +0000424 // Rotate Loop - disable header duplication at -Oz
425 LPM1.addPass(LoopRotatePass(Level != Oz));
426 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000427 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000428 LPM2.addPass(IndVarSimplifyPass());
429 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000430
431 for (auto &C : LateLoopOptimizationsEPCallbacks)
432 C(LPM2, Level);
433
Chandler Carruth79b733b2017-01-26 23:21:17 +0000434 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000435 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000436 // because it changes IR to makes profile annotation in back compile
437 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000438 if (Phase != ThinLTOPhase::PreLink ||
439 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000440 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000441
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000442 for (auto &C : LoopOptimizerEndEPCallbacks)
443 C(LPM2, Level);
444
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000445 // We provide the opt remark emitter pass for LICM to use. We only need to do
446 // this once as it is immutable.
447 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000448 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000449 FPM.addPass(SimplifyCFGPass());
450 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000451 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000452
453 // Eliminate redundancies.
454 if (Level != O1) {
455 // These passes add substantial compile time so skip them at O1.
456 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000457 if (RunNewGVN)
458 FPM.addPass(NewGVNPass());
459 else
460 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000461 }
462
463 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
464 FPM.addPass(MemCpyOptPass());
465
466 // Sparse conditional constant propagation.
467 // FIXME: It isn't clear why we do this *after* loop passes rather than
468 // before...
469 FPM.addPass(SCCPPass());
470
471 // Delete dead bit computations (instcombine runs after to fold away the dead
472 // computations, and then ADCE will run later to exploit any new DCE
473 // opportunities that creates).
474 FPM.addPass(BDCEPass());
475
476 // Run instcombine after redundancy and dead bit elimination to exploit
477 // opportunities opened up by them.
478 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000479 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000480
481 // Re-consider control flow based optimizations after redundancy elimination,
482 // redo DCE, etc.
483 FPM.addPass(JumpThreadingPass());
484 FPM.addPass(CorrelatedValuePropagationPass());
485 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000486 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000487
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000488 for (auto &C : ScalarOptimizerLateEPCallbacks)
489 C(FPM, Level);
490
Chandler Carruthe3f50642016-12-22 06:59:15 +0000491 // Finally, do an expensive DCE pass to catch all the dead code exposed by
492 // the simplifications and basic cleanup after all the simplifications.
493 FPM.addPass(ADCEPass());
494 FPM.addPass(SimplifyCFGPass());
495 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000496 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000497
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000498 if (EnableCHR && Level == O3 && PGOOpt &&
499 (!PGOOpt->ProfileUseFile.empty() || !PGOOpt->SampleProfileFile.empty()))
500 FPM.addPass(ControlHeightReductionPass());
501
Chandler Carruthe3f50642016-12-22 06:59:15 +0000502 return FPM;
503}
504
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000505void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
506 PassBuilder::OptimizationLevel Level,
507 bool RunProfileGen,
508 std::string ProfileGenFile,
Richard Smith6c676622018-10-10 23:13:47 +0000509 std::string ProfileUseFile,
510 std::string ProfileRemappingFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000511 // Generally running simplification passes and the inliner with an high
512 // threshold results in smaller executables, but there may be cases where
513 // the size grows, so let's be conservative here and skip this simplification
514 // at -Os/Oz.
515 if (!isOptimizingForSize(Level)) {
516 InlineParams IP;
517
518 // In the old pass manager, this is a cl::opt. Should still this be one?
519 IP.DefaultThreshold = 75;
520
521 // FIXME: The hint threshold has the same value used by the regular inliner.
522 // This should probably be lowered after performance testing.
523 // FIXME: this comment is cargo culted from the old pass manager, revisit).
524 IP.HintThreshold = 325;
525
526 CGSCCPassManager CGPipeline(DebugLogging);
527
528 CGPipeline.addPass(InlinerPass(IP));
529
530 FunctionPassManager FPM;
531 FPM.addPass(SROA());
532 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
533 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
534 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000535 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000536
Davide Italiano513dfaa2017-02-13 15:26:22 +0000537 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
538
539 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
540 }
541
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000542 // Delete anything that is now dead to make sure that we don't instrument
543 // dead code. Instrumentation can end up keeping dead code around and
544 // dramatically increase code size.
545 MPM.addPass(GlobalDCEPass());
546
Davide Italiano513dfaa2017-02-13 15:26:22 +0000547 if (RunProfileGen) {
548 MPM.addPass(PGOInstrumentationGen());
549
Xinliang David Lib67530e2017-06-25 00:26:43 +0000550 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000551 FPM.addPass(
552 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000553 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
554
Davide Italiano513dfaa2017-02-13 15:26:22 +0000555 // Add the profile lowering pass.
556 InstrProfOptions Options;
557 if (!ProfileGenFile.empty())
558 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000559 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000560 MPM.addPass(InstrProfiling(Options));
561 }
562
563 if (!ProfileUseFile.empty())
Richard Smith6c676622018-10-10 23:13:47 +0000564 MPM.addPass(PGOInstrumentationUse(ProfileUseFile, ProfileRemappingFile));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000565}
566
Easwaran Raman8249fac2017-06-28 13:33:49 +0000567static InlineParams
568getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
569 auto O3 = PassBuilder::O3;
570 unsigned OptLevel = Level > O3 ? 2 : Level;
571 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
572 return getInlineParams(OptLevel, SizeLevel);
573}
574
Chandler Carruthe3f50642016-12-22 06:59:15 +0000575ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000576PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000577 ThinLTOPhase Phase,
578 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000579 ModulePassManager MPM(DebugLogging);
580
Chandler Carruthe3f50642016-12-22 06:59:15 +0000581 // Do basic inference of function attributes from known properties of system
582 // libraries and other oracles.
583 MPM.addPass(InferFunctionAttrsPass());
584
585 // Create an early function pass manager to cleanup the output of the
586 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000587 FunctionPassManager EarlyFPM(DebugLogging);
588 EarlyFPM.addPass(SimplifyCFGPass());
589 EarlyFPM.addPass(SROA());
590 EarlyFPM.addPass(EarlyCSEPass());
591 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000592 if (Level == O3)
593 EarlyFPM.addPass(CallSiteSplittingPass());
594
Dehao Chen08f88312017-08-07 20:23:20 +0000595 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
596 // to convert bitcast to direct calls so that they can be inlined during the
597 // profile annotation prepration step.
598 // More details about SamplePGO design can be found in:
599 // https://research.google.com/pubs/pub45290.html
600 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
601 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
602 Phase == ThinLTOPhase::PostLink)
603 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000604 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000605
Dehao Chen08f88312017-08-07 20:23:20 +0000606 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
607 // Annotate sample profile right after early FPM to ensure freshness of
608 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000609 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
Richard Smith6c676622018-10-10 23:13:47 +0000610 PGOOpt->ProfileRemappingFile,
Dehao Chend26dae02017-10-01 05:24:51 +0000611 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000612 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
613 // for the profile annotation to be accurate in the ThinLTO backend.
614 if (Phase != ThinLTOPhase::PreLink)
615 // We perform early indirect call promotion here, before globalopt.
616 // This is important for the ThinLTO backend phase because otherwise
617 // imported available_externally functions look unreferenced and are
618 // removed.
619 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
620 true));
621 }
622
Aditya Kumar9e20ade2018-10-03 05:55:20 +0000623 if (EnableHotColdSplit)
624 MPM.addPass(HotColdSplittingPass());
625
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000626 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000627 // and prior to optimizing globals.
628 // FIXME: This position in the pipeline hasn't been carefully considered in
629 // years, it should be re-analyzed.
630 MPM.addPass(IPSCCPPass());
631
Matthew Simpsoncb585582017-10-25 13:40:08 +0000632 // Attach metadata to indirect call sites indicating the set of functions
633 // they may target at run-time. This should follow IPSCCP.
634 MPM.addPass(CalledValuePropagationPass());
635
Chandler Carruthe3f50642016-12-22 06:59:15 +0000636 // Optimize globals to try and fold them into constants.
637 MPM.addPass(GlobalOptPass());
638
639 // Promote any localized globals to SSA registers.
640 // FIXME: Should this instead by a run of SROA?
641 // FIXME: We should probably run instcombine and simplify-cfg afterward to
642 // delete control flows that are dead once globals have been folded to
643 // constants.
644 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
645
646 // Remove any dead arguments exposed by cleanups and constand folding
647 // globals.
648 MPM.addPass(DeadArgumentEliminationPass());
649
650 // Create a small function pass pipeline to cleanup after all the global
651 // optimizations.
652 FunctionPassManager GlobalCleanupPM(DebugLogging);
653 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000654 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
655
Chandler Carruthe3f50642016-12-22 06:59:15 +0000656 GlobalCleanupPM.addPass(SimplifyCFGPass());
657 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
658
Dehao Chen89d32262017-08-02 01:28:31 +0000659 // Add all the requested passes for instrumentation PGO, if requested.
660 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
661 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
662 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
Richard Smith6c676622018-10-10 23:13:47 +0000663 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile,
664 PGOOpt->ProfileRemappingFile);
Dehao Chen89d32262017-08-02 01:28:31 +0000665 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000666 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000667
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000668 // Synthesize function entry counts for non-PGO compilation.
669 if (EnableSyntheticCounts && !PGOOpt)
670 MPM.addPass(SyntheticCountsPropagation());
671
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000672 // Require the GlobalsAA analysis for the module so we can query it within
673 // the CGSCC pipeline.
674 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000675
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000676 // Require the ProfileSummaryAnalysis for the module so we can query it within
677 // the inliner pass.
678 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
679
Chandler Carruthe3f50642016-12-22 06:59:15 +0000680 // Now begin the main postorder CGSCC pipeline.
681 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
682 // manager and trying to emulate its precise behavior. Much of this doesn't
683 // make a lot of sense and we should revisit the core CGSCC structure.
684 CGSCCPassManager MainCGPipeline(DebugLogging);
685
686 // Note: historically, the PruneEH pass was run first to deduce nounwind and
687 // generally clean up exception handling overhead. It isn't clear this is
688 // valuable as the inliner doesn't currently care whether it is inlining an
689 // invoke or a call.
690
691 // Run the inliner first. The theory is that we are walking bottom-up and so
692 // the callees have already been fully optimized, and we want to inline them
693 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000694 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000695 // because it makes profile annotation in the backend inaccurate.
696 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000697 if (Phase == ThinLTOPhase::PreLink &&
698 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000699 IP.HotCallSiteThreshold = 0;
700 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000701
702 // Now deduce any function attributes based in the current code.
703 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
704
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000705 // When at O3 add argument promotion to the pass pipeline.
706 // FIXME: It isn't at all clear why this should be limited to O3.
707 if (Level == O3)
708 MainCGPipeline.addPass(ArgumentPromotionPass());
709
Chandler Carruthe3f50642016-12-22 06:59:15 +0000710 // Lastly, add the core function simplification pipeline nested inside the
711 // CGSCC walk.
712 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000713 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000714
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000715 for (auto &C : CGSCCOptimizerLateEPCallbacks)
716 C(MainCGPipeline, Level);
717
Chandler Carruth719ffe12017-02-12 05:38:04 +0000718 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
719 // to detect when we devirtualize indirect calls and iterate the SCC passes
720 // in that case to try and catch knock-on inlining or function attrs
721 // opportunities. Then we add it to the module pipeline by walking the SCCs
722 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000723 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000724 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000725 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000726
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000727 return MPM;
728}
729
730ModulePassManager
731PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
732 bool DebugLogging) {
733 ModulePassManager MPM(DebugLogging);
734
735 // Optimize globals now that the module is fully simplified.
736 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000737 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000738
Xinliang David Li126157c2017-05-22 16:41:57 +0000739 // Run partial inlining pass to partially inline functions that have
740 // large bodies.
741 if (RunPartialInlining)
742 MPM.addPass(PartialInlinerPass());
743
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000744 // Remove avail extern fns and globals definitions since we aren't compiling
745 // an object file for later LTO. For LTO we want to preserve these so they
746 // are eligible for inlining at link-time. Note if they are unreferenced they
747 // will be removed by GlobalDCE later, so this only impacts referenced
748 // available externally globals. Eventually they will be suppressed during
749 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
750 // may make globals referenced by available external functions dead and saves
751 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000752 MPM.addPass(EliminateAvailableExternallyPass());
753
754 // Do RPO function attribute inference across the module to forward-propagate
755 // attributes where applicable.
756 // FIXME: Is this really an optimization rather than a canonicalization?
757 MPM.addPass(ReversePostOrderFunctionAttrsPass());
758
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000759 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000760 // useful as the above will have inlined, DCE'ed, and function-attr
761 // propagated everything. We should at this point have a reasonably minimal
762 // and richly annotated call graph. By computing aliasing and mod/ref
763 // information for all local globals here, the late loop passes and notably
764 // the vectorizer will be able to use them to help recognize vectorizable
765 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000766 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000767
768 FunctionPassManager OptimizePM(DebugLogging);
769 OptimizePM.addPass(Float2IntPass());
770 // FIXME: We need to run some loop optimizations to re-rotate loops after
771 // simplify-cfg and others undo their rotation.
772
773 // Optimize the loop execution. These passes operate on entire loop nests
774 // rather than on each loop in an inside-out manner, and so they are actually
775 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000776
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000777 for (auto &C : VectorizerStartEPCallbacks)
778 C(OptimizePM, Level);
779
Chandler Carrutha95ff382017-01-27 00:50:21 +0000780 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000781 OptimizePM.addPass(
782 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000783
784 // Distribute loops to allow partial vectorization. I.e. isolate dependences
785 // into separate loop that would otherwise inhibit vectorization. This is
786 // currently only performed for loops marked with the metadata
787 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000788 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000789
790 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000791 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000792
Chandler Carruthbaabda92017-01-27 01:32:26 +0000793 // Eliminate loads by forwarding stores from the previous iteration to loads
794 // of the current iteration.
795 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000796
797 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000798 OptimizePM.addPass(InstCombinePass());
799
Chandler Carrutha95ff382017-01-27 00:50:21 +0000800 // Now that we've formed fast to execute loop structures, we do further
801 // optimizations. These are run afterward as they might block doing complex
802 // analyses and transforms such as what are needed for loop vectorization.
803
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000804 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
805 // GVN, loop transforms, and others have already run, so it's now better to
806 // convert to more optimized IR using more aggressive simplify CFG options.
807 // The extra sinking transform can create larger basic blocks, so do this
808 // before SLP vectorization.
809 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
810 forwardSwitchCondToPhi(true).
811 convertSwitchToLookupTable(true).
812 needCanonicalLoops(false).
813 sinkCommonInsts(true)));
814
Chandler Carruthe3f50642016-12-22 06:59:15 +0000815 // Optimize parallel scalar instruction chains into SIMD instructions.
816 OptimizePM.addPass(SLPVectorizerPass());
817
Chandler Carruthe3f50642016-12-22 06:59:15 +0000818 OptimizePM.addPass(InstCombinePass());
819
820 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000821 // execution resources of an out-of-order processor. We also then need to
822 // clean up redundancies and loop invariant code.
823 // FIXME: It would be really good to use a loop-integrated instruction
824 // combiner for cleanup here so that the unrolling and LICM can be pipelined
825 // across the loop nests.
David Green963401d2018-07-01 12:47:30 +0000826 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
827 if (EnableUnrollAndJam) {
828 OptimizePM.addPass(
829 createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
830 }
Teresa Johnsonecd90132017-08-02 20:35:29 +0000831 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000832 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000833 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000834 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000835
836 // Now that we've vectorized and unrolled loops, we may have more refined
837 // alignment information, try to re-derive it here.
838 OptimizePM.addPass(AlignmentFromAssumptionsPass());
839
Chandler Carrutha95ff382017-01-27 00:50:21 +0000840 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
841 // canonicalization pass that enables other optimizations. As a result,
842 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
843 // result too early.
844 OptimizePM.addPass(LoopSinkPass());
845
846 // And finally clean up LCSSA form before generating code.
Chandler Carruth7c557f82018-06-29 23:36:03 +0000847 OptimizePM.addPass(InstSimplifyPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000848
Sanjay Patel6fd43912017-09-09 13:38:18 +0000849 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
850 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
851 // flattening of blocks.
852 OptimizePM.addPass(DivRemPairsPass());
853
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000854 // LoopSink (and other loop passes since the last simplifyCFG) might have
855 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
856 OptimizePM.addPass(SimplifyCFGPass());
857
Chandler Carruthc34f7892017-11-28 11:32:31 +0000858 // Optimize PHIs by speculating around them when profitable. Note that this
859 // pass needs to be run after any PRE or similar pass as it is essentially
860 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
861 OptimizePM.addPass(SpeculateAroundPHIsPass());
862
Chandler Carrutha95ff382017-01-27 00:50:21 +0000863 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000864 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
865
Michael J. Spencer7bb27672018-07-16 00:28:24 +0000866 MPM.addPass(CGProfilePass());
867
Chandler Carruthe3f50642016-12-22 06:59:15 +0000868 // Now we need to do some global optimization transforms.
869 // FIXME: It would seem like these should come first in the optimization
870 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
871 // ordering here.
872 MPM.addPass(GlobalDCEPass());
873 MPM.addPass(ConstantMergePass());
874
875 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000876}
877
Chandler Carruthe3f50642016-12-22 06:59:15 +0000878ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000879PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
880 bool DebugLogging) {
881 assert(Level != O0 && "Must request optimizations for the default pipeline!");
882
883 ModulePassManager MPM(DebugLogging);
884
885 // Force any function attributes we want the rest of the pipeline to observe.
886 MPM.addPass(ForceFunctionAttrsPass());
887
David Blaikie0c64f5a2018-01-23 01:25:20 +0000888 // Apply module pipeline start EP callback.
889 for (auto &C : PipelineStartEPCallbacks)
890 C(MPM);
891
Dehao Chen08f88312017-08-07 20:23:20 +0000892 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000893 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
894
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000895 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000896 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
897 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000898
899 // Now add the optimization pipeline.
900 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
901
902 return MPM;
903}
904
905ModulePassManager
906PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
907 bool DebugLogging) {
908 assert(Level != O0 && "Must request optimizations for the default pipeline!");
909
910 ModulePassManager MPM(DebugLogging);
911
912 // Force any function attributes we want the rest of the pipeline to observe.
913 MPM.addPass(ForceFunctionAttrsPass());
914
Dehao Chen08f88312017-08-07 20:23:20 +0000915 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000916 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
917
David Blaikie0c64f5a2018-01-23 01:25:20 +0000918 // Apply module pipeline start EP callback.
919 for (auto &C : PipelineStartEPCallbacks)
920 C(MPM);
921
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000922 // If we are planning to perform ThinLTO later, we don't bloat the code with
923 // unrolling/vectorization/... now. Just simplify the module as much as we
924 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000925 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
926 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000927
928 // Run partial inlining pass to partially inline functions that have
929 // large bodies.
930 // FIXME: It isn't clear whether this is really the right place to run this
931 // in ThinLTO. Because there is another canonicalization and simplification
932 // phase that will run after the thin link, running this here ends up with
933 // less information than will be available later and it may grow functions in
934 // ways that aren't beneficial.
935 if (RunPartialInlining)
936 MPM.addPass(PartialInlinerPass());
937
938 // Reduce the size of the IR as much as possible.
939 MPM.addPass(GlobalOptPass());
940
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000941 return MPM;
942}
943
Teresa Johnson28023db2018-07-19 14:51:32 +0000944ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
945 OptimizationLevel Level, bool DebugLogging,
946 const ModuleSummaryIndex *ImportSummary) {
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000947 ModulePassManager MPM(DebugLogging);
948
Teresa Johnson28023db2018-07-19 14:51:32 +0000949 if (ImportSummary) {
950 // These passes import type identifier resolutions for whole-program
951 // devirtualization and CFI. They must run early because other passes may
952 // disturb the specific instruction patterns that these passes look for,
953 // creating dependencies on resolutions that may not appear in the summary.
954 //
955 // For example, GVN may transform the pattern assume(type.test) appearing in
956 // two basic blocks into assume(phi(type.test, type.test)), which would
957 // transform a dependency on a WPD resolution into a dependency on a type
958 // identifier resolution for CFI.
959 //
960 // Also, WPD has access to more precise information than ICP and can
961 // devirtualize more effectively, so it should operate on the IR first.
962 MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
963 MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
964 }
965
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000966 // Force any function attributes we want the rest of the pipeline to observe.
967 MPM.addPass(ForceFunctionAttrsPass());
968
969 // During the ThinLTO backend phase we perform early indirect call promotion
970 // here, before globalopt. Otherwise imported available_externally functions
971 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000972 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
973 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000974 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000975 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
976 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000977
978 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000979 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
980 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000981
982 // Now add the optimization pipeline.
983 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
984
985 return MPM;
986}
987
988ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000989PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
990 bool DebugLogging) {
991 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000992 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000993 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000994}
995
Teresa Johnson28023db2018-07-19 14:51:32 +0000996ModulePassManager
997PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
998 ModuleSummaryIndex *ExportSummary) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000999 assert(Level != O0 && "Must request optimizations for the default pipeline!");
1000 ModulePassManager MPM(DebugLogging);
1001
Davide Italiano089a9122017-01-24 00:57:39 +00001002 // Remove unused virtual tables to improve the quality of code generated by
1003 // whole-program devirtualization and bitset lowering.
1004 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001005
Davide Italiano089a9122017-01-24 00:57:39 +00001006 // Force any function attributes we want the rest of the pipeline to observe.
1007 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +00001008
Davide Italiano089a9122017-01-24 00:57:39 +00001009 // Do basic inference of function attributes from known properties of system
1010 // libraries and other oracles.
1011 MPM.addPass(InferFunctionAttrsPass());
1012
1013 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +00001014 FunctionPassManager EarlyFPM(DebugLogging);
1015 EarlyFPM.addPass(CallSiteSplittingPass());
1016 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
1017
Davide Italiano089a9122017-01-24 00:57:39 +00001018 // Indirect call promotion. This should promote all the targets that are
1019 // left by the earlier promotion pass that promotes intra-module targets.
1020 // This two-step promotion is to save the compile time. For LTO, it should
1021 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +00001022 MPM.addPass(PGOIndirectCallPromotion(
1023 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +00001024 // Propagate constants at call sites into the functions they call. This
1025 // opens opportunities for globalopt (and inlining) by substituting function
1026 // pointers passed as arguments to direct uses of functions.
1027 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +00001028
1029 // Attach metadata to indirect call sites indicating the set of functions
1030 // they may target at run-time. This should follow IPSCCP.
1031 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +00001032 }
1033
1034 // Now deduce any function attributes based in the current code.
1035 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1036 PostOrderFunctionAttrsPass()));
1037
1038 // Do RPO function attribute inference across the module to forward-propagate
1039 // attributes where applicable.
1040 // FIXME: Is this really an optimization rather than a canonicalization?
1041 MPM.addPass(ReversePostOrderFunctionAttrsPass());
1042
1043 // Use inragne annotations on GEP indices to split globals where beneficial.
1044 MPM.addPass(GlobalSplitPass());
1045
1046 // Run whole program optimization of virtual call when the list of callees
1047 // is fixed.
Teresa Johnson28023db2018-07-19 14:51:32 +00001048 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001049
1050 // Stop here at -O1.
Teresa Johnson28023db2018-07-19 14:51:32 +00001051 if (Level == 1) {
1052 // The LowerTypeTestsPass needs to run to lower type metadata and the
1053 // type.test intrinsics. The pass does nothing if CFI is disabled.
1054 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001055 return MPM;
Teresa Johnson28023db2018-07-19 14:51:32 +00001056 }
Davide Italiano089a9122017-01-24 00:57:39 +00001057
1058 // Optimize globals to try and fold them into constants.
1059 MPM.addPass(GlobalOptPass());
1060
1061 // Promote any localized globals to SSA registers.
1062 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1063
1064 // Linking modules together can lead to duplicate global constant, only
1065 // keep one copy of each constant.
1066 MPM.addPass(ConstantMergePass());
1067
1068 // Remove unused arguments from functions.
1069 MPM.addPass(DeadArgumentEliminationPass());
1070
1071 // Reduce the code after globalopt and ipsccp. Both can open up significant
1072 // simplification opportunities, and both can propagate functions through
1073 // function pointers. When this happens, we often have to resolve varargs
1074 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001075 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001076 if (Level == O3)
1077 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001078 PeepholeFPM.addPass(InstCombinePass());
1079 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1080
1081 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001082
1083 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1084 // generally clean up exception handling overhead. It isn't clear this is
1085 // valuable as the inliner doesn't currently care whether it is inlining an
1086 // invoke or a call.
1087 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001088 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1089 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001090
1091 // Optimize globals again after we ran the inliner.
1092 MPM.addPass(GlobalOptPass());
1093
1094 // Garbage collect dead functions.
1095 // FIXME: Add ArgumentPromotion pass after once it's ported.
1096 MPM.addPass(GlobalDCEPass());
1097
1098 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001099 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001100 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001101 invokePeepholeEPCallbacks(FPM, Level);
1102
Davide Italiano089a9122017-01-24 00:57:39 +00001103 FPM.addPass(JumpThreadingPass());
1104
1105 // Break up allocas
1106 FPM.addPass(SROA());
1107
1108 // Run a few AA driver optimizations here and now to cleanup the code.
1109 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1110
1111 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1112 PostOrderFunctionAttrsPass()));
1113 // FIXME: here we run IP alias analysis in the legacy PM.
1114
1115 FunctionPassManager MainFPM;
1116
1117 // FIXME: once we fix LoopPass Manager, add LICM here.
1118 // FIXME: once we provide support for enabling MLSM, add it here.
1119 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001120 if (RunNewGVN)
1121 MainFPM.addPass(NewGVNPass());
1122 else
1123 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001124
1125 // Remove dead memcpy()'s.
1126 MainFPM.addPass(MemCpyOptPass());
1127
1128 // Nuke dead stores.
1129 MainFPM.addPass(DSEPass());
1130
1131 // FIXME: at this point, we run a bunch of loop passes:
1132 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1133 // loopVectorize. Enable them once the remaining issue with LPM
1134 // are sorted out.
1135
1136 MainFPM.addPass(InstCombinePass());
1137 MainFPM.addPass(SimplifyCFGPass());
1138 MainFPM.addPass(SCCPPass());
1139 MainFPM.addPass(InstCombinePass());
1140 MainFPM.addPass(BDCEPass());
1141
1142 // FIXME: We may want to run SLPVectorizer here.
1143 // After vectorization, assume intrinsics may tell us more
1144 // about pointer alignments.
1145#if 0
1146 MainFPM.add(AlignmentFromAssumptionsPass());
1147#endif
1148
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001149 // FIXME: Conditionally run LoadCombine here, after it's ported
1150 // (in case we still have this pass, given its questionable usefulness).
1151
Davide Italiano089a9122017-01-24 00:57:39 +00001152 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001153 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001154 MainFPM.addPass(JumpThreadingPass());
1155 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1156
1157 // Create a function that performs CFI checks for cross-DSO calls with
1158 // targets in the current module.
1159 MPM.addPass(CrossDSOCFIPass());
1160
1161 // Lower type metadata and the type.test intrinsic. This pass supports
1162 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1163 // to be run at link time if CFI is enabled. This pass does nothing if
1164 // CFI is disabled.
Teresa Johnson28023db2018-07-19 14:51:32 +00001165 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001166
1167 // Add late LTO optimization passes.
1168 // Delete basic blocks, which optimization passes may have killed.
1169 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1170
1171 // Drop bodies of available eternally objects to improve GlobalDCE.
1172 MPM.addPass(EliminateAvailableExternallyPass());
1173
1174 // Now that we have optimized the program, discard unreachable functions.
1175 MPM.addPass(GlobalDCEPass());
1176
1177 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001178 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001179}
1180
Chandler Carruth060ad612016-12-23 20:38:19 +00001181AAManager PassBuilder::buildDefaultAAPipeline() {
1182 AAManager AA;
1183
1184 // The order in which these are registered determines their priority when
1185 // being queried.
1186
1187 // First we register the basic alias analysis that provides the majority of
1188 // per-function local AA logic. This is a stateless, on-demand local set of
1189 // AA techniques.
1190 AA.registerFunctionAnalysis<BasicAA>();
1191
1192 // Next we query fast, specialized alias analyses that wrap IR-embedded
1193 // information about aliasing.
1194 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1195 AA.registerFunctionAnalysis<TypeBasedAA>();
1196
1197 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001198 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1199 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001200 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001201 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001202
1203 return AA;
1204}
1205
Chandler Carruth241bf242016-08-03 07:44:48 +00001206static Optional<int> parseRepeatPassName(StringRef Name) {
1207 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1208 return None;
1209 int Count;
1210 if (Name.getAsInteger(0, Count) || Count <= 0)
1211 return None;
1212 return Count;
1213}
1214
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001215static Optional<int> parseDevirtPassName(StringRef Name) {
1216 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1217 return None;
1218 int Count;
1219 if (Name.getAsInteger(0, Count) || Count <= 0)
1220 return None;
1221 return Count;
1222}
1223
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001224/// Tests whether a pass name starts with a valid prefix for a default pipeline
1225/// alias.
1226static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1227 return Name.startswith("default") || Name.startswith("thinlto") ||
1228 Name.startswith("lto");
1229}
1230
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001231/// Tests whether registered callbacks will accept a given pass name.
1232///
1233/// When parsing a pipeline text, the type of the outermost pipeline may be
1234/// omitted, in which case the type is automatically determined from the first
1235/// pass name in the text. This may be a name that is handled through one of the
1236/// callbacks. We check this through the oridinary parsing callbacks by setting
1237/// up a dummy PassManager in order to not force the client to also handle this
1238/// type of query.
1239template <typename PassManagerT, typename CallbacksT>
1240static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1241 if (!Callbacks.empty()) {
1242 PassManagerT DummyPM;
1243 for (auto &CB : Callbacks)
1244 if (CB(Name, DummyPM, {}))
1245 return true;
1246 }
1247 return false;
1248}
1249
1250template <typename CallbacksT>
1251static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001252 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001253 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001254 return DefaultAliasRegex.match(Name);
1255
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001256 // Explicitly handle pass manager names.
1257 if (Name == "module")
1258 return true;
1259 if (Name == "cgscc")
1260 return true;
1261 if (Name == "function")
1262 return true;
1263
Chandler Carruth241bf242016-08-03 07:44:48 +00001264 // Explicitly handle custom-parsed pass names.
1265 if (parseRepeatPassName(Name))
1266 return true;
1267
Chandler Carruth74a8a222016-06-17 07:15:29 +00001268#define MODULE_PASS(NAME, CREATE_PASS) \
1269 if (Name == NAME) \
1270 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001271#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001272 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001273 return true;
1274#include "PassRegistry.def"
1275
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001276 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001277}
1278
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001279template <typename CallbacksT>
1280static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001281 // Explicitly handle pass manager names.
1282 if (Name == "cgscc")
1283 return true;
1284 if (Name == "function")
1285 return true;
1286
Chandler Carruth241bf242016-08-03 07:44:48 +00001287 // Explicitly handle custom-parsed pass names.
1288 if (parseRepeatPassName(Name))
1289 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001290 if (parseDevirtPassName(Name))
1291 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001292
Chandler Carruth74a8a222016-06-17 07:15:29 +00001293#define CGSCC_PASS(NAME, CREATE_PASS) \
1294 if (Name == NAME) \
1295 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001296#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001297 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001298 return true;
1299#include "PassRegistry.def"
1300
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001301 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001302}
1303
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001304template <typename CallbacksT>
1305static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001306 // Explicitly handle pass manager names.
1307 if (Name == "function")
1308 return true;
1309 if (Name == "loop")
1310 return true;
1311
Chandler Carruth241bf242016-08-03 07:44:48 +00001312 // Explicitly handle custom-parsed pass names.
1313 if (parseRepeatPassName(Name))
1314 return true;
1315
Chandler Carruth74a8a222016-06-17 07:15:29 +00001316#define FUNCTION_PASS(NAME, CREATE_PASS) \
1317 if (Name == NAME) \
1318 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001319#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001320 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001321 return true;
1322#include "PassRegistry.def"
1323
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001324 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001325}
1326
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001327template <typename CallbacksT>
1328static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001329 // Explicitly handle pass manager names.
1330 if (Name == "loop")
1331 return true;
1332
Chandler Carruth241bf242016-08-03 07:44:48 +00001333 // Explicitly handle custom-parsed pass names.
1334 if (parseRepeatPassName(Name))
1335 return true;
1336
Chandler Carruth74a8a222016-06-17 07:15:29 +00001337#define LOOP_PASS(NAME, CREATE_PASS) \
1338 if (Name == NAME) \
1339 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001340#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1341 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1342 return true;
1343#include "PassRegistry.def"
1344
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001345 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001346}
1347
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001348Optional<std::vector<PassBuilder::PipelineElement>>
1349PassBuilder::parsePipelineText(StringRef Text) {
1350 std::vector<PipelineElement> ResultPipeline;
1351
1352 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1353 &ResultPipeline};
1354 for (;;) {
1355 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1356 size_t Pos = Text.find_first_of(",()");
1357 Pipeline.push_back({Text.substr(0, Pos), {}});
1358
1359 // If we have a single terminating name, we're done.
1360 if (Pos == Text.npos)
1361 break;
1362
1363 char Sep = Text[Pos];
1364 Text = Text.substr(Pos + 1);
1365 if (Sep == ',')
1366 // Just a name ending in a comma, continue.
1367 continue;
1368
1369 if (Sep == '(') {
1370 // Push the inner pipeline onto the stack to continue processing.
1371 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1372 continue;
1373 }
1374
1375 assert(Sep == ')' && "Bogus separator!");
1376 // When handling the close parenthesis, we greedily consume them to avoid
1377 // empty strings in the pipeline.
1378 do {
1379 // If we try to pop the outer pipeline we have unbalanced parentheses.
1380 if (PipelineStack.size() == 1)
1381 return None;
1382
1383 PipelineStack.pop_back();
1384 } while (Text.consume_front(")"));
1385
1386 // Check if we've finished parsing.
1387 if (Text.empty())
1388 break;
1389
1390 // Otherwise, the end of an inner pipeline always has to be followed by
1391 // a comma, and then we can continue.
1392 if (!Text.consume_front(","))
1393 return None;
1394 }
1395
1396 if (PipelineStack.size() > 1)
1397 // Unbalanced paretheses.
1398 return None;
1399
1400 assert(PipelineStack.back() == &ResultPipeline &&
1401 "Wrong pipeline at the bottom of the stack!");
1402 return {std::move(ResultPipeline)};
1403}
1404
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001405bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1406 const PipelineElement &E, bool VerifyEachPass,
1407 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001408 auto &Name = E.Name;
1409 auto &InnerPipeline = E.InnerPipeline;
1410
1411 // First handle complex passes like the pass managers which carry pipelines.
1412 if (!InnerPipeline.empty()) {
1413 if (Name == "module") {
1414 ModulePassManager NestedMPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001415 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1416 DebugLogging))
1417 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001418 MPM.addPass(std::move(NestedMPM));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001419 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001420 }
1421 if (Name == "cgscc") {
1422 CGSCCPassManager CGPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001423 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1424 DebugLogging))
1425 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001426 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001427 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001428 }
1429 if (Name == "function") {
1430 FunctionPassManager FPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001431 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1432 DebugLogging))
1433 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001434 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001435 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001436 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001437 if (auto Count = parseRepeatPassName(Name)) {
1438 ModulePassManager NestedMPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001439 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1440 DebugLogging))
1441 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001442 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001443 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001444 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001445
1446 for (auto &C : ModulePipelineParsingCallbacks)
1447 if (C(Name, MPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001448 return true;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001449
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001450 // Normal passes can't have pipelines.
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001451 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001452 }
1453
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001454 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001455 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001456 SmallVector<StringRef, 3> Matches;
1457 if (!DefaultAliasRegex.match(Name, &Matches))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001458 return false;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001459 assert(Matches.size() == 3 && "Must capture two matched strings!");
1460
Jordan Rosef85a95f2016-07-25 18:34:51 +00001461 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001462 .Case("O0", O0)
1463 .Case("O1", O1)
1464 .Case("O2", O2)
1465 .Case("O3", O3)
1466 .Case("Os", Os)
1467 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001468 if (L == O0)
1469 // At O0 we do nothing at all!
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001470 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001471
1472 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001473 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001474 } else if (Matches[1] == "thinlto-pre-link") {
1475 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1476 } else if (Matches[1] == "thinlto") {
Teresa Johnson28023db2018-07-19 14:51:32 +00001477 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001478 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001479 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001480 } else {
1481 assert(Matches[1] == "lto" && "Not one of the matched options!");
Teresa Johnson28023db2018-07-19 14:51:32 +00001482 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001483 }
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001484 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001485 }
1486
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001487 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001488#define MODULE_PASS(NAME, CREATE_PASS) \
1489 if (Name == NAME) { \
1490 MPM.addPass(CREATE_PASS); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001491 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001492 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001493#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1494 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001495 MPM.addPass( \
1496 RequireAnalysisPass< \
1497 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001498 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001499 } \
1500 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001501 MPM.addPass(InvalidateAnalysisPass< \
1502 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001503 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001504 }
1505#include "PassRegistry.def"
1506
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001507 for (auto &C : ModulePipelineParsingCallbacks)
1508 if (C(Name, MPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001509 return true;
1510 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001511}
1512
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001513bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1514 const PipelineElement &E, bool VerifyEachPass,
1515 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001516 auto &Name = E.Name;
1517 auto &InnerPipeline = E.InnerPipeline;
1518
1519 // First handle complex passes like the pass managers which carry pipelines.
1520 if (!InnerPipeline.empty()) {
1521 if (Name == "cgscc") {
1522 CGSCCPassManager NestedCGPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001523 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1524 DebugLogging))
1525 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001526 // Add the nested pass manager with the appropriate adaptor.
1527 CGPM.addPass(std::move(NestedCGPM));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001528 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001529 }
1530 if (Name == "function") {
1531 FunctionPassManager FPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001532 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1533 DebugLogging))
1534 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001535 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001536 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001537 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001538 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001539 if (auto Count = parseRepeatPassName(Name)) {
1540 CGSCCPassManager NestedCGPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001541 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1542 DebugLogging))
1543 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001544 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001545 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001546 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001547 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1548 CGSCCPassManager NestedCGPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001549 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1550 DebugLogging))
1551 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001552 CGPM.addPass(
1553 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001554 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001555 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001556
1557 for (auto &C : CGSCCPipelineParsingCallbacks)
1558 if (C(Name, CGPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001559 return true;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001560
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001561 // Normal passes can't have pipelines.
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001562 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001563 }
1564
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001565// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001566#define CGSCC_PASS(NAME, CREATE_PASS) \
1567 if (Name == NAME) { \
1568 CGPM.addPass(CREATE_PASS); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001569 return true; \
Chandler Carruth572e3402014-04-21 11:12:00 +00001570 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001571#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1572 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001573 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001574 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001575 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1576 CGSCCUpdateResult &>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001577 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001578 } \
1579 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001580 CGPM.addPass(InvalidateAnalysisPass< \
1581 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001582 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001583 }
1584#include "PassRegistry.def"
1585
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001586 for (auto &C : CGSCCPipelineParsingCallbacks)
1587 if (C(Name, CGPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001588 return true;
1589 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001590}
1591
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001592bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1593 const PipelineElement &E,
1594 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001595 auto &Name = E.Name;
1596 auto &InnerPipeline = E.InnerPipeline;
1597
1598 // First handle complex passes like the pass managers which carry pipelines.
1599 if (!InnerPipeline.empty()) {
1600 if (Name == "function") {
1601 FunctionPassManager NestedFPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001602 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1603 DebugLogging))
1604 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001605 // Add the nested pass manager with the appropriate adaptor.
1606 FPM.addPass(std::move(NestedFPM));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001607 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001608 }
1609 if (Name == "loop") {
1610 LoopPassManager LPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001611 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1612 DebugLogging))
1613 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001614 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001615 FPM.addPass(
1616 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001617 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001618 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001619 if (auto Count = parseRepeatPassName(Name)) {
1620 FunctionPassManager NestedFPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001621 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1622 DebugLogging))
1623 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001624 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001625 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001626 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001627
1628 for (auto &C : FunctionPipelineParsingCallbacks)
1629 if (C(Name, FPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001630 return true;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001631
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001632 // Normal passes can't have pipelines.
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001633 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001634 }
1635
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001636// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001637#define FUNCTION_PASS(NAME, CREATE_PASS) \
1638 if (Name == NAME) { \
1639 FPM.addPass(CREATE_PASS); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001640 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001641 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001642#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1643 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001644 FPM.addPass( \
1645 RequireAnalysisPass< \
1646 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001647 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001648 } \
1649 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001650 FPM.addPass(InvalidateAnalysisPass< \
1651 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001652 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001653 }
1654#include "PassRegistry.def"
1655
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001656 for (auto &C : FunctionPipelineParsingCallbacks)
1657 if (C(Name, FPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001658 return true;
1659 return false;
Chandler Carruthd8330982014-01-12 09:34:22 +00001660}
1661
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001662bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
1663 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001664 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001665 auto &InnerPipeline = E.InnerPipeline;
1666
1667 // First handle complex passes like the pass managers which carry pipelines.
1668 if (!InnerPipeline.empty()) {
1669 if (Name == "loop") {
1670 LoopPassManager NestedLPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001671 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1672 DebugLogging))
1673 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001674 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001675 LPM.addPass(std::move(NestedLPM));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001676 return true;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001677 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001678 if (auto Count = parseRepeatPassName(Name)) {
1679 LoopPassManager NestedLPM(DebugLogging);
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001680 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1681 DebugLogging))
1682 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001683 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001684 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001685 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001686
1687 for (auto &C : LoopPipelineParsingCallbacks)
1688 if (C(Name, LPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001689 return true;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001690
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001691 // Normal passes can't have pipelines.
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001692 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001693 }
1694
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001695// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001696#define LOOP_PASS(NAME, CREATE_PASS) \
1697 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001698 LPM.addPass(CREATE_PASS); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001699 return true; \
Justin Bognereecc3c82016-02-25 07:23:08 +00001700 }
1701#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1702 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001703 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001704 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1705 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1706 LPMUpdater &>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001707 return true; \
Justin Bognereecc3c82016-02-25 07:23:08 +00001708 } \
1709 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001710 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001711 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001712 return true; \
Justin Bognereecc3c82016-02-25 07:23:08 +00001713 }
1714#include "PassRegistry.def"
1715
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001716 for (auto &C : LoopPipelineParsingCallbacks)
1717 if (C(Name, LPM, InnerPipeline))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001718 return true;
1719 return false;
Justin Bognereecc3c82016-02-25 07:23:08 +00001720}
1721
Chandler Carruthedf59962016-02-18 09:45:17 +00001722bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001723#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1724 if (Name == NAME) { \
1725 AA.registerModuleAnalysis< \
1726 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1727 return true; \
1728 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001729#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1730 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001731 AA.registerFunctionAnalysis< \
1732 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001733 return true; \
1734 }
1735#include "PassRegistry.def"
1736
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001737 for (auto &C : AAParsingCallbacks)
1738 if (C(Name, AA))
1739 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001740 return false;
1741}
1742
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001743bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
1744 ArrayRef<PipelineElement> Pipeline,
1745 bool VerifyEachPass,
1746 bool DebugLogging) {
1747 for (const auto &Element : Pipeline) {
1748 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1749 return false;
1750 // FIXME: No verifier support for Loop passes!
1751 }
1752 return true;
1753}
1754
1755bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
1756 ArrayRef<PipelineElement> Pipeline,
1757 bool VerifyEachPass,
1758 bool DebugLogging) {
1759 for (const auto &Element : Pipeline) {
1760 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1761 return false;
1762 if (VerifyEachPass)
1763 FPM.addPass(VerifierPass());
1764 }
1765 return true;
1766}
1767
1768bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001769 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001770 bool VerifyEachPass,
1771 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001772 for (const auto &Element : Pipeline) {
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001773 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1774 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001775 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001776 }
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001777 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001778}
1779
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001780void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1781 FunctionAnalysisManager &FAM,
1782 CGSCCAnalysisManager &CGAM,
1783 ModuleAnalysisManager &MAM) {
1784 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1785 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001786 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1787 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1788 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1789 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1790 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1791}
1792
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001793bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
1794 ArrayRef<PipelineElement> Pipeline,
1795 bool VerifyEachPass,
1796 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001797 for (const auto &Element : Pipeline) {
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001798 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1799 return false;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001800 if (VerifyEachPass)
1801 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001802 }
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001803 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001804}
1805
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001806// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001807// FIXME: Should this routine accept a TargetMachine or require the caller to
1808// pre-populate the analysis managers with target-specific stuff?
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001809bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1810 StringRef PipelineText, bool VerifyEachPass,
1811 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001812 auto Pipeline = parsePipelineText(PipelineText);
1813 if (!Pipeline || Pipeline->empty())
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001814 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001815
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001816 // If the first name isn't at the module layer, wrap the pipeline up
1817 // automatically.
1818 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001819
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001820 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1821 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001822 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001823 } else if (isFunctionPassName(FirstName,
1824 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001825 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001826 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001827 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001828 } else {
1829 for (auto &C : TopLevelPipelineParsingCallbacks)
1830 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001831 return true;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001832
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001833 // Unknown pass name!
1834 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001835 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001836 }
1837
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001838 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001839}
Chandler Carruthedf59962016-02-18 09:45:17 +00001840
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001841// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001842bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1843 StringRef PipelineText, bool VerifyEachPass,
1844 bool DebugLogging) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001845 auto Pipeline = parsePipelineText(PipelineText);
1846 if (!Pipeline || Pipeline->empty())
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001847 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001848
1849 StringRef FirstName = Pipeline->front().Name;
1850 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001851 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001852
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001853 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001854}
1855
1856// Primary pass pipeline description parsing routine for a \c
1857// FunctionPassManager
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001858bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1859 StringRef PipelineText, bool VerifyEachPass,
1860 bool DebugLogging) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001861 auto Pipeline = parsePipelineText(PipelineText);
1862 if (!Pipeline || Pipeline->empty())
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001863 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001864
1865 StringRef FirstName = Pipeline->front().Name;
1866 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001867 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001868
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001869 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1870 DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001871}
1872
1873// Primary pass pipeline description parsing routine for a \c LoopPassManager
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001874bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1875 StringRef PipelineText, bool VerifyEachPass,
1876 bool DebugLogging) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001877 auto Pipeline = parsePipelineText(PipelineText);
1878 if (!Pipeline || Pipeline->empty())
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001879 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001880
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001881 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001882}
1883
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001884bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001885 // If the pipeline just consists of the word 'default' just replace the AA
1886 // manager with our default one.
1887 if (PipelineText == "default") {
1888 AA = buildDefaultAAPipeline();
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001889 return true;
Chandler Carruth060ad612016-12-23 20:38:19 +00001890 }
1891
Chandler Carruthedf59962016-02-18 09:45:17 +00001892 while (!PipelineText.empty()) {
1893 StringRef Name;
1894 std::tie(Name, PipelineText) = PipelineText.split(',');
1895 if (!parseAAPassName(AA, Name))
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001896 return false;
Chandler Carruthedf59962016-02-18 09:45:17 +00001897 }
1898
Fedor Sergeeva01be0f2018-10-15 15:36:08 +00001899 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001900}