blob: c9cc0c5ae3811509d6f9087ed504b08aea28a51d [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"
25#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000026#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000027#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000028#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
29#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000030#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000031#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000032#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000033#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000034#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000035#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000036#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000037#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000038#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000039#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000040#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000041#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000042#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000043#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet79ac42a2016-07-18 16:29:21 +000044#include "llvm/Analysis/OptimizationDiagnosticInfo.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"
Xinliang David Li64dbb292016-06-05 05:12:23 +000063#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000066#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000067#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000068#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000069#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000070#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000071#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000072#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000073#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000074#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000075#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000076#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000077#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000078#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000079#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000080#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000081#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000082#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000083#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000084#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000085#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000086#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000087#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000088#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000089#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000090#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000091#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000092#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000093#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000094#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000095#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000096#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000097#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000098#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000099#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000100#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000101#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000102#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000103#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000104#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000105#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000106#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000107#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000108#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000109#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000110#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000111#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000112#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000113#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000114#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000115#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000116#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000117#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000118#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000119#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000120#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000121#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000122#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000123#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000124#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000125#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000126#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000127#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000128#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000129#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000130#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000131#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000132#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000133#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000134#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000135#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000136#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000137#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000138#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000139#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000140#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000141#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000142#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000143#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000144#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000145#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000146#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000147
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000148#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000149
150using namespace llvm;
151
Chandler Carruth719ffe12017-02-12 05:38:04 +0000152static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
153 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000154static cl::opt<bool>
155 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
156 cl::Hidden, cl::ZeroOrMore,
157 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000158
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000159static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000160 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000161 cl::Hidden, cl::ZeroOrMore,
162 cl::desc("Run NewGVN instead of GVN"));
163
Geoff Berry3cca1da2017-06-10 15:20:03 +0000164static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000165 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
166 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000167
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000168static cl::opt<bool> EnableGVNHoist(
169 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
170 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
171
Davide Italianobe1b6a92017-06-03 23:18:29 +0000172static cl::opt<bool> EnableGVNSink(
173 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
174 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
175
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000176static Regex DefaultAliasRegex(
177 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000178
Chandler Carruthe3f50642016-12-22 06:59:15 +0000179static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
180 switch (Level) {
181 case PassBuilder::O0:
182 case PassBuilder::O1:
183 case PassBuilder::O2:
184 case PassBuilder::O3:
185 return false;
186
187 case PassBuilder::Os:
188 case PassBuilder::Oz:
189 return true;
190 }
191 llvm_unreachable("Invalid optimization level!");
192}
193
Chandler Carruth66445382014-01-11 08:16:35 +0000194namespace {
195
Chandler Carruthd8330982014-01-12 09:34:22 +0000196/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000197struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000198 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000199 return PreservedAnalyses::all();
200 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000201 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000202};
203
Chandler Carruth0b576b32015-01-06 02:50:06 +0000204/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000205class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
206 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000207 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000208
209public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000210 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000211 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000213};
214
Chandler Carruth572e3402014-04-21 11:12:00 +0000215/// \brief No-op CGSCC pass which does nothing.
216struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000217 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
218 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000219 return PreservedAnalyses::all();
220 }
221 static StringRef name() { return "NoOpCGSCCPass"; }
222};
223
Chandler Carruth0b576b32015-01-06 02:50:06 +0000224/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000225class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
226 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000227 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000228
229public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000230 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000231 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000232 return Result();
233 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235};
236
Chandler Carruthd8330982014-01-12 09:34:22 +0000237/// \brief No-op function pass which does nothing.
238struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000239 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000240 return PreservedAnalyses::all();
241 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000242 static StringRef name() { return "NoOpFunctionPass"; }
243};
244
Chandler Carruth0b576b32015-01-06 02:50:06 +0000245/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000246class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
247 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000248 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000249
250public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000251 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000252 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000254};
255
Justin Bognereecc3c82016-02-25 07:23:08 +0000256/// \brief No-op loop pass which does nothing.
257struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000258 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
259 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000260 return PreservedAnalyses::all();
261 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000262 static StringRef name() { return "NoOpLoopPass"; }
263};
264
265/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000266class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
267 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000268 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000269
270public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000271 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000272 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
273 return Result();
274 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000275 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000276};
277
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000278AnalysisKey NoOpModuleAnalysis::Key;
279AnalysisKey NoOpCGSCCAnalysis::Key;
280AnalysisKey NoOpFunctionAnalysis::Key;
281AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000282
Chandler Carruth66445382014-01-11 08:16:35 +0000283} // End anonymous namespace.
284
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000285void PassBuilder::invokePeepholeEPCallbacks(
286 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
287 for (auto &C : PeepholeEPCallbacks)
288 C(FPM, Level);
289}
290
Chandler Carruth1ff77242015-03-07 09:02:36 +0000291void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000292#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000293 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000294#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000295
296 for (auto &C : ModuleAnalysisRegistrationCallbacks)
297 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000298}
299
Chandler Carruth1ff77242015-03-07 09:02:36 +0000300void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000301#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000302 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000303#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000304
305 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
306 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000307}
308
Chandler Carruth1ff77242015-03-07 09:02:36 +0000309void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000310#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000311 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000312#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000313
314 for (auto &C : FunctionAnalysisRegistrationCallbacks)
315 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000316}
317
Justin Bognereecc3c82016-02-25 07:23:08 +0000318void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000319#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000320 LAM.registerPass([&] { return CREATE_PASS; });
321#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000322
323 for (auto &C : LoopAnalysisRegistrationCallbacks)
324 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000325}
326
Chandler Carruthe3f50642016-12-22 06:59:15 +0000327FunctionPassManager
328PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000329 ThinLTOPhase Phase,
330 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000331 assert(Level != O0 && "Must request optimizations!");
332 FunctionPassManager FPM(DebugLogging);
333
334 // Form SSA out of local memory accesses after breaking apart aggregates into
335 // scalars.
336 FPM.addPass(SROA());
337
338 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000339 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000340
Davide Italianoc3688312017-06-01 23:08:14 +0000341 // Hoisting of scalars and load expressions.
342 if (EnableGVNHoist)
343 FPM.addPass(GVNHoistPass());
344
Davide Italianobe1b6a92017-06-03 23:18:29 +0000345 // Global value numbering based sinking.
346 if (EnableGVNSink) {
347 FPM.addPass(GVNSinkPass());
348 FPM.addPass(SimplifyCFGPass());
349 }
350
Chandler Carruthe3f50642016-12-22 06:59:15 +0000351 // Speculative execution if the target has divergent branches; otherwise nop.
352 FPM.addPass(SpeculativeExecutionPass());
353
354 // Optimize based on known information about branches, and cleanup afterward.
355 FPM.addPass(JumpThreadingPass());
356 FPM.addPass(CorrelatedValuePropagationPass());
357 FPM.addPass(SimplifyCFGPass());
358 FPM.addPass(InstCombinePass());
359
360 if (!isOptimizingForSize(Level))
361 FPM.addPass(LibCallsShrinkWrapPass());
362
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000363 invokePeepholeEPCallbacks(FPM, Level);
364
Chandler Carruthe3f50642016-12-22 06:59:15 +0000365 FPM.addPass(TailCallElimPass());
366 FPM.addPass(SimplifyCFGPass());
367
368 // Form canonically associated expression trees, and simplify the trees using
369 // basic mathematical properties. For example, this will form (nearly)
370 // minimal multiplication trees.
371 FPM.addPass(ReassociatePass());
372
373 // Add the primary loop simplification pipeline.
374 // FIXME: Currently this is split into two loop pass pipelines because we run
375 // some function passes in between them. These can and should be replaced by
376 // loop pass equivalenst but those aren't ready yet. Specifically,
377 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
378 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
379 // the other we have is `LoopInstSimplify`.
380 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
381
382 // Rotate Loop - disable header duplication at -Oz
383 LPM1.addPass(LoopRotatePass(Level != Oz));
384 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000385 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000386 LPM2.addPass(IndVarSimplifyPass());
387 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000388
389 for (auto &C : LateLoopOptimizationsEPCallbacks)
390 C(LPM2, Level);
391
Chandler Carruth79b733b2017-01-26 23:21:17 +0000392 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000393 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000394 // because it changes IR to makes profile annotation in back compile
395 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000396 if (Phase != ThinLTOPhase::PreLink ||
397 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000398 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000399
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000400 for (auto &C : LoopOptimizerEndEPCallbacks)
401 C(LPM2, Level);
402
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000403 // We provide the opt remark emitter pass for LICM to use. We only need to do
404 // this once as it is immutable.
405 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000406 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
407 FPM.addPass(SimplifyCFGPass());
408 FPM.addPass(InstCombinePass());
409 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
410
411 // Eliminate redundancies.
412 if (Level != O1) {
413 // These passes add substantial compile time so skip them at O1.
414 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000415 if (RunNewGVN)
416 FPM.addPass(NewGVNPass());
417 else
418 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000419 }
420
421 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
422 FPM.addPass(MemCpyOptPass());
423
424 // Sparse conditional constant propagation.
425 // FIXME: It isn't clear why we do this *after* loop passes rather than
426 // before...
427 FPM.addPass(SCCPPass());
428
429 // Delete dead bit computations (instcombine runs after to fold away the dead
430 // computations, and then ADCE will run later to exploit any new DCE
431 // opportunities that creates).
432 FPM.addPass(BDCEPass());
433
434 // Run instcombine after redundancy and dead bit elimination to exploit
435 // opportunities opened up by them.
436 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000437 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000438
439 // Re-consider control flow based optimizations after redundancy elimination,
440 // redo DCE, etc.
441 FPM.addPass(JumpThreadingPass());
442 FPM.addPass(CorrelatedValuePropagationPass());
443 FPM.addPass(DSEPass());
444 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
445
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000446 for (auto &C : ScalarOptimizerLateEPCallbacks)
447 C(FPM, Level);
448
Chandler Carruthe3f50642016-12-22 06:59:15 +0000449 // Finally, do an expensive DCE pass to catch all the dead code exposed by
450 // the simplifications and basic cleanup after all the simplifications.
451 FPM.addPass(ADCEPass());
452 FPM.addPass(SimplifyCFGPass());
453 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000454 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000455
456 return FPM;
457}
458
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000459void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
460 PassBuilder::OptimizationLevel Level,
461 bool RunProfileGen,
462 std::string ProfileGenFile,
463 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000464 // Generally running simplification passes and the inliner with an high
465 // threshold results in smaller executables, but there may be cases where
466 // the size grows, so let's be conservative here and skip this simplification
467 // at -Os/Oz.
468 if (!isOptimizingForSize(Level)) {
469 InlineParams IP;
470
471 // In the old pass manager, this is a cl::opt. Should still this be one?
472 IP.DefaultThreshold = 75;
473
474 // FIXME: The hint threshold has the same value used by the regular inliner.
475 // This should probably be lowered after performance testing.
476 // FIXME: this comment is cargo culted from the old pass manager, revisit).
477 IP.HintThreshold = 325;
478
479 CGSCCPassManager CGPipeline(DebugLogging);
480
481 CGPipeline.addPass(InlinerPass(IP));
482
483 FunctionPassManager FPM;
484 FPM.addPass(SROA());
485 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
486 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
487 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000488 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000489
Davide Italiano513dfaa2017-02-13 15:26:22 +0000490 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
491
492 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
493 }
494
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000495 // Delete anything that is now dead to make sure that we don't instrument
496 // dead code. Instrumentation can end up keeping dead code around and
497 // dramatically increase code size.
498 MPM.addPass(GlobalDCEPass());
499
Davide Italiano513dfaa2017-02-13 15:26:22 +0000500 if (RunProfileGen) {
501 MPM.addPass(PGOInstrumentationGen());
502
Xinliang David Lib67530e2017-06-25 00:26:43 +0000503 FunctionPassManager FPM;
504 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
505 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
506
Davide Italiano513dfaa2017-02-13 15:26:22 +0000507 // Add the profile lowering pass.
508 InstrProfOptions Options;
509 if (!ProfileGenFile.empty())
510 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000511 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000512 MPM.addPass(InstrProfiling(Options));
513 }
514
515 if (!ProfileUseFile.empty())
516 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
517}
518
Easwaran Raman8249fac2017-06-28 13:33:49 +0000519static InlineParams
520getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
521 auto O3 = PassBuilder::O3;
522 unsigned OptLevel = Level > O3 ? 2 : Level;
523 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
524 return getInlineParams(OptLevel, SizeLevel);
525}
526
Chandler Carruthe3f50642016-12-22 06:59:15 +0000527ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000528PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000529 ThinLTOPhase Phase,
530 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000531 ModulePassManager MPM(DebugLogging);
532
Chandler Carruthe3f50642016-12-22 06:59:15 +0000533 // Do basic inference of function attributes from known properties of system
534 // libraries and other oracles.
535 MPM.addPass(InferFunctionAttrsPass());
536
537 // Create an early function pass manager to cleanup the output of the
538 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000539 FunctionPassManager EarlyFPM(DebugLogging);
540 EarlyFPM.addPass(SimplifyCFGPass());
541 EarlyFPM.addPass(SROA());
542 EarlyFPM.addPass(EarlyCSEPass());
543 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Dehao Chen08f88312017-08-07 20:23:20 +0000544 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
545 // to convert bitcast to direct calls so that they can be inlined during the
546 // profile annotation prepration step.
547 // More details about SamplePGO design can be found in:
548 // https://research.google.com/pubs/pub45290.html
549 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
550 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
551 Phase == ThinLTOPhase::PostLink)
552 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000553 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000554
Dehao Chen08f88312017-08-07 20:23:20 +0000555 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
556 // Annotate sample profile right after early FPM to ensure freshness of
557 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000558 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
559 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000560 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
561 // for the profile annotation to be accurate in the ThinLTO backend.
562 if (Phase != ThinLTOPhase::PreLink)
563 // We perform early indirect call promotion here, before globalopt.
564 // This is important for the ThinLTO backend phase because otherwise
565 // imported available_externally functions look unreferenced and are
566 // removed.
567 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
568 true));
569 }
570
Chandler Carruthe3f50642016-12-22 06:59:15 +0000571 // Interprocedural constant propagation now that basic cleanup has occured
572 // and prior to optimizing globals.
573 // FIXME: This position in the pipeline hasn't been carefully considered in
574 // years, it should be re-analyzed.
575 MPM.addPass(IPSCCPPass());
576
577 // Optimize globals to try and fold them into constants.
578 MPM.addPass(GlobalOptPass());
579
580 // Promote any localized globals to SSA registers.
581 // FIXME: Should this instead by a run of SROA?
582 // FIXME: We should probably run instcombine and simplify-cfg afterward to
583 // delete control flows that are dead once globals have been folded to
584 // constants.
585 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
586
587 // Remove any dead arguments exposed by cleanups and constand folding
588 // globals.
589 MPM.addPass(DeadArgumentEliminationPass());
590
591 // Create a small function pass pipeline to cleanup after all the global
592 // optimizations.
593 FunctionPassManager GlobalCleanupPM(DebugLogging);
594 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000595 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
596
Chandler Carruthe3f50642016-12-22 06:59:15 +0000597 GlobalCleanupPM.addPass(SimplifyCFGPass());
598 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
599
Dehao Chen89d32262017-08-02 01:28:31 +0000600 // Add all the requested passes for instrumentation PGO, if requested.
601 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
602 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
603 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
604 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
605 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000606 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000607
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000608 // Require the GlobalsAA analysis for the module so we can query it within
609 // the CGSCC pipeline.
610 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000611
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000612 // Require the ProfileSummaryAnalysis for the module so we can query it within
613 // the inliner pass.
614 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
615
Chandler Carruthe3f50642016-12-22 06:59:15 +0000616 // Now begin the main postorder CGSCC pipeline.
617 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
618 // manager and trying to emulate its precise behavior. Much of this doesn't
619 // make a lot of sense and we should revisit the core CGSCC structure.
620 CGSCCPassManager MainCGPipeline(DebugLogging);
621
622 // Note: historically, the PruneEH pass was run first to deduce nounwind and
623 // generally clean up exception handling overhead. It isn't clear this is
624 // valuable as the inliner doesn't currently care whether it is inlining an
625 // invoke or a call.
626
627 // Run the inliner first. The theory is that we are walking bottom-up and so
628 // the callees have already been fully optimized, and we want to inline them
629 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000630 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000631 // because it makes profile annotation in the backend inaccurate.
632 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000633 if (Phase == ThinLTOPhase::PreLink &&
634 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000635 IP.HotCallSiteThreshold = 0;
636 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000637
638 // Now deduce any function attributes based in the current code.
639 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
640
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000641 // When at O3 add argument promotion to the pass pipeline.
642 // FIXME: It isn't at all clear why this should be limited to O3.
643 if (Level == O3)
644 MainCGPipeline.addPass(ArgumentPromotionPass());
645
Chandler Carruthe3f50642016-12-22 06:59:15 +0000646 // Lastly, add the core function simplification pipeline nested inside the
647 // CGSCC walk.
648 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000649 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000650
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000651 for (auto &C : CGSCCOptimizerLateEPCallbacks)
652 C(MainCGPipeline, Level);
653
Chandler Carruth719ffe12017-02-12 05:38:04 +0000654 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
655 // to detect when we devirtualize indirect calls and iterate the SCC passes
656 // in that case to try and catch knock-on inlining or function attrs
657 // opportunities. Then we add it to the module pipeline by walking the SCCs
658 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000659 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000660 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000661 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000662
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000663 return MPM;
664}
665
666ModulePassManager
667PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
668 bool DebugLogging) {
669 ModulePassManager MPM(DebugLogging);
670
671 // Optimize globals now that the module is fully simplified.
672 MPM.addPass(GlobalOptPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000673
Xinliang David Li126157c2017-05-22 16:41:57 +0000674 // Run partial inlining pass to partially inline functions that have
675 // large bodies.
676 if (RunPartialInlining)
677 MPM.addPass(PartialInlinerPass());
678
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000679 // Remove avail extern fns and globals definitions since we aren't compiling
680 // an object file for later LTO. For LTO we want to preserve these so they
681 // are eligible for inlining at link-time. Note if they are unreferenced they
682 // will be removed by GlobalDCE later, so this only impacts referenced
683 // available externally globals. Eventually they will be suppressed during
684 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
685 // may make globals referenced by available external functions dead and saves
686 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000687 MPM.addPass(EliminateAvailableExternallyPass());
688
689 // Do RPO function attribute inference across the module to forward-propagate
690 // attributes where applicable.
691 // FIXME: Is this really an optimization rather than a canonicalization?
692 MPM.addPass(ReversePostOrderFunctionAttrsPass());
693
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000694 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000695 // useful as the above will have inlined, DCE'ed, and function-attr
696 // propagated everything. We should at this point have a reasonably minimal
697 // and richly annotated call graph. By computing aliasing and mod/ref
698 // information for all local globals here, the late loop passes and notably
699 // the vectorizer will be able to use them to help recognize vectorizable
700 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000701 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000702
703 FunctionPassManager OptimizePM(DebugLogging);
704 OptimizePM.addPass(Float2IntPass());
705 // FIXME: We need to run some loop optimizations to re-rotate loops after
706 // simplify-cfg and others undo their rotation.
707
708 // Optimize the loop execution. These passes operate on entire loop nests
709 // rather than on each loop in an inside-out manner, and so they are actually
710 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000711
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000712 for (auto &C : VectorizerStartEPCallbacks)
713 C(OptimizePM, Level);
714
Chandler Carrutha95ff382017-01-27 00:50:21 +0000715 // First rotate loops that may have been un-rotated by prior passes.
716 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
717
718 // Distribute loops to allow partial vectorization. I.e. isolate dependences
719 // into separate loop that would otherwise inhibit vectorization. This is
720 // currently only performed for loops marked with the metadata
721 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000722 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000723
724 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000725 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000726
Chandler Carruthbaabda92017-01-27 01:32:26 +0000727 // Eliminate loads by forwarding stores from the previous iteration to loads
728 // of the current iteration.
729 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000730
731 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000732 OptimizePM.addPass(InstCombinePass());
733
Chandler Carrutha95ff382017-01-27 00:50:21 +0000734
735 // Now that we've formed fast to execute loop structures, we do further
736 // optimizations. These are run afterward as they might block doing complex
737 // analyses and transforms such as what are needed for loop vectorization.
738
Chandler Carruthe3f50642016-12-22 06:59:15 +0000739 // Optimize parallel scalar instruction chains into SIMD instructions.
740 OptimizePM.addPass(SLPVectorizerPass());
741
Chandler Carrutha95ff382017-01-27 00:50:21 +0000742 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000743 OptimizePM.addPass(SimplifyCFGPass());
744 OptimizePM.addPass(InstCombinePass());
745
746 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000747 // execution resources of an out-of-order processor. We also then need to
748 // clean up redundancies and loop invariant code.
749 // FIXME: It would be really good to use a loop-integrated instruction
750 // combiner for cleanup here so that the unrolling and LICM can be pipelined
751 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000752 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000753 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000754 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000755 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000756
757 // Now that we've vectorized and unrolled loops, we may have more refined
758 // alignment information, try to re-derive it here.
759 OptimizePM.addPass(AlignmentFromAssumptionsPass());
760
Chandler Carrutha95ff382017-01-27 00:50:21 +0000761 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
762 // canonicalization pass that enables other optimizations. As a result,
763 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
764 // result too early.
765 OptimizePM.addPass(LoopSinkPass());
766
767 // And finally clean up LCSSA form before generating code.
768 OptimizePM.addPass(InstSimplifierPass());
769
Sanjay Patel6fd43912017-09-09 13:38:18 +0000770 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
771 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
772 // flattening of blocks.
773 OptimizePM.addPass(DivRemPairsPass());
774
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000775 // LoopSink (and other loop passes since the last simplifyCFG) might have
776 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
777 OptimizePM.addPass(SimplifyCFGPass());
778
Chandler Carrutha95ff382017-01-27 00:50:21 +0000779 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000780 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
781
782 // Now we need to do some global optimization transforms.
783 // FIXME: It would seem like these should come first in the optimization
784 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
785 // ordering here.
786 MPM.addPass(GlobalDCEPass());
787 MPM.addPass(ConstantMergePass());
788
789 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000790}
791
Chandler Carruthe3f50642016-12-22 06:59:15 +0000792ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000793PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
794 bool DebugLogging) {
795 assert(Level != O0 && "Must request optimizations for the default pipeline!");
796
797 ModulePassManager MPM(DebugLogging);
798
799 // Force any function attributes we want the rest of the pipeline to observe.
800 MPM.addPass(ForceFunctionAttrsPass());
801
Dehao Chen08f88312017-08-07 20:23:20 +0000802 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000803 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
804
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000805 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000806 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
807 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000808
809 // Now add the optimization pipeline.
810 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
811
812 return MPM;
813}
814
815ModulePassManager
816PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
817 bool DebugLogging) {
818 assert(Level != O0 && "Must request optimizations for the default pipeline!");
819
820 ModulePassManager MPM(DebugLogging);
821
822 // Force any function attributes we want the rest of the pipeline to observe.
823 MPM.addPass(ForceFunctionAttrsPass());
824
Dehao Chen08f88312017-08-07 20:23:20 +0000825 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000826 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
827
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000828 // If we are planning to perform ThinLTO later, we don't bloat the code with
829 // unrolling/vectorization/... now. Just simplify the module as much as we
830 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000831 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
832 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000833
834 // Run partial inlining pass to partially inline functions that have
835 // large bodies.
836 // FIXME: It isn't clear whether this is really the right place to run this
837 // in ThinLTO. Because there is another canonicalization and simplification
838 // phase that will run after the thin link, running this here ends up with
839 // less information than will be available later and it may grow functions in
840 // ways that aren't beneficial.
841 if (RunPartialInlining)
842 MPM.addPass(PartialInlinerPass());
843
844 // Reduce the size of the IR as much as possible.
845 MPM.addPass(GlobalOptPass());
846
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000847 return MPM;
848}
849
850ModulePassManager
851PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
852 bool DebugLogging) {
853 // FIXME: The summary index is not hooked in the new pass manager yet.
854 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
855 // here.
856
857 ModulePassManager MPM(DebugLogging);
858
859 // Force any function attributes we want the rest of the pipeline to observe.
860 MPM.addPass(ForceFunctionAttrsPass());
861
862 // During the ThinLTO backend phase we perform early indirect call promotion
863 // here, before globalopt. Otherwise imported available_externally functions
864 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000865 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
866 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000867 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000868 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
869 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000870
871 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000872 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
873 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000874
875 // Now add the optimization pipeline.
876 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
877
878 return MPM;
879}
880
881ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000882PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
883 bool DebugLogging) {
884 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000885 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000886 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000887}
888
Chandler Carruthe3f50642016-12-22 06:59:15 +0000889ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
890 bool DebugLogging) {
891 assert(Level != O0 && "Must request optimizations for the default pipeline!");
892 ModulePassManager MPM(DebugLogging);
893
Davide Italiano089a9122017-01-24 00:57:39 +0000894 // Remove unused virtual tables to improve the quality of code generated by
895 // whole-program devirtualization and bitset lowering.
896 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000897
Davide Italiano089a9122017-01-24 00:57:39 +0000898 // Force any function attributes we want the rest of the pipeline to observe.
899 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000900
Davide Italiano089a9122017-01-24 00:57:39 +0000901 // Do basic inference of function attributes from known properties of system
902 // libraries and other oracles.
903 MPM.addPass(InferFunctionAttrsPass());
904
905 if (Level > 1) {
906 // Indirect call promotion. This should promote all the targets that are
907 // left by the earlier promotion pass that promotes intra-module targets.
908 // This two-step promotion is to save the compile time. For LTO, it should
909 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000910 MPM.addPass(PGOIndirectCallPromotion(
911 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000912
913 // Propagate constants at call sites into the functions they call. This
914 // opens opportunities for globalopt (and inlining) by substituting function
915 // pointers passed as arguments to direct uses of functions.
916 MPM.addPass(IPSCCPPass());
917 }
918
919 // Now deduce any function attributes based in the current code.
920 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
921 PostOrderFunctionAttrsPass()));
922
923 // Do RPO function attribute inference across the module to forward-propagate
924 // attributes where applicable.
925 // FIXME: Is this really an optimization rather than a canonicalization?
926 MPM.addPass(ReversePostOrderFunctionAttrsPass());
927
928 // Use inragne annotations on GEP indices to split globals where beneficial.
929 MPM.addPass(GlobalSplitPass());
930
931 // Run whole program optimization of virtual call when the list of callees
932 // is fixed.
933 MPM.addPass(WholeProgramDevirtPass());
934
935 // Stop here at -O1.
936 if (Level == 1)
937 return MPM;
938
939 // Optimize globals to try and fold them into constants.
940 MPM.addPass(GlobalOptPass());
941
942 // Promote any localized globals to SSA registers.
943 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
944
945 // Linking modules together can lead to duplicate global constant, only
946 // keep one copy of each constant.
947 MPM.addPass(ConstantMergePass());
948
949 // Remove unused arguments from functions.
950 MPM.addPass(DeadArgumentEliminationPass());
951
952 // Reduce the code after globalopt and ipsccp. Both can open up significant
953 // simplification opportunities, and both can propagate functions through
954 // function pointers. When this happens, we often have to resolve varargs
955 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000956 FunctionPassManager PeepholeFPM(DebugLogging);
957 PeepholeFPM.addPass(InstCombinePass());
958 invokePeepholeEPCallbacks(PeepholeFPM, Level);
959
960 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000961
962 // Note: historically, the PruneEH pass was run first to deduce nounwind and
963 // generally clean up exception handling overhead. It isn't clear this is
964 // valuable as the inliner doesn't currently care whether it is inlining an
965 // invoke or a call.
966 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000967 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
968 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000969
970 // Optimize globals again after we ran the inliner.
971 MPM.addPass(GlobalOptPass());
972
973 // Garbage collect dead functions.
974 // FIXME: Add ArgumentPromotion pass after once it's ported.
975 MPM.addPass(GlobalDCEPass());
976
977 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +0000978 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +0000979 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000980 invokePeepholeEPCallbacks(FPM, Level);
981
Davide Italiano089a9122017-01-24 00:57:39 +0000982 FPM.addPass(JumpThreadingPass());
983
984 // Break up allocas
985 FPM.addPass(SROA());
986
987 // Run a few AA driver optimizations here and now to cleanup the code.
988 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
989
990 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
991 PostOrderFunctionAttrsPass()));
992 // FIXME: here we run IP alias analysis in the legacy PM.
993
994 FunctionPassManager MainFPM;
995
996 // FIXME: once we fix LoopPass Manager, add LICM here.
997 // FIXME: once we provide support for enabling MLSM, add it here.
998 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000999 if (RunNewGVN)
1000 MainFPM.addPass(NewGVNPass());
1001 else
1002 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001003
1004 // Remove dead memcpy()'s.
1005 MainFPM.addPass(MemCpyOptPass());
1006
1007 // Nuke dead stores.
1008 MainFPM.addPass(DSEPass());
1009
1010 // FIXME: at this point, we run a bunch of loop passes:
1011 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1012 // loopVectorize. Enable them once the remaining issue with LPM
1013 // are sorted out.
1014
1015 MainFPM.addPass(InstCombinePass());
1016 MainFPM.addPass(SimplifyCFGPass());
1017 MainFPM.addPass(SCCPPass());
1018 MainFPM.addPass(InstCombinePass());
1019 MainFPM.addPass(BDCEPass());
1020
1021 // FIXME: We may want to run SLPVectorizer here.
1022 // After vectorization, assume intrinsics may tell us more
1023 // about pointer alignments.
1024#if 0
1025 MainFPM.add(AlignmentFromAssumptionsPass());
1026#endif
1027
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001028 // FIXME: Conditionally run LoadCombine here, after it's ported
1029 // (in case we still have this pass, given its questionable usefulness).
1030
Davide Italiano089a9122017-01-24 00:57:39 +00001031 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001032 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001033 MainFPM.addPass(JumpThreadingPass());
1034 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1035
1036 // Create a function that performs CFI checks for cross-DSO calls with
1037 // targets in the current module.
1038 MPM.addPass(CrossDSOCFIPass());
1039
1040 // Lower type metadata and the type.test intrinsic. This pass supports
1041 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1042 // to be run at link time if CFI is enabled. This pass does nothing if
1043 // CFI is disabled.
1044 // Enable once we add support for the summary in the new PM.
1045#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001046 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1047 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001048 Summary));
1049#endif
1050
1051 // Add late LTO optimization passes.
1052 // Delete basic blocks, which optimization passes may have killed.
1053 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1054
1055 // Drop bodies of available eternally objects to improve GlobalDCE.
1056 MPM.addPass(EliminateAvailableExternallyPass());
1057
1058 // Now that we have optimized the program, discard unreachable functions.
1059 MPM.addPass(GlobalDCEPass());
1060
1061 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001062 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001063}
1064
Chandler Carruth060ad612016-12-23 20:38:19 +00001065AAManager PassBuilder::buildDefaultAAPipeline() {
1066 AAManager AA;
1067
1068 // The order in which these are registered determines their priority when
1069 // being queried.
1070
1071 // First we register the basic alias analysis that provides the majority of
1072 // per-function local AA logic. This is a stateless, on-demand local set of
1073 // AA techniques.
1074 AA.registerFunctionAnalysis<BasicAA>();
1075
1076 // Next we query fast, specialized alias analyses that wrap IR-embedded
1077 // information about aliasing.
1078 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1079 AA.registerFunctionAnalysis<TypeBasedAA>();
1080
1081 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001082 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1083 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001084 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001085 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001086
1087 return AA;
1088}
1089
Chandler Carruth241bf242016-08-03 07:44:48 +00001090static Optional<int> parseRepeatPassName(StringRef Name) {
1091 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1092 return None;
1093 int Count;
1094 if (Name.getAsInteger(0, Count) || Count <= 0)
1095 return None;
1096 return Count;
1097}
1098
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001099static Optional<int> parseDevirtPassName(StringRef Name) {
1100 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1101 return None;
1102 int Count;
1103 if (Name.getAsInteger(0, Count) || Count <= 0)
1104 return None;
1105 return Count;
1106}
1107
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001108/// Tests whether a pass name starts with a valid prefix for a default pipeline
1109/// alias.
1110static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1111 return Name.startswith("default") || Name.startswith("thinlto") ||
1112 Name.startswith("lto");
1113}
1114
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001115/// Tests whether registered callbacks will accept a given pass name.
1116///
1117/// When parsing a pipeline text, the type of the outermost pipeline may be
1118/// omitted, in which case the type is automatically determined from the first
1119/// pass name in the text. This may be a name that is handled through one of the
1120/// callbacks. We check this through the oridinary parsing callbacks by setting
1121/// up a dummy PassManager in order to not force the client to also handle this
1122/// type of query.
1123template <typename PassManagerT, typename CallbacksT>
1124static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1125 if (!Callbacks.empty()) {
1126 PassManagerT DummyPM;
1127 for (auto &CB : Callbacks)
1128 if (CB(Name, DummyPM, {}))
1129 return true;
1130 }
1131 return false;
1132}
1133
1134template <typename CallbacksT>
1135static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001136 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001137 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001138 return DefaultAliasRegex.match(Name);
1139
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001140 // Explicitly handle pass manager names.
1141 if (Name == "module")
1142 return true;
1143 if (Name == "cgscc")
1144 return true;
1145 if (Name == "function")
1146 return true;
1147
Chandler Carruth241bf242016-08-03 07:44:48 +00001148 // Explicitly handle custom-parsed pass names.
1149 if (parseRepeatPassName(Name))
1150 return true;
1151
Chandler Carruth74a8a222016-06-17 07:15:29 +00001152#define MODULE_PASS(NAME, CREATE_PASS) \
1153 if (Name == NAME) \
1154 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001155#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001156 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001157 return true;
1158#include "PassRegistry.def"
1159
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001160 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001161}
1162
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001163template <typename CallbacksT>
1164static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001165 // Explicitly handle pass manager names.
1166 if (Name == "cgscc")
1167 return true;
1168 if (Name == "function")
1169 return true;
1170
Chandler Carruth241bf242016-08-03 07:44:48 +00001171 // Explicitly handle custom-parsed pass names.
1172 if (parseRepeatPassName(Name))
1173 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001174 if (parseDevirtPassName(Name))
1175 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001176
Chandler Carruth74a8a222016-06-17 07:15:29 +00001177#define CGSCC_PASS(NAME, CREATE_PASS) \
1178 if (Name == NAME) \
1179 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001180#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001181 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001182 return true;
1183#include "PassRegistry.def"
1184
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001185 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001186}
1187
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001188template <typename CallbacksT>
1189static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001190 // Explicitly handle pass manager names.
1191 if (Name == "function")
1192 return true;
1193 if (Name == "loop")
1194 return true;
1195
Chandler Carruth241bf242016-08-03 07:44:48 +00001196 // Explicitly handle custom-parsed pass names.
1197 if (parseRepeatPassName(Name))
1198 return true;
1199
Chandler Carruth74a8a222016-06-17 07:15:29 +00001200#define FUNCTION_PASS(NAME, CREATE_PASS) \
1201 if (Name == NAME) \
1202 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001203#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001204 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001205 return true;
1206#include "PassRegistry.def"
1207
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001208 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001209}
1210
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001211template <typename CallbacksT>
1212static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001213 // Explicitly handle pass manager names.
1214 if (Name == "loop")
1215 return true;
1216
Chandler Carruth241bf242016-08-03 07:44:48 +00001217 // Explicitly handle custom-parsed pass names.
1218 if (parseRepeatPassName(Name))
1219 return true;
1220
Chandler Carruth74a8a222016-06-17 07:15:29 +00001221#define LOOP_PASS(NAME, CREATE_PASS) \
1222 if (Name == NAME) \
1223 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001224#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1225 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1226 return true;
1227#include "PassRegistry.def"
1228
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001229 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001230}
1231
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001232Optional<std::vector<PassBuilder::PipelineElement>>
1233PassBuilder::parsePipelineText(StringRef Text) {
1234 std::vector<PipelineElement> ResultPipeline;
1235
1236 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1237 &ResultPipeline};
1238 for (;;) {
1239 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1240 size_t Pos = Text.find_first_of(",()");
1241 Pipeline.push_back({Text.substr(0, Pos), {}});
1242
1243 // If we have a single terminating name, we're done.
1244 if (Pos == Text.npos)
1245 break;
1246
1247 char Sep = Text[Pos];
1248 Text = Text.substr(Pos + 1);
1249 if (Sep == ',')
1250 // Just a name ending in a comma, continue.
1251 continue;
1252
1253 if (Sep == '(') {
1254 // Push the inner pipeline onto the stack to continue processing.
1255 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1256 continue;
1257 }
1258
1259 assert(Sep == ')' && "Bogus separator!");
1260 // When handling the close parenthesis, we greedily consume them to avoid
1261 // empty strings in the pipeline.
1262 do {
1263 // If we try to pop the outer pipeline we have unbalanced parentheses.
1264 if (PipelineStack.size() == 1)
1265 return None;
1266
1267 PipelineStack.pop_back();
1268 } while (Text.consume_front(")"));
1269
1270 // Check if we've finished parsing.
1271 if (Text.empty())
1272 break;
1273
1274 // Otherwise, the end of an inner pipeline always has to be followed by
1275 // a comma, and then we can continue.
1276 if (!Text.consume_front(","))
1277 return None;
1278 }
1279
1280 if (PipelineStack.size() > 1)
1281 // Unbalanced paretheses.
1282 return None;
1283
1284 assert(PipelineStack.back() == &ResultPipeline &&
1285 "Wrong pipeline at the bottom of the stack!");
1286 return {std::move(ResultPipeline)};
1287}
1288
1289bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1290 const PipelineElement &E, bool VerifyEachPass,
1291 bool DebugLogging) {
1292 auto &Name = E.Name;
1293 auto &InnerPipeline = E.InnerPipeline;
1294
1295 // First handle complex passes like the pass managers which carry pipelines.
1296 if (!InnerPipeline.empty()) {
1297 if (Name == "module") {
1298 ModulePassManager NestedMPM(DebugLogging);
1299 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1300 DebugLogging))
1301 return false;
1302 MPM.addPass(std::move(NestedMPM));
1303 return true;
1304 }
1305 if (Name == "cgscc") {
1306 CGSCCPassManager CGPM(DebugLogging);
1307 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1308 DebugLogging))
1309 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001310 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001311 return true;
1312 }
1313 if (Name == "function") {
1314 FunctionPassManager FPM(DebugLogging);
1315 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1316 DebugLogging))
1317 return false;
1318 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1319 return true;
1320 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001321 if (auto Count = parseRepeatPassName(Name)) {
1322 ModulePassManager NestedMPM(DebugLogging);
1323 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1324 DebugLogging))
1325 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001326 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001327 return true;
1328 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001329
1330 for (auto &C : ModulePipelineParsingCallbacks)
1331 if (C(Name, MPM, InnerPipeline))
1332 return true;
1333
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001334 // Normal passes can't have pipelines.
1335 return false;
1336 }
1337
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001338 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001339 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001340 SmallVector<StringRef, 3> Matches;
1341 if (!DefaultAliasRegex.match(Name, &Matches))
1342 return false;
1343 assert(Matches.size() == 3 && "Must capture two matched strings!");
1344
Jordan Rosef85a95f2016-07-25 18:34:51 +00001345 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001346 .Case("O0", O0)
1347 .Case("O1", O1)
1348 .Case("O2", O2)
1349 .Case("O3", O3)
1350 .Case("Os", Os)
1351 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001352 if (L == O0)
1353 // At O0 we do nothing at all!
1354 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001355
1356 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001357 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001358 } else if (Matches[1] == "thinlto-pre-link") {
1359 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1360 } else if (Matches[1] == "thinlto") {
1361 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001362 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001363 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001364 } else {
1365 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001366 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001367 }
1368 return true;
1369 }
1370
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001371 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001372#define MODULE_PASS(NAME, CREATE_PASS) \
1373 if (Name == NAME) { \
1374 MPM.addPass(CREATE_PASS); \
1375 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001376 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001377#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1378 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001379 MPM.addPass( \
1380 RequireAnalysisPass< \
1381 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001382 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001383 } \
1384 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001385 MPM.addPass(InvalidateAnalysisPass< \
1386 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001387 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001388 }
1389#include "PassRegistry.def"
1390
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001391 for (auto &C : ModulePipelineParsingCallbacks)
1392 if (C(Name, MPM, InnerPipeline))
1393 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001394 return false;
1395}
1396
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001397bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1398 const PipelineElement &E, bool VerifyEachPass,
1399 bool DebugLogging) {
1400 auto &Name = E.Name;
1401 auto &InnerPipeline = E.InnerPipeline;
1402
1403 // First handle complex passes like the pass managers which carry pipelines.
1404 if (!InnerPipeline.empty()) {
1405 if (Name == "cgscc") {
1406 CGSCCPassManager NestedCGPM(DebugLogging);
1407 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1408 DebugLogging))
1409 return false;
1410 // Add the nested pass manager with the appropriate adaptor.
1411 CGPM.addPass(std::move(NestedCGPM));
1412 return true;
1413 }
1414 if (Name == "function") {
1415 FunctionPassManager FPM(DebugLogging);
1416 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1417 DebugLogging))
1418 return false;
1419 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001420 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001421 return true;
1422 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001423 if (auto Count = parseRepeatPassName(Name)) {
1424 CGSCCPassManager NestedCGPM(DebugLogging);
1425 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1426 DebugLogging))
1427 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001428 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001429 return true;
1430 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001431 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1432 CGSCCPassManager NestedCGPM(DebugLogging);
1433 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1434 DebugLogging))
1435 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001436 CGPM.addPass(
1437 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001438 return true;
1439 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001440
1441 for (auto &C : CGSCCPipelineParsingCallbacks)
1442 if (C(Name, CGPM, InnerPipeline))
1443 return true;
1444
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001445 // Normal passes can't have pipelines.
1446 return false;
1447 }
1448
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001449// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001450#define CGSCC_PASS(NAME, CREATE_PASS) \
1451 if (Name == NAME) { \
1452 CGPM.addPass(CREATE_PASS); \
1453 return true; \
1454 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001455#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1456 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001457 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001458 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001459 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1460 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001461 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001462 } \
1463 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001464 CGPM.addPass(InvalidateAnalysisPass< \
1465 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001466 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001467 }
1468#include "PassRegistry.def"
1469
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001470 for (auto &C : CGSCCPipelineParsingCallbacks)
1471 if (C(Name, CGPM, InnerPipeline))
1472 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001473 return false;
1474}
1475
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001476bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1477 const PipelineElement &E,
1478 bool VerifyEachPass, bool DebugLogging) {
1479 auto &Name = E.Name;
1480 auto &InnerPipeline = E.InnerPipeline;
1481
1482 // First handle complex passes like the pass managers which carry pipelines.
1483 if (!InnerPipeline.empty()) {
1484 if (Name == "function") {
1485 FunctionPassManager NestedFPM(DebugLogging);
1486 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1487 DebugLogging))
1488 return false;
1489 // Add the nested pass manager with the appropriate adaptor.
1490 FPM.addPass(std::move(NestedFPM));
1491 return true;
1492 }
1493 if (Name == "loop") {
1494 LoopPassManager LPM(DebugLogging);
1495 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1496 DebugLogging))
1497 return false;
1498 // Add the nested pass manager with the appropriate adaptor.
1499 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1500 return true;
1501 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001502 if (auto Count = parseRepeatPassName(Name)) {
1503 FunctionPassManager NestedFPM(DebugLogging);
1504 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1505 DebugLogging))
1506 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001507 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001508 return true;
1509 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001510
1511 for (auto &C : FunctionPipelineParsingCallbacks)
1512 if (C(Name, FPM, InnerPipeline))
1513 return true;
1514
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001515 // Normal passes can't have pipelines.
1516 return false;
1517 }
1518
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001519// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001520#define FUNCTION_PASS(NAME, CREATE_PASS) \
1521 if (Name == NAME) { \
1522 FPM.addPass(CREATE_PASS); \
1523 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001524 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001525#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1526 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001527 FPM.addPass( \
1528 RequireAnalysisPass< \
1529 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001530 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001531 } \
1532 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001533 FPM.addPass(InvalidateAnalysisPass< \
1534 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001535 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001536 }
1537#include "PassRegistry.def"
1538
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001539 for (auto &C : FunctionPipelineParsingCallbacks)
1540 if (C(Name, FPM, InnerPipeline))
1541 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001542 return false;
1543}
1544
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001545bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001546 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001547 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001548 auto &InnerPipeline = E.InnerPipeline;
1549
1550 // First handle complex passes like the pass managers which carry pipelines.
1551 if (!InnerPipeline.empty()) {
1552 if (Name == "loop") {
1553 LoopPassManager NestedLPM(DebugLogging);
1554 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1555 DebugLogging))
1556 return false;
1557 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001558 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001559 return true;
1560 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001561 if (auto Count = parseRepeatPassName(Name)) {
1562 LoopPassManager NestedLPM(DebugLogging);
1563 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1564 DebugLogging))
1565 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001566 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001567 return true;
1568 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001569
1570 for (auto &C : LoopPipelineParsingCallbacks)
1571 if (C(Name, LPM, InnerPipeline))
1572 return true;
1573
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001574 // Normal passes can't have pipelines.
1575 return false;
1576 }
1577
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001578// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001579#define LOOP_PASS(NAME, CREATE_PASS) \
1580 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001581 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001582 return true; \
1583 }
1584#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1585 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001586 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001587 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1588 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1589 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001590 return true; \
1591 } \
1592 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001593 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001594 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001595 return true; \
1596 }
1597#include "PassRegistry.def"
1598
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001599 for (auto &C : LoopPipelineParsingCallbacks)
1600 if (C(Name, LPM, InnerPipeline))
1601 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001602 return false;
1603}
1604
Chandler Carruthedf59962016-02-18 09:45:17 +00001605bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001606#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1607 if (Name == NAME) { \
1608 AA.registerModuleAnalysis< \
1609 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1610 return true; \
1611 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001612#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1613 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001614 AA.registerFunctionAnalysis< \
1615 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001616 return true; \
1617 }
1618#include "PassRegistry.def"
1619
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001620 for (auto &C : AAParsingCallbacks)
1621 if (C(Name, AA))
1622 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001623 return false;
1624}
1625
Justin Bognereecc3c82016-02-25 07:23:08 +00001626bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001627 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001628 bool VerifyEachPass,
1629 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001630 for (const auto &Element : Pipeline) {
1631 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1632 return false;
1633 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001634 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001635 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001636}
1637
Chandler Carruth1ff77242015-03-07 09:02:36 +00001638bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001639 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001640 bool VerifyEachPass,
1641 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001642 for (const auto &Element : Pipeline) {
1643 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1644 return false;
1645 if (VerifyEachPass)
1646 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001647 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001648 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001649}
1650
Chandler Carruth1ff77242015-03-07 09:02:36 +00001651bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001652 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001653 bool VerifyEachPass,
1654 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001655 for (const auto &Element : Pipeline) {
1656 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1657 return false;
1658 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001659 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001660 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001661}
1662
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001663void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1664 FunctionAnalysisManager &FAM,
1665 CGSCCAnalysisManager &CGAM,
1666 ModuleAnalysisManager &MAM) {
1667 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1668 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001669 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1670 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1671 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1672 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1673 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1674}
1675
Chandler Carruth1ff77242015-03-07 09:02:36 +00001676bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001677 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001678 bool VerifyEachPass,
1679 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001680 for (const auto &Element : Pipeline) {
1681 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1682 return false;
1683 if (VerifyEachPass)
1684 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001685 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001686 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001687}
1688
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001689// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001690// FIXME: Should this routine accept a TargetMachine or require the caller to
1691// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001692bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1693 StringRef PipelineText, bool VerifyEachPass,
1694 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001695 auto Pipeline = parsePipelineText(PipelineText);
1696 if (!Pipeline || Pipeline->empty())
1697 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001698
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001699 // If the first name isn't at the module layer, wrap the pipeline up
1700 // automatically.
1701 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001702
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001703 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1704 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001706 } else if (isFunctionPassName(FirstName,
1707 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001708 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001709 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001710 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001711 } else {
1712 for (auto &C : TopLevelPipelineParsingCallbacks)
1713 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1714 return true;
1715
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001716 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001717 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001718 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001719 }
1720
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001721 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001722}
Chandler Carruthedf59962016-02-18 09:45:17 +00001723
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001724// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1725bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1726 StringRef PipelineText, bool VerifyEachPass,
1727 bool DebugLogging) {
1728 auto Pipeline = parsePipelineText(PipelineText);
1729 if (!Pipeline || Pipeline->empty())
1730 return false;
1731
1732 StringRef FirstName = Pipeline->front().Name;
1733 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1734 return false;
1735
1736 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1737}
1738
1739// Primary pass pipeline description parsing routine for a \c
1740// FunctionPassManager
1741bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1742 StringRef PipelineText, bool VerifyEachPass,
1743 bool DebugLogging) {
1744 auto Pipeline = parsePipelineText(PipelineText);
1745 if (!Pipeline || Pipeline->empty())
1746 return false;
1747
1748 StringRef FirstName = Pipeline->front().Name;
1749 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1750 return false;
1751
1752 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1753 DebugLogging);
1754}
1755
1756// Primary pass pipeline description parsing routine for a \c LoopPassManager
1757bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1758 StringRef PipelineText, bool VerifyEachPass,
1759 bool DebugLogging) {
1760 auto Pipeline = parsePipelineText(PipelineText);
1761 if (!Pipeline || Pipeline->empty())
1762 return false;
1763
1764 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1765}
1766
Chandler Carruthedf59962016-02-18 09:45:17 +00001767bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001768 // If the pipeline just consists of the word 'default' just replace the AA
1769 // manager with our default one.
1770 if (PipelineText == "default") {
1771 AA = buildDefaultAAPipeline();
1772 return true;
1773 }
1774
Chandler Carruthedf59962016-02-18 09:45:17 +00001775 while (!PipelineText.empty()) {
1776 StringRef Name;
1777 std::tie(Name, PipelineText) = PipelineText.split(',');
1778 if (!parseAAPassName(AA, Name))
1779 return false;
1780 }
1781
1782 return true;
1783}