blob: c9e4839c93166d44fdabe650eb46fb377557ad9e [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 Chen3a9861422017-07-07 20:53:10 +0000328 bool DebugLogging,
329 bool PrepareForThinLTO) {
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 Chen3a9861422017-07-07 20:53:10 +0000392 // Do not enable unrolling in PrepareForThinLTO phase during sample PGO
393 // because it changes IR to makes profile annotation in back compile
394 // inaccurate.
395 if (!PrepareForThinLTO || !PGOOpt || PGOOpt->SampleProfileFile.empty())
396 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000397
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000398 for (auto &C : LoopOptimizerEndEPCallbacks)
399 C(LPM2, Level);
400
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000401 // We provide the opt remark emitter pass for LICM to use. We only need to do
402 // this once as it is immutable.
403 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000404 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
405 FPM.addPass(SimplifyCFGPass());
406 FPM.addPass(InstCombinePass());
407 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
408
409 // Eliminate redundancies.
410 if (Level != O1) {
411 // These passes add substantial compile time so skip them at O1.
412 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000413 if (RunNewGVN)
414 FPM.addPass(NewGVNPass());
415 else
416 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000417 }
418
419 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
420 FPM.addPass(MemCpyOptPass());
421
422 // Sparse conditional constant propagation.
423 // FIXME: It isn't clear why we do this *after* loop passes rather than
424 // before...
425 FPM.addPass(SCCPPass());
426
427 // Delete dead bit computations (instcombine runs after to fold away the dead
428 // computations, and then ADCE will run later to exploit any new DCE
429 // opportunities that creates).
430 FPM.addPass(BDCEPass());
431
432 // Run instcombine after redundancy and dead bit elimination to exploit
433 // opportunities opened up by them.
434 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000435 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000436
437 // Re-consider control flow based optimizations after redundancy elimination,
438 // redo DCE, etc.
439 FPM.addPass(JumpThreadingPass());
440 FPM.addPass(CorrelatedValuePropagationPass());
441 FPM.addPass(DSEPass());
442 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
443
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000444 for (auto &C : ScalarOptimizerLateEPCallbacks)
445 C(FPM, Level);
446
Chandler Carruthe3f50642016-12-22 06:59:15 +0000447 // Finally, do an expensive DCE pass to catch all the dead code exposed by
448 // the simplifications and basic cleanup after all the simplifications.
449 FPM.addPass(ADCEPass());
450 FPM.addPass(SimplifyCFGPass());
451 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000452 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000453
454 return FPM;
455}
456
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000457void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
458 PassBuilder::OptimizationLevel Level,
459 bool RunProfileGen,
460 std::string ProfileGenFile,
461 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000462 // Generally running simplification passes and the inliner with an high
463 // threshold results in smaller executables, but there may be cases where
464 // the size grows, so let's be conservative here and skip this simplification
465 // at -Os/Oz.
466 if (!isOptimizingForSize(Level)) {
467 InlineParams IP;
468
469 // In the old pass manager, this is a cl::opt. Should still this be one?
470 IP.DefaultThreshold = 75;
471
472 // FIXME: The hint threshold has the same value used by the regular inliner.
473 // This should probably be lowered after performance testing.
474 // FIXME: this comment is cargo culted from the old pass manager, revisit).
475 IP.HintThreshold = 325;
476
477 CGSCCPassManager CGPipeline(DebugLogging);
478
479 CGPipeline.addPass(InlinerPass(IP));
480
481 FunctionPassManager FPM;
482 FPM.addPass(SROA());
483 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
484 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
485 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000486 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000487
Davide Italiano513dfaa2017-02-13 15:26:22 +0000488 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
489
490 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
491 }
492
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000493 // Delete anything that is now dead to make sure that we don't instrument
494 // dead code. Instrumentation can end up keeping dead code around and
495 // dramatically increase code size.
496 MPM.addPass(GlobalDCEPass());
497
Davide Italiano513dfaa2017-02-13 15:26:22 +0000498 if (RunProfileGen) {
499 MPM.addPass(PGOInstrumentationGen());
500
Xinliang David Lib67530e2017-06-25 00:26:43 +0000501 FunctionPassManager FPM;
502 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
503 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
504
Davide Italiano513dfaa2017-02-13 15:26:22 +0000505 // Add the profile lowering pass.
506 InstrProfOptions Options;
507 if (!ProfileGenFile.empty())
508 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000509 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000510 MPM.addPass(InstrProfiling(Options));
511 }
512
513 if (!ProfileUseFile.empty())
514 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
515}
516
Easwaran Raman8249fac2017-06-28 13:33:49 +0000517static InlineParams
518getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
519 auto O3 = PassBuilder::O3;
520 unsigned OptLevel = Level > O3 ? 2 : Level;
521 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
522 return getInlineParams(OptLevel, SizeLevel);
523}
524
Chandler Carruthe3f50642016-12-22 06:59:15 +0000525ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000526PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen3a9861422017-07-07 20:53:10 +0000527 bool DebugLogging,
528 bool PrepareForThinLTO) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000529 ModulePassManager MPM(DebugLogging);
530
Chandler Carruthe3f50642016-12-22 06:59:15 +0000531 // Do basic inference of function attributes from known properties of system
532 // libraries and other oracles.
533 MPM.addPass(InferFunctionAttrsPass());
534
535 // Create an early function pass manager to cleanup the output of the
536 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000537 FunctionPassManager EarlyFPM(DebugLogging);
538 EarlyFPM.addPass(SimplifyCFGPass());
539 EarlyFPM.addPass(SROA());
540 EarlyFPM.addPass(EarlyCSEPass());
541 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000542 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000543
544 // Interprocedural constant propagation now that basic cleanup has occured
545 // and prior to optimizing globals.
546 // FIXME: This position in the pipeline hasn't been carefully considered in
547 // years, it should be re-analyzed.
548 MPM.addPass(IPSCCPPass());
549
550 // Optimize globals to try and fold them into constants.
551 MPM.addPass(GlobalOptPass());
552
553 // Promote any localized globals to SSA registers.
554 // FIXME: Should this instead by a run of SROA?
555 // FIXME: We should probably run instcombine and simplify-cfg afterward to
556 // delete control flows that are dead once globals have been folded to
557 // constants.
558 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
559
560 // Remove any dead arguments exposed by cleanups and constand folding
561 // globals.
562 MPM.addPass(DeadArgumentEliminationPass());
563
564 // Create a small function pass pipeline to cleanup after all the global
565 // optimizations.
566 FunctionPassManager GlobalCleanupPM(DebugLogging);
567 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000568 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
569
Chandler Carruthe3f50642016-12-22 06:59:15 +0000570 GlobalCleanupPM.addPass(SimplifyCFGPass());
571 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
572
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000573 // Add all the requested passes for PGO, if requested.
Davide Italiano513dfaa2017-02-13 15:26:22 +0000574 if (PGOOpt) {
Dehao Chence0842c2017-07-29 04:10:24 +0000575 if (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())
576 // Instrumentation based PGO (gen and use)
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000577 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
578 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
Dehao Chence0842c2017-07-29 04:10:24 +0000579 else if (!PGOOpt->SampleProfileFile.empty())
580 // SamplePGO use
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000581 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000582
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000583 // Indirect call promotion that promotes intra-module targes only.
Dehao Chen3a9861422017-07-07 20:53:10 +0000584 // Do not enable it in PrepareForThinLTO phase during sample PGO because
585 // it changes IR to makes profile annotation in back compile inaccurate.
Dehao Chence0842c2017-07-29 04:10:24 +0000586 if ((!PrepareForThinLTO && !PGOOpt->SampleProfileFile.empty())
587 || !PGOOpt->ProfileUseFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000588 MPM.addPass(PGOIndirectCallPromotion(
589 false, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000590 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000591
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000592 // Require the GlobalsAA analysis for the module so we can query it within
593 // the CGSCC pipeline.
594 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000595
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000596 // Require the ProfileSummaryAnalysis for the module so we can query it within
597 // the inliner pass.
598 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
599
Chandler Carruthe3f50642016-12-22 06:59:15 +0000600 // Now begin the main postorder CGSCC pipeline.
601 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
602 // manager and trying to emulate its precise behavior. Much of this doesn't
603 // make a lot of sense and we should revisit the core CGSCC structure.
604 CGSCCPassManager MainCGPipeline(DebugLogging);
605
606 // Note: historically, the PruneEH pass was run first to deduce nounwind and
607 // generally clean up exception handling overhead. It isn't clear this is
608 // valuable as the inliner doesn't currently care whether it is inlining an
609 // invoke or a call.
610
611 // Run the inliner first. The theory is that we are walking bottom-up and so
612 // the callees have already been fully optimized, and we want to inline them
613 // into the callers so that our optimizations can reflect that.
Dehao Chen3a9861422017-07-07 20:53:10 +0000614 // For PrepareForThinLTO pass, we disable hot-caller heuristic for sample PGO
615 // because it makes profile annotation in the backend inaccurate.
616 InlineParams IP = getInlineParamsFromOptLevel(Level);
617 if (PrepareForThinLTO && PGOOpt && !PGOOpt->SampleProfileFile.empty())
618 IP.HotCallSiteThreshold = 0;
619 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000620
621 // Now deduce any function attributes based in the current code.
622 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
623
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000624 // When at O3 add argument promotion to the pass pipeline.
625 // FIXME: It isn't at all clear why this should be limited to O3.
626 if (Level == O3)
627 MainCGPipeline.addPass(ArgumentPromotionPass());
628
Chandler Carruthe3f50642016-12-22 06:59:15 +0000629 // Lastly, add the core function simplification pipeline nested inside the
630 // CGSCC walk.
631 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen3a9861422017-07-07 20:53:10 +0000632 buildFunctionSimplificationPipeline(Level, DebugLogging,
633 PrepareForThinLTO)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000634
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000635 for (auto &C : CGSCCOptimizerLateEPCallbacks)
636 C(MainCGPipeline, Level);
637
Chandler Carruth719ffe12017-02-12 05:38:04 +0000638 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
639 // to detect when we devirtualize indirect calls and iterate the SCC passes
640 // in that case to try and catch knock-on inlining or function attrs
641 // opportunities. Then we add it to the module pipeline by walking the SCCs
642 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000643 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000644 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
645 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000646
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000647 return MPM;
648}
649
650ModulePassManager
651PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
652 bool DebugLogging) {
653 ModulePassManager MPM(DebugLogging);
654
655 // Optimize globals now that the module is fully simplified.
656 MPM.addPass(GlobalOptPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000657
Xinliang David Li126157c2017-05-22 16:41:57 +0000658 // Run partial inlining pass to partially inline functions that have
659 // large bodies.
660 if (RunPartialInlining)
661 MPM.addPass(PartialInlinerPass());
662
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000663 // Remove avail extern fns and globals definitions since we aren't compiling
664 // an object file for later LTO. For LTO we want to preserve these so they
665 // are eligible for inlining at link-time. Note if they are unreferenced they
666 // will be removed by GlobalDCE later, so this only impacts referenced
667 // available externally globals. Eventually they will be suppressed during
668 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
669 // may make globals referenced by available external functions dead and saves
670 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000671 MPM.addPass(EliminateAvailableExternallyPass());
672
673 // Do RPO function attribute inference across the module to forward-propagate
674 // attributes where applicable.
675 // FIXME: Is this really an optimization rather than a canonicalization?
676 MPM.addPass(ReversePostOrderFunctionAttrsPass());
677
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000678 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000679 // useful as the above will have inlined, DCE'ed, and function-attr
680 // propagated everything. We should at this point have a reasonably minimal
681 // and richly annotated call graph. By computing aliasing and mod/ref
682 // information for all local globals here, the late loop passes and notably
683 // the vectorizer will be able to use them to help recognize vectorizable
684 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000685 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000686
687 FunctionPassManager OptimizePM(DebugLogging);
688 OptimizePM.addPass(Float2IntPass());
689 // FIXME: We need to run some loop optimizations to re-rotate loops after
690 // simplify-cfg and others undo their rotation.
691
692 // Optimize the loop execution. These passes operate on entire loop nests
693 // rather than on each loop in an inside-out manner, and so they are actually
694 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000695
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000696 for (auto &C : VectorizerStartEPCallbacks)
697 C(OptimizePM, Level);
698
Chandler Carrutha95ff382017-01-27 00:50:21 +0000699 // First rotate loops that may have been un-rotated by prior passes.
700 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
701
702 // Distribute loops to allow partial vectorization. I.e. isolate dependences
703 // into separate loop that would otherwise inhibit vectorization. This is
704 // currently only performed for loops marked with the metadata
705 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000706 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000707
708 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000709 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000710
Chandler Carruthbaabda92017-01-27 01:32:26 +0000711 // Eliminate loads by forwarding stores from the previous iteration to loads
712 // of the current iteration.
713 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000714
715 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000716 OptimizePM.addPass(InstCombinePass());
717
Chandler Carrutha95ff382017-01-27 00:50:21 +0000718
719 // Now that we've formed fast to execute loop structures, we do further
720 // optimizations. These are run afterward as they might block doing complex
721 // analyses and transforms such as what are needed for loop vectorization.
722
Chandler Carruthe3f50642016-12-22 06:59:15 +0000723 // Optimize parallel scalar instruction chains into SIMD instructions.
724 OptimizePM.addPass(SLPVectorizerPass());
725
Chandler Carrutha95ff382017-01-27 00:50:21 +0000726 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000727 OptimizePM.addPass(SimplifyCFGPass());
728 OptimizePM.addPass(InstCombinePass());
729
730 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000731 // execution resources of an out-of-order processor. We also then need to
732 // clean up redundancies and loop invariant code.
733 // FIXME: It would be really good to use a loop-integrated instruction
734 // combiner for cleanup here so that the unrolling and LICM can be pipelined
735 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000736 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000737 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000738 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000739 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000740
741 // Now that we've vectorized and unrolled loops, we may have more refined
742 // alignment information, try to re-derive it here.
743 OptimizePM.addPass(AlignmentFromAssumptionsPass());
744
Chandler Carrutha95ff382017-01-27 00:50:21 +0000745 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
746 // canonicalization pass that enables other optimizations. As a result,
747 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
748 // result too early.
749 OptimizePM.addPass(LoopSinkPass());
750
751 // And finally clean up LCSSA form before generating code.
752 OptimizePM.addPass(InstSimplifierPass());
753
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000754 // LoopSink (and other loop passes since the last simplifyCFG) might have
755 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
756 OptimizePM.addPass(SimplifyCFGPass());
757
Chandler Carrutha95ff382017-01-27 00:50:21 +0000758 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000759 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
760
761 // Now we need to do some global optimization transforms.
762 // FIXME: It would seem like these should come first in the optimization
763 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
764 // ordering here.
765 MPM.addPass(GlobalDCEPass());
766 MPM.addPass(ConstantMergePass());
767
768 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000769}
770
Chandler Carruthe3f50642016-12-22 06:59:15 +0000771ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000772PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
773 bool DebugLogging) {
774 assert(Level != O0 && "Must request optimizations for the default pipeline!");
775
776 ModulePassManager MPM(DebugLogging);
777
778 // Force any function attributes we want the rest of the pipeline to observe.
779 MPM.addPass(ForceFunctionAttrsPass());
780
Dehao Chence0842c2017-07-29 04:10:24 +0000781 if (PGOOpt && PGOOpt->SamplePGOSupport)
782 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
783
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000784 // Add the core simplification pipeline.
Dehao Chen3a9861422017-07-07 20:53:10 +0000785 MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging,
786 /*PrepareForThinLTO=*/false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000787
788 // Now add the optimization pipeline.
789 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
790
791 return MPM;
792}
793
794ModulePassManager
795PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
796 bool DebugLogging) {
797 assert(Level != O0 && "Must request optimizations for the default pipeline!");
798
799 ModulePassManager MPM(DebugLogging);
800
801 // Force any function attributes we want the rest of the pipeline to observe.
802 MPM.addPass(ForceFunctionAttrsPass());
803
Dehao Chence0842c2017-07-29 04:10:24 +0000804 if (PGOOpt && PGOOpt->SamplePGOSupport)
805 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
806
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000807 // If we are planning to perform ThinLTO later, we don't bloat the code with
808 // unrolling/vectorization/... now. Just simplify the module as much as we
809 // can.
Dehao Chen3a9861422017-07-07 20:53:10 +0000810 MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging,
811 /*PrepareForThinLTO=*/true));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000812
813 // Run partial inlining pass to partially inline functions that have
814 // large bodies.
815 // FIXME: It isn't clear whether this is really the right place to run this
816 // in ThinLTO. Because there is another canonicalization and simplification
817 // phase that will run after the thin link, running this here ends up with
818 // less information than will be available later and it may grow functions in
819 // ways that aren't beneficial.
820 if (RunPartialInlining)
821 MPM.addPass(PartialInlinerPass());
822
823 // Reduce the size of the IR as much as possible.
824 MPM.addPass(GlobalOptPass());
825
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000826 return MPM;
827}
828
829ModulePassManager
830PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
831 bool DebugLogging) {
832 // FIXME: The summary index is not hooked in the new pass manager yet.
833 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
834 // here.
835
836 ModulePassManager MPM(DebugLogging);
837
838 // Force any function attributes we want the rest of the pipeline to observe.
839 MPM.addPass(ForceFunctionAttrsPass());
840
841 // During the ThinLTO backend phase we perform early indirect call promotion
842 // here, before globalopt. Otherwise imported available_externally functions
843 // look unreferenced and are removed.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000844 MPM.addPass(PGOIndirectCallPromotion(
845 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
846 !PGOOpt->ProfileUseFile.empty()));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000847
848 // Add the core simplification pipeline.
Dehao Chen3a9861422017-07-07 20:53:10 +0000849 MPM.addPass(buildModuleSimplificationPipeline(Level, DebugLogging,
850 /*PrepareForThinLTO=*/false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000851
852 // Now add the optimization pipeline.
853 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
854
855 return MPM;
856}
857
858ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000859PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
860 bool DebugLogging) {
861 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000862 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000863 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000864}
865
Chandler Carruthe3f50642016-12-22 06:59:15 +0000866ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
867 bool DebugLogging) {
868 assert(Level != O0 && "Must request optimizations for the default pipeline!");
869 ModulePassManager MPM(DebugLogging);
870
Davide Italiano089a9122017-01-24 00:57:39 +0000871 // Remove unused virtual tables to improve the quality of code generated by
872 // whole-program devirtualization and bitset lowering.
873 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000874
Davide Italiano089a9122017-01-24 00:57:39 +0000875 // Force any function attributes we want the rest of the pipeline to observe.
876 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000877
Davide Italiano089a9122017-01-24 00:57:39 +0000878 // Do basic inference of function attributes from known properties of system
879 // libraries and other oracles.
880 MPM.addPass(InferFunctionAttrsPass());
881
882 if (Level > 1) {
883 // Indirect call promotion. This should promote all the targets that are
884 // left by the earlier promotion pass that promotes intra-module targets.
885 // This two-step promotion is to save the compile time. For LTO, it should
886 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000887 MPM.addPass(PGOIndirectCallPromotion(
888 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000889
890 // Propagate constants at call sites into the functions they call. This
891 // opens opportunities for globalopt (and inlining) by substituting function
892 // pointers passed as arguments to direct uses of functions.
893 MPM.addPass(IPSCCPPass());
894 }
895
896 // Now deduce any function attributes based in the current code.
897 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
898 PostOrderFunctionAttrsPass()));
899
900 // Do RPO function attribute inference across the module to forward-propagate
901 // attributes where applicable.
902 // FIXME: Is this really an optimization rather than a canonicalization?
903 MPM.addPass(ReversePostOrderFunctionAttrsPass());
904
905 // Use inragne annotations on GEP indices to split globals where beneficial.
906 MPM.addPass(GlobalSplitPass());
907
908 // Run whole program optimization of virtual call when the list of callees
909 // is fixed.
910 MPM.addPass(WholeProgramDevirtPass());
911
912 // Stop here at -O1.
913 if (Level == 1)
914 return MPM;
915
916 // Optimize globals to try and fold them into constants.
917 MPM.addPass(GlobalOptPass());
918
919 // Promote any localized globals to SSA registers.
920 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
921
922 // Linking modules together can lead to duplicate global constant, only
923 // keep one copy of each constant.
924 MPM.addPass(ConstantMergePass());
925
926 // Remove unused arguments from functions.
927 MPM.addPass(DeadArgumentEliminationPass());
928
929 // Reduce the code after globalopt and ipsccp. Both can open up significant
930 // simplification opportunities, and both can propagate functions through
931 // function pointers. When this happens, we often have to resolve varargs
932 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000933 FunctionPassManager PeepholeFPM(DebugLogging);
934 PeepholeFPM.addPass(InstCombinePass());
935 invokePeepholeEPCallbacks(PeepholeFPM, Level);
936
937 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000938
939 // Note: historically, the PruneEH pass was run first to deduce nounwind and
940 // generally clean up exception handling overhead. It isn't clear this is
941 // valuable as the inliner doesn't currently care whether it is inlining an
942 // invoke or a call.
943 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000944 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
945 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000946
947 // Optimize globals again after we ran the inliner.
948 MPM.addPass(GlobalOptPass());
949
950 // Garbage collect dead functions.
951 // FIXME: Add ArgumentPromotion pass after once it's ported.
952 MPM.addPass(GlobalDCEPass());
953
954 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +0000955 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +0000956 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000957 invokePeepholeEPCallbacks(FPM, Level);
958
Davide Italiano089a9122017-01-24 00:57:39 +0000959 FPM.addPass(JumpThreadingPass());
960
961 // Break up allocas
962 FPM.addPass(SROA());
963
964 // Run a few AA driver optimizations here and now to cleanup the code.
965 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
966
967 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
968 PostOrderFunctionAttrsPass()));
969 // FIXME: here we run IP alias analysis in the legacy PM.
970
971 FunctionPassManager MainFPM;
972
973 // FIXME: once we fix LoopPass Manager, add LICM here.
974 // FIXME: once we provide support for enabling MLSM, add it here.
975 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000976 if (RunNewGVN)
977 MainFPM.addPass(NewGVNPass());
978 else
979 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +0000980
981 // Remove dead memcpy()'s.
982 MainFPM.addPass(MemCpyOptPass());
983
984 // Nuke dead stores.
985 MainFPM.addPass(DSEPass());
986
987 // FIXME: at this point, we run a bunch of loop passes:
988 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
989 // loopVectorize. Enable them once the remaining issue with LPM
990 // are sorted out.
991
992 MainFPM.addPass(InstCombinePass());
993 MainFPM.addPass(SimplifyCFGPass());
994 MainFPM.addPass(SCCPPass());
995 MainFPM.addPass(InstCombinePass());
996 MainFPM.addPass(BDCEPass());
997
998 // FIXME: We may want to run SLPVectorizer here.
999 // After vectorization, assume intrinsics may tell us more
1000 // about pointer alignments.
1001#if 0
1002 MainFPM.add(AlignmentFromAssumptionsPass());
1003#endif
1004
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001005 // FIXME: Conditionally run LoadCombine here, after it's ported
1006 // (in case we still have this pass, given its questionable usefulness).
1007
Davide Italiano089a9122017-01-24 00:57:39 +00001008 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001009 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001010 MainFPM.addPass(JumpThreadingPass());
1011 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1012
1013 // Create a function that performs CFI checks for cross-DSO calls with
1014 // targets in the current module.
1015 MPM.addPass(CrossDSOCFIPass());
1016
1017 // Lower type metadata and the type.test intrinsic. This pass supports
1018 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1019 // to be run at link time if CFI is enabled. This pass does nothing if
1020 // CFI is disabled.
1021 // Enable once we add support for the summary in the new PM.
1022#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001023 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1024 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001025 Summary));
1026#endif
1027
1028 // Add late LTO optimization passes.
1029 // Delete basic blocks, which optimization passes may have killed.
1030 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1031
1032 // Drop bodies of available eternally objects to improve GlobalDCE.
1033 MPM.addPass(EliminateAvailableExternallyPass());
1034
1035 // Now that we have optimized the program, discard unreachable functions.
1036 MPM.addPass(GlobalDCEPass());
1037
1038 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001039 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001040}
1041
Chandler Carruth060ad612016-12-23 20:38:19 +00001042AAManager PassBuilder::buildDefaultAAPipeline() {
1043 AAManager AA;
1044
1045 // The order in which these are registered determines their priority when
1046 // being queried.
1047
1048 // First we register the basic alias analysis that provides the majority of
1049 // per-function local AA logic. This is a stateless, on-demand local set of
1050 // AA techniques.
1051 AA.registerFunctionAnalysis<BasicAA>();
1052
1053 // Next we query fast, specialized alias analyses that wrap IR-embedded
1054 // information about aliasing.
1055 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1056 AA.registerFunctionAnalysis<TypeBasedAA>();
1057
1058 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001059 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1060 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001061 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001062 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001063
1064 return AA;
1065}
1066
Chandler Carruth241bf242016-08-03 07:44:48 +00001067static Optional<int> parseRepeatPassName(StringRef Name) {
1068 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1069 return None;
1070 int Count;
1071 if (Name.getAsInteger(0, Count) || Count <= 0)
1072 return None;
1073 return Count;
1074}
1075
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001076static Optional<int> parseDevirtPassName(StringRef Name) {
1077 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1078 return None;
1079 int Count;
1080 if (Name.getAsInteger(0, Count) || Count <= 0)
1081 return None;
1082 return Count;
1083}
1084
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001085/// Tests whether a pass name starts with a valid prefix for a default pipeline
1086/// alias.
1087static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1088 return Name.startswith("default") || Name.startswith("thinlto") ||
1089 Name.startswith("lto");
1090}
1091
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001092/// Tests whether registered callbacks will accept a given pass name.
1093///
1094/// When parsing a pipeline text, the type of the outermost pipeline may be
1095/// omitted, in which case the type is automatically determined from the first
1096/// pass name in the text. This may be a name that is handled through one of the
1097/// callbacks. We check this through the oridinary parsing callbacks by setting
1098/// up a dummy PassManager in order to not force the client to also handle this
1099/// type of query.
1100template <typename PassManagerT, typename CallbacksT>
1101static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1102 if (!Callbacks.empty()) {
1103 PassManagerT DummyPM;
1104 for (auto &CB : Callbacks)
1105 if (CB(Name, DummyPM, {}))
1106 return true;
1107 }
1108 return false;
1109}
1110
1111template <typename CallbacksT>
1112static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001113 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001114 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001115 return DefaultAliasRegex.match(Name);
1116
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001117 // Explicitly handle pass manager names.
1118 if (Name == "module")
1119 return true;
1120 if (Name == "cgscc")
1121 return true;
1122 if (Name == "function")
1123 return true;
1124
Chandler Carruth241bf242016-08-03 07:44:48 +00001125 // Explicitly handle custom-parsed pass names.
1126 if (parseRepeatPassName(Name))
1127 return true;
1128
Chandler Carruth74a8a222016-06-17 07:15:29 +00001129#define MODULE_PASS(NAME, CREATE_PASS) \
1130 if (Name == NAME) \
1131 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001132#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001133 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001134 return true;
1135#include "PassRegistry.def"
1136
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001137 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001138}
1139
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001140template <typename CallbacksT>
1141static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001142 // Explicitly handle pass manager names.
1143 if (Name == "cgscc")
1144 return true;
1145 if (Name == "function")
1146 return true;
1147
Chandler Carruth241bf242016-08-03 07:44:48 +00001148 // Explicitly handle custom-parsed pass names.
1149 if (parseRepeatPassName(Name))
1150 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001151 if (parseDevirtPassName(Name))
1152 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001153
Chandler Carruth74a8a222016-06-17 07:15:29 +00001154#define CGSCC_PASS(NAME, CREATE_PASS) \
1155 if (Name == NAME) \
1156 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001157#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001158 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001159 return true;
1160#include "PassRegistry.def"
1161
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001162 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001163}
1164
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001165template <typename CallbacksT>
1166static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001167 // Explicitly handle pass manager names.
1168 if (Name == "function")
1169 return true;
1170 if (Name == "loop")
1171 return true;
1172
Chandler Carruth241bf242016-08-03 07:44:48 +00001173 // Explicitly handle custom-parsed pass names.
1174 if (parseRepeatPassName(Name))
1175 return true;
1176
Chandler Carruth74a8a222016-06-17 07:15:29 +00001177#define FUNCTION_PASS(NAME, CREATE_PASS) \
1178 if (Name == NAME) \
1179 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001180#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001181 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001182 return true;
1183#include "PassRegistry.def"
1184
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001185 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001186}
1187
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001188template <typename CallbacksT>
1189static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001190 // Explicitly handle pass manager names.
1191 if (Name == "loop")
1192 return true;
1193
Chandler Carruth241bf242016-08-03 07:44:48 +00001194 // Explicitly handle custom-parsed pass names.
1195 if (parseRepeatPassName(Name))
1196 return true;
1197
Chandler Carruth74a8a222016-06-17 07:15:29 +00001198#define LOOP_PASS(NAME, CREATE_PASS) \
1199 if (Name == NAME) \
1200 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001201#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1202 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1203 return true;
1204#include "PassRegistry.def"
1205
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001206 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001207}
1208
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001209Optional<std::vector<PassBuilder::PipelineElement>>
1210PassBuilder::parsePipelineText(StringRef Text) {
1211 std::vector<PipelineElement> ResultPipeline;
1212
1213 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1214 &ResultPipeline};
1215 for (;;) {
1216 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1217 size_t Pos = Text.find_first_of(",()");
1218 Pipeline.push_back({Text.substr(0, Pos), {}});
1219
1220 // If we have a single terminating name, we're done.
1221 if (Pos == Text.npos)
1222 break;
1223
1224 char Sep = Text[Pos];
1225 Text = Text.substr(Pos + 1);
1226 if (Sep == ',')
1227 // Just a name ending in a comma, continue.
1228 continue;
1229
1230 if (Sep == '(') {
1231 // Push the inner pipeline onto the stack to continue processing.
1232 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1233 continue;
1234 }
1235
1236 assert(Sep == ')' && "Bogus separator!");
1237 // When handling the close parenthesis, we greedily consume them to avoid
1238 // empty strings in the pipeline.
1239 do {
1240 // If we try to pop the outer pipeline we have unbalanced parentheses.
1241 if (PipelineStack.size() == 1)
1242 return None;
1243
1244 PipelineStack.pop_back();
1245 } while (Text.consume_front(")"));
1246
1247 // Check if we've finished parsing.
1248 if (Text.empty())
1249 break;
1250
1251 // Otherwise, the end of an inner pipeline always has to be followed by
1252 // a comma, and then we can continue.
1253 if (!Text.consume_front(","))
1254 return None;
1255 }
1256
1257 if (PipelineStack.size() > 1)
1258 // Unbalanced paretheses.
1259 return None;
1260
1261 assert(PipelineStack.back() == &ResultPipeline &&
1262 "Wrong pipeline at the bottom of the stack!");
1263 return {std::move(ResultPipeline)};
1264}
1265
1266bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1267 const PipelineElement &E, bool VerifyEachPass,
1268 bool DebugLogging) {
1269 auto &Name = E.Name;
1270 auto &InnerPipeline = E.InnerPipeline;
1271
1272 // First handle complex passes like the pass managers which carry pipelines.
1273 if (!InnerPipeline.empty()) {
1274 if (Name == "module") {
1275 ModulePassManager NestedMPM(DebugLogging);
1276 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1277 DebugLogging))
1278 return false;
1279 MPM.addPass(std::move(NestedMPM));
1280 return true;
1281 }
1282 if (Name == "cgscc") {
1283 CGSCCPassManager CGPM(DebugLogging);
1284 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1285 DebugLogging))
1286 return false;
1287 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1288 DebugLogging));
1289 return true;
1290 }
1291 if (Name == "function") {
1292 FunctionPassManager FPM(DebugLogging);
1293 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1294 DebugLogging))
1295 return false;
1296 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1297 return true;
1298 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001299 if (auto Count = parseRepeatPassName(Name)) {
1300 ModulePassManager NestedMPM(DebugLogging);
1301 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1302 DebugLogging))
1303 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001304 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001305 return true;
1306 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001307
1308 for (auto &C : ModulePipelineParsingCallbacks)
1309 if (C(Name, MPM, InnerPipeline))
1310 return true;
1311
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001312 // Normal passes can't have pipelines.
1313 return false;
1314 }
1315
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001316 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001317 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001318 SmallVector<StringRef, 3> Matches;
1319 if (!DefaultAliasRegex.match(Name, &Matches))
1320 return false;
1321 assert(Matches.size() == 3 && "Must capture two matched strings!");
1322
Jordan Rosef85a95f2016-07-25 18:34:51 +00001323 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001324 .Case("O0", O0)
1325 .Case("O1", O1)
1326 .Case("O2", O2)
1327 .Case("O3", O3)
1328 .Case("Os", Os)
1329 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001330 if (L == O0)
1331 // At O0 we do nothing at all!
1332 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001333
1334 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001335 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001336 } else if (Matches[1] == "thinlto-pre-link") {
1337 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1338 } else if (Matches[1] == "thinlto") {
1339 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001340 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001341 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001342 } else {
1343 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001344 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001345 }
1346 return true;
1347 }
1348
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001349 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001350#define MODULE_PASS(NAME, CREATE_PASS) \
1351 if (Name == NAME) { \
1352 MPM.addPass(CREATE_PASS); \
1353 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001354 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001355#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1356 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001357 MPM.addPass( \
1358 RequireAnalysisPass< \
1359 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001360 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001361 } \
1362 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001363 MPM.addPass(InvalidateAnalysisPass< \
1364 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001365 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001366 }
1367#include "PassRegistry.def"
1368
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001369 for (auto &C : ModulePipelineParsingCallbacks)
1370 if (C(Name, MPM, InnerPipeline))
1371 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001372 return false;
1373}
1374
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001375bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1376 const PipelineElement &E, bool VerifyEachPass,
1377 bool DebugLogging) {
1378 auto &Name = E.Name;
1379 auto &InnerPipeline = E.InnerPipeline;
1380
1381 // First handle complex passes like the pass managers which carry pipelines.
1382 if (!InnerPipeline.empty()) {
1383 if (Name == "cgscc") {
1384 CGSCCPassManager NestedCGPM(DebugLogging);
1385 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1386 DebugLogging))
1387 return false;
1388 // Add the nested pass manager with the appropriate adaptor.
1389 CGPM.addPass(std::move(NestedCGPM));
1390 return true;
1391 }
1392 if (Name == "function") {
1393 FunctionPassManager FPM(DebugLogging);
1394 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1395 DebugLogging))
1396 return false;
1397 // Add the nested pass manager with the appropriate adaptor.
1398 CGPM.addPass(
1399 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1400 return true;
1401 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001402 if (auto Count = parseRepeatPassName(Name)) {
1403 CGSCCPassManager NestedCGPM(DebugLogging);
1404 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1405 DebugLogging))
1406 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001407 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001408 return true;
1409 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001410 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1411 CGSCCPassManager NestedCGPM(DebugLogging);
1412 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1413 DebugLogging))
1414 return false;
1415 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1416 *MaxRepetitions, DebugLogging));
1417 return true;
1418 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001419
1420 for (auto &C : CGSCCPipelineParsingCallbacks)
1421 if (C(Name, CGPM, InnerPipeline))
1422 return true;
1423
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001424 // Normal passes can't have pipelines.
1425 return false;
1426 }
1427
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001428// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001429#define CGSCC_PASS(NAME, CREATE_PASS) \
1430 if (Name == NAME) { \
1431 CGPM.addPass(CREATE_PASS); \
1432 return true; \
1433 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001434#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1435 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001436 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001437 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001438 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1439 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001440 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001441 } \
1442 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001443 CGPM.addPass(InvalidateAnalysisPass< \
1444 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001445 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001446 }
1447#include "PassRegistry.def"
1448
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001449 for (auto &C : CGSCCPipelineParsingCallbacks)
1450 if (C(Name, CGPM, InnerPipeline))
1451 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001452 return false;
1453}
1454
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001455bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1456 const PipelineElement &E,
1457 bool VerifyEachPass, bool DebugLogging) {
1458 auto &Name = E.Name;
1459 auto &InnerPipeline = E.InnerPipeline;
1460
1461 // First handle complex passes like the pass managers which carry pipelines.
1462 if (!InnerPipeline.empty()) {
1463 if (Name == "function") {
1464 FunctionPassManager NestedFPM(DebugLogging);
1465 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1466 DebugLogging))
1467 return false;
1468 // Add the nested pass manager with the appropriate adaptor.
1469 FPM.addPass(std::move(NestedFPM));
1470 return true;
1471 }
1472 if (Name == "loop") {
1473 LoopPassManager LPM(DebugLogging);
1474 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1475 DebugLogging))
1476 return false;
1477 // Add the nested pass manager with the appropriate adaptor.
1478 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1479 return true;
1480 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001481 if (auto Count = parseRepeatPassName(Name)) {
1482 FunctionPassManager NestedFPM(DebugLogging);
1483 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1484 DebugLogging))
1485 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001486 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001487 return true;
1488 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001489
1490 for (auto &C : FunctionPipelineParsingCallbacks)
1491 if (C(Name, FPM, InnerPipeline))
1492 return true;
1493
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001494 // Normal passes can't have pipelines.
1495 return false;
1496 }
1497
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001498// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001499#define FUNCTION_PASS(NAME, CREATE_PASS) \
1500 if (Name == NAME) { \
1501 FPM.addPass(CREATE_PASS); \
1502 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001503 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001504#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1505 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001506 FPM.addPass( \
1507 RequireAnalysisPass< \
1508 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001509 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001510 } \
1511 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001512 FPM.addPass(InvalidateAnalysisPass< \
1513 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001514 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001515 }
1516#include "PassRegistry.def"
1517
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001518 for (auto &C : FunctionPipelineParsingCallbacks)
1519 if (C(Name, FPM, InnerPipeline))
1520 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001521 return false;
1522}
1523
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001524bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001525 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001526 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001527 auto &InnerPipeline = E.InnerPipeline;
1528
1529 // First handle complex passes like the pass managers which carry pipelines.
1530 if (!InnerPipeline.empty()) {
1531 if (Name == "loop") {
1532 LoopPassManager NestedLPM(DebugLogging);
1533 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1534 DebugLogging))
1535 return false;
1536 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001537 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001538 return true;
1539 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001540 if (auto Count = parseRepeatPassName(Name)) {
1541 LoopPassManager NestedLPM(DebugLogging);
1542 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1543 DebugLogging))
1544 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001545 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001546 return true;
1547 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001548
1549 for (auto &C : LoopPipelineParsingCallbacks)
1550 if (C(Name, LPM, InnerPipeline))
1551 return true;
1552
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001553 // Normal passes can't have pipelines.
1554 return false;
1555 }
1556
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001557// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001558#define LOOP_PASS(NAME, CREATE_PASS) \
1559 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001560 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001561 return true; \
1562 }
1563#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1564 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001565 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001566 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1567 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1568 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001569 return true; \
1570 } \
1571 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001572 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001573 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001574 return true; \
1575 }
1576#include "PassRegistry.def"
1577
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001578 for (auto &C : LoopPipelineParsingCallbacks)
1579 if (C(Name, LPM, InnerPipeline))
1580 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001581 return false;
1582}
1583
Chandler Carruthedf59962016-02-18 09:45:17 +00001584bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001585#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1586 if (Name == NAME) { \
1587 AA.registerModuleAnalysis< \
1588 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1589 return true; \
1590 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001591#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1592 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001593 AA.registerFunctionAnalysis< \
1594 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001595 return true; \
1596 }
1597#include "PassRegistry.def"
1598
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001599 for (auto &C : AAParsingCallbacks)
1600 if (C(Name, AA))
1601 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001602 return false;
1603}
1604
Justin Bognereecc3c82016-02-25 07:23:08 +00001605bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001606 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001607 bool VerifyEachPass,
1608 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001609 for (const auto &Element : Pipeline) {
1610 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1611 return false;
1612 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001613 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001614 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001615}
1616
Chandler Carruth1ff77242015-03-07 09:02:36 +00001617bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001618 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001619 bool VerifyEachPass,
1620 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001621 for (const auto &Element : Pipeline) {
1622 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1623 return false;
1624 if (VerifyEachPass)
1625 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001626 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001627 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001628}
1629
Chandler Carruth1ff77242015-03-07 09:02:36 +00001630bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001631 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001632 bool VerifyEachPass,
1633 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001634 for (const auto &Element : Pipeline) {
1635 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1636 return false;
1637 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001638 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001639 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001640}
1641
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001642void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1643 FunctionAnalysisManager &FAM,
1644 CGSCCAnalysisManager &CGAM,
1645 ModuleAnalysisManager &MAM) {
1646 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1647 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001648 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1649 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1650 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1651 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1652 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1653}
1654
Chandler Carruth1ff77242015-03-07 09:02:36 +00001655bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001656 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001657 bool VerifyEachPass,
1658 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001659 for (const auto &Element : Pipeline) {
1660 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1661 return false;
1662 if (VerifyEachPass)
1663 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001664 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001665 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001666}
1667
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001668// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001669// FIXME: Should this routine accept a TargetMachine or require the caller to
1670// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001671bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1672 StringRef PipelineText, bool VerifyEachPass,
1673 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001674 auto Pipeline = parsePipelineText(PipelineText);
1675 if (!Pipeline || Pipeline->empty())
1676 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001677
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001678 // If the first name isn't at the module layer, wrap the pipeline up
1679 // automatically.
1680 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001681
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001682 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1683 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001684 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001685 } else if (isFunctionPassName(FirstName,
1686 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001687 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001688 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001689 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001690 } else {
1691 for (auto &C : TopLevelPipelineParsingCallbacks)
1692 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1693 return true;
1694
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001695 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001696 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001697 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001698 }
1699
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001700 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001701}
Chandler Carruthedf59962016-02-18 09:45:17 +00001702
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001703// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1704bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1705 StringRef PipelineText, bool VerifyEachPass,
1706 bool DebugLogging) {
1707 auto Pipeline = parsePipelineText(PipelineText);
1708 if (!Pipeline || Pipeline->empty())
1709 return false;
1710
1711 StringRef FirstName = Pipeline->front().Name;
1712 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1713 return false;
1714
1715 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1716}
1717
1718// Primary pass pipeline description parsing routine for a \c
1719// FunctionPassManager
1720bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
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 (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1729 return false;
1730
1731 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1732 DebugLogging);
1733}
1734
1735// Primary pass pipeline description parsing routine for a \c LoopPassManager
1736bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1737 StringRef PipelineText, bool VerifyEachPass,
1738 bool DebugLogging) {
1739 auto Pipeline = parsePipelineText(PipelineText);
1740 if (!Pipeline || Pipeline->empty())
1741 return false;
1742
1743 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1744}
1745
Chandler Carruthedf59962016-02-18 09:45:17 +00001746bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001747 // If the pipeline just consists of the word 'default' just replace the AA
1748 // manager with our default one.
1749 if (PipelineText == "default") {
1750 AA = buildDefaultAAPipeline();
1751 return true;
1752 }
1753
Chandler Carruthedf59962016-02-18 09:45:17 +00001754 while (!PipelineText.empty()) {
1755 StringRef Name;
1756 std::tie(Name, PipelineText) = PipelineText.split(',');
1757 if (!parseAAPassName(AA, Name))
1758 return false;
1759 }
1760
1761 return true;
1762}