blob: e5f6cb01d21c23fe813aac05350e32106551ef70 [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"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000095#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000096#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000097#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000098#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000099#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000100#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000101#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000102#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000103#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000104#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000105#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000106#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000107#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000108#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000109#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000110#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000111#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000112#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000113#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000114#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000115#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000116#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000117#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000118#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000119#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000120#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000121#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000122#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000123#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000124#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000125#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000126#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000127#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000128#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000129#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000130#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000131#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000132#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000133#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000134#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000135#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000136#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000137#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000138#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000139#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000140#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000141#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000142#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000143#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000144#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000145#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000146
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000147#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000148
149using namespace llvm;
150
Chandler Carruth719ffe12017-02-12 05:38:04 +0000151static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
152 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000153static cl::opt<bool>
154 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
155 cl::Hidden, cl::ZeroOrMore,
156 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000157
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000158static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000159 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000160 cl::Hidden, cl::ZeroOrMore,
161 cl::desc("Run NewGVN instead of GVN"));
162
Geoff Berry3cca1da2017-06-10 15:20:03 +0000163static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000164 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
165 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000166
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000167static cl::opt<bool> EnableGVNHoist(
168 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
169 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
170
Davide Italianobe1b6a92017-06-03 23:18:29 +0000171static cl::opt<bool> EnableGVNSink(
172 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
173 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
174
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000175static Regex DefaultAliasRegex(
176 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000177
Chandler Carruthe3f50642016-12-22 06:59:15 +0000178static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
179 switch (Level) {
180 case PassBuilder::O0:
181 case PassBuilder::O1:
182 case PassBuilder::O2:
183 case PassBuilder::O3:
184 return false;
185
186 case PassBuilder::Os:
187 case PassBuilder::Oz:
188 return true;
189 }
190 llvm_unreachable("Invalid optimization level!");
191}
192
Chandler Carruth66445382014-01-11 08:16:35 +0000193namespace {
194
Chandler Carruthd8330982014-01-12 09:34:22 +0000195/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000196struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000197 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000198 return PreservedAnalyses::all();
199 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000200 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000201};
202
Chandler Carruth0b576b32015-01-06 02:50:06 +0000203/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000204class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
205 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000206 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000207
208public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000209 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000210 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000211 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212};
213
Chandler Carruth572e3402014-04-21 11:12:00 +0000214/// \brief No-op CGSCC pass which does nothing.
215struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000216 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
217 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000218 return PreservedAnalyses::all();
219 }
220 static StringRef name() { return "NoOpCGSCCPass"; }
221};
222
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000224class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
225 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000226 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000227
228public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000229 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000230 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000231 return Result();
232 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000233 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234};
235
Chandler Carruthd8330982014-01-12 09:34:22 +0000236/// \brief No-op function pass which does nothing.
237struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000238 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000239 return PreservedAnalyses::all();
240 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000241 static StringRef name() { return "NoOpFunctionPass"; }
242};
243
Chandler Carruth0b576b32015-01-06 02:50:06 +0000244/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000245class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
246 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000247 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000248
249public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000250 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000251 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000252 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253};
254
Justin Bognereecc3c82016-02-25 07:23:08 +0000255/// \brief No-op loop pass which does nothing.
256struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000257 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
258 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000259 return PreservedAnalyses::all();
260 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000261 static StringRef name() { return "NoOpLoopPass"; }
262};
263
264/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000265class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
266 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000267 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000268
269public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000270 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000271 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
272 return Result();
273 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000274 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000275};
276
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000277AnalysisKey NoOpModuleAnalysis::Key;
278AnalysisKey NoOpCGSCCAnalysis::Key;
279AnalysisKey NoOpFunctionAnalysis::Key;
280AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000281
Chandler Carruth66445382014-01-11 08:16:35 +0000282} // End anonymous namespace.
283
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000284void PassBuilder::invokePeepholeEPCallbacks(
285 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
286 for (auto &C : PeepholeEPCallbacks)
287 C(FPM, Level);
288}
289
Chandler Carruth1ff77242015-03-07 09:02:36 +0000290void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000291#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000292 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000293#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000294
295 for (auto &C : ModuleAnalysisRegistrationCallbacks)
296 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000297}
298
Chandler Carruth1ff77242015-03-07 09:02:36 +0000299void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000300#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000301 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000302#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000303
304 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
305 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000306}
307
Chandler Carruth1ff77242015-03-07 09:02:36 +0000308void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000309#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000310 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000311#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000312
313 for (auto &C : FunctionAnalysisRegistrationCallbacks)
314 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000315}
316
Justin Bognereecc3c82016-02-25 07:23:08 +0000317void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000318#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000319 LAM.registerPass([&] { return CREATE_PASS; });
320#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000321
322 for (auto &C : LoopAnalysisRegistrationCallbacks)
323 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000324}
325
Chandler Carruthe3f50642016-12-22 06:59:15 +0000326FunctionPassManager
327PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000328 ThinLTOPhase Phase,
329 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000330 assert(Level != O0 && "Must request optimizations!");
331 FunctionPassManager FPM(DebugLogging);
332
333 // Form SSA out of local memory accesses after breaking apart aggregates into
334 // scalars.
335 FPM.addPass(SROA());
336
337 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000338 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000339
Davide Italianoc3688312017-06-01 23:08:14 +0000340 // Hoisting of scalars and load expressions.
341 if (EnableGVNHoist)
342 FPM.addPass(GVNHoistPass());
343
Davide Italianobe1b6a92017-06-03 23:18:29 +0000344 // Global value numbering based sinking.
345 if (EnableGVNSink) {
346 FPM.addPass(GVNSinkPass());
347 FPM.addPass(SimplifyCFGPass());
348 }
349
Chandler Carruthe3f50642016-12-22 06:59:15 +0000350 // Speculative execution if the target has divergent branches; otherwise nop.
351 FPM.addPass(SpeculativeExecutionPass());
352
353 // Optimize based on known information about branches, and cleanup afterward.
354 FPM.addPass(JumpThreadingPass());
355 FPM.addPass(CorrelatedValuePropagationPass());
356 FPM.addPass(SimplifyCFGPass());
357 FPM.addPass(InstCombinePass());
358
359 if (!isOptimizingForSize(Level))
360 FPM.addPass(LibCallsShrinkWrapPass());
361
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000362 invokePeepholeEPCallbacks(FPM, Level);
363
Chandler Carruthe3f50642016-12-22 06:59:15 +0000364 FPM.addPass(TailCallElimPass());
365 FPM.addPass(SimplifyCFGPass());
366
367 // Form canonically associated expression trees, and simplify the trees using
368 // basic mathematical properties. For example, this will form (nearly)
369 // minimal multiplication trees.
370 FPM.addPass(ReassociatePass());
371
372 // Add the primary loop simplification pipeline.
373 // FIXME: Currently this is split into two loop pass pipelines because we run
374 // some function passes in between them. These can and should be replaced by
375 // loop pass equivalenst but those aren't ready yet. Specifically,
376 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
377 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
378 // the other we have is `LoopInstSimplify`.
379 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
380
381 // Rotate Loop - disable header duplication at -Oz
382 LPM1.addPass(LoopRotatePass(Level != Oz));
383 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000384 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000385 LPM2.addPass(IndVarSimplifyPass());
386 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000387
388 for (auto &C : LateLoopOptimizationsEPCallbacks)
389 C(LPM2, Level);
390
Chandler Carruth79b733b2017-01-26 23:21:17 +0000391 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000392 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000393 // because it changes IR to makes profile annotation in back compile
394 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000395 if (Phase != ThinLTOPhase::PreLink ||
396 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000397 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000398
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000399 for (auto &C : LoopOptimizerEndEPCallbacks)
400 C(LPM2, Level);
401
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000402 // We provide the opt remark emitter pass for LICM to use. We only need to do
403 // this once as it is immutable.
404 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000405 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
406 FPM.addPass(SimplifyCFGPass());
407 FPM.addPass(InstCombinePass());
408 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
409
410 // Eliminate redundancies.
411 if (Level != O1) {
412 // These passes add substantial compile time so skip them at O1.
413 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000414 if (RunNewGVN)
415 FPM.addPass(NewGVNPass());
416 else
417 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000418 }
419
420 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
421 FPM.addPass(MemCpyOptPass());
422
423 // Sparse conditional constant propagation.
424 // FIXME: It isn't clear why we do this *after* loop passes rather than
425 // before...
426 FPM.addPass(SCCPPass());
427
428 // Delete dead bit computations (instcombine runs after to fold away the dead
429 // computations, and then ADCE will run later to exploit any new DCE
430 // opportunities that creates).
431 FPM.addPass(BDCEPass());
432
433 // Run instcombine after redundancy and dead bit elimination to exploit
434 // opportunities opened up by them.
435 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000436 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000437
438 // Re-consider control flow based optimizations after redundancy elimination,
439 // redo DCE, etc.
440 FPM.addPass(JumpThreadingPass());
441 FPM.addPass(CorrelatedValuePropagationPass());
442 FPM.addPass(DSEPass());
443 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
444
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000445 for (auto &C : ScalarOptimizerLateEPCallbacks)
446 C(FPM, Level);
447
Chandler Carruthe3f50642016-12-22 06:59:15 +0000448 // Finally, do an expensive DCE pass to catch all the dead code exposed by
449 // the simplifications and basic cleanup after all the simplifications.
450 FPM.addPass(ADCEPass());
451 FPM.addPass(SimplifyCFGPass());
452 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000453 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000454
455 return FPM;
456}
457
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000458void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
459 PassBuilder::OptimizationLevel Level,
460 bool RunProfileGen,
461 std::string ProfileGenFile,
462 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000463 // Generally running simplification passes and the inliner with an high
464 // threshold results in smaller executables, but there may be cases where
465 // the size grows, so let's be conservative here and skip this simplification
466 // at -Os/Oz.
467 if (!isOptimizingForSize(Level)) {
468 InlineParams IP;
469
470 // In the old pass manager, this is a cl::opt. Should still this be one?
471 IP.DefaultThreshold = 75;
472
473 // FIXME: The hint threshold has the same value used by the regular inliner.
474 // This should probably be lowered after performance testing.
475 // FIXME: this comment is cargo culted from the old pass manager, revisit).
476 IP.HintThreshold = 325;
477
478 CGSCCPassManager CGPipeline(DebugLogging);
479
480 CGPipeline.addPass(InlinerPass(IP));
481
482 FunctionPassManager FPM;
483 FPM.addPass(SROA());
484 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
485 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
486 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000487 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000488
Davide Italiano513dfaa2017-02-13 15:26:22 +0000489 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
490
491 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
492 }
493
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000494 // Delete anything that is now dead to make sure that we don't instrument
495 // dead code. Instrumentation can end up keeping dead code around and
496 // dramatically increase code size.
497 MPM.addPass(GlobalDCEPass());
498
Davide Italiano513dfaa2017-02-13 15:26:22 +0000499 if (RunProfileGen) {
500 MPM.addPass(PGOInstrumentationGen());
501
Xinliang David Lib67530e2017-06-25 00:26:43 +0000502 FunctionPassManager FPM;
503 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
504 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
505
Davide Italiano513dfaa2017-02-13 15:26:22 +0000506 // Add the profile lowering pass.
507 InstrProfOptions Options;
508 if (!ProfileGenFile.empty())
509 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000510 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000511 MPM.addPass(InstrProfiling(Options));
512 }
513
514 if (!ProfileUseFile.empty())
515 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
516}
517
Easwaran Raman8249fac2017-06-28 13:33:49 +0000518static InlineParams
519getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
520 auto O3 = PassBuilder::O3;
521 unsigned OptLevel = Level > O3 ? 2 : Level;
522 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
523 return getInlineParams(OptLevel, SizeLevel);
524}
525
Chandler Carruthe3f50642016-12-22 06:59:15 +0000526ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000527PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000528 ThinLTOPhase Phase,
529 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000530 ModulePassManager MPM(DebugLogging);
531
Chandler Carruthe3f50642016-12-22 06:59:15 +0000532 // Do basic inference of function attributes from known properties of system
533 // libraries and other oracles.
534 MPM.addPass(InferFunctionAttrsPass());
535
536 // Create an early function pass manager to cleanup the output of the
537 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000538 FunctionPassManager EarlyFPM(DebugLogging);
539 EarlyFPM.addPass(SimplifyCFGPass());
540 EarlyFPM.addPass(SROA());
541 EarlyFPM.addPass(EarlyCSEPass());
542 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Dehao Chen08f88312017-08-07 20:23:20 +0000543 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
544 // to convert bitcast to direct calls so that they can be inlined during the
545 // profile annotation prepration step.
546 // More details about SamplePGO design can be found in:
547 // https://research.google.com/pubs/pub45290.html
548 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
549 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
550 Phase == ThinLTOPhase::PostLink)
551 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000552 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000553
Dehao Chen08f88312017-08-07 20:23:20 +0000554 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
555 // Annotate sample profile right after early FPM to ensure freshness of
556 // the debug info.
557 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile));
558 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
559 // for the profile annotation to be accurate in the ThinLTO backend.
560 if (Phase != ThinLTOPhase::PreLink)
561 // We perform early indirect call promotion here, before globalopt.
562 // This is important for the ThinLTO backend phase because otherwise
563 // imported available_externally functions look unreferenced and are
564 // removed.
565 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
566 true));
567 }
568
Chandler Carruthe3f50642016-12-22 06:59:15 +0000569 // Interprocedural constant propagation now that basic cleanup has occured
570 // and prior to optimizing globals.
571 // FIXME: This position in the pipeline hasn't been carefully considered in
572 // years, it should be re-analyzed.
573 MPM.addPass(IPSCCPPass());
574
575 // Optimize globals to try and fold them into constants.
576 MPM.addPass(GlobalOptPass());
577
578 // Promote any localized globals to SSA registers.
579 // FIXME: Should this instead by a run of SROA?
580 // FIXME: We should probably run instcombine and simplify-cfg afterward to
581 // delete control flows that are dead once globals have been folded to
582 // constants.
583 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
584
585 // Remove any dead arguments exposed by cleanups and constand folding
586 // globals.
587 MPM.addPass(DeadArgumentEliminationPass());
588
589 // Create a small function pass pipeline to cleanup after all the global
590 // optimizations.
591 FunctionPassManager GlobalCleanupPM(DebugLogging);
592 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000593 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
594
Chandler Carruthe3f50642016-12-22 06:59:15 +0000595 GlobalCleanupPM.addPass(SimplifyCFGPass());
596 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
597
Dehao Chen89d32262017-08-02 01:28:31 +0000598 // Add all the requested passes for instrumentation PGO, if requested.
599 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
600 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
601 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
602 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
603 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000604 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000605
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000606 // Require the GlobalsAA analysis for the module so we can query it within
607 // the CGSCC pipeline.
608 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000609
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000610 // Require the ProfileSummaryAnalysis for the module so we can query it within
611 // the inliner pass.
612 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
613
Chandler Carruthe3f50642016-12-22 06:59:15 +0000614 // Now begin the main postorder CGSCC pipeline.
615 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
616 // manager and trying to emulate its precise behavior. Much of this doesn't
617 // make a lot of sense and we should revisit the core CGSCC structure.
618 CGSCCPassManager MainCGPipeline(DebugLogging);
619
620 // Note: historically, the PruneEH pass was run first to deduce nounwind and
621 // generally clean up exception handling overhead. It isn't clear this is
622 // valuable as the inliner doesn't currently care whether it is inlining an
623 // invoke or a call.
624
625 // Run the inliner first. The theory is that we are walking bottom-up and so
626 // the callees have already been fully optimized, and we want to inline them
627 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000628 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000629 // because it makes profile annotation in the backend inaccurate.
630 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000631 if (Phase == ThinLTOPhase::PreLink &&
632 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000633 IP.HotCallSiteThreshold = 0;
634 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000635
636 // Now deduce any function attributes based in the current code.
637 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
638
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000639 // When at O3 add argument promotion to the pass pipeline.
640 // FIXME: It isn't at all clear why this should be limited to O3.
641 if (Level == O3)
642 MainCGPipeline.addPass(ArgumentPromotionPass());
643
Chandler Carruthe3f50642016-12-22 06:59:15 +0000644 // Lastly, add the core function simplification pipeline nested inside the
645 // CGSCC walk.
646 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000647 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000648
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000649 for (auto &C : CGSCCOptimizerLateEPCallbacks)
650 C(MainCGPipeline, Level);
651
Chandler Carruth719ffe12017-02-12 05:38:04 +0000652 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
653 // to detect when we devirtualize indirect calls and iterate the SCC passes
654 // in that case to try and catch knock-on inlining or function attrs
655 // opportunities. Then we add it to the module pipeline by walking the SCCs
656 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000657 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000658 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
659 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000660
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000661 return MPM;
662}
663
664ModulePassManager
665PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
666 bool DebugLogging) {
667 ModulePassManager MPM(DebugLogging);
668
669 // Optimize globals now that the module is fully simplified.
670 MPM.addPass(GlobalOptPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000671
Xinliang David Li126157c2017-05-22 16:41:57 +0000672 // Run partial inlining pass to partially inline functions that have
673 // large bodies.
674 if (RunPartialInlining)
675 MPM.addPass(PartialInlinerPass());
676
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000677 // Remove avail extern fns and globals definitions since we aren't compiling
678 // an object file for later LTO. For LTO we want to preserve these so they
679 // are eligible for inlining at link-time. Note if they are unreferenced they
680 // will be removed by GlobalDCE later, so this only impacts referenced
681 // available externally globals. Eventually they will be suppressed during
682 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
683 // may make globals referenced by available external functions dead and saves
684 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000685 MPM.addPass(EliminateAvailableExternallyPass());
686
687 // Do RPO function attribute inference across the module to forward-propagate
688 // attributes where applicable.
689 // FIXME: Is this really an optimization rather than a canonicalization?
690 MPM.addPass(ReversePostOrderFunctionAttrsPass());
691
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000692 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000693 // useful as the above will have inlined, DCE'ed, and function-attr
694 // propagated everything. We should at this point have a reasonably minimal
695 // and richly annotated call graph. By computing aliasing and mod/ref
696 // information for all local globals here, the late loop passes and notably
697 // the vectorizer will be able to use them to help recognize vectorizable
698 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000699 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000700
701 FunctionPassManager OptimizePM(DebugLogging);
702 OptimizePM.addPass(Float2IntPass());
703 // FIXME: We need to run some loop optimizations to re-rotate loops after
704 // simplify-cfg and others undo their rotation.
705
706 // Optimize the loop execution. These passes operate on entire loop nests
707 // rather than on each loop in an inside-out manner, and so they are actually
708 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000709
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000710 for (auto &C : VectorizerStartEPCallbacks)
711 C(OptimizePM, Level);
712
Chandler Carrutha95ff382017-01-27 00:50:21 +0000713 // First rotate loops that may have been un-rotated by prior passes.
714 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
715
716 // Distribute loops to allow partial vectorization. I.e. isolate dependences
717 // into separate loop that would otherwise inhibit vectorization. This is
718 // currently only performed for loops marked with the metadata
719 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000720 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000721
722 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000723 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000724
Chandler Carruthbaabda92017-01-27 01:32:26 +0000725 // Eliminate loads by forwarding stores from the previous iteration to loads
726 // of the current iteration.
727 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000728
729 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000730 OptimizePM.addPass(InstCombinePass());
731
Chandler Carrutha95ff382017-01-27 00:50:21 +0000732
733 // Now that we've formed fast to execute loop structures, we do further
734 // optimizations. These are run afterward as they might block doing complex
735 // analyses and transforms such as what are needed for loop vectorization.
736
Chandler Carruthe3f50642016-12-22 06:59:15 +0000737 // Optimize parallel scalar instruction chains into SIMD instructions.
738 OptimizePM.addPass(SLPVectorizerPass());
739
Chandler Carrutha95ff382017-01-27 00:50:21 +0000740 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000741 OptimizePM.addPass(SimplifyCFGPass());
742 OptimizePM.addPass(InstCombinePass());
743
744 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000745 // execution resources of an out-of-order processor. We also then need to
746 // clean up redundancies and loop invariant code.
747 // FIXME: It would be really good to use a loop-integrated instruction
748 // combiner for cleanup here so that the unrolling and LICM can be pipelined
749 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000750 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000751 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000752 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000753 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000754
755 // Now that we've vectorized and unrolled loops, we may have more refined
756 // alignment information, try to re-derive it here.
757 OptimizePM.addPass(AlignmentFromAssumptionsPass());
758
Chandler Carrutha95ff382017-01-27 00:50:21 +0000759 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
760 // canonicalization pass that enables other optimizations. As a result,
761 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
762 // result too early.
763 OptimizePM.addPass(LoopSinkPass());
764
765 // And finally clean up LCSSA form before generating code.
766 OptimizePM.addPass(InstSimplifierPass());
767
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000768 // LoopSink (and other loop passes since the last simplifyCFG) might have
769 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
770 OptimizePM.addPass(SimplifyCFGPass());
771
Chandler Carrutha95ff382017-01-27 00:50:21 +0000772 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000773 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
774
775 // Now we need to do some global optimization transforms.
776 // FIXME: It would seem like these should come first in the optimization
777 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
778 // ordering here.
779 MPM.addPass(GlobalDCEPass());
780 MPM.addPass(ConstantMergePass());
781
782 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000783}
784
Chandler Carruthe3f50642016-12-22 06:59:15 +0000785ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000786PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
787 bool DebugLogging) {
788 assert(Level != O0 && "Must request optimizations for the default pipeline!");
789
790 ModulePassManager MPM(DebugLogging);
791
792 // Force any function attributes we want the rest of the pipeline to observe.
793 MPM.addPass(ForceFunctionAttrsPass());
794
Dehao Chen08f88312017-08-07 20:23:20 +0000795 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000796 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
797
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000798 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000799 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
800 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000801
802 // Now add the optimization pipeline.
803 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
804
805 return MPM;
806}
807
808ModulePassManager
809PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
810 bool DebugLogging) {
811 assert(Level != O0 && "Must request optimizations for the default pipeline!");
812
813 ModulePassManager MPM(DebugLogging);
814
815 // Force any function attributes we want the rest of the pipeline to observe.
816 MPM.addPass(ForceFunctionAttrsPass());
817
Dehao Chen08f88312017-08-07 20:23:20 +0000818 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000819 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
820
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000821 // If we are planning to perform ThinLTO later, we don't bloat the code with
822 // unrolling/vectorization/... now. Just simplify the module as much as we
823 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000824 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
825 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000826
827 // Run partial inlining pass to partially inline functions that have
828 // large bodies.
829 // FIXME: It isn't clear whether this is really the right place to run this
830 // in ThinLTO. Because there is another canonicalization and simplification
831 // phase that will run after the thin link, running this here ends up with
832 // less information than will be available later and it may grow functions in
833 // ways that aren't beneficial.
834 if (RunPartialInlining)
835 MPM.addPass(PartialInlinerPass());
836
837 // Reduce the size of the IR as much as possible.
838 MPM.addPass(GlobalOptPass());
839
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000840 return MPM;
841}
842
843ModulePassManager
844PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
845 bool DebugLogging) {
846 // FIXME: The summary index is not hooked in the new pass manager yet.
847 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
848 // here.
849
850 ModulePassManager MPM(DebugLogging);
851
852 // Force any function attributes we want the rest of the pipeline to observe.
853 MPM.addPass(ForceFunctionAttrsPass());
854
855 // During the ThinLTO backend phase we perform early indirect call promotion
856 // here, before globalopt. Otherwise imported available_externally functions
857 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000858 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
859 // with SamplePGO.
860 if (PGOOpt && !PGOOpt->ProfileUseFile.empty())
861 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
862 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000863
864 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000865 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
866 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000867
868 // Now add the optimization pipeline.
869 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
870
871 return MPM;
872}
873
874ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000875PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
876 bool DebugLogging) {
877 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000878 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000879 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000880}
881
Chandler Carruthe3f50642016-12-22 06:59:15 +0000882ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
883 bool DebugLogging) {
884 assert(Level != O0 && "Must request optimizations for the default pipeline!");
885 ModulePassManager MPM(DebugLogging);
886
Davide Italiano089a9122017-01-24 00:57:39 +0000887 // Remove unused virtual tables to improve the quality of code generated by
888 // whole-program devirtualization and bitset lowering.
889 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000890
Davide Italiano089a9122017-01-24 00:57:39 +0000891 // Force any function attributes we want the rest of the pipeline to observe.
892 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000893
Davide Italiano089a9122017-01-24 00:57:39 +0000894 // Do basic inference of function attributes from known properties of system
895 // libraries and other oracles.
896 MPM.addPass(InferFunctionAttrsPass());
897
898 if (Level > 1) {
899 // Indirect call promotion. This should promote all the targets that are
900 // left by the earlier promotion pass that promotes intra-module targets.
901 // This two-step promotion is to save the compile time. For LTO, it should
902 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000903 MPM.addPass(PGOIndirectCallPromotion(
904 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000905
906 // Propagate constants at call sites into the functions they call. This
907 // opens opportunities for globalopt (and inlining) by substituting function
908 // pointers passed as arguments to direct uses of functions.
909 MPM.addPass(IPSCCPPass());
910 }
911
912 // Now deduce any function attributes based in the current code.
913 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
914 PostOrderFunctionAttrsPass()));
915
916 // Do RPO function attribute inference across the module to forward-propagate
917 // attributes where applicable.
918 // FIXME: Is this really an optimization rather than a canonicalization?
919 MPM.addPass(ReversePostOrderFunctionAttrsPass());
920
921 // Use inragne annotations on GEP indices to split globals where beneficial.
922 MPM.addPass(GlobalSplitPass());
923
924 // Run whole program optimization of virtual call when the list of callees
925 // is fixed.
926 MPM.addPass(WholeProgramDevirtPass());
927
928 // Stop here at -O1.
929 if (Level == 1)
930 return MPM;
931
932 // Optimize globals to try and fold them into constants.
933 MPM.addPass(GlobalOptPass());
934
935 // Promote any localized globals to SSA registers.
936 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
937
938 // Linking modules together can lead to duplicate global constant, only
939 // keep one copy of each constant.
940 MPM.addPass(ConstantMergePass());
941
942 // Remove unused arguments from functions.
943 MPM.addPass(DeadArgumentEliminationPass());
944
945 // Reduce the code after globalopt and ipsccp. Both can open up significant
946 // simplification opportunities, and both can propagate functions through
947 // function pointers. When this happens, we often have to resolve varargs
948 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000949 FunctionPassManager PeepholeFPM(DebugLogging);
950 PeepholeFPM.addPass(InstCombinePass());
951 invokePeepholeEPCallbacks(PeepholeFPM, Level);
952
953 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000954
955 // Note: historically, the PruneEH pass was run first to deduce nounwind and
956 // generally clean up exception handling overhead. It isn't clear this is
957 // valuable as the inliner doesn't currently care whether it is inlining an
958 // invoke or a call.
959 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000960 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
961 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000962
963 // Optimize globals again after we ran the inliner.
964 MPM.addPass(GlobalOptPass());
965
966 // Garbage collect dead functions.
967 // FIXME: Add ArgumentPromotion pass after once it's ported.
968 MPM.addPass(GlobalDCEPass());
969
970 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +0000971 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +0000972 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000973 invokePeepholeEPCallbacks(FPM, Level);
974
Davide Italiano089a9122017-01-24 00:57:39 +0000975 FPM.addPass(JumpThreadingPass());
976
977 // Break up allocas
978 FPM.addPass(SROA());
979
980 // Run a few AA driver optimizations here and now to cleanup the code.
981 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
982
983 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
984 PostOrderFunctionAttrsPass()));
985 // FIXME: here we run IP alias analysis in the legacy PM.
986
987 FunctionPassManager MainFPM;
988
989 // FIXME: once we fix LoopPass Manager, add LICM here.
990 // FIXME: once we provide support for enabling MLSM, add it here.
991 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000992 if (RunNewGVN)
993 MainFPM.addPass(NewGVNPass());
994 else
995 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +0000996
997 // Remove dead memcpy()'s.
998 MainFPM.addPass(MemCpyOptPass());
999
1000 // Nuke dead stores.
1001 MainFPM.addPass(DSEPass());
1002
1003 // FIXME: at this point, we run a bunch of loop passes:
1004 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1005 // loopVectorize. Enable them once the remaining issue with LPM
1006 // are sorted out.
1007
1008 MainFPM.addPass(InstCombinePass());
1009 MainFPM.addPass(SimplifyCFGPass());
1010 MainFPM.addPass(SCCPPass());
1011 MainFPM.addPass(InstCombinePass());
1012 MainFPM.addPass(BDCEPass());
1013
1014 // FIXME: We may want to run SLPVectorizer here.
1015 // After vectorization, assume intrinsics may tell us more
1016 // about pointer alignments.
1017#if 0
1018 MainFPM.add(AlignmentFromAssumptionsPass());
1019#endif
1020
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001021 // FIXME: Conditionally run LoadCombine here, after it's ported
1022 // (in case we still have this pass, given its questionable usefulness).
1023
Davide Italiano089a9122017-01-24 00:57:39 +00001024 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001025 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001026 MainFPM.addPass(JumpThreadingPass());
1027 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1028
1029 // Create a function that performs CFI checks for cross-DSO calls with
1030 // targets in the current module.
1031 MPM.addPass(CrossDSOCFIPass());
1032
1033 // Lower type metadata and the type.test intrinsic. This pass supports
1034 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1035 // to be run at link time if CFI is enabled. This pass does nothing if
1036 // CFI is disabled.
1037 // Enable once we add support for the summary in the new PM.
1038#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001039 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1040 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001041 Summary));
1042#endif
1043
1044 // Add late LTO optimization passes.
1045 // Delete basic blocks, which optimization passes may have killed.
1046 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1047
1048 // Drop bodies of available eternally objects to improve GlobalDCE.
1049 MPM.addPass(EliminateAvailableExternallyPass());
1050
1051 // Now that we have optimized the program, discard unreachable functions.
1052 MPM.addPass(GlobalDCEPass());
1053
1054 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001055 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001056}
1057
Chandler Carruth060ad612016-12-23 20:38:19 +00001058AAManager PassBuilder::buildDefaultAAPipeline() {
1059 AAManager AA;
1060
1061 // The order in which these are registered determines their priority when
1062 // being queried.
1063
1064 // First we register the basic alias analysis that provides the majority of
1065 // per-function local AA logic. This is a stateless, on-demand local set of
1066 // AA techniques.
1067 AA.registerFunctionAnalysis<BasicAA>();
1068
1069 // Next we query fast, specialized alias analyses that wrap IR-embedded
1070 // information about aliasing.
1071 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1072 AA.registerFunctionAnalysis<TypeBasedAA>();
1073
1074 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001075 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1076 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001077 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001078 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001079
1080 return AA;
1081}
1082
Chandler Carruth241bf242016-08-03 07:44:48 +00001083static Optional<int> parseRepeatPassName(StringRef Name) {
1084 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1085 return None;
1086 int Count;
1087 if (Name.getAsInteger(0, Count) || Count <= 0)
1088 return None;
1089 return Count;
1090}
1091
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001092static Optional<int> parseDevirtPassName(StringRef Name) {
1093 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1094 return None;
1095 int Count;
1096 if (Name.getAsInteger(0, Count) || Count <= 0)
1097 return None;
1098 return Count;
1099}
1100
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001101/// Tests whether a pass name starts with a valid prefix for a default pipeline
1102/// alias.
1103static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1104 return Name.startswith("default") || Name.startswith("thinlto") ||
1105 Name.startswith("lto");
1106}
1107
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001108/// Tests whether registered callbacks will accept a given pass name.
1109///
1110/// When parsing a pipeline text, the type of the outermost pipeline may be
1111/// omitted, in which case the type is automatically determined from the first
1112/// pass name in the text. This may be a name that is handled through one of the
1113/// callbacks. We check this through the oridinary parsing callbacks by setting
1114/// up a dummy PassManager in order to not force the client to also handle this
1115/// type of query.
1116template <typename PassManagerT, typename CallbacksT>
1117static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1118 if (!Callbacks.empty()) {
1119 PassManagerT DummyPM;
1120 for (auto &CB : Callbacks)
1121 if (CB(Name, DummyPM, {}))
1122 return true;
1123 }
1124 return false;
1125}
1126
1127template <typename CallbacksT>
1128static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001129 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001130 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001131 return DefaultAliasRegex.match(Name);
1132
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001133 // Explicitly handle pass manager names.
1134 if (Name == "module")
1135 return true;
1136 if (Name == "cgscc")
1137 return true;
1138 if (Name == "function")
1139 return true;
1140
Chandler Carruth241bf242016-08-03 07:44:48 +00001141 // Explicitly handle custom-parsed pass names.
1142 if (parseRepeatPassName(Name))
1143 return true;
1144
Chandler Carruth74a8a222016-06-17 07:15:29 +00001145#define MODULE_PASS(NAME, CREATE_PASS) \
1146 if (Name == NAME) \
1147 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001148#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001149 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001150 return true;
1151#include "PassRegistry.def"
1152
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001153 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001154}
1155
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001156template <typename CallbacksT>
1157static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001158 // Explicitly handle pass manager names.
1159 if (Name == "cgscc")
1160 return true;
1161 if (Name == "function")
1162 return true;
1163
Chandler Carruth241bf242016-08-03 07:44:48 +00001164 // Explicitly handle custom-parsed pass names.
1165 if (parseRepeatPassName(Name))
1166 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001167 if (parseDevirtPassName(Name))
1168 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001169
Chandler Carruth74a8a222016-06-17 07:15:29 +00001170#define CGSCC_PASS(NAME, CREATE_PASS) \
1171 if (Name == NAME) \
1172 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001173#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001174 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001175 return true;
1176#include "PassRegistry.def"
1177
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001178 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001179}
1180
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001181template <typename CallbacksT>
1182static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001183 // Explicitly handle pass manager names.
1184 if (Name == "function")
1185 return true;
1186 if (Name == "loop")
1187 return true;
1188
Chandler Carruth241bf242016-08-03 07:44:48 +00001189 // Explicitly handle custom-parsed pass names.
1190 if (parseRepeatPassName(Name))
1191 return true;
1192
Chandler Carruth74a8a222016-06-17 07:15:29 +00001193#define FUNCTION_PASS(NAME, CREATE_PASS) \
1194 if (Name == NAME) \
1195 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001196#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001197 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001198 return true;
1199#include "PassRegistry.def"
1200
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001201 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001202}
1203
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001204template <typename CallbacksT>
1205static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001206 // Explicitly handle pass manager names.
1207 if (Name == "loop")
1208 return true;
1209
Chandler Carruth241bf242016-08-03 07:44:48 +00001210 // Explicitly handle custom-parsed pass names.
1211 if (parseRepeatPassName(Name))
1212 return true;
1213
Chandler Carruth74a8a222016-06-17 07:15:29 +00001214#define LOOP_PASS(NAME, CREATE_PASS) \
1215 if (Name == NAME) \
1216 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001217#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1218 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1219 return true;
1220#include "PassRegistry.def"
1221
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001222 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001223}
1224
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001225Optional<std::vector<PassBuilder::PipelineElement>>
1226PassBuilder::parsePipelineText(StringRef Text) {
1227 std::vector<PipelineElement> ResultPipeline;
1228
1229 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1230 &ResultPipeline};
1231 for (;;) {
1232 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1233 size_t Pos = Text.find_first_of(",()");
1234 Pipeline.push_back({Text.substr(0, Pos), {}});
1235
1236 // If we have a single terminating name, we're done.
1237 if (Pos == Text.npos)
1238 break;
1239
1240 char Sep = Text[Pos];
1241 Text = Text.substr(Pos + 1);
1242 if (Sep == ',')
1243 // Just a name ending in a comma, continue.
1244 continue;
1245
1246 if (Sep == '(') {
1247 // Push the inner pipeline onto the stack to continue processing.
1248 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1249 continue;
1250 }
1251
1252 assert(Sep == ')' && "Bogus separator!");
1253 // When handling the close parenthesis, we greedily consume them to avoid
1254 // empty strings in the pipeline.
1255 do {
1256 // If we try to pop the outer pipeline we have unbalanced parentheses.
1257 if (PipelineStack.size() == 1)
1258 return None;
1259
1260 PipelineStack.pop_back();
1261 } while (Text.consume_front(")"));
1262
1263 // Check if we've finished parsing.
1264 if (Text.empty())
1265 break;
1266
1267 // Otherwise, the end of an inner pipeline always has to be followed by
1268 // a comma, and then we can continue.
1269 if (!Text.consume_front(","))
1270 return None;
1271 }
1272
1273 if (PipelineStack.size() > 1)
1274 // Unbalanced paretheses.
1275 return None;
1276
1277 assert(PipelineStack.back() == &ResultPipeline &&
1278 "Wrong pipeline at the bottom of the stack!");
1279 return {std::move(ResultPipeline)};
1280}
1281
1282bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1283 const PipelineElement &E, bool VerifyEachPass,
1284 bool DebugLogging) {
1285 auto &Name = E.Name;
1286 auto &InnerPipeline = E.InnerPipeline;
1287
1288 // First handle complex passes like the pass managers which carry pipelines.
1289 if (!InnerPipeline.empty()) {
1290 if (Name == "module") {
1291 ModulePassManager NestedMPM(DebugLogging);
1292 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1293 DebugLogging))
1294 return false;
1295 MPM.addPass(std::move(NestedMPM));
1296 return true;
1297 }
1298 if (Name == "cgscc") {
1299 CGSCCPassManager CGPM(DebugLogging);
1300 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1301 DebugLogging))
1302 return false;
1303 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1304 DebugLogging));
1305 return true;
1306 }
1307 if (Name == "function") {
1308 FunctionPassManager FPM(DebugLogging);
1309 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1310 DebugLogging))
1311 return false;
1312 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1313 return true;
1314 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001315 if (auto Count = parseRepeatPassName(Name)) {
1316 ModulePassManager NestedMPM(DebugLogging);
1317 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1318 DebugLogging))
1319 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001320 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001321 return true;
1322 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001323
1324 for (auto &C : ModulePipelineParsingCallbacks)
1325 if (C(Name, MPM, InnerPipeline))
1326 return true;
1327
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001328 // Normal passes can't have pipelines.
1329 return false;
1330 }
1331
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001332 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001333 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001334 SmallVector<StringRef, 3> Matches;
1335 if (!DefaultAliasRegex.match(Name, &Matches))
1336 return false;
1337 assert(Matches.size() == 3 && "Must capture two matched strings!");
1338
Jordan Rosef85a95f2016-07-25 18:34:51 +00001339 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001340 .Case("O0", O0)
1341 .Case("O1", O1)
1342 .Case("O2", O2)
1343 .Case("O3", O3)
1344 .Case("Os", Os)
1345 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001346 if (L == O0)
1347 // At O0 we do nothing at all!
1348 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001349
1350 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001351 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001352 } else if (Matches[1] == "thinlto-pre-link") {
1353 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1354 } else if (Matches[1] == "thinlto") {
1355 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001356 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001357 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001358 } else {
1359 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001360 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001361 }
1362 return true;
1363 }
1364
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001365 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001366#define MODULE_PASS(NAME, CREATE_PASS) \
1367 if (Name == NAME) { \
1368 MPM.addPass(CREATE_PASS); \
1369 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001370 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001371#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1372 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001373 MPM.addPass( \
1374 RequireAnalysisPass< \
1375 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001376 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001377 } \
1378 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001379 MPM.addPass(InvalidateAnalysisPass< \
1380 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001381 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001382 }
1383#include "PassRegistry.def"
1384
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001385 for (auto &C : ModulePipelineParsingCallbacks)
1386 if (C(Name, MPM, InnerPipeline))
1387 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001388 return false;
1389}
1390
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001391bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1392 const PipelineElement &E, bool VerifyEachPass,
1393 bool DebugLogging) {
1394 auto &Name = E.Name;
1395 auto &InnerPipeline = E.InnerPipeline;
1396
1397 // First handle complex passes like the pass managers which carry pipelines.
1398 if (!InnerPipeline.empty()) {
1399 if (Name == "cgscc") {
1400 CGSCCPassManager NestedCGPM(DebugLogging);
1401 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1402 DebugLogging))
1403 return false;
1404 // Add the nested pass manager with the appropriate adaptor.
1405 CGPM.addPass(std::move(NestedCGPM));
1406 return true;
1407 }
1408 if (Name == "function") {
1409 FunctionPassManager FPM(DebugLogging);
1410 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1411 DebugLogging))
1412 return false;
1413 // Add the nested pass manager with the appropriate adaptor.
1414 CGPM.addPass(
1415 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1416 return true;
1417 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001418 if (auto Count = parseRepeatPassName(Name)) {
1419 CGSCCPassManager NestedCGPM(DebugLogging);
1420 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1421 DebugLogging))
1422 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001423 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001424 return true;
1425 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001426 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1427 CGSCCPassManager NestedCGPM(DebugLogging);
1428 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1429 DebugLogging))
1430 return false;
1431 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1432 *MaxRepetitions, DebugLogging));
1433 return true;
1434 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001435
1436 for (auto &C : CGSCCPipelineParsingCallbacks)
1437 if (C(Name, CGPM, InnerPipeline))
1438 return true;
1439
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001440 // Normal passes can't have pipelines.
1441 return false;
1442 }
1443
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001444// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001445#define CGSCC_PASS(NAME, CREATE_PASS) \
1446 if (Name == NAME) { \
1447 CGPM.addPass(CREATE_PASS); \
1448 return true; \
1449 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001450#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1451 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001452 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001453 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001454 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1455 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001456 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001457 } \
1458 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001459 CGPM.addPass(InvalidateAnalysisPass< \
1460 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001461 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001462 }
1463#include "PassRegistry.def"
1464
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001465 for (auto &C : CGSCCPipelineParsingCallbacks)
1466 if (C(Name, CGPM, InnerPipeline))
1467 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001468 return false;
1469}
1470
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001471bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1472 const PipelineElement &E,
1473 bool VerifyEachPass, bool DebugLogging) {
1474 auto &Name = E.Name;
1475 auto &InnerPipeline = E.InnerPipeline;
1476
1477 // First handle complex passes like the pass managers which carry pipelines.
1478 if (!InnerPipeline.empty()) {
1479 if (Name == "function") {
1480 FunctionPassManager NestedFPM(DebugLogging);
1481 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1482 DebugLogging))
1483 return false;
1484 // Add the nested pass manager with the appropriate adaptor.
1485 FPM.addPass(std::move(NestedFPM));
1486 return true;
1487 }
1488 if (Name == "loop") {
1489 LoopPassManager LPM(DebugLogging);
1490 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1491 DebugLogging))
1492 return false;
1493 // Add the nested pass manager with the appropriate adaptor.
1494 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1495 return true;
1496 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001497 if (auto Count = parseRepeatPassName(Name)) {
1498 FunctionPassManager NestedFPM(DebugLogging);
1499 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1500 DebugLogging))
1501 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001502 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001503 return true;
1504 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001505
1506 for (auto &C : FunctionPipelineParsingCallbacks)
1507 if (C(Name, FPM, InnerPipeline))
1508 return true;
1509
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001510 // Normal passes can't have pipelines.
1511 return false;
1512 }
1513
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001514// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001515#define FUNCTION_PASS(NAME, CREATE_PASS) \
1516 if (Name == NAME) { \
1517 FPM.addPass(CREATE_PASS); \
1518 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001519 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001520#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1521 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001522 FPM.addPass( \
1523 RequireAnalysisPass< \
1524 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001525 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001526 } \
1527 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001528 FPM.addPass(InvalidateAnalysisPass< \
1529 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001530 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001531 }
1532#include "PassRegistry.def"
1533
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001534 for (auto &C : FunctionPipelineParsingCallbacks)
1535 if (C(Name, FPM, InnerPipeline))
1536 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001537 return false;
1538}
1539
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001540bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001541 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001542 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001543 auto &InnerPipeline = E.InnerPipeline;
1544
1545 // First handle complex passes like the pass managers which carry pipelines.
1546 if (!InnerPipeline.empty()) {
1547 if (Name == "loop") {
1548 LoopPassManager NestedLPM(DebugLogging);
1549 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1550 DebugLogging))
1551 return false;
1552 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001553 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001554 return true;
1555 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001556 if (auto Count = parseRepeatPassName(Name)) {
1557 LoopPassManager NestedLPM(DebugLogging);
1558 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1559 DebugLogging))
1560 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001561 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001562 return true;
1563 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001564
1565 for (auto &C : LoopPipelineParsingCallbacks)
1566 if (C(Name, LPM, InnerPipeline))
1567 return true;
1568
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001569 // Normal passes can't have pipelines.
1570 return false;
1571 }
1572
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001573// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001574#define LOOP_PASS(NAME, CREATE_PASS) \
1575 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001576 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001577 return true; \
1578 }
1579#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1580 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001581 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001582 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1583 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1584 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001585 return true; \
1586 } \
1587 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001588 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001589 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001590 return true; \
1591 }
1592#include "PassRegistry.def"
1593
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001594 for (auto &C : LoopPipelineParsingCallbacks)
1595 if (C(Name, LPM, InnerPipeline))
1596 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001597 return false;
1598}
1599
Chandler Carruthedf59962016-02-18 09:45:17 +00001600bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001601#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1602 if (Name == NAME) { \
1603 AA.registerModuleAnalysis< \
1604 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1605 return true; \
1606 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001607#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1608 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001609 AA.registerFunctionAnalysis< \
1610 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001611 return true; \
1612 }
1613#include "PassRegistry.def"
1614
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001615 for (auto &C : AAParsingCallbacks)
1616 if (C(Name, AA))
1617 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001618 return false;
1619}
1620
Justin Bognereecc3c82016-02-25 07:23:08 +00001621bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001622 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001623 bool VerifyEachPass,
1624 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001625 for (const auto &Element : Pipeline) {
1626 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1627 return false;
1628 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001629 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001630 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001631}
1632
Chandler Carruth1ff77242015-03-07 09:02:36 +00001633bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001634 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001635 bool VerifyEachPass,
1636 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001637 for (const auto &Element : Pipeline) {
1638 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1639 return false;
1640 if (VerifyEachPass)
1641 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001642 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001643 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001644}
1645
Chandler Carruth1ff77242015-03-07 09:02:36 +00001646bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001647 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001648 bool VerifyEachPass,
1649 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001650 for (const auto &Element : Pipeline) {
1651 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1652 return false;
1653 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001654 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001655 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001656}
1657
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001658void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1659 FunctionAnalysisManager &FAM,
1660 CGSCCAnalysisManager &CGAM,
1661 ModuleAnalysisManager &MAM) {
1662 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1663 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001664 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1665 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1666 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1667 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1668 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1669}
1670
Chandler Carruth1ff77242015-03-07 09:02:36 +00001671bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001672 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001673 bool VerifyEachPass,
1674 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001675 for (const auto &Element : Pipeline) {
1676 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1677 return false;
1678 if (VerifyEachPass)
1679 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001680 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001681 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001682}
1683
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001684// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001685// FIXME: Should this routine accept a TargetMachine or require the caller to
1686// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001687bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1688 StringRef PipelineText, bool VerifyEachPass,
1689 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001690 auto Pipeline = parsePipelineText(PipelineText);
1691 if (!Pipeline || Pipeline->empty())
1692 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001693
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001694 // If the first name isn't at the module layer, wrap the pipeline up
1695 // automatically.
1696 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001697
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001698 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1699 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001700 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001701 } else if (isFunctionPassName(FirstName,
1702 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001703 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001704 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001706 } else {
1707 for (auto &C : TopLevelPipelineParsingCallbacks)
1708 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1709 return true;
1710
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001711 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001712 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001713 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001714 }
1715
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001716 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001717}
Chandler Carruthedf59962016-02-18 09:45:17 +00001718
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001719// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1720bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1721 StringRef PipelineText, bool VerifyEachPass,
1722 bool DebugLogging) {
1723 auto Pipeline = parsePipelineText(PipelineText);
1724 if (!Pipeline || Pipeline->empty())
1725 return false;
1726
1727 StringRef FirstName = Pipeline->front().Name;
1728 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1729 return false;
1730
1731 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1732}
1733
1734// Primary pass pipeline description parsing routine for a \c
1735// FunctionPassManager
1736bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1737 StringRef PipelineText, bool VerifyEachPass,
1738 bool DebugLogging) {
1739 auto Pipeline = parsePipelineText(PipelineText);
1740 if (!Pipeline || Pipeline->empty())
1741 return false;
1742
1743 StringRef FirstName = Pipeline->front().Name;
1744 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1745 return false;
1746
1747 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1748 DebugLogging);
1749}
1750
1751// Primary pass pipeline description parsing routine for a \c LoopPassManager
1752bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1753 StringRef PipelineText, bool VerifyEachPass,
1754 bool DebugLogging) {
1755 auto Pipeline = parsePipelineText(PipelineText);
1756 if (!Pipeline || Pipeline->empty())
1757 return false;
1758
1759 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1760}
1761
Chandler Carruthedf59962016-02-18 09:45:17 +00001762bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001763 // If the pipeline just consists of the word 'default' just replace the AA
1764 // manager with our default one.
1765 if (PipelineText == "default") {
1766 AA = buildDefaultAAPipeline();
1767 return true;
1768 }
1769
Chandler Carruthedf59962016-02-18 09:45:17 +00001770 while (!PipelineText.empty()) {
1771 StringRef Name;
1772 std::tie(Name, PipelineText) = PipelineText.split(',');
1773 if (!parseAAPassName(AA, Name))
1774 return false;
1775 }
1776
1777 return true;
1778}