blob: f2dd950f22ef4e31349b02e2f0a333d2224b305e [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
25#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000026#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000027#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000028#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
29#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000030#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000031#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000032#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000033#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000034#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000035#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000036#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000037#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000038#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000039#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000040#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000041#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000042#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000043#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet79ac42a2016-07-18 16:29:21 +000044#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000063#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000066#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000067#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000068#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000069#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000070#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000071#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000072#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000073#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000074#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000075#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000076#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000077#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000078#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000079#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000080#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000081#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000082#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000083#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000084#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000085#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000086#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000087#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000088#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000089#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000090#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000091#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000092#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000093#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000094#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000095#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000096#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000097#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000098#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000099#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000100#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000101#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000102#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000103#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000104#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000105#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000106#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000107#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000108#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000109#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000110#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000111#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000112#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000113#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000114#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000115#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000116#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000117#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000118#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000119#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000120#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000121#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000122#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000123#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000124#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000125#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000126#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000127#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000128#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000129#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000130#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000131#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000132#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000133#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000134#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000135#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000136#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000137#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000138#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000139#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000140#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000141#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000142#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000143#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000144#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000145#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000146
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000147#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000148
149using namespace llvm;
150
Chandler Carruth719ffe12017-02-12 05:38:04 +0000151static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
152 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000153static cl::opt<bool>
154 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
155 cl::Hidden, cl::ZeroOrMore,
156 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000157
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000158static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000159 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000160 cl::Hidden, cl::ZeroOrMore,
161 cl::desc("Run NewGVN instead of GVN"));
162
Geoff Berry3cca1da2017-06-10 15:20:03 +0000163static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000164 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
165 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000166
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000167static cl::opt<bool> EnableGVNHoist(
168 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
169 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
170
Davide Italianobe1b6a92017-06-03 23:18:29 +0000171static cl::opt<bool> EnableGVNSink(
172 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
173 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
174
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000175static Regex DefaultAliasRegex(
176 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000177
Chandler Carruthe3f50642016-12-22 06:59:15 +0000178static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
179 switch (Level) {
180 case PassBuilder::O0:
181 case PassBuilder::O1:
182 case PassBuilder::O2:
183 case PassBuilder::O3:
184 return false;
185
186 case PassBuilder::Os:
187 case PassBuilder::Oz:
188 return true;
189 }
190 llvm_unreachable("Invalid optimization level!");
191}
192
Chandler Carruth66445382014-01-11 08:16:35 +0000193namespace {
194
Chandler Carruthd8330982014-01-12 09:34:22 +0000195/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000196struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000197 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000198 return PreservedAnalyses::all();
199 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000200 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000201};
202
Chandler Carruth0b576b32015-01-06 02:50:06 +0000203/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000204class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
205 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000206 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000207
208public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000209 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000210 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000211 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212};
213
Chandler Carruth572e3402014-04-21 11:12:00 +0000214/// \brief No-op CGSCC pass which does nothing.
215struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000216 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
217 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000218 return PreservedAnalyses::all();
219 }
220 static StringRef name() { return "NoOpCGSCCPass"; }
221};
222
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000224class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
225 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000226 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000227
228public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000229 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000230 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000231 return Result();
232 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000233 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234};
235
Chandler Carruthd8330982014-01-12 09:34:22 +0000236/// \brief No-op function pass which does nothing.
237struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000238 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000239 return PreservedAnalyses::all();
240 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000241 static StringRef name() { return "NoOpFunctionPass"; }
242};
243
Chandler Carruth0b576b32015-01-06 02:50:06 +0000244/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000245class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
246 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000247 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000248
249public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000250 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000251 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000252 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253};
254
Justin Bognereecc3c82016-02-25 07:23:08 +0000255/// \brief No-op loop pass which does nothing.
256struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000257 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
258 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000259 return PreservedAnalyses::all();
260 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000261 static StringRef name() { return "NoOpLoopPass"; }
262};
263
264/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000265class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
266 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000267 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000268
269public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000270 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000271 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
272 return Result();
273 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000274 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000275};
276
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000277AnalysisKey NoOpModuleAnalysis::Key;
278AnalysisKey NoOpCGSCCAnalysis::Key;
279AnalysisKey NoOpFunctionAnalysis::Key;
280AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000281
Chandler Carruth66445382014-01-11 08:16:35 +0000282} // End anonymous namespace.
283
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000284void PassBuilder::invokePeepholeEPCallbacks(
285 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
286 for (auto &C : PeepholeEPCallbacks)
287 C(FPM, Level);
288}
289
Chandler Carruth1ff77242015-03-07 09:02:36 +0000290void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000291#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000292 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000293#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000294
295 for (auto &C : ModuleAnalysisRegistrationCallbacks)
296 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000297}
298
Chandler Carruth1ff77242015-03-07 09:02:36 +0000299void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000300#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000301 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000302#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000303
304 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
305 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000306}
307
Chandler Carruth1ff77242015-03-07 09:02:36 +0000308void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000309#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000310 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000311#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000312
313 for (auto &C : FunctionAnalysisRegistrationCallbacks)
314 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000315}
316
Justin Bognereecc3c82016-02-25 07:23:08 +0000317void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000318#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000319 LAM.registerPass([&] { return CREATE_PASS; });
320#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000321
322 for (auto &C : LoopAnalysisRegistrationCallbacks)
323 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000324}
325
Chandler Carruthe3f50642016-12-22 06:59:15 +0000326FunctionPassManager
327PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000328 ThinLTOPhase Phase,
329 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000330 assert(Level != O0 && "Must request optimizations!");
331 FunctionPassManager FPM(DebugLogging);
332
333 // Form SSA out of local memory accesses after breaking apart aggregates into
334 // scalars.
335 FPM.addPass(SROA());
336
337 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000338 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000339
Davide Italianoc3688312017-06-01 23:08:14 +0000340 // Hoisting of scalars and load expressions.
341 if (EnableGVNHoist)
342 FPM.addPass(GVNHoistPass());
343
Davide Italianobe1b6a92017-06-03 23:18:29 +0000344 // Global value numbering based sinking.
345 if (EnableGVNSink) {
346 FPM.addPass(GVNSinkPass());
347 FPM.addPass(SimplifyCFGPass());
348 }
349
Chandler Carruthe3f50642016-12-22 06:59:15 +0000350 // Speculative execution if the target has divergent branches; otherwise nop.
351 FPM.addPass(SpeculativeExecutionPass());
352
353 // Optimize based on known information about branches, and cleanup afterward.
354 FPM.addPass(JumpThreadingPass());
355 FPM.addPass(CorrelatedValuePropagationPass());
356 FPM.addPass(SimplifyCFGPass());
357 FPM.addPass(InstCombinePass());
358
359 if (!isOptimizingForSize(Level))
360 FPM.addPass(LibCallsShrinkWrapPass());
361
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000362 invokePeepholeEPCallbacks(FPM, Level);
363
Chandler Carruthe3f50642016-12-22 06:59:15 +0000364 FPM.addPass(TailCallElimPass());
365 FPM.addPass(SimplifyCFGPass());
366
367 // Form canonically associated expression trees, and simplify the trees using
368 // basic mathematical properties. For example, this will form (nearly)
369 // minimal multiplication trees.
370 FPM.addPass(ReassociatePass());
371
372 // Add the primary loop simplification pipeline.
373 // FIXME: Currently this is split into two loop pass pipelines because we run
374 // some function passes in between them. These can and should be replaced by
375 // loop pass equivalenst but those aren't ready yet. Specifically,
376 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
377 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
378 // the other we have is `LoopInstSimplify`.
379 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
380
381 // Rotate Loop - disable header duplication at -Oz
382 LPM1.addPass(LoopRotatePass(Level != Oz));
383 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000384 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000385 LPM2.addPass(IndVarSimplifyPass());
386 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000387
388 for (auto &C : LateLoopOptimizationsEPCallbacks)
389 C(LPM2, Level);
390
Chandler Carruth79b733b2017-01-26 23:21:17 +0000391 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000392 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000393 // because it changes IR to makes profile annotation in back compile
394 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000395 if (Phase != ThinLTOPhase::PreLink ||
396 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000397 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000398
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000399 for (auto &C : LoopOptimizerEndEPCallbacks)
400 C(LPM2, Level);
401
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000402 // We provide the opt remark emitter pass for LICM to use. We only need to do
403 // this once as it is immutable.
404 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000405 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
406 FPM.addPass(SimplifyCFGPass());
407 FPM.addPass(InstCombinePass());
408 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
409
410 // Eliminate redundancies.
411 if (Level != O1) {
412 // These passes add substantial compile time so skip them at O1.
413 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000414 if (RunNewGVN)
415 FPM.addPass(NewGVNPass());
416 else
417 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000418 }
419
420 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
421 FPM.addPass(MemCpyOptPass());
422
423 // Sparse conditional constant propagation.
424 // FIXME: It isn't clear why we do this *after* loop passes rather than
425 // before...
426 FPM.addPass(SCCPPass());
427
428 // Delete dead bit computations (instcombine runs after to fold away the dead
429 // computations, and then ADCE will run later to exploit any new DCE
430 // opportunities that creates).
431 FPM.addPass(BDCEPass());
432
433 // Run instcombine after redundancy and dead bit elimination to exploit
434 // opportunities opened up by them.
435 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000436 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000437
438 // Re-consider control flow based optimizations after redundancy elimination,
439 // redo DCE, etc.
440 FPM.addPass(JumpThreadingPass());
441 FPM.addPass(CorrelatedValuePropagationPass());
442 FPM.addPass(DSEPass());
443 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
444
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000445 for (auto &C : ScalarOptimizerLateEPCallbacks)
446 C(FPM, Level);
447
Chandler Carruthe3f50642016-12-22 06:59:15 +0000448 // Finally, do an expensive DCE pass to catch all the dead code exposed by
449 // the simplifications and basic cleanup after all the simplifications.
450 FPM.addPass(ADCEPass());
451 FPM.addPass(SimplifyCFGPass());
452 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000453 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000454
455 return FPM;
456}
457
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000458void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
459 PassBuilder::OptimizationLevel Level,
460 bool RunProfileGen,
461 std::string ProfileGenFile,
462 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000463 // Generally running simplification passes and the inliner with an high
464 // threshold results in smaller executables, but there may be cases where
465 // the size grows, so let's be conservative here and skip this simplification
466 // at -Os/Oz.
467 if (!isOptimizingForSize(Level)) {
468 InlineParams IP;
469
470 // In the old pass manager, this is a cl::opt. Should still this be one?
471 IP.DefaultThreshold = 75;
472
473 // FIXME: The hint threshold has the same value used by the regular inliner.
474 // This should probably be lowered after performance testing.
475 // FIXME: this comment is cargo culted from the old pass manager, revisit).
476 IP.HintThreshold = 325;
477
478 CGSCCPassManager CGPipeline(DebugLogging);
479
480 CGPipeline.addPass(InlinerPass(IP));
481
482 FunctionPassManager FPM;
483 FPM.addPass(SROA());
484 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
485 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
486 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000487 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000488
Davide Italiano513dfaa2017-02-13 15:26:22 +0000489 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
490
491 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
492 }
493
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000494 // Delete anything that is now dead to make sure that we don't instrument
495 // dead code. Instrumentation can end up keeping dead code around and
496 // dramatically increase code size.
497 MPM.addPass(GlobalDCEPass());
498
Davide Italiano513dfaa2017-02-13 15:26:22 +0000499 if (RunProfileGen) {
500 MPM.addPass(PGOInstrumentationGen());
501
Xinliang David Lib67530e2017-06-25 00:26:43 +0000502 FunctionPassManager FPM;
503 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
504 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
505
Davide Italiano513dfaa2017-02-13 15:26:22 +0000506 // Add the profile lowering pass.
507 InstrProfOptions Options;
508 if (!ProfileGenFile.empty())
509 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000510 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000511 MPM.addPass(InstrProfiling(Options));
512 }
513
514 if (!ProfileUseFile.empty())
515 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
516}
517
Easwaran Raman8249fac2017-06-28 13:33:49 +0000518static InlineParams
519getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
520 auto O3 = PassBuilder::O3;
521 unsigned OptLevel = Level > O3 ? 2 : Level;
522 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
523 return getInlineParams(OptLevel, SizeLevel);
524}
525
Chandler Carruthe3f50642016-12-22 06:59:15 +0000526ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000527PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000528 ThinLTOPhase Phase,
529 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000530 ModulePassManager MPM(DebugLogging);
531
Chandler Carruthe3f50642016-12-22 06:59:15 +0000532 // Do basic inference of function attributes from known properties of system
533 // libraries and other oracles.
534 MPM.addPass(InferFunctionAttrsPass());
535
536 // Create an early function pass manager to cleanup the output of the
537 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000538 FunctionPassManager EarlyFPM(DebugLogging);
539 EarlyFPM.addPass(SimplifyCFGPass());
540 EarlyFPM.addPass(SROA());
541 EarlyFPM.addPass(EarlyCSEPass());
542 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000543 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000544
545 // Interprocedural constant propagation now that basic cleanup has occured
546 // and prior to optimizing globals.
547 // FIXME: This position in the pipeline hasn't been carefully considered in
548 // years, it should be re-analyzed.
549 MPM.addPass(IPSCCPPass());
550
551 // Optimize globals to try and fold them into constants.
552 MPM.addPass(GlobalOptPass());
553
554 // Promote any localized globals to SSA registers.
555 // FIXME: Should this instead by a run of SROA?
556 // FIXME: We should probably run instcombine and simplify-cfg afterward to
557 // delete control flows that are dead once globals have been folded to
558 // constants.
559 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
560
561 // Remove any dead arguments exposed by cleanups and constand folding
562 // globals.
563 MPM.addPass(DeadArgumentEliminationPass());
564
565 // Create a small function pass pipeline to cleanup after all the global
566 // optimizations.
567 FunctionPassManager GlobalCleanupPM(DebugLogging);
568 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000569 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
570
Chandler Carruthe3f50642016-12-22 06:59:15 +0000571 GlobalCleanupPM.addPass(SimplifyCFGPass());
572 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
573
Dehao Chen89d32262017-08-02 01:28:31 +0000574 // Add all the requested passes for instrumentation PGO, if requested.
575 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
576 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
577 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
578 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
579 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000580 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000581
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000582 // Require the GlobalsAA analysis for the module so we can query it within
583 // the CGSCC pipeline.
584 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000585
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000586 // Require the ProfileSummaryAnalysis for the module so we can query it within
587 // the inliner pass.
588 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
589
Chandler Carruthe3f50642016-12-22 06:59:15 +0000590 // Now begin the main postorder CGSCC pipeline.
591 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
592 // manager and trying to emulate its precise behavior. Much of this doesn't
593 // make a lot of sense and we should revisit the core CGSCC structure.
594 CGSCCPassManager MainCGPipeline(DebugLogging);
595
596 // Note: historically, the PruneEH pass was run first to deduce nounwind and
597 // generally clean up exception handling overhead. It isn't clear this is
598 // valuable as the inliner doesn't currently care whether it is inlining an
599 // invoke or a call.
600
601 // Run the inliner first. The theory is that we are walking bottom-up and so
602 // the callees have already been fully optimized, and we want to inline them
603 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000604 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000605 // because it makes profile annotation in the backend inaccurate.
606 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000607 if (Phase == ThinLTOPhase::PreLink &&
608 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000609 IP.HotCallSiteThreshold = 0;
610 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000611
612 // Now deduce any function attributes based in the current code.
613 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
614
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000615 // When at O3 add argument promotion to the pass pipeline.
616 // FIXME: It isn't at all clear why this should be limited to O3.
617 if (Level == O3)
618 MainCGPipeline.addPass(ArgumentPromotionPass());
619
Chandler Carruthe3f50642016-12-22 06:59:15 +0000620 // Lastly, add the core function simplification pipeline nested inside the
621 // CGSCC walk.
622 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000623 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000624
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000625 for (auto &C : CGSCCOptimizerLateEPCallbacks)
626 C(MainCGPipeline, Level);
627
Chandler Carruth719ffe12017-02-12 05:38:04 +0000628 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
629 // to detect when we devirtualize indirect calls and iterate the SCC passes
630 // in that case to try and catch knock-on inlining or function attrs
631 // opportunities. Then we add it to the module pipeline by walking the SCCs
632 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000633 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000634 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
635 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000636
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000637 return MPM;
638}
639
640ModulePassManager
641PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
642 bool DebugLogging) {
643 ModulePassManager MPM(DebugLogging);
644
645 // Optimize globals now that the module is fully simplified.
646 MPM.addPass(GlobalOptPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000647
Xinliang David Li126157c2017-05-22 16:41:57 +0000648 // Run partial inlining pass to partially inline functions that have
649 // large bodies.
650 if (RunPartialInlining)
651 MPM.addPass(PartialInlinerPass());
652
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000653 // Remove avail extern fns and globals definitions since we aren't compiling
654 // an object file for later LTO. For LTO we want to preserve these so they
655 // are eligible for inlining at link-time. Note if they are unreferenced they
656 // will be removed by GlobalDCE later, so this only impacts referenced
657 // available externally globals. Eventually they will be suppressed during
658 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
659 // may make globals referenced by available external functions dead and saves
660 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000661 MPM.addPass(EliminateAvailableExternallyPass());
662
663 // Do RPO function attribute inference across the module to forward-propagate
664 // attributes where applicable.
665 // FIXME: Is this really an optimization rather than a canonicalization?
666 MPM.addPass(ReversePostOrderFunctionAttrsPass());
667
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000668 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000669 // useful as the above will have inlined, DCE'ed, and function-attr
670 // propagated everything. We should at this point have a reasonably minimal
671 // and richly annotated call graph. By computing aliasing and mod/ref
672 // information for all local globals here, the late loop passes and notably
673 // the vectorizer will be able to use them to help recognize vectorizable
674 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000675 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676
677 FunctionPassManager OptimizePM(DebugLogging);
678 OptimizePM.addPass(Float2IntPass());
679 // FIXME: We need to run some loop optimizations to re-rotate loops after
680 // simplify-cfg and others undo their rotation.
681
682 // Optimize the loop execution. These passes operate on entire loop nests
683 // rather than on each loop in an inside-out manner, and so they are actually
684 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000685
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000686 for (auto &C : VectorizerStartEPCallbacks)
687 C(OptimizePM, Level);
688
Chandler Carrutha95ff382017-01-27 00:50:21 +0000689 // First rotate loops that may have been un-rotated by prior passes.
690 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
691
692 // Distribute loops to allow partial vectorization. I.e. isolate dependences
693 // into separate loop that would otherwise inhibit vectorization. This is
694 // currently only performed for loops marked with the metadata
695 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000696 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000697
698 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000699 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000700
Chandler Carruthbaabda92017-01-27 01:32:26 +0000701 // Eliminate loads by forwarding stores from the previous iteration to loads
702 // of the current iteration.
703 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000704
705 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000706 OptimizePM.addPass(InstCombinePass());
707
Chandler Carrutha95ff382017-01-27 00:50:21 +0000708
709 // Now that we've formed fast to execute loop structures, we do further
710 // optimizations. These are run afterward as they might block doing complex
711 // analyses and transforms such as what are needed for loop vectorization.
712
Chandler Carruthe3f50642016-12-22 06:59:15 +0000713 // Optimize parallel scalar instruction chains into SIMD instructions.
714 OptimizePM.addPass(SLPVectorizerPass());
715
Chandler Carrutha95ff382017-01-27 00:50:21 +0000716 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000717 OptimizePM.addPass(SimplifyCFGPass());
718 OptimizePM.addPass(InstCombinePass());
719
720 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000721 // execution resources of an out-of-order processor. We also then need to
722 // clean up redundancies and loop invariant code.
723 // FIXME: It would be really good to use a loop-integrated instruction
724 // combiner for cleanup here so that the unrolling and LICM can be pipelined
725 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000726 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000727 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000728 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000729 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000730
731 // Now that we've vectorized and unrolled loops, we may have more refined
732 // alignment information, try to re-derive it here.
733 OptimizePM.addPass(AlignmentFromAssumptionsPass());
734
Chandler Carrutha95ff382017-01-27 00:50:21 +0000735 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
736 // canonicalization pass that enables other optimizations. As a result,
737 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
738 // result too early.
739 OptimizePM.addPass(LoopSinkPass());
740
741 // And finally clean up LCSSA form before generating code.
742 OptimizePM.addPass(InstSimplifierPass());
743
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000744 // LoopSink (and other loop passes since the last simplifyCFG) might have
745 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
746 OptimizePM.addPass(SimplifyCFGPass());
747
Chandler Carrutha95ff382017-01-27 00:50:21 +0000748 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000749 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
750
751 // Now we need to do some global optimization transforms.
752 // FIXME: It would seem like these should come first in the optimization
753 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
754 // ordering here.
755 MPM.addPass(GlobalDCEPass());
756 MPM.addPass(ConstantMergePass());
757
758 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000759}
760
Chandler Carruthe3f50642016-12-22 06:59:15 +0000761ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000762PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
763 bool DebugLogging) {
764 assert(Level != O0 && "Must request optimizations for the default pipeline!");
765
766 ModulePassManager MPM(DebugLogging);
767
768 // Force any function attributes we want the rest of the pipeline to observe.
769 MPM.addPass(ForceFunctionAttrsPass());
770
Dehao Chen89d32262017-08-02 01:28:31 +0000771 if (PGOOpt && PGOOpt->SamplePGOSupport) {
Dehao Chence0842c2017-07-29 04:10:24 +0000772 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
Dehao Chen89d32262017-08-02 01:28:31 +0000773 if (!PGOOpt->SampleProfileFile.empty()) {
774 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile));
775 MPM.addPass(PGOIndirectCallPromotion(false, true));
776 }
777 }
Dehao Chence0842c2017-07-29 04:10:24 +0000778
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000779 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000780 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
781 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000782
783 // Now add the optimization pipeline.
784 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
785
786 return MPM;
787}
788
789ModulePassManager
790PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
791 bool DebugLogging) {
792 assert(Level != O0 && "Must request optimizations for the default pipeline!");
793
794 ModulePassManager MPM(DebugLogging);
795
796 // Force any function attributes we want the rest of the pipeline to observe.
797 MPM.addPass(ForceFunctionAttrsPass());
798
Dehao Chen89d32262017-08-02 01:28:31 +0000799 // Invoke the SamplePGO annotation pass for the first time to annotate
800 // profile for functions in the current module to give ThinLink info
801 // about module grouping.
802 if (PGOOpt && PGOOpt->SamplePGOSupport) {
Dehao Chence0842c2017-07-29 04:10:24 +0000803 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
Dehao Chen89d32262017-08-02 01:28:31 +0000804 if (!PGOOpt->SampleProfileFile.empty())
805 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile));
806 }
Dehao Chence0842c2017-07-29 04:10:24 +0000807
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000808 // If we are planning to perform ThinLTO later, we don't bloat the code with
809 // unrolling/vectorization/... now. Just simplify the module as much as we
810 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000811 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
812 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000813
814 // Run partial inlining pass to partially inline functions that have
815 // large bodies.
816 // FIXME: It isn't clear whether this is really the right place to run this
817 // in ThinLTO. Because there is another canonicalization and simplification
818 // phase that will run after the thin link, running this here ends up with
819 // less information than will be available later and it may grow functions in
820 // ways that aren't beneficial.
821 if (RunPartialInlining)
822 MPM.addPass(PartialInlinerPass());
823
824 // Reduce the size of the IR as much as possible.
825 MPM.addPass(GlobalOptPass());
826
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000827 return MPM;
828}
829
830ModulePassManager
831PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
832 bool DebugLogging) {
833 // FIXME: The summary index is not hooked in the new pass manager yet.
834 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
835 // here.
836
837 ModulePassManager MPM(DebugLogging);
838
839 // Force any function attributes we want the rest of the pipeline to observe.
840 MPM.addPass(ForceFunctionAttrsPass());
841
Dehao Chen89d32262017-08-02 01:28:31 +0000842 // Invoke the SamplePGO annotation pass for the second time to annotate on
843 // functions imported from other modules.
844 if (PGOOpt && !PGOOpt->SampleProfileFile.empty())
845 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile));
846
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000847 // During the ThinLTO backend phase we perform early indirect call promotion
848 // here, before globalopt. Otherwise imported available_externally functions
849 // look unreferenced and are removed.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000850 MPM.addPass(PGOIndirectCallPromotion(
Dehao Chen89d32262017-08-02 01:28:31 +0000851 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000852
853 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000854 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
855 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000856
857 // Now add the optimization pipeline.
858 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
859
860 return MPM;
861}
862
863ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000864PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
865 bool DebugLogging) {
866 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000867 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000868 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000869}
870
Chandler Carruthe3f50642016-12-22 06:59:15 +0000871ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
872 bool DebugLogging) {
873 assert(Level != O0 && "Must request optimizations for the default pipeline!");
874 ModulePassManager MPM(DebugLogging);
875
Davide Italiano089a9122017-01-24 00:57:39 +0000876 // Remove unused virtual tables to improve the quality of code generated by
877 // whole-program devirtualization and bitset lowering.
878 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000879
Davide Italiano089a9122017-01-24 00:57:39 +0000880 // Force any function attributes we want the rest of the pipeline to observe.
881 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000882
Davide Italiano089a9122017-01-24 00:57:39 +0000883 // Do basic inference of function attributes from known properties of system
884 // libraries and other oracles.
885 MPM.addPass(InferFunctionAttrsPass());
886
887 if (Level > 1) {
888 // Indirect call promotion. This should promote all the targets that are
889 // left by the earlier promotion pass that promotes intra-module targets.
890 // This two-step promotion is to save the compile time. For LTO, it should
891 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000892 MPM.addPass(PGOIndirectCallPromotion(
893 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000894
895 // Propagate constants at call sites into the functions they call. This
896 // opens opportunities for globalopt (and inlining) by substituting function
897 // pointers passed as arguments to direct uses of functions.
898 MPM.addPass(IPSCCPPass());
899 }
900
901 // Now deduce any function attributes based in the current code.
902 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
903 PostOrderFunctionAttrsPass()));
904
905 // Do RPO function attribute inference across the module to forward-propagate
906 // attributes where applicable.
907 // FIXME: Is this really an optimization rather than a canonicalization?
908 MPM.addPass(ReversePostOrderFunctionAttrsPass());
909
910 // Use inragne annotations on GEP indices to split globals where beneficial.
911 MPM.addPass(GlobalSplitPass());
912
913 // Run whole program optimization of virtual call when the list of callees
914 // is fixed.
915 MPM.addPass(WholeProgramDevirtPass());
916
917 // Stop here at -O1.
918 if (Level == 1)
919 return MPM;
920
921 // Optimize globals to try and fold them into constants.
922 MPM.addPass(GlobalOptPass());
923
924 // Promote any localized globals to SSA registers.
925 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
926
927 // Linking modules together can lead to duplicate global constant, only
928 // keep one copy of each constant.
929 MPM.addPass(ConstantMergePass());
930
931 // Remove unused arguments from functions.
932 MPM.addPass(DeadArgumentEliminationPass());
933
934 // Reduce the code after globalopt and ipsccp. Both can open up significant
935 // simplification opportunities, and both can propagate functions through
936 // function pointers. When this happens, we often have to resolve varargs
937 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000938 FunctionPassManager PeepholeFPM(DebugLogging);
939 PeepholeFPM.addPass(InstCombinePass());
940 invokePeepholeEPCallbacks(PeepholeFPM, Level);
941
942 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000943
944 // Note: historically, the PruneEH pass was run first to deduce nounwind and
945 // generally clean up exception handling overhead. It isn't clear this is
946 // valuable as the inliner doesn't currently care whether it is inlining an
947 // invoke or a call.
948 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000949 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
950 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000951
952 // Optimize globals again after we ran the inliner.
953 MPM.addPass(GlobalOptPass());
954
955 // Garbage collect dead functions.
956 // FIXME: Add ArgumentPromotion pass after once it's ported.
957 MPM.addPass(GlobalDCEPass());
958
959 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +0000960 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +0000961 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000962 invokePeepholeEPCallbacks(FPM, Level);
963
Davide Italiano089a9122017-01-24 00:57:39 +0000964 FPM.addPass(JumpThreadingPass());
965
966 // Break up allocas
967 FPM.addPass(SROA());
968
969 // Run a few AA driver optimizations here and now to cleanup the code.
970 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
971
972 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
973 PostOrderFunctionAttrsPass()));
974 // FIXME: here we run IP alias analysis in the legacy PM.
975
976 FunctionPassManager MainFPM;
977
978 // FIXME: once we fix LoopPass Manager, add LICM here.
979 // FIXME: once we provide support for enabling MLSM, add it here.
980 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000981 if (RunNewGVN)
982 MainFPM.addPass(NewGVNPass());
983 else
984 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +0000985
986 // Remove dead memcpy()'s.
987 MainFPM.addPass(MemCpyOptPass());
988
989 // Nuke dead stores.
990 MainFPM.addPass(DSEPass());
991
992 // FIXME: at this point, we run a bunch of loop passes:
993 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
994 // loopVectorize. Enable them once the remaining issue with LPM
995 // are sorted out.
996
997 MainFPM.addPass(InstCombinePass());
998 MainFPM.addPass(SimplifyCFGPass());
999 MainFPM.addPass(SCCPPass());
1000 MainFPM.addPass(InstCombinePass());
1001 MainFPM.addPass(BDCEPass());
1002
1003 // FIXME: We may want to run SLPVectorizer here.
1004 // After vectorization, assume intrinsics may tell us more
1005 // about pointer alignments.
1006#if 0
1007 MainFPM.add(AlignmentFromAssumptionsPass());
1008#endif
1009
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001010 // FIXME: Conditionally run LoadCombine here, after it's ported
1011 // (in case we still have this pass, given its questionable usefulness).
1012
Davide Italiano089a9122017-01-24 00:57:39 +00001013 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001014 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001015 MainFPM.addPass(JumpThreadingPass());
1016 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1017
1018 // Create a function that performs CFI checks for cross-DSO calls with
1019 // targets in the current module.
1020 MPM.addPass(CrossDSOCFIPass());
1021
1022 // Lower type metadata and the type.test intrinsic. This pass supports
1023 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1024 // to be run at link time if CFI is enabled. This pass does nothing if
1025 // CFI is disabled.
1026 // Enable once we add support for the summary in the new PM.
1027#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001028 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1029 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001030 Summary));
1031#endif
1032
1033 // Add late LTO optimization passes.
1034 // Delete basic blocks, which optimization passes may have killed.
1035 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1036
1037 // Drop bodies of available eternally objects to improve GlobalDCE.
1038 MPM.addPass(EliminateAvailableExternallyPass());
1039
1040 // Now that we have optimized the program, discard unreachable functions.
1041 MPM.addPass(GlobalDCEPass());
1042
1043 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001044 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001045}
1046
Chandler Carruth060ad612016-12-23 20:38:19 +00001047AAManager PassBuilder::buildDefaultAAPipeline() {
1048 AAManager AA;
1049
1050 // The order in which these are registered determines their priority when
1051 // being queried.
1052
1053 // First we register the basic alias analysis that provides the majority of
1054 // per-function local AA logic. This is a stateless, on-demand local set of
1055 // AA techniques.
1056 AA.registerFunctionAnalysis<BasicAA>();
1057
1058 // Next we query fast, specialized alias analyses that wrap IR-embedded
1059 // information about aliasing.
1060 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1061 AA.registerFunctionAnalysis<TypeBasedAA>();
1062
1063 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001064 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1065 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001066 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001067 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001068
1069 return AA;
1070}
1071
Chandler Carruth241bf242016-08-03 07:44:48 +00001072static Optional<int> parseRepeatPassName(StringRef Name) {
1073 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1074 return None;
1075 int Count;
1076 if (Name.getAsInteger(0, Count) || Count <= 0)
1077 return None;
1078 return Count;
1079}
1080
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001081static Optional<int> parseDevirtPassName(StringRef Name) {
1082 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1083 return None;
1084 int Count;
1085 if (Name.getAsInteger(0, Count) || Count <= 0)
1086 return None;
1087 return Count;
1088}
1089
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001090/// Tests whether a pass name starts with a valid prefix for a default pipeline
1091/// alias.
1092static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1093 return Name.startswith("default") || Name.startswith("thinlto") ||
1094 Name.startswith("lto");
1095}
1096
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001097/// Tests whether registered callbacks will accept a given pass name.
1098///
1099/// When parsing a pipeline text, the type of the outermost pipeline may be
1100/// omitted, in which case the type is automatically determined from the first
1101/// pass name in the text. This may be a name that is handled through one of the
1102/// callbacks. We check this through the oridinary parsing callbacks by setting
1103/// up a dummy PassManager in order to not force the client to also handle this
1104/// type of query.
1105template <typename PassManagerT, typename CallbacksT>
1106static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1107 if (!Callbacks.empty()) {
1108 PassManagerT DummyPM;
1109 for (auto &CB : Callbacks)
1110 if (CB(Name, DummyPM, {}))
1111 return true;
1112 }
1113 return false;
1114}
1115
1116template <typename CallbacksT>
1117static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001118 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001119 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001120 return DefaultAliasRegex.match(Name);
1121
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001122 // Explicitly handle pass manager names.
1123 if (Name == "module")
1124 return true;
1125 if (Name == "cgscc")
1126 return true;
1127 if (Name == "function")
1128 return true;
1129
Chandler Carruth241bf242016-08-03 07:44:48 +00001130 // Explicitly handle custom-parsed pass names.
1131 if (parseRepeatPassName(Name))
1132 return true;
1133
Chandler Carruth74a8a222016-06-17 07:15:29 +00001134#define MODULE_PASS(NAME, CREATE_PASS) \
1135 if (Name == NAME) \
1136 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001137#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001138 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001139 return true;
1140#include "PassRegistry.def"
1141
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001142 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001143}
1144
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001145template <typename CallbacksT>
1146static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001147 // Explicitly handle pass manager names.
1148 if (Name == "cgscc")
1149 return true;
1150 if (Name == "function")
1151 return true;
1152
Chandler Carruth241bf242016-08-03 07:44:48 +00001153 // Explicitly handle custom-parsed pass names.
1154 if (parseRepeatPassName(Name))
1155 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001156 if (parseDevirtPassName(Name))
1157 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001158
Chandler Carruth74a8a222016-06-17 07:15:29 +00001159#define CGSCC_PASS(NAME, CREATE_PASS) \
1160 if (Name == NAME) \
1161 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001162#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001163 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001164 return true;
1165#include "PassRegistry.def"
1166
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001167 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001168}
1169
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001170template <typename CallbacksT>
1171static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001172 // Explicitly handle pass manager names.
1173 if (Name == "function")
1174 return true;
1175 if (Name == "loop")
1176 return true;
1177
Chandler Carruth241bf242016-08-03 07:44:48 +00001178 // Explicitly handle custom-parsed pass names.
1179 if (parseRepeatPassName(Name))
1180 return true;
1181
Chandler Carruth74a8a222016-06-17 07:15:29 +00001182#define FUNCTION_PASS(NAME, CREATE_PASS) \
1183 if (Name == NAME) \
1184 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001185#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001186 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001187 return true;
1188#include "PassRegistry.def"
1189
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001190 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001191}
1192
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001193template <typename CallbacksT>
1194static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001195 // Explicitly handle pass manager names.
1196 if (Name == "loop")
1197 return true;
1198
Chandler Carruth241bf242016-08-03 07:44:48 +00001199 // Explicitly handle custom-parsed pass names.
1200 if (parseRepeatPassName(Name))
1201 return true;
1202
Chandler Carruth74a8a222016-06-17 07:15:29 +00001203#define LOOP_PASS(NAME, CREATE_PASS) \
1204 if (Name == NAME) \
1205 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001206#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1207 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1208 return true;
1209#include "PassRegistry.def"
1210
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001211 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001212}
1213
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001214Optional<std::vector<PassBuilder::PipelineElement>>
1215PassBuilder::parsePipelineText(StringRef Text) {
1216 std::vector<PipelineElement> ResultPipeline;
1217
1218 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1219 &ResultPipeline};
1220 for (;;) {
1221 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1222 size_t Pos = Text.find_first_of(",()");
1223 Pipeline.push_back({Text.substr(0, Pos), {}});
1224
1225 // If we have a single terminating name, we're done.
1226 if (Pos == Text.npos)
1227 break;
1228
1229 char Sep = Text[Pos];
1230 Text = Text.substr(Pos + 1);
1231 if (Sep == ',')
1232 // Just a name ending in a comma, continue.
1233 continue;
1234
1235 if (Sep == '(') {
1236 // Push the inner pipeline onto the stack to continue processing.
1237 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1238 continue;
1239 }
1240
1241 assert(Sep == ')' && "Bogus separator!");
1242 // When handling the close parenthesis, we greedily consume them to avoid
1243 // empty strings in the pipeline.
1244 do {
1245 // If we try to pop the outer pipeline we have unbalanced parentheses.
1246 if (PipelineStack.size() == 1)
1247 return None;
1248
1249 PipelineStack.pop_back();
1250 } while (Text.consume_front(")"));
1251
1252 // Check if we've finished parsing.
1253 if (Text.empty())
1254 break;
1255
1256 // Otherwise, the end of an inner pipeline always has to be followed by
1257 // a comma, and then we can continue.
1258 if (!Text.consume_front(","))
1259 return None;
1260 }
1261
1262 if (PipelineStack.size() > 1)
1263 // Unbalanced paretheses.
1264 return None;
1265
1266 assert(PipelineStack.back() == &ResultPipeline &&
1267 "Wrong pipeline at the bottom of the stack!");
1268 return {std::move(ResultPipeline)};
1269}
1270
1271bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1272 const PipelineElement &E, bool VerifyEachPass,
1273 bool DebugLogging) {
1274 auto &Name = E.Name;
1275 auto &InnerPipeline = E.InnerPipeline;
1276
1277 // First handle complex passes like the pass managers which carry pipelines.
1278 if (!InnerPipeline.empty()) {
1279 if (Name == "module") {
1280 ModulePassManager NestedMPM(DebugLogging);
1281 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1282 DebugLogging))
1283 return false;
1284 MPM.addPass(std::move(NestedMPM));
1285 return true;
1286 }
1287 if (Name == "cgscc") {
1288 CGSCCPassManager CGPM(DebugLogging);
1289 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1290 DebugLogging))
1291 return false;
1292 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1293 DebugLogging));
1294 return true;
1295 }
1296 if (Name == "function") {
1297 FunctionPassManager FPM(DebugLogging);
1298 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1299 DebugLogging))
1300 return false;
1301 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1302 return true;
1303 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001304 if (auto Count = parseRepeatPassName(Name)) {
1305 ModulePassManager NestedMPM(DebugLogging);
1306 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1307 DebugLogging))
1308 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001309 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001310 return true;
1311 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001312
1313 for (auto &C : ModulePipelineParsingCallbacks)
1314 if (C(Name, MPM, InnerPipeline))
1315 return true;
1316
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001317 // Normal passes can't have pipelines.
1318 return false;
1319 }
1320
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001321 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001322 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001323 SmallVector<StringRef, 3> Matches;
1324 if (!DefaultAliasRegex.match(Name, &Matches))
1325 return false;
1326 assert(Matches.size() == 3 && "Must capture two matched strings!");
1327
Jordan Rosef85a95f2016-07-25 18:34:51 +00001328 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001329 .Case("O0", O0)
1330 .Case("O1", O1)
1331 .Case("O2", O2)
1332 .Case("O3", O3)
1333 .Case("Os", Os)
1334 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001335 if (L == O0)
1336 // At O0 we do nothing at all!
1337 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001338
1339 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001340 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001341 } else if (Matches[1] == "thinlto-pre-link") {
1342 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1343 } else if (Matches[1] == "thinlto") {
1344 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001345 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001346 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001347 } else {
1348 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001349 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001350 }
1351 return true;
1352 }
1353
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001354 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001355#define MODULE_PASS(NAME, CREATE_PASS) \
1356 if (Name == NAME) { \
1357 MPM.addPass(CREATE_PASS); \
1358 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001359 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001360#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1361 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001362 MPM.addPass( \
1363 RequireAnalysisPass< \
1364 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001365 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001366 } \
1367 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001368 MPM.addPass(InvalidateAnalysisPass< \
1369 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001370 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001371 }
1372#include "PassRegistry.def"
1373
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001374 for (auto &C : ModulePipelineParsingCallbacks)
1375 if (C(Name, MPM, InnerPipeline))
1376 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001377 return false;
1378}
1379
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001380bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1381 const PipelineElement &E, bool VerifyEachPass,
1382 bool DebugLogging) {
1383 auto &Name = E.Name;
1384 auto &InnerPipeline = E.InnerPipeline;
1385
1386 // First handle complex passes like the pass managers which carry pipelines.
1387 if (!InnerPipeline.empty()) {
1388 if (Name == "cgscc") {
1389 CGSCCPassManager NestedCGPM(DebugLogging);
1390 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1391 DebugLogging))
1392 return false;
1393 // Add the nested pass manager with the appropriate adaptor.
1394 CGPM.addPass(std::move(NestedCGPM));
1395 return true;
1396 }
1397 if (Name == "function") {
1398 FunctionPassManager FPM(DebugLogging);
1399 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1400 DebugLogging))
1401 return false;
1402 // Add the nested pass manager with the appropriate adaptor.
1403 CGPM.addPass(
1404 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1405 return true;
1406 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001407 if (auto Count = parseRepeatPassName(Name)) {
1408 CGSCCPassManager NestedCGPM(DebugLogging);
1409 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1410 DebugLogging))
1411 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001412 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001413 return true;
1414 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001415 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1416 CGSCCPassManager NestedCGPM(DebugLogging);
1417 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1418 DebugLogging))
1419 return false;
1420 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1421 *MaxRepetitions, DebugLogging));
1422 return true;
1423 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001424
1425 for (auto &C : CGSCCPipelineParsingCallbacks)
1426 if (C(Name, CGPM, InnerPipeline))
1427 return true;
1428
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001429 // Normal passes can't have pipelines.
1430 return false;
1431 }
1432
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001433// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001434#define CGSCC_PASS(NAME, CREATE_PASS) \
1435 if (Name == NAME) { \
1436 CGPM.addPass(CREATE_PASS); \
1437 return true; \
1438 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001439#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1440 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001441 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001442 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001443 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1444 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001445 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001446 } \
1447 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001448 CGPM.addPass(InvalidateAnalysisPass< \
1449 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001450 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001451 }
1452#include "PassRegistry.def"
1453
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001454 for (auto &C : CGSCCPipelineParsingCallbacks)
1455 if (C(Name, CGPM, InnerPipeline))
1456 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001457 return false;
1458}
1459
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001460bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1461 const PipelineElement &E,
1462 bool VerifyEachPass, bool DebugLogging) {
1463 auto &Name = E.Name;
1464 auto &InnerPipeline = E.InnerPipeline;
1465
1466 // First handle complex passes like the pass managers which carry pipelines.
1467 if (!InnerPipeline.empty()) {
1468 if (Name == "function") {
1469 FunctionPassManager NestedFPM(DebugLogging);
1470 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1471 DebugLogging))
1472 return false;
1473 // Add the nested pass manager with the appropriate adaptor.
1474 FPM.addPass(std::move(NestedFPM));
1475 return true;
1476 }
1477 if (Name == "loop") {
1478 LoopPassManager LPM(DebugLogging);
1479 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1480 DebugLogging))
1481 return false;
1482 // Add the nested pass manager with the appropriate adaptor.
1483 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1484 return true;
1485 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001486 if (auto Count = parseRepeatPassName(Name)) {
1487 FunctionPassManager NestedFPM(DebugLogging);
1488 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1489 DebugLogging))
1490 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001491 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001492 return true;
1493 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001494
1495 for (auto &C : FunctionPipelineParsingCallbacks)
1496 if (C(Name, FPM, InnerPipeline))
1497 return true;
1498
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001499 // Normal passes can't have pipelines.
1500 return false;
1501 }
1502
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001503// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001504#define FUNCTION_PASS(NAME, CREATE_PASS) \
1505 if (Name == NAME) { \
1506 FPM.addPass(CREATE_PASS); \
1507 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001508 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001509#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1510 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001511 FPM.addPass( \
1512 RequireAnalysisPass< \
1513 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001514 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001515 } \
1516 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001517 FPM.addPass(InvalidateAnalysisPass< \
1518 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001519 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001520 }
1521#include "PassRegistry.def"
1522
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001523 for (auto &C : FunctionPipelineParsingCallbacks)
1524 if (C(Name, FPM, InnerPipeline))
1525 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001526 return false;
1527}
1528
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001529bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001530 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001531 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001532 auto &InnerPipeline = E.InnerPipeline;
1533
1534 // First handle complex passes like the pass managers which carry pipelines.
1535 if (!InnerPipeline.empty()) {
1536 if (Name == "loop") {
1537 LoopPassManager NestedLPM(DebugLogging);
1538 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1539 DebugLogging))
1540 return false;
1541 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001542 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001543 return true;
1544 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001545 if (auto Count = parseRepeatPassName(Name)) {
1546 LoopPassManager NestedLPM(DebugLogging);
1547 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1548 DebugLogging))
1549 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001550 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001551 return true;
1552 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001553
1554 for (auto &C : LoopPipelineParsingCallbacks)
1555 if (C(Name, LPM, InnerPipeline))
1556 return true;
1557
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001558 // Normal passes can't have pipelines.
1559 return false;
1560 }
1561
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001562// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001563#define LOOP_PASS(NAME, CREATE_PASS) \
1564 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001565 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001566 return true; \
1567 }
1568#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1569 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001570 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001571 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1572 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1573 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001574 return true; \
1575 } \
1576 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001577 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001578 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001579 return true; \
1580 }
1581#include "PassRegistry.def"
1582
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001583 for (auto &C : LoopPipelineParsingCallbacks)
1584 if (C(Name, LPM, InnerPipeline))
1585 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001586 return false;
1587}
1588
Chandler Carruthedf59962016-02-18 09:45:17 +00001589bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001590#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1591 if (Name == NAME) { \
1592 AA.registerModuleAnalysis< \
1593 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1594 return true; \
1595 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001596#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1597 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001598 AA.registerFunctionAnalysis< \
1599 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001600 return true; \
1601 }
1602#include "PassRegistry.def"
1603
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001604 for (auto &C : AAParsingCallbacks)
1605 if (C(Name, AA))
1606 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001607 return false;
1608}
1609
Justin Bognereecc3c82016-02-25 07:23:08 +00001610bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001611 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001612 bool VerifyEachPass,
1613 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001614 for (const auto &Element : Pipeline) {
1615 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1616 return false;
1617 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001618 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001619 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001620}
1621
Chandler Carruth1ff77242015-03-07 09:02:36 +00001622bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001623 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001624 bool VerifyEachPass,
1625 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001626 for (const auto &Element : Pipeline) {
1627 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1628 return false;
1629 if (VerifyEachPass)
1630 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001631 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001632 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001633}
1634
Chandler Carruth1ff77242015-03-07 09:02:36 +00001635bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001636 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001637 bool VerifyEachPass,
1638 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001639 for (const auto &Element : Pipeline) {
1640 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1641 return false;
1642 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001643 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001644 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001645}
1646
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001647void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1648 FunctionAnalysisManager &FAM,
1649 CGSCCAnalysisManager &CGAM,
1650 ModuleAnalysisManager &MAM) {
1651 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1652 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001653 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1654 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1655 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1656 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1657 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1658}
1659
Chandler Carruth1ff77242015-03-07 09:02:36 +00001660bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001661 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001662 bool VerifyEachPass,
1663 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001664 for (const auto &Element : Pipeline) {
1665 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1666 return false;
1667 if (VerifyEachPass)
1668 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001669 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001670 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001671}
1672
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001673// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001674// FIXME: Should this routine accept a TargetMachine or require the caller to
1675// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001676bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1677 StringRef PipelineText, bool VerifyEachPass,
1678 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001679 auto Pipeline = parsePipelineText(PipelineText);
1680 if (!Pipeline || Pipeline->empty())
1681 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001682
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001683 // If the first name isn't at the module layer, wrap the pipeline up
1684 // automatically.
1685 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001686
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001687 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1688 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001689 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001690 } else if (isFunctionPassName(FirstName,
1691 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001692 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001693 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001694 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001695 } else {
1696 for (auto &C : TopLevelPipelineParsingCallbacks)
1697 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1698 return true;
1699
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001700 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001701 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001702 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001703 }
1704
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001706}
Chandler Carruthedf59962016-02-18 09:45:17 +00001707
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001708// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1709bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1710 StringRef PipelineText, bool VerifyEachPass,
1711 bool DebugLogging) {
1712 auto Pipeline = parsePipelineText(PipelineText);
1713 if (!Pipeline || Pipeline->empty())
1714 return false;
1715
1716 StringRef FirstName = Pipeline->front().Name;
1717 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1718 return false;
1719
1720 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1721}
1722
1723// Primary pass pipeline description parsing routine for a \c
1724// FunctionPassManager
1725bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1726 StringRef PipelineText, bool VerifyEachPass,
1727 bool DebugLogging) {
1728 auto Pipeline = parsePipelineText(PipelineText);
1729 if (!Pipeline || Pipeline->empty())
1730 return false;
1731
1732 StringRef FirstName = Pipeline->front().Name;
1733 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1734 return false;
1735
1736 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1737 DebugLogging);
1738}
1739
1740// Primary pass pipeline description parsing routine for a \c LoopPassManager
1741bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1742 StringRef PipelineText, bool VerifyEachPass,
1743 bool DebugLogging) {
1744 auto Pipeline = parsePipelineText(PipelineText);
1745 if (!Pipeline || Pipeline->empty())
1746 return false;
1747
1748 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1749}
1750
Chandler Carruthedf59962016-02-18 09:45:17 +00001751bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001752 // If the pipeline just consists of the word 'default' just replace the AA
1753 // manager with our default one.
1754 if (PipelineText == "default") {
1755 AA = buildDefaultAAPipeline();
1756 return true;
1757 }
1758
Chandler Carruthedf59962016-02-18 09:45:17 +00001759 while (!PipelineText.empty()) {
1760 StringRef Name;
1761 std::tie(Name, PipelineText) = PipelineText.split(',');
1762 if (!parseAAPassName(AA, Name))
1763 return false;
1764 }
1765
1766 return true;
1767}