blob: c277b5b14e750f233e3b54885c90e7f6c8b40225 [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.
558 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile));
559 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
560 // for the profile annotation to be accurate in the ThinLTO backend.
561 if (Phase != ThinLTOPhase::PreLink)
562 // We perform early indirect call promotion here, before globalopt.
563 // This is important for the ThinLTO backend phase because otherwise
564 // imported available_externally functions look unreferenced and are
565 // removed.
566 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
567 true));
568 }
569
Chandler Carruthe3f50642016-12-22 06:59:15 +0000570 // Interprocedural constant propagation now that basic cleanup has occured
571 // and prior to optimizing globals.
572 // FIXME: This position in the pipeline hasn't been carefully considered in
573 // years, it should be re-analyzed.
574 MPM.addPass(IPSCCPPass());
575
576 // Optimize globals to try and fold them into constants.
577 MPM.addPass(GlobalOptPass());
578
579 // Promote any localized globals to SSA registers.
580 // FIXME: Should this instead by a run of SROA?
581 // FIXME: We should probably run instcombine and simplify-cfg afterward to
582 // delete control flows that are dead once globals have been folded to
583 // constants.
584 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
585
586 // Remove any dead arguments exposed by cleanups and constand folding
587 // globals.
588 MPM.addPass(DeadArgumentEliminationPass());
589
590 // Create a small function pass pipeline to cleanup after all the global
591 // optimizations.
592 FunctionPassManager GlobalCleanupPM(DebugLogging);
593 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000594 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
595
Chandler Carruthe3f50642016-12-22 06:59:15 +0000596 GlobalCleanupPM.addPass(SimplifyCFGPass());
597 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
598
Dehao Chen89d32262017-08-02 01:28:31 +0000599 // Add all the requested passes for instrumentation PGO, if requested.
600 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
601 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
602 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
603 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
604 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000605 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000606
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000607 // Require the GlobalsAA analysis for the module so we can query it within
608 // the CGSCC pipeline.
609 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000610
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000611 // Require the ProfileSummaryAnalysis for the module so we can query it within
612 // the inliner pass.
613 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
614
Chandler Carruthe3f50642016-12-22 06:59:15 +0000615 // Now begin the main postorder CGSCC pipeline.
616 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
617 // manager and trying to emulate its precise behavior. Much of this doesn't
618 // make a lot of sense and we should revisit the core CGSCC structure.
619 CGSCCPassManager MainCGPipeline(DebugLogging);
620
621 // Note: historically, the PruneEH pass was run first to deduce nounwind and
622 // generally clean up exception handling overhead. It isn't clear this is
623 // valuable as the inliner doesn't currently care whether it is inlining an
624 // invoke or a call.
625
626 // Run the inliner first. The theory is that we are walking bottom-up and so
627 // the callees have already been fully optimized, and we want to inline them
628 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000629 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000630 // because it makes profile annotation in the backend inaccurate.
631 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000632 if (Phase == ThinLTOPhase::PreLink &&
633 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000634 IP.HotCallSiteThreshold = 0;
635 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000636
637 // Now deduce any function attributes based in the current code.
638 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
639
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000640 // When at O3 add argument promotion to the pass pipeline.
641 // FIXME: It isn't at all clear why this should be limited to O3.
642 if (Level == O3)
643 MainCGPipeline.addPass(ArgumentPromotionPass());
644
Chandler Carruthe3f50642016-12-22 06:59:15 +0000645 // Lastly, add the core function simplification pipeline nested inside the
646 // CGSCC walk.
647 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000648 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000649
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000650 for (auto &C : CGSCCOptimizerLateEPCallbacks)
651 C(MainCGPipeline, Level);
652
Chandler Carruth719ffe12017-02-12 05:38:04 +0000653 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
654 // to detect when we devirtualize indirect calls and iterate the SCC passes
655 // in that case to try and catch knock-on inlining or function attrs
656 // opportunities. Then we add it to the module pipeline by walking the SCCs
657 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000658 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000659 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000660 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000661
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000662 return MPM;
663}
664
665ModulePassManager
666PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
667 bool DebugLogging) {
668 ModulePassManager MPM(DebugLogging);
669
670 // Optimize globals now that the module is fully simplified.
671 MPM.addPass(GlobalOptPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000672
Xinliang David Li126157c2017-05-22 16:41:57 +0000673 // Run partial inlining pass to partially inline functions that have
674 // large bodies.
675 if (RunPartialInlining)
676 MPM.addPass(PartialInlinerPass());
677
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000678 // Remove avail extern fns and globals definitions since we aren't compiling
679 // an object file for later LTO. For LTO we want to preserve these so they
680 // are eligible for inlining at link-time. Note if they are unreferenced they
681 // will be removed by GlobalDCE later, so this only impacts referenced
682 // available externally globals. Eventually they will be suppressed during
683 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
684 // may make globals referenced by available external functions dead and saves
685 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000686 MPM.addPass(EliminateAvailableExternallyPass());
687
688 // Do RPO function attribute inference across the module to forward-propagate
689 // attributes where applicable.
690 // FIXME: Is this really an optimization rather than a canonicalization?
691 MPM.addPass(ReversePostOrderFunctionAttrsPass());
692
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000693 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000694 // useful as the above will have inlined, DCE'ed, and function-attr
695 // propagated everything. We should at this point have a reasonably minimal
696 // and richly annotated call graph. By computing aliasing and mod/ref
697 // information for all local globals here, the late loop passes and notably
698 // the vectorizer will be able to use them to help recognize vectorizable
699 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000700 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000701
702 FunctionPassManager OptimizePM(DebugLogging);
703 OptimizePM.addPass(Float2IntPass());
704 // FIXME: We need to run some loop optimizations to re-rotate loops after
705 // simplify-cfg and others undo their rotation.
706
707 // Optimize the loop execution. These passes operate on entire loop nests
708 // rather than on each loop in an inside-out manner, and so they are actually
709 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000710
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000711 for (auto &C : VectorizerStartEPCallbacks)
712 C(OptimizePM, Level);
713
Chandler Carrutha95ff382017-01-27 00:50:21 +0000714 // First rotate loops that may have been un-rotated by prior passes.
715 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
716
717 // Distribute loops to allow partial vectorization. I.e. isolate dependences
718 // into separate loop that would otherwise inhibit vectorization. This is
719 // currently only performed for loops marked with the metadata
720 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000721 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000722
723 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000724 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000725
Chandler Carruthbaabda92017-01-27 01:32:26 +0000726 // Eliminate loads by forwarding stores from the previous iteration to loads
727 // of the current iteration.
728 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000729
730 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000731 OptimizePM.addPass(InstCombinePass());
732
Chandler Carrutha95ff382017-01-27 00:50:21 +0000733
734 // Now that we've formed fast to execute loop structures, we do further
735 // optimizations. These are run afterward as they might block doing complex
736 // analyses and transforms such as what are needed for loop vectorization.
737
Chandler Carruthe3f50642016-12-22 06:59:15 +0000738 // Optimize parallel scalar instruction chains into SIMD instructions.
739 OptimizePM.addPass(SLPVectorizerPass());
740
Chandler Carrutha95ff382017-01-27 00:50:21 +0000741 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000742 OptimizePM.addPass(SimplifyCFGPass());
743 OptimizePM.addPass(InstCombinePass());
744
745 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000746 // execution resources of an out-of-order processor. We also then need to
747 // clean up redundancies and loop invariant code.
748 // FIXME: It would be really good to use a loop-integrated instruction
749 // combiner for cleanup here so that the unrolling and LICM can be pipelined
750 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000751 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000752 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000753 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000754 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000755
756 // Now that we've vectorized and unrolled loops, we may have more refined
757 // alignment information, try to re-derive it here.
758 OptimizePM.addPass(AlignmentFromAssumptionsPass());
759
Chandler Carrutha95ff382017-01-27 00:50:21 +0000760 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
761 // canonicalization pass that enables other optimizations. As a result,
762 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
763 // result too early.
764 OptimizePM.addPass(LoopSinkPass());
765
766 // And finally clean up LCSSA form before generating code.
767 OptimizePM.addPass(InstSimplifierPass());
768
Sanjay Patel6fd43912017-09-09 13:38:18 +0000769 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
770 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
771 // flattening of blocks.
772 OptimizePM.addPass(DivRemPairsPass());
773
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000774 // LoopSink (and other loop passes since the last simplifyCFG) might have
775 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
776 OptimizePM.addPass(SimplifyCFGPass());
777
Chandler Carrutha95ff382017-01-27 00:50:21 +0000778 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000779 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
780
781 // Now we need to do some global optimization transforms.
782 // FIXME: It would seem like these should come first in the optimization
783 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
784 // ordering here.
785 MPM.addPass(GlobalDCEPass());
786 MPM.addPass(ConstantMergePass());
787
788 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000789}
790
Chandler Carruthe3f50642016-12-22 06:59:15 +0000791ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000792PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
793 bool DebugLogging) {
794 assert(Level != O0 && "Must request optimizations for the default pipeline!");
795
796 ModulePassManager MPM(DebugLogging);
797
798 // Force any function attributes we want the rest of the pipeline to observe.
799 MPM.addPass(ForceFunctionAttrsPass());
800
Dehao Chen08f88312017-08-07 20:23:20 +0000801 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000802 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
803
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000804 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000805 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
806 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000807
808 // Now add the optimization pipeline.
809 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
810
811 return MPM;
812}
813
814ModulePassManager
815PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
816 bool DebugLogging) {
817 assert(Level != O0 && "Must request optimizations for the default pipeline!");
818
819 ModulePassManager MPM(DebugLogging);
820
821 // Force any function attributes we want the rest of the pipeline to observe.
822 MPM.addPass(ForceFunctionAttrsPass());
823
Dehao Chen08f88312017-08-07 20:23:20 +0000824 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000825 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
826
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000827 // If we are planning to perform ThinLTO later, we don't bloat the code with
828 // unrolling/vectorization/... now. Just simplify the module as much as we
829 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000830 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
831 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000832
833 // Run partial inlining pass to partially inline functions that have
834 // large bodies.
835 // FIXME: It isn't clear whether this is really the right place to run this
836 // in ThinLTO. Because there is another canonicalization and simplification
837 // phase that will run after the thin link, running this here ends up with
838 // less information than will be available later and it may grow functions in
839 // ways that aren't beneficial.
840 if (RunPartialInlining)
841 MPM.addPass(PartialInlinerPass());
842
843 // Reduce the size of the IR as much as possible.
844 MPM.addPass(GlobalOptPass());
845
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000846 return MPM;
847}
848
849ModulePassManager
850PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
851 bool DebugLogging) {
852 // FIXME: The summary index is not hooked in the new pass manager yet.
853 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
854 // here.
855
856 ModulePassManager MPM(DebugLogging);
857
858 // Force any function attributes we want the rest of the pipeline to observe.
859 MPM.addPass(ForceFunctionAttrsPass());
860
861 // During the ThinLTO backend phase we perform early indirect call promotion
862 // here, before globalopt. Otherwise imported available_externally functions
863 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000864 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
865 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000866 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000867 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
868 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000869
870 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000871 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
872 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000873
874 // Now add the optimization pipeline.
875 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
876
877 return MPM;
878}
879
880ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000881PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
882 bool DebugLogging) {
883 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000884 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000885 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000886}
887
Chandler Carruthe3f50642016-12-22 06:59:15 +0000888ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
889 bool DebugLogging) {
890 assert(Level != O0 && "Must request optimizations for the default pipeline!");
891 ModulePassManager MPM(DebugLogging);
892
Davide Italiano089a9122017-01-24 00:57:39 +0000893 // Remove unused virtual tables to improve the quality of code generated by
894 // whole-program devirtualization and bitset lowering.
895 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000896
Davide Italiano089a9122017-01-24 00:57:39 +0000897 // Force any function attributes we want the rest of the pipeline to observe.
898 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000899
Davide Italiano089a9122017-01-24 00:57:39 +0000900 // Do basic inference of function attributes from known properties of system
901 // libraries and other oracles.
902 MPM.addPass(InferFunctionAttrsPass());
903
904 if (Level > 1) {
905 // Indirect call promotion. This should promote all the targets that are
906 // left by the earlier promotion pass that promotes intra-module targets.
907 // This two-step promotion is to save the compile time. For LTO, it should
908 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000909 MPM.addPass(PGOIndirectCallPromotion(
910 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000911
912 // Propagate constants at call sites into the functions they call. This
913 // opens opportunities for globalopt (and inlining) by substituting function
914 // pointers passed as arguments to direct uses of functions.
915 MPM.addPass(IPSCCPPass());
916 }
917
918 // Now deduce any function attributes based in the current code.
919 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
920 PostOrderFunctionAttrsPass()));
921
922 // Do RPO function attribute inference across the module to forward-propagate
923 // attributes where applicable.
924 // FIXME: Is this really an optimization rather than a canonicalization?
925 MPM.addPass(ReversePostOrderFunctionAttrsPass());
926
927 // Use inragne annotations on GEP indices to split globals where beneficial.
928 MPM.addPass(GlobalSplitPass());
929
930 // Run whole program optimization of virtual call when the list of callees
931 // is fixed.
932 MPM.addPass(WholeProgramDevirtPass());
933
934 // Stop here at -O1.
935 if (Level == 1)
936 return MPM;
937
938 // Optimize globals to try and fold them into constants.
939 MPM.addPass(GlobalOptPass());
940
941 // Promote any localized globals to SSA registers.
942 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
943
944 // Linking modules together can lead to duplicate global constant, only
945 // keep one copy of each constant.
946 MPM.addPass(ConstantMergePass());
947
948 // Remove unused arguments from functions.
949 MPM.addPass(DeadArgumentEliminationPass());
950
951 // Reduce the code after globalopt and ipsccp. Both can open up significant
952 // simplification opportunities, and both can propagate functions through
953 // function pointers. When this happens, we often have to resolve varargs
954 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000955 FunctionPassManager PeepholeFPM(DebugLogging);
956 PeepholeFPM.addPass(InstCombinePass());
957 invokePeepholeEPCallbacks(PeepholeFPM, Level);
958
959 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000960
961 // Note: historically, the PruneEH pass was run first to deduce nounwind and
962 // generally clean up exception handling overhead. It isn't clear this is
963 // valuable as the inliner doesn't currently care whether it is inlining an
964 // invoke or a call.
965 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000966 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
967 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000968
969 // Optimize globals again after we ran the inliner.
970 MPM.addPass(GlobalOptPass());
971
972 // Garbage collect dead functions.
973 // FIXME: Add ArgumentPromotion pass after once it's ported.
974 MPM.addPass(GlobalDCEPass());
975
976 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +0000977 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +0000978 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000979 invokePeepholeEPCallbacks(FPM, Level);
980
Davide Italiano089a9122017-01-24 00:57:39 +0000981 FPM.addPass(JumpThreadingPass());
982
983 // Break up allocas
984 FPM.addPass(SROA());
985
986 // Run a few AA driver optimizations here and now to cleanup the code.
987 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
988
989 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
990 PostOrderFunctionAttrsPass()));
991 // FIXME: here we run IP alias analysis in the legacy PM.
992
993 FunctionPassManager MainFPM;
994
995 // FIXME: once we fix LoopPass Manager, add LICM here.
996 // FIXME: once we provide support for enabling MLSM, add it here.
997 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000998 if (RunNewGVN)
999 MainFPM.addPass(NewGVNPass());
1000 else
1001 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001002
1003 // Remove dead memcpy()'s.
1004 MainFPM.addPass(MemCpyOptPass());
1005
1006 // Nuke dead stores.
1007 MainFPM.addPass(DSEPass());
1008
1009 // FIXME: at this point, we run a bunch of loop passes:
1010 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1011 // loopVectorize. Enable them once the remaining issue with LPM
1012 // are sorted out.
1013
1014 MainFPM.addPass(InstCombinePass());
1015 MainFPM.addPass(SimplifyCFGPass());
1016 MainFPM.addPass(SCCPPass());
1017 MainFPM.addPass(InstCombinePass());
1018 MainFPM.addPass(BDCEPass());
1019
1020 // FIXME: We may want to run SLPVectorizer here.
1021 // After vectorization, assume intrinsics may tell us more
1022 // about pointer alignments.
1023#if 0
1024 MainFPM.add(AlignmentFromAssumptionsPass());
1025#endif
1026
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001027 // FIXME: Conditionally run LoadCombine here, after it's ported
1028 // (in case we still have this pass, given its questionable usefulness).
1029
Davide Italiano089a9122017-01-24 00:57:39 +00001030 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001031 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001032 MainFPM.addPass(JumpThreadingPass());
1033 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1034
1035 // Create a function that performs CFI checks for cross-DSO calls with
1036 // targets in the current module.
1037 MPM.addPass(CrossDSOCFIPass());
1038
1039 // Lower type metadata and the type.test intrinsic. This pass supports
1040 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1041 // to be run at link time if CFI is enabled. This pass does nothing if
1042 // CFI is disabled.
1043 // Enable once we add support for the summary in the new PM.
1044#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001045 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1046 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001047 Summary));
1048#endif
1049
1050 // Add late LTO optimization passes.
1051 // Delete basic blocks, which optimization passes may have killed.
1052 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1053
1054 // Drop bodies of available eternally objects to improve GlobalDCE.
1055 MPM.addPass(EliminateAvailableExternallyPass());
1056
1057 // Now that we have optimized the program, discard unreachable functions.
1058 MPM.addPass(GlobalDCEPass());
1059
1060 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001061 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001062}
1063
Chandler Carruth060ad612016-12-23 20:38:19 +00001064AAManager PassBuilder::buildDefaultAAPipeline() {
1065 AAManager AA;
1066
1067 // The order in which these are registered determines their priority when
1068 // being queried.
1069
1070 // First we register the basic alias analysis that provides the majority of
1071 // per-function local AA logic. This is a stateless, on-demand local set of
1072 // AA techniques.
1073 AA.registerFunctionAnalysis<BasicAA>();
1074
1075 // Next we query fast, specialized alias analyses that wrap IR-embedded
1076 // information about aliasing.
1077 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1078 AA.registerFunctionAnalysis<TypeBasedAA>();
1079
1080 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001081 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1082 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001083 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001084 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001085
1086 return AA;
1087}
1088
Chandler Carruth241bf242016-08-03 07:44:48 +00001089static Optional<int> parseRepeatPassName(StringRef Name) {
1090 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1091 return None;
1092 int Count;
1093 if (Name.getAsInteger(0, Count) || Count <= 0)
1094 return None;
1095 return Count;
1096}
1097
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001098static Optional<int> parseDevirtPassName(StringRef Name) {
1099 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1100 return None;
1101 int Count;
1102 if (Name.getAsInteger(0, Count) || Count <= 0)
1103 return None;
1104 return Count;
1105}
1106
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001107/// Tests whether a pass name starts with a valid prefix for a default pipeline
1108/// alias.
1109static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1110 return Name.startswith("default") || Name.startswith("thinlto") ||
1111 Name.startswith("lto");
1112}
1113
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001114/// Tests whether registered callbacks will accept a given pass name.
1115///
1116/// When parsing a pipeline text, the type of the outermost pipeline may be
1117/// omitted, in which case the type is automatically determined from the first
1118/// pass name in the text. This may be a name that is handled through one of the
1119/// callbacks. We check this through the oridinary parsing callbacks by setting
1120/// up a dummy PassManager in order to not force the client to also handle this
1121/// type of query.
1122template <typename PassManagerT, typename CallbacksT>
1123static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1124 if (!Callbacks.empty()) {
1125 PassManagerT DummyPM;
1126 for (auto &CB : Callbacks)
1127 if (CB(Name, DummyPM, {}))
1128 return true;
1129 }
1130 return false;
1131}
1132
1133template <typename CallbacksT>
1134static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001135 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001136 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001137 return DefaultAliasRegex.match(Name);
1138
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001139 // Explicitly handle pass manager names.
1140 if (Name == "module")
1141 return true;
1142 if (Name == "cgscc")
1143 return true;
1144 if (Name == "function")
1145 return true;
1146
Chandler Carruth241bf242016-08-03 07:44:48 +00001147 // Explicitly handle custom-parsed pass names.
1148 if (parseRepeatPassName(Name))
1149 return true;
1150
Chandler Carruth74a8a222016-06-17 07:15:29 +00001151#define MODULE_PASS(NAME, CREATE_PASS) \
1152 if (Name == NAME) \
1153 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001154#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001155 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001156 return true;
1157#include "PassRegistry.def"
1158
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001159 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001160}
1161
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001162template <typename CallbacksT>
1163static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001164 // Explicitly handle pass manager names.
1165 if (Name == "cgscc")
1166 return true;
1167 if (Name == "function")
1168 return true;
1169
Chandler Carruth241bf242016-08-03 07:44:48 +00001170 // Explicitly handle custom-parsed pass names.
1171 if (parseRepeatPassName(Name))
1172 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001173 if (parseDevirtPassName(Name))
1174 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001175
Chandler Carruth74a8a222016-06-17 07:15:29 +00001176#define CGSCC_PASS(NAME, CREATE_PASS) \
1177 if (Name == NAME) \
1178 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001179#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001180 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001181 return true;
1182#include "PassRegistry.def"
1183
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001184 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001185}
1186
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001187template <typename CallbacksT>
1188static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001189 // Explicitly handle pass manager names.
1190 if (Name == "function")
1191 return true;
1192 if (Name == "loop")
1193 return true;
1194
Chandler Carruth241bf242016-08-03 07:44:48 +00001195 // Explicitly handle custom-parsed pass names.
1196 if (parseRepeatPassName(Name))
1197 return true;
1198
Chandler Carruth74a8a222016-06-17 07:15:29 +00001199#define FUNCTION_PASS(NAME, CREATE_PASS) \
1200 if (Name == NAME) \
1201 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001202#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001203 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001204 return true;
1205#include "PassRegistry.def"
1206
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001207 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001208}
1209
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001210template <typename CallbacksT>
1211static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001212 // Explicitly handle pass manager names.
1213 if (Name == "loop")
1214 return true;
1215
Chandler Carruth241bf242016-08-03 07:44:48 +00001216 // Explicitly handle custom-parsed pass names.
1217 if (parseRepeatPassName(Name))
1218 return true;
1219
Chandler Carruth74a8a222016-06-17 07:15:29 +00001220#define LOOP_PASS(NAME, CREATE_PASS) \
1221 if (Name == NAME) \
1222 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001223#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1224 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1225 return true;
1226#include "PassRegistry.def"
1227
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001228 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001229}
1230
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001231Optional<std::vector<PassBuilder::PipelineElement>>
1232PassBuilder::parsePipelineText(StringRef Text) {
1233 std::vector<PipelineElement> ResultPipeline;
1234
1235 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1236 &ResultPipeline};
1237 for (;;) {
1238 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1239 size_t Pos = Text.find_first_of(",()");
1240 Pipeline.push_back({Text.substr(0, Pos), {}});
1241
1242 // If we have a single terminating name, we're done.
1243 if (Pos == Text.npos)
1244 break;
1245
1246 char Sep = Text[Pos];
1247 Text = Text.substr(Pos + 1);
1248 if (Sep == ',')
1249 // Just a name ending in a comma, continue.
1250 continue;
1251
1252 if (Sep == '(') {
1253 // Push the inner pipeline onto the stack to continue processing.
1254 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1255 continue;
1256 }
1257
1258 assert(Sep == ')' && "Bogus separator!");
1259 // When handling the close parenthesis, we greedily consume them to avoid
1260 // empty strings in the pipeline.
1261 do {
1262 // If we try to pop the outer pipeline we have unbalanced parentheses.
1263 if (PipelineStack.size() == 1)
1264 return None;
1265
1266 PipelineStack.pop_back();
1267 } while (Text.consume_front(")"));
1268
1269 // Check if we've finished parsing.
1270 if (Text.empty())
1271 break;
1272
1273 // Otherwise, the end of an inner pipeline always has to be followed by
1274 // a comma, and then we can continue.
1275 if (!Text.consume_front(","))
1276 return None;
1277 }
1278
1279 if (PipelineStack.size() > 1)
1280 // Unbalanced paretheses.
1281 return None;
1282
1283 assert(PipelineStack.back() == &ResultPipeline &&
1284 "Wrong pipeline at the bottom of the stack!");
1285 return {std::move(ResultPipeline)};
1286}
1287
1288bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1289 const PipelineElement &E, bool VerifyEachPass,
1290 bool DebugLogging) {
1291 auto &Name = E.Name;
1292 auto &InnerPipeline = E.InnerPipeline;
1293
1294 // First handle complex passes like the pass managers which carry pipelines.
1295 if (!InnerPipeline.empty()) {
1296 if (Name == "module") {
1297 ModulePassManager NestedMPM(DebugLogging);
1298 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1299 DebugLogging))
1300 return false;
1301 MPM.addPass(std::move(NestedMPM));
1302 return true;
1303 }
1304 if (Name == "cgscc") {
1305 CGSCCPassManager CGPM(DebugLogging);
1306 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1307 DebugLogging))
1308 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001309 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001310 return true;
1311 }
1312 if (Name == "function") {
1313 FunctionPassManager FPM(DebugLogging);
1314 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1315 DebugLogging))
1316 return false;
1317 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1318 return true;
1319 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001320 if (auto Count = parseRepeatPassName(Name)) {
1321 ModulePassManager NestedMPM(DebugLogging);
1322 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1323 DebugLogging))
1324 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001325 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001326 return true;
1327 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001328
1329 for (auto &C : ModulePipelineParsingCallbacks)
1330 if (C(Name, MPM, InnerPipeline))
1331 return true;
1332
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001333 // Normal passes can't have pipelines.
1334 return false;
1335 }
1336
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001337 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001338 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001339 SmallVector<StringRef, 3> Matches;
1340 if (!DefaultAliasRegex.match(Name, &Matches))
1341 return false;
1342 assert(Matches.size() == 3 && "Must capture two matched strings!");
1343
Jordan Rosef85a95f2016-07-25 18:34:51 +00001344 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001345 .Case("O0", O0)
1346 .Case("O1", O1)
1347 .Case("O2", O2)
1348 .Case("O3", O3)
1349 .Case("Os", Os)
1350 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001351 if (L == O0)
1352 // At O0 we do nothing at all!
1353 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001354
1355 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001356 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001357 } else if (Matches[1] == "thinlto-pre-link") {
1358 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1359 } else if (Matches[1] == "thinlto") {
1360 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001361 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001362 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001363 } else {
1364 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001365 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001366 }
1367 return true;
1368 }
1369
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001370 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001371#define MODULE_PASS(NAME, CREATE_PASS) \
1372 if (Name == NAME) { \
1373 MPM.addPass(CREATE_PASS); \
1374 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001375 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001376#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1377 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001378 MPM.addPass( \
1379 RequireAnalysisPass< \
1380 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001381 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001382 } \
1383 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001384 MPM.addPass(InvalidateAnalysisPass< \
1385 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001386 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001387 }
1388#include "PassRegistry.def"
1389
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001390 for (auto &C : ModulePipelineParsingCallbacks)
1391 if (C(Name, MPM, InnerPipeline))
1392 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001393 return false;
1394}
1395
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001396bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1397 const PipelineElement &E, bool VerifyEachPass,
1398 bool DebugLogging) {
1399 auto &Name = E.Name;
1400 auto &InnerPipeline = E.InnerPipeline;
1401
1402 // First handle complex passes like the pass managers which carry pipelines.
1403 if (!InnerPipeline.empty()) {
1404 if (Name == "cgscc") {
1405 CGSCCPassManager NestedCGPM(DebugLogging);
1406 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1407 DebugLogging))
1408 return false;
1409 // Add the nested pass manager with the appropriate adaptor.
1410 CGPM.addPass(std::move(NestedCGPM));
1411 return true;
1412 }
1413 if (Name == "function") {
1414 FunctionPassManager FPM(DebugLogging);
1415 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1416 DebugLogging))
1417 return false;
1418 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001419 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001420 return true;
1421 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001422 if (auto Count = parseRepeatPassName(Name)) {
1423 CGSCCPassManager NestedCGPM(DebugLogging);
1424 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1425 DebugLogging))
1426 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001427 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001428 return true;
1429 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001430 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1431 CGSCCPassManager NestedCGPM(DebugLogging);
1432 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1433 DebugLogging))
1434 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001435 CGPM.addPass(
1436 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001437 return true;
1438 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001439
1440 for (auto &C : CGSCCPipelineParsingCallbacks)
1441 if (C(Name, CGPM, InnerPipeline))
1442 return true;
1443
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001444 // Normal passes can't have pipelines.
1445 return false;
1446 }
1447
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001448// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001449#define CGSCC_PASS(NAME, CREATE_PASS) \
1450 if (Name == NAME) { \
1451 CGPM.addPass(CREATE_PASS); \
1452 return true; \
1453 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001454#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1455 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001456 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001457 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001458 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1459 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001460 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001461 } \
1462 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001463 CGPM.addPass(InvalidateAnalysisPass< \
1464 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001465 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001466 }
1467#include "PassRegistry.def"
1468
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001469 for (auto &C : CGSCCPipelineParsingCallbacks)
1470 if (C(Name, CGPM, InnerPipeline))
1471 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001472 return false;
1473}
1474
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001475bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1476 const PipelineElement &E,
1477 bool VerifyEachPass, bool DebugLogging) {
1478 auto &Name = E.Name;
1479 auto &InnerPipeline = E.InnerPipeline;
1480
1481 // First handle complex passes like the pass managers which carry pipelines.
1482 if (!InnerPipeline.empty()) {
1483 if (Name == "function") {
1484 FunctionPassManager NestedFPM(DebugLogging);
1485 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1486 DebugLogging))
1487 return false;
1488 // Add the nested pass manager with the appropriate adaptor.
1489 FPM.addPass(std::move(NestedFPM));
1490 return true;
1491 }
1492 if (Name == "loop") {
1493 LoopPassManager LPM(DebugLogging);
1494 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1495 DebugLogging))
1496 return false;
1497 // Add the nested pass manager with the appropriate adaptor.
1498 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1499 return true;
1500 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001501 if (auto Count = parseRepeatPassName(Name)) {
1502 FunctionPassManager NestedFPM(DebugLogging);
1503 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1504 DebugLogging))
1505 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001506 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001507 return true;
1508 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001509
1510 for (auto &C : FunctionPipelineParsingCallbacks)
1511 if (C(Name, FPM, InnerPipeline))
1512 return true;
1513
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001514 // Normal passes can't have pipelines.
1515 return false;
1516 }
1517
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001518// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001519#define FUNCTION_PASS(NAME, CREATE_PASS) \
1520 if (Name == NAME) { \
1521 FPM.addPass(CREATE_PASS); \
1522 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001523 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001524#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1525 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001526 FPM.addPass( \
1527 RequireAnalysisPass< \
1528 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001529 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001530 } \
1531 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001532 FPM.addPass(InvalidateAnalysisPass< \
1533 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001534 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001535 }
1536#include "PassRegistry.def"
1537
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001538 for (auto &C : FunctionPipelineParsingCallbacks)
1539 if (C(Name, FPM, InnerPipeline))
1540 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001541 return false;
1542}
1543
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001544bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001545 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001546 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001547 auto &InnerPipeline = E.InnerPipeline;
1548
1549 // First handle complex passes like the pass managers which carry pipelines.
1550 if (!InnerPipeline.empty()) {
1551 if (Name == "loop") {
1552 LoopPassManager NestedLPM(DebugLogging);
1553 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1554 DebugLogging))
1555 return false;
1556 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001557 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001558 return true;
1559 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001560 if (auto Count = parseRepeatPassName(Name)) {
1561 LoopPassManager NestedLPM(DebugLogging);
1562 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1563 DebugLogging))
1564 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001565 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001566 return true;
1567 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001568
1569 for (auto &C : LoopPipelineParsingCallbacks)
1570 if (C(Name, LPM, InnerPipeline))
1571 return true;
1572
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001573 // Normal passes can't have pipelines.
1574 return false;
1575 }
1576
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001577// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001578#define LOOP_PASS(NAME, CREATE_PASS) \
1579 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001580 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001581 return true; \
1582 }
1583#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1584 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001585 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001586 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1587 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1588 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001589 return true; \
1590 } \
1591 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001592 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001593 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001594 return true; \
1595 }
1596#include "PassRegistry.def"
1597
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001598 for (auto &C : LoopPipelineParsingCallbacks)
1599 if (C(Name, LPM, InnerPipeline))
1600 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001601 return false;
1602}
1603
Chandler Carruthedf59962016-02-18 09:45:17 +00001604bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001605#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1606 if (Name == NAME) { \
1607 AA.registerModuleAnalysis< \
1608 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1609 return true; \
1610 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001611#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1612 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001613 AA.registerFunctionAnalysis< \
1614 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001615 return true; \
1616 }
1617#include "PassRegistry.def"
1618
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001619 for (auto &C : AAParsingCallbacks)
1620 if (C(Name, AA))
1621 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001622 return false;
1623}
1624
Justin Bognereecc3c82016-02-25 07:23:08 +00001625bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001626 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001627 bool VerifyEachPass,
1628 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001629 for (const auto &Element : Pipeline) {
1630 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1631 return false;
1632 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001633 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001634 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001635}
1636
Chandler Carruth1ff77242015-03-07 09:02:36 +00001637bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001638 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001639 bool VerifyEachPass,
1640 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001641 for (const auto &Element : Pipeline) {
1642 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1643 return false;
1644 if (VerifyEachPass)
1645 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001646 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001647 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001648}
1649
Chandler Carruth1ff77242015-03-07 09:02:36 +00001650bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001651 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001652 bool VerifyEachPass,
1653 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001654 for (const auto &Element : Pipeline) {
1655 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1656 return false;
1657 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001658 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001659 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001660}
1661
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001662void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1663 FunctionAnalysisManager &FAM,
1664 CGSCCAnalysisManager &CGAM,
1665 ModuleAnalysisManager &MAM) {
1666 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1667 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001668 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1669 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1670 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1671 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1672 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1673}
1674
Chandler Carruth1ff77242015-03-07 09:02:36 +00001675bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001676 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001677 bool VerifyEachPass,
1678 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001679 for (const auto &Element : Pipeline) {
1680 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1681 return false;
1682 if (VerifyEachPass)
1683 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001684 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001685 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001686}
1687
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001688// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001689// FIXME: Should this routine accept a TargetMachine or require the caller to
1690// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001691bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1692 StringRef PipelineText, bool VerifyEachPass,
1693 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001694 auto Pipeline = parsePipelineText(PipelineText);
1695 if (!Pipeline || Pipeline->empty())
1696 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001697
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001698 // If the first name isn't at the module layer, wrap the pipeline up
1699 // automatically.
1700 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001701
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001702 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1703 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001704 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001705 } else if (isFunctionPassName(FirstName,
1706 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001707 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001708 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001709 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001710 } else {
1711 for (auto &C : TopLevelPipelineParsingCallbacks)
1712 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1713 return true;
1714
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001715 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001716 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001717 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001718 }
1719
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001720 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001721}
Chandler Carruthedf59962016-02-18 09:45:17 +00001722
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001723// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1724bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1725 StringRef PipelineText, bool VerifyEachPass,
1726 bool DebugLogging) {
1727 auto Pipeline = parsePipelineText(PipelineText);
1728 if (!Pipeline || Pipeline->empty())
1729 return false;
1730
1731 StringRef FirstName = Pipeline->front().Name;
1732 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1733 return false;
1734
1735 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1736}
1737
1738// Primary pass pipeline description parsing routine for a \c
1739// FunctionPassManager
1740bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1741 StringRef PipelineText, bool VerifyEachPass,
1742 bool DebugLogging) {
1743 auto Pipeline = parsePipelineText(PipelineText);
1744 if (!Pipeline || Pipeline->empty())
1745 return false;
1746
1747 StringRef FirstName = Pipeline->front().Name;
1748 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1749 return false;
1750
1751 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1752 DebugLogging);
1753}
1754
1755// Primary pass pipeline description parsing routine for a \c LoopPassManager
1756bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1757 StringRef PipelineText, bool VerifyEachPass,
1758 bool DebugLogging) {
1759 auto Pipeline = parsePipelineText(PipelineText);
1760 if (!Pipeline || Pipeline->empty())
1761 return false;
1762
1763 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1764}
1765
Chandler Carruthedf59962016-02-18 09:45:17 +00001766bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001767 // If the pipeline just consists of the word 'default' just replace the AA
1768 // manager with our default one.
1769 if (PipelineText == "default") {
1770 AA = buildDefaultAAPipeline();
1771 return true;
1772 }
1773
Chandler Carruthedf59962016-02-18 09:45:17 +00001774 while (!PipelineText.empty()) {
1775 StringRef Name;
1776 std::tie(Name, PipelineText) = PipelineText.split(',');
1777 if (!parseAAPassName(AA, Name))
1778 return false;
1779 }
1780
1781 return true;
1782}