blob: 7076e751071dcc2c2b84f4949b759af95508f8c7 [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));
153
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000154static cl::opt<bool> EnableGVNHoist(
155 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
156 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
157
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000158static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
159
Chandler Carruthe3f50642016-12-22 06:59:15 +0000160static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
161 switch (Level) {
162 case PassBuilder::O0:
163 case PassBuilder::O1:
164 case PassBuilder::O2:
165 case PassBuilder::O3:
166 return false;
167
168 case PassBuilder::Os:
169 case PassBuilder::Oz:
170 return true;
171 }
172 llvm_unreachable("Invalid optimization level!");
173}
174
Chandler Carruth66445382014-01-11 08:16:35 +0000175namespace {
176
Chandler Carruthd8330982014-01-12 09:34:22 +0000177/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000178struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000179 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000180 return PreservedAnalyses::all();
181 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000182 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000183};
184
Chandler Carruth0b576b32015-01-06 02:50:06 +0000185/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000186class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
187 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000188 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000189
190public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000191 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000192 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000193 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000194};
195
Chandler Carruth572e3402014-04-21 11:12:00 +0000196/// \brief No-op CGSCC pass which does nothing.
197struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000198 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
199 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000200 return PreservedAnalyses::all();
201 }
202 static StringRef name() { return "NoOpCGSCCPass"; }
203};
204
Chandler Carruth0b576b32015-01-06 02:50:06 +0000205/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000206class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
207 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000208 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000209
210public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000211 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000212 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000213 return Result();
214 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000215 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000216};
217
Chandler Carruthd8330982014-01-12 09:34:22 +0000218/// \brief No-op function pass which does nothing.
219struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000220 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000221 return PreservedAnalyses::all();
222 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000223 static StringRef name() { return "NoOpFunctionPass"; }
224};
225
Chandler Carruth0b576b32015-01-06 02:50:06 +0000226/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000227class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
228 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000229 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000230
231public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000232 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000233 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235};
236
Justin Bognereecc3c82016-02-25 07:23:08 +0000237/// \brief No-op loop pass which does nothing.
238struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000239 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
240 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000241 return PreservedAnalyses::all();
242 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000243 static StringRef name() { return "NoOpLoopPass"; }
244};
245
246/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000247class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
248 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000249 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000250
251public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000252 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000253 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
254 return Result();
255 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000256 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000257};
258
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000259AnalysisKey NoOpModuleAnalysis::Key;
260AnalysisKey NoOpCGSCCAnalysis::Key;
261AnalysisKey NoOpFunctionAnalysis::Key;
262AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000263
Chandler Carruth66445382014-01-11 08:16:35 +0000264} // End anonymous namespace.
265
Chandler Carruth1ff77242015-03-07 09:02:36 +0000266void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000267#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000268 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000269#include "PassRegistry.def"
270}
271
Chandler Carruth1ff77242015-03-07 09:02:36 +0000272void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000273#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000274 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000275#include "PassRegistry.def"
276}
277
Chandler Carruth1ff77242015-03-07 09:02:36 +0000278void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000279#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000280 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000281#include "PassRegistry.def"
282}
283
Justin Bognereecc3c82016-02-25 07:23:08 +0000284void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000285#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000286 LAM.registerPass([&] { return CREATE_PASS; });
287#include "PassRegistry.def"
288}
289
Chandler Carruthe3f50642016-12-22 06:59:15 +0000290FunctionPassManager
291PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
292 bool DebugLogging) {
293 assert(Level != O0 && "Must request optimizations!");
294 FunctionPassManager FPM(DebugLogging);
295
296 // Form SSA out of local memory accesses after breaking apart aggregates into
297 // scalars.
298 FPM.addPass(SROA());
299
300 // Catch trivial redundancies
301 FPM.addPass(EarlyCSEPass());
302
303 // Speculative execution if the target has divergent branches; otherwise nop.
304 FPM.addPass(SpeculativeExecutionPass());
305
306 // Optimize based on known information about branches, and cleanup afterward.
307 FPM.addPass(JumpThreadingPass());
308 FPM.addPass(CorrelatedValuePropagationPass());
309 FPM.addPass(SimplifyCFGPass());
310 FPM.addPass(InstCombinePass());
311
312 if (!isOptimizingForSize(Level))
313 FPM.addPass(LibCallsShrinkWrapPass());
314
315 FPM.addPass(TailCallElimPass());
316 FPM.addPass(SimplifyCFGPass());
317
318 // Form canonically associated expression trees, and simplify the trees using
319 // basic mathematical properties. For example, this will form (nearly)
320 // minimal multiplication trees.
321 FPM.addPass(ReassociatePass());
322
323 // Add the primary loop simplification pipeline.
324 // FIXME: Currently this is split into two loop pass pipelines because we run
325 // some function passes in between them. These can and should be replaced by
326 // loop pass equivalenst but those aren't ready yet. Specifically,
327 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
328 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
329 // the other we have is `LoopInstSimplify`.
330 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
331
332 // Rotate Loop - disable header duplication at -Oz
333 LPM1.addPass(LoopRotatePass(Level != Oz));
334 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000335#if 0
336 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000337 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000338#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000339 LPM2.addPass(IndVarSimplifyPass());
340 LPM2.addPass(LoopIdiomRecognizePass());
341 LPM2.addPass(LoopDeletionPass());
Dehao Chen7d230322017-02-18 03:46:51 +0000342 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000343
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000344 // We provide the opt remark emitter pass for LICM to use. We only need to do
345 // this once as it is immutable.
346 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000347 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
348 FPM.addPass(SimplifyCFGPass());
349 FPM.addPass(InstCombinePass());
350 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
351
352 // Eliminate redundancies.
353 if (Level != O1) {
354 // These passes add substantial compile time so skip them at O1.
355 FPM.addPass(MergedLoadStoreMotionPass());
356 FPM.addPass(GVN());
357 }
358
359 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
360 FPM.addPass(MemCpyOptPass());
361
362 // Sparse conditional constant propagation.
363 // FIXME: It isn't clear why we do this *after* loop passes rather than
364 // before...
365 FPM.addPass(SCCPPass());
366
367 // Delete dead bit computations (instcombine runs after to fold away the dead
368 // computations, and then ADCE will run later to exploit any new DCE
369 // opportunities that creates).
370 FPM.addPass(BDCEPass());
371
372 // Run instcombine after redundancy and dead bit elimination to exploit
373 // opportunities opened up by them.
374 FPM.addPass(InstCombinePass());
375
376 // Re-consider control flow based optimizations after redundancy elimination,
377 // redo DCE, etc.
378 FPM.addPass(JumpThreadingPass());
379 FPM.addPass(CorrelatedValuePropagationPass());
380 FPM.addPass(DSEPass());
381 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
382
383 // Finally, do an expensive DCE pass to catch all the dead code exposed by
384 // the simplifications and basic cleanup after all the simplifications.
385 FPM.addPass(ADCEPass());
386 FPM.addPass(SimplifyCFGPass());
387 FPM.addPass(InstCombinePass());
388
389 return FPM;
390}
391
Davide Italiano513dfaa2017-02-13 15:26:22 +0000392static void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
393 PassBuilder::OptimizationLevel Level,
394 bool RunProfileGen, std::string ProfileGenFile,
395 std::string ProfileUseFile) {
396 // Generally running simplification passes and the inliner with an high
397 // threshold results in smaller executables, but there may be cases where
398 // the size grows, so let's be conservative here and skip this simplification
399 // at -Os/Oz.
400 if (!isOptimizingForSize(Level)) {
401 InlineParams IP;
402
403 // In the old pass manager, this is a cl::opt. Should still this be one?
404 IP.DefaultThreshold = 75;
405
406 // FIXME: The hint threshold has the same value used by the regular inliner.
407 // This should probably be lowered after performance testing.
408 // FIXME: this comment is cargo culted from the old pass manager, revisit).
409 IP.HintThreshold = 325;
410
411 CGSCCPassManager CGPipeline(DebugLogging);
412
413 CGPipeline.addPass(InlinerPass(IP));
414
415 FunctionPassManager FPM;
416 FPM.addPass(SROA());
417 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
418 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
419 FPM.addPass(InstCombinePass()); // Combine silly sequences.
420
421 // FIXME: Here the old pass manager inserts peephole extensions.
422 // Add them when they're supported.
423 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
424
425 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
426 }
427
428 if (RunProfileGen) {
429 MPM.addPass(PGOInstrumentationGen());
430
431 // Add the profile lowering pass.
432 InstrProfOptions Options;
433 if (!ProfileGenFile.empty())
434 Options.InstrProfileOutput = ProfileGenFile;
435 MPM.addPass(InstrProfiling(Options));
436 }
437
438 if (!ProfileUseFile.empty())
439 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
440}
441
Chandler Carruthe3f50642016-12-22 06:59:15 +0000442ModulePassManager
443PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
444 bool DebugLogging) {
445 assert(Level != O0 && "Must request optimizations for the default pipeline!");
446 ModulePassManager MPM(DebugLogging);
447
448 // Force any function attributes we want the rest of the pipeline te observe.
449 MPM.addPass(ForceFunctionAttrsPass());
450
451 // Do basic inference of function attributes from known properties of system
452 // libraries and other oracles.
453 MPM.addPass(InferFunctionAttrsPass());
454
455 // Create an early function pass manager to cleanup the output of the
456 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000457 FunctionPassManager EarlyFPM(DebugLogging);
458 EarlyFPM.addPass(SimplifyCFGPass());
459 EarlyFPM.addPass(SROA());
460 EarlyFPM.addPass(EarlyCSEPass());
461 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000462 if (EnableGVNHoist)
463 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000464 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000465
466 // Interprocedural constant propagation now that basic cleanup has occured
467 // and prior to optimizing globals.
468 // FIXME: This position in the pipeline hasn't been carefully considered in
469 // years, it should be re-analyzed.
470 MPM.addPass(IPSCCPPass());
471
472 // Optimize globals to try and fold them into constants.
473 MPM.addPass(GlobalOptPass());
474
475 // Promote any localized globals to SSA registers.
476 // FIXME: Should this instead by a run of SROA?
477 // FIXME: We should probably run instcombine and simplify-cfg afterward to
478 // delete control flows that are dead once globals have been folded to
479 // constants.
480 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
481
482 // Remove any dead arguments exposed by cleanups and constand folding
483 // globals.
484 MPM.addPass(DeadArgumentEliminationPass());
485
486 // Create a small function pass pipeline to cleanup after all the global
487 // optimizations.
488 FunctionPassManager GlobalCleanupPM(DebugLogging);
489 GlobalCleanupPM.addPass(InstCombinePass());
490 GlobalCleanupPM.addPass(SimplifyCFGPass());
491 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
492
Davide Italiano513dfaa2017-02-13 15:26:22 +0000493 // Add all the requested passes for PGO Instrumentation, if requested.
494 if (PGOOpt) {
Dehao Chencc75d242017-02-23 22:15:18 +0000495 assert(PGOOpt->RunProfileGen || PGOOpt->SamplePGO ||
496 !PGOOpt->ProfileUseFile.empty());
Davide Italiano513dfaa2017-02-13 15:26:22 +0000497 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
498 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
499 }
500
501 // Indirect call promotion that promotes intra-module targes only.
Dehao Chencc75d242017-02-23 22:15:18 +0000502 MPM.addPass(PGOIndirectCallPromotion(false, PGOOpt && PGOOpt->SamplePGO));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000503
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000504 // Require the GlobalsAA analysis for the module so we can query it within
505 // the CGSCC pipeline.
506 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000507
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000508 // Require the ProfileSummaryAnalysis for the module so we can query it within
509 // the inliner pass.
510 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
511
Chandler Carruthe3f50642016-12-22 06:59:15 +0000512 // Now begin the main postorder CGSCC pipeline.
513 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
514 // manager and trying to emulate its precise behavior. Much of this doesn't
515 // make a lot of sense and we should revisit the core CGSCC structure.
516 CGSCCPassManager MainCGPipeline(DebugLogging);
517
518 // Note: historically, the PruneEH pass was run first to deduce nounwind and
519 // generally clean up exception handling overhead. It isn't clear this is
520 // valuable as the inliner doesn't currently care whether it is inlining an
521 // invoke or a call.
522
523 // Run the inliner first. The theory is that we are walking bottom-up and so
524 // the callees have already been fully optimized, and we want to inline them
525 // into the callers so that our optimizations can reflect that.
526 // FIXME; Customize the threshold based on optimization level.
527 MainCGPipeline.addPass(InlinerPass());
528
529 // Now deduce any function attributes based in the current code.
530 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
531
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000532 // When at O3 add argument promotion to the pass pipeline.
533 // FIXME: It isn't at all clear why this should be limited to O3.
534 if (Level == O3)
535 MainCGPipeline.addPass(ArgumentPromotionPass());
536
Chandler Carruthe3f50642016-12-22 06:59:15 +0000537 // Lastly, add the core function simplification pipeline nested inside the
538 // CGSCC walk.
539 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
540 buildFunctionSimplificationPipeline(Level, DebugLogging)));
541
Chandler Carruth719ffe12017-02-12 05:38:04 +0000542 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
543 // to detect when we devirtualize indirect calls and iterate the SCC passes
544 // in that case to try and catch knock-on inlining or function attrs
545 // opportunities. Then we add it to the module pipeline by walking the SCCs
546 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000547 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000548 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
549 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000550
551 // This ends the canonicalization and simplification phase of the pipeline.
552 // At this point, we expect to have canonical and simple IR which we begin
553 // *optimizing* for efficient execution going forward.
554
555 // Eliminate externally available functions now that inlining is over -- we
556 // won't emit these anyways.
557 MPM.addPass(EliminateAvailableExternallyPass());
558
559 // Do RPO function attribute inference across the module to forward-propagate
560 // attributes where applicable.
561 // FIXME: Is this really an optimization rather than a canonicalization?
562 MPM.addPass(ReversePostOrderFunctionAttrsPass());
563
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000564 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000565 // useful as the above will have inlined, DCE'ed, and function-attr
566 // propagated everything. We should at this point have a reasonably minimal
567 // and richly annotated call graph. By computing aliasing and mod/ref
568 // information for all local globals here, the late loop passes and notably
569 // the vectorizer will be able to use them to help recognize vectorizable
570 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000571 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000572
573 FunctionPassManager OptimizePM(DebugLogging);
574 OptimizePM.addPass(Float2IntPass());
575 // FIXME: We need to run some loop optimizations to re-rotate loops after
576 // simplify-cfg and others undo their rotation.
577
578 // Optimize the loop execution. These passes operate on entire loop nests
579 // rather than on each loop in an inside-out manner, and so they are actually
580 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000581
582 // First rotate loops that may have been un-rotated by prior passes.
583 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
584
585 // Distribute loops to allow partial vectorization. I.e. isolate dependences
586 // into separate loop that would otherwise inhibit vectorization. This is
587 // currently only performed for loops marked with the metadata
588 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000589 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000590
591 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000592 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000593
Chandler Carruthbaabda92017-01-27 01:32:26 +0000594 // Eliminate loads by forwarding stores from the previous iteration to loads
595 // of the current iteration.
596 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000597
598 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000599 OptimizePM.addPass(InstCombinePass());
600
Chandler Carrutha95ff382017-01-27 00:50:21 +0000601
602 // Now that we've formed fast to execute loop structures, we do further
603 // optimizations. These are run afterward as they might block doing complex
604 // analyses and transforms such as what are needed for loop vectorization.
605
Chandler Carruthe3f50642016-12-22 06:59:15 +0000606 // Optimize parallel scalar instruction chains into SIMD instructions.
607 OptimizePM.addPass(SLPVectorizerPass());
608
Chandler Carrutha95ff382017-01-27 00:50:21 +0000609 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000610 OptimizePM.addPass(SimplifyCFGPass());
611 OptimizePM.addPass(InstCombinePass());
612
613 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000614 // execution resources of an out-of-order processor. We also then need to
615 // clean up redundancies and loop invariant code.
616 // FIXME: It would be really good to use a loop-integrated instruction
617 // combiner for cleanup here so that the unrolling and LICM can be pipelined
618 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000619 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000620 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000621 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000622 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000623
624 // Now that we've vectorized and unrolled loops, we may have more refined
625 // alignment information, try to re-derive it here.
626 OptimizePM.addPass(AlignmentFromAssumptionsPass());
627
Chandler Carrutha95ff382017-01-27 00:50:21 +0000628 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
629 // canonicalization pass that enables other optimizations. As a result,
630 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
631 // result too early.
632 OptimizePM.addPass(LoopSinkPass());
633
634 // And finally clean up LCSSA form before generating code.
635 OptimizePM.addPass(InstSimplifierPass());
636
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000637 // LoopSink (and other loop passes since the last simplifyCFG) might have
638 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
639 OptimizePM.addPass(SimplifyCFGPass());
640
Chandler Carrutha95ff382017-01-27 00:50:21 +0000641 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000642 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
643
644 // Now we need to do some global optimization transforms.
645 // FIXME: It would seem like these should come first in the optimization
646 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
647 // ordering here.
648 MPM.addPass(GlobalDCEPass());
649 MPM.addPass(ConstantMergePass());
650
651 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000652}
653
Chandler Carruthe3f50642016-12-22 06:59:15 +0000654ModulePassManager
655PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
656 bool DebugLogging) {
657 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000658 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000659 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000660}
661
Chandler Carruthe3f50642016-12-22 06:59:15 +0000662ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
663 bool DebugLogging) {
664 assert(Level != O0 && "Must request optimizations for the default pipeline!");
665 ModulePassManager MPM(DebugLogging);
666
Davide Italiano089a9122017-01-24 00:57:39 +0000667 // Remove unused virtual tables to improve the quality of code generated by
668 // whole-program devirtualization and bitset lowering.
669 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000670
Davide Italiano089a9122017-01-24 00:57:39 +0000671 // Force any function attributes we want the rest of the pipeline to observe.
672 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000673
Davide Italiano089a9122017-01-24 00:57:39 +0000674 // Do basic inference of function attributes from known properties of system
675 // libraries and other oracles.
676 MPM.addPass(InferFunctionAttrsPass());
677
678 if (Level > 1) {
679 // Indirect call promotion. This should promote all the targets that are
680 // left by the earlier promotion pass that promotes intra-module targets.
681 // This two-step promotion is to save the compile time. For LTO, it should
682 // produce the same result as if we only do promotion here.
Dehao Chencc75d242017-02-23 22:15:18 +0000683 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
684 PGOOpt && PGOOpt->SamplePGO));
Davide Italiano089a9122017-01-24 00:57:39 +0000685
686 // Propagate constants at call sites into the functions they call. This
687 // opens opportunities for globalopt (and inlining) by substituting function
688 // pointers passed as arguments to direct uses of functions.
689 MPM.addPass(IPSCCPPass());
690 }
691
692 // Now deduce any function attributes based in the current code.
693 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
694 PostOrderFunctionAttrsPass()));
695
696 // Do RPO function attribute inference across the module to forward-propagate
697 // attributes where applicable.
698 // FIXME: Is this really an optimization rather than a canonicalization?
699 MPM.addPass(ReversePostOrderFunctionAttrsPass());
700
701 // Use inragne annotations on GEP indices to split globals where beneficial.
702 MPM.addPass(GlobalSplitPass());
703
704 // Run whole program optimization of virtual call when the list of callees
705 // is fixed.
706 MPM.addPass(WholeProgramDevirtPass());
707
708 // Stop here at -O1.
709 if (Level == 1)
710 return MPM;
711
712 // Optimize globals to try and fold them into constants.
713 MPM.addPass(GlobalOptPass());
714
715 // Promote any localized globals to SSA registers.
716 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
717
718 // Linking modules together can lead to duplicate global constant, only
719 // keep one copy of each constant.
720 MPM.addPass(ConstantMergePass());
721
722 // Remove unused arguments from functions.
723 MPM.addPass(DeadArgumentEliminationPass());
724
725 // Reduce the code after globalopt and ipsccp. Both can open up significant
726 // simplification opportunities, and both can propagate functions through
727 // function pointers. When this happens, we often have to resolve varargs
728 // calls, etc, so let instcombine do this.
729 // FIXME: add peephole extensions here as the legacy PM does.
730 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
731
732 // Note: historically, the PruneEH pass was run first to deduce nounwind and
733 // generally clean up exception handling overhead. It isn't clear this is
734 // valuable as the inliner doesn't currently care whether it is inlining an
735 // invoke or a call.
736 // Run the inliner now.
737 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
738
739 // Optimize globals again after we ran the inliner.
740 MPM.addPass(GlobalOptPass());
741
742 // Garbage collect dead functions.
743 // FIXME: Add ArgumentPromotion pass after once it's ported.
744 MPM.addPass(GlobalDCEPass());
745
746 FunctionPassManager FPM(DebugLogging);
747
748 // The IPO Passes may leave cruft around. Clean up after them.
749 // FIXME: add peephole extensions here as the legacy PM does.
750 FPM.addPass(InstCombinePass());
751 FPM.addPass(JumpThreadingPass());
752
753 // Break up allocas
754 FPM.addPass(SROA());
755
756 // Run a few AA driver optimizations here and now to cleanup the code.
757 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
758
759 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
760 PostOrderFunctionAttrsPass()));
761 // FIXME: here we run IP alias analysis in the legacy PM.
762
763 FunctionPassManager MainFPM;
764
765 // FIXME: once we fix LoopPass Manager, add LICM here.
766 // FIXME: once we provide support for enabling MLSM, add it here.
767 // FIXME: once we provide support for enabling NewGVN, add it here.
768 MainFPM.addPass(GVN());
769
770 // Remove dead memcpy()'s.
771 MainFPM.addPass(MemCpyOptPass());
772
773 // Nuke dead stores.
774 MainFPM.addPass(DSEPass());
775
776 // FIXME: at this point, we run a bunch of loop passes:
777 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
778 // loopVectorize. Enable them once the remaining issue with LPM
779 // are sorted out.
780
781 MainFPM.addPass(InstCombinePass());
782 MainFPM.addPass(SimplifyCFGPass());
783 MainFPM.addPass(SCCPPass());
784 MainFPM.addPass(InstCombinePass());
785 MainFPM.addPass(BDCEPass());
786
787 // FIXME: We may want to run SLPVectorizer here.
788 // After vectorization, assume intrinsics may tell us more
789 // about pointer alignments.
790#if 0
791 MainFPM.add(AlignmentFromAssumptionsPass());
792#endif
793
794 // FIXME: Conditionally run LoadCombine here, after it's ported
795 // (in case we still have this pass, given its questionable usefulness).
796
797 // FIXME: add peephole extensions to the PM here.
798 MainFPM.addPass(InstCombinePass());
799 MainFPM.addPass(JumpThreadingPass());
800 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
801
802 // Create a function that performs CFI checks for cross-DSO calls with
803 // targets in the current module.
804 MPM.addPass(CrossDSOCFIPass());
805
806 // Lower type metadata and the type.test intrinsic. This pass supports
807 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
808 // to be run at link time if CFI is enabled. This pass does nothing if
809 // CFI is disabled.
810 // Enable once we add support for the summary in the new PM.
811#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000812 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
813 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000814 Summary));
815#endif
816
817 // Add late LTO optimization passes.
818 // Delete basic blocks, which optimization passes may have killed.
819 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
820
821 // Drop bodies of available eternally objects to improve GlobalDCE.
822 MPM.addPass(EliminateAvailableExternallyPass());
823
824 // Now that we have optimized the program, discard unreachable functions.
825 MPM.addPass(GlobalDCEPass());
826
827 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000828 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000829}
830
Chandler Carruth060ad612016-12-23 20:38:19 +0000831AAManager PassBuilder::buildDefaultAAPipeline() {
832 AAManager AA;
833
834 // The order in which these are registered determines their priority when
835 // being queried.
836
837 // First we register the basic alias analysis that provides the majority of
838 // per-function local AA logic. This is a stateless, on-demand local set of
839 // AA techniques.
840 AA.registerFunctionAnalysis<BasicAA>();
841
842 // Next we query fast, specialized alias analyses that wrap IR-embedded
843 // information about aliasing.
844 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
845 AA.registerFunctionAnalysis<TypeBasedAA>();
846
847 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000848 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
849 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000850 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +0000851 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +0000852
853 return AA;
854}
855
Chandler Carruth241bf242016-08-03 07:44:48 +0000856static Optional<int> parseRepeatPassName(StringRef Name) {
857 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
858 return None;
859 int Count;
860 if (Name.getAsInteger(0, Count) || Count <= 0)
861 return None;
862 return Count;
863}
864
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000865static Optional<int> parseDevirtPassName(StringRef Name) {
866 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
867 return None;
868 int Count;
869 if (Name.getAsInteger(0, Count) || Count <= 0)
870 return None;
871 return Count;
872}
873
Chandler Carruth66445382014-01-11 08:16:35 +0000874static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000875 // Manually handle aliases for pre-configured pipeline fragments.
876 if (Name.startswith("default") || Name.startswith("lto"))
877 return DefaultAliasRegex.match(Name);
878
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000879 // Explicitly handle pass manager names.
880 if (Name == "module")
881 return true;
882 if (Name == "cgscc")
883 return true;
884 if (Name == "function")
885 return true;
886
Chandler Carruth241bf242016-08-03 07:44:48 +0000887 // Explicitly handle custom-parsed pass names.
888 if (parseRepeatPassName(Name))
889 return true;
890
Chandler Carruth74a8a222016-06-17 07:15:29 +0000891#define MODULE_PASS(NAME, CREATE_PASS) \
892 if (Name == NAME) \
893 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000894#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000895 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000896 return true;
897#include "PassRegistry.def"
898
Chandler Carruth66445382014-01-11 08:16:35 +0000899 return false;
900}
901
Chandler Carruth572e3402014-04-21 11:12:00 +0000902static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000903 // Explicitly handle pass manager names.
904 if (Name == "cgscc")
905 return true;
906 if (Name == "function")
907 return true;
908
Chandler Carruth241bf242016-08-03 07:44:48 +0000909 // Explicitly handle custom-parsed pass names.
910 if (parseRepeatPassName(Name))
911 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000912 if (parseDevirtPassName(Name))
913 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000914
Chandler Carruth74a8a222016-06-17 07:15:29 +0000915#define CGSCC_PASS(NAME, CREATE_PASS) \
916 if (Name == NAME) \
917 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000918#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000919 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000920 return true;
921#include "PassRegistry.def"
922
Chandler Carruth572e3402014-04-21 11:12:00 +0000923 return false;
924}
925
Chandler Carruthd8330982014-01-12 09:34:22 +0000926static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000927 // Explicitly handle pass manager names.
928 if (Name == "function")
929 return true;
930 if (Name == "loop")
931 return true;
932
Chandler Carruth241bf242016-08-03 07:44:48 +0000933 // Explicitly handle custom-parsed pass names.
934 if (parseRepeatPassName(Name))
935 return true;
936
Chandler Carruth74a8a222016-06-17 07:15:29 +0000937#define FUNCTION_PASS(NAME, CREATE_PASS) \
938 if (Name == NAME) \
939 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000940#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000941 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000942 return true;
943#include "PassRegistry.def"
944
Chandler Carruthd8330982014-01-12 09:34:22 +0000945 return false;
946}
947
Justin Bognereecc3c82016-02-25 07:23:08 +0000948static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000949 // Explicitly handle pass manager names.
950 if (Name == "loop")
951 return true;
952
Chandler Carruth241bf242016-08-03 07:44:48 +0000953 // Explicitly handle custom-parsed pass names.
954 if (parseRepeatPassName(Name))
955 return true;
956
Chandler Carruth74a8a222016-06-17 07:15:29 +0000957#define LOOP_PASS(NAME, CREATE_PASS) \
958 if (Name == NAME) \
959 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000960#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
961 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
962 return true;
963#include "PassRegistry.def"
964
965 return false;
966}
967
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000968Optional<std::vector<PassBuilder::PipelineElement>>
969PassBuilder::parsePipelineText(StringRef Text) {
970 std::vector<PipelineElement> ResultPipeline;
971
972 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
973 &ResultPipeline};
974 for (;;) {
975 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
976 size_t Pos = Text.find_first_of(",()");
977 Pipeline.push_back({Text.substr(0, Pos), {}});
978
979 // If we have a single terminating name, we're done.
980 if (Pos == Text.npos)
981 break;
982
983 char Sep = Text[Pos];
984 Text = Text.substr(Pos + 1);
985 if (Sep == ',')
986 // Just a name ending in a comma, continue.
987 continue;
988
989 if (Sep == '(') {
990 // Push the inner pipeline onto the stack to continue processing.
991 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
992 continue;
993 }
994
995 assert(Sep == ')' && "Bogus separator!");
996 // When handling the close parenthesis, we greedily consume them to avoid
997 // empty strings in the pipeline.
998 do {
999 // If we try to pop the outer pipeline we have unbalanced parentheses.
1000 if (PipelineStack.size() == 1)
1001 return None;
1002
1003 PipelineStack.pop_back();
1004 } while (Text.consume_front(")"));
1005
1006 // Check if we've finished parsing.
1007 if (Text.empty())
1008 break;
1009
1010 // Otherwise, the end of an inner pipeline always has to be followed by
1011 // a comma, and then we can continue.
1012 if (!Text.consume_front(","))
1013 return None;
1014 }
1015
1016 if (PipelineStack.size() > 1)
1017 // Unbalanced paretheses.
1018 return None;
1019
1020 assert(PipelineStack.back() == &ResultPipeline &&
1021 "Wrong pipeline at the bottom of the stack!");
1022 return {std::move(ResultPipeline)};
1023}
1024
1025bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1026 const PipelineElement &E, bool VerifyEachPass,
1027 bool DebugLogging) {
1028 auto &Name = E.Name;
1029 auto &InnerPipeline = E.InnerPipeline;
1030
1031 // First handle complex passes like the pass managers which carry pipelines.
1032 if (!InnerPipeline.empty()) {
1033 if (Name == "module") {
1034 ModulePassManager NestedMPM(DebugLogging);
1035 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1036 DebugLogging))
1037 return false;
1038 MPM.addPass(std::move(NestedMPM));
1039 return true;
1040 }
1041 if (Name == "cgscc") {
1042 CGSCCPassManager CGPM(DebugLogging);
1043 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1044 DebugLogging))
1045 return false;
1046 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1047 DebugLogging));
1048 return true;
1049 }
1050 if (Name == "function") {
1051 FunctionPassManager FPM(DebugLogging);
1052 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1053 DebugLogging))
1054 return false;
1055 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1056 return true;
1057 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001058 if (auto Count = parseRepeatPassName(Name)) {
1059 ModulePassManager NestedMPM(DebugLogging);
1060 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1061 DebugLogging))
1062 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001063 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001064 return true;
1065 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001066 // Normal passes can't have pipelines.
1067 return false;
1068 }
1069
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001070 // Manually handle aliases for pre-configured pipeline fragments.
1071 if (Name.startswith("default") || Name.startswith("lto")) {
1072 SmallVector<StringRef, 3> Matches;
1073 if (!DefaultAliasRegex.match(Name, &Matches))
1074 return false;
1075 assert(Matches.size() == 3 && "Must capture two matched strings!");
1076
Jordan Rosef85a95f2016-07-25 18:34:51 +00001077 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1078 .Case("O0", O0)
1079 .Case("O1", O1)
1080 .Case("O2", O2)
1081 .Case("O3", O3)
1082 .Case("Os", Os)
1083 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001084 if (L == O0)
1085 // At O0 we do nothing at all!
1086 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001087
1088 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001089 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001090 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001091 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001092 } else {
1093 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001094 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001095 }
1096 return true;
1097 }
1098
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001099 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001100#define MODULE_PASS(NAME, CREATE_PASS) \
1101 if (Name == NAME) { \
1102 MPM.addPass(CREATE_PASS); \
1103 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001104 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001105#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1106 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001107 MPM.addPass( \
1108 RequireAnalysisPass< \
1109 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001110 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001111 } \
1112 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001113 MPM.addPass(InvalidateAnalysisPass< \
1114 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001115 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001116 }
1117#include "PassRegistry.def"
1118
Chandler Carruth66445382014-01-11 08:16:35 +00001119 return false;
1120}
1121
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001122bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1123 const PipelineElement &E, bool VerifyEachPass,
1124 bool DebugLogging) {
1125 auto &Name = E.Name;
1126 auto &InnerPipeline = E.InnerPipeline;
1127
1128 // First handle complex passes like the pass managers which carry pipelines.
1129 if (!InnerPipeline.empty()) {
1130 if (Name == "cgscc") {
1131 CGSCCPassManager NestedCGPM(DebugLogging);
1132 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1133 DebugLogging))
1134 return false;
1135 // Add the nested pass manager with the appropriate adaptor.
1136 CGPM.addPass(std::move(NestedCGPM));
1137 return true;
1138 }
1139 if (Name == "function") {
1140 FunctionPassManager FPM(DebugLogging);
1141 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1142 DebugLogging))
1143 return false;
1144 // Add the nested pass manager with the appropriate adaptor.
1145 CGPM.addPass(
1146 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1147 return true;
1148 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001149 if (auto Count = parseRepeatPassName(Name)) {
1150 CGSCCPassManager NestedCGPM(DebugLogging);
1151 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1152 DebugLogging))
1153 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001154 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001155 return true;
1156 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001157 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1158 CGSCCPassManager NestedCGPM(DebugLogging);
1159 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1160 DebugLogging))
1161 return false;
1162 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1163 *MaxRepetitions, DebugLogging));
1164 return true;
1165 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001166 // Normal passes can't have pipelines.
1167 return false;
1168 }
1169
1170 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001171#define CGSCC_PASS(NAME, CREATE_PASS) \
1172 if (Name == NAME) { \
1173 CGPM.addPass(CREATE_PASS); \
1174 return true; \
1175 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001176#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1177 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001178 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001179 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001180 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1181 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001182 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001183 } \
1184 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001185 CGPM.addPass(InvalidateAnalysisPass< \
1186 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001187 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001188 }
1189#include "PassRegistry.def"
1190
Chandler Carruth572e3402014-04-21 11:12:00 +00001191 return false;
1192}
1193
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001194bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1195 const PipelineElement &E,
1196 bool VerifyEachPass, bool DebugLogging) {
1197 auto &Name = E.Name;
1198 auto &InnerPipeline = E.InnerPipeline;
1199
1200 // First handle complex passes like the pass managers which carry pipelines.
1201 if (!InnerPipeline.empty()) {
1202 if (Name == "function") {
1203 FunctionPassManager NestedFPM(DebugLogging);
1204 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1205 DebugLogging))
1206 return false;
1207 // Add the nested pass manager with the appropriate adaptor.
1208 FPM.addPass(std::move(NestedFPM));
1209 return true;
1210 }
1211 if (Name == "loop") {
1212 LoopPassManager LPM(DebugLogging);
1213 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1214 DebugLogging))
1215 return false;
1216 // Add the nested pass manager with the appropriate adaptor.
1217 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1218 return true;
1219 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001220 if (auto Count = parseRepeatPassName(Name)) {
1221 FunctionPassManager NestedFPM(DebugLogging);
1222 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1223 DebugLogging))
1224 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001225 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001226 return true;
1227 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001228 // Normal passes can't have pipelines.
1229 return false;
1230 }
1231
1232 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001233#define FUNCTION_PASS(NAME, CREATE_PASS) \
1234 if (Name == NAME) { \
1235 FPM.addPass(CREATE_PASS); \
1236 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001237 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001238#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1239 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001240 FPM.addPass( \
1241 RequireAnalysisPass< \
1242 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001243 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001244 } \
1245 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001246 FPM.addPass(InvalidateAnalysisPass< \
1247 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001248 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001249 }
1250#include "PassRegistry.def"
1251
Chandler Carruthd8330982014-01-12 09:34:22 +00001252 return false;
1253}
1254
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001255bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001256 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001257 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001258 auto &InnerPipeline = E.InnerPipeline;
1259
1260 // First handle complex passes like the pass managers which carry pipelines.
1261 if (!InnerPipeline.empty()) {
1262 if (Name == "loop") {
1263 LoopPassManager NestedLPM(DebugLogging);
1264 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1265 DebugLogging))
1266 return false;
1267 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001268 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001269 return true;
1270 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001271 if (auto Count = parseRepeatPassName(Name)) {
1272 LoopPassManager NestedLPM(DebugLogging);
1273 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1274 DebugLogging))
1275 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001276 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001277 return true;
1278 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001279 // Normal passes can't have pipelines.
1280 return false;
1281 }
1282
1283 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001284#define LOOP_PASS(NAME, CREATE_PASS) \
1285 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001286 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001287 return true; \
1288 }
1289#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1290 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001291 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001292 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1293 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1294 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001295 return true; \
1296 } \
1297 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001298 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001299 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001300 return true; \
1301 }
1302#include "PassRegistry.def"
1303
1304 return false;
1305}
1306
Chandler Carruthedf59962016-02-18 09:45:17 +00001307bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001308#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1309 if (Name == NAME) { \
1310 AA.registerModuleAnalysis< \
1311 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1312 return true; \
1313 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001314#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1315 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001316 AA.registerFunctionAnalysis< \
1317 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001318 return true; \
1319 }
1320#include "PassRegistry.def"
1321
1322 return false;
1323}
1324
Justin Bognereecc3c82016-02-25 07:23:08 +00001325bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001326 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001327 bool VerifyEachPass,
1328 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001329 for (const auto &Element : Pipeline) {
1330 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1331 return false;
1332 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001333 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001334 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001335}
1336
Chandler Carruth1ff77242015-03-07 09:02:36 +00001337bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001338 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001339 bool VerifyEachPass,
1340 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001341 for (const auto &Element : Pipeline) {
1342 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1343 return false;
1344 if (VerifyEachPass)
1345 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001346 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001347 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001348}
1349
Chandler Carruth1ff77242015-03-07 09:02:36 +00001350bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001351 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001352 bool VerifyEachPass,
1353 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001354 for (const auto &Element : Pipeline) {
1355 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1356 return false;
1357 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001358 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001359 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001360}
1361
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001362void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1363 FunctionAnalysisManager &FAM,
1364 CGSCCAnalysisManager &CGAM,
1365 ModuleAnalysisManager &MAM) {
1366 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1367 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001368 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1369 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1370 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1371 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1372 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1373}
1374
Chandler Carruth1ff77242015-03-07 09:02:36 +00001375bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001376 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001377 bool VerifyEachPass,
1378 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001379 for (const auto &Element : Pipeline) {
1380 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1381 return false;
1382 if (VerifyEachPass)
1383 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001384 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001385 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001386}
1387
1388// Primary pass pipeline description parsing routine.
1389// FIXME: Should this routine accept a TargetMachine or require the caller to
1390// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001391bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1392 StringRef PipelineText, bool VerifyEachPass,
1393 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001394 auto Pipeline = parsePipelineText(PipelineText);
1395 if (!Pipeline || Pipeline->empty())
1396 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001397
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001398 // If the first name isn't at the module layer, wrap the pipeline up
1399 // automatically.
1400 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001401
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001402 if (!isModulePassName(FirstName)) {
1403 if (isCGSCCPassName(FirstName))
1404 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1405 else if (isFunctionPassName(FirstName))
1406 Pipeline = {{"function", std::move(*Pipeline)}};
1407 else if (isLoopPassName(FirstName))
1408 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1409 else
1410 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001411 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001412 }
1413
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001414 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001415}
Chandler Carruthedf59962016-02-18 09:45:17 +00001416
1417bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001418 // If the pipeline just consists of the word 'default' just replace the AA
1419 // manager with our default one.
1420 if (PipelineText == "default") {
1421 AA = buildDefaultAAPipeline();
1422 return true;
1423 }
1424
Chandler Carruthedf59962016-02-18 09:45:17 +00001425 while (!PipelineText.empty()) {
1426 StringRef Name;
1427 std::tie(Name, PipelineText) = PipelineText.split(',');
1428 if (!parseAAPassName(AA, Name))
1429 return false;
1430 }
1431
1432 return true;
1433}