blob: 4845bd2957b3970d18ed91c1b4a05ead5a455781 [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
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000163static cl::opt<bool> EnableGVNHoist(
164 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
165 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
166
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000167static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
168
Chandler Carruthe3f50642016-12-22 06:59:15 +0000169static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
170 switch (Level) {
171 case PassBuilder::O0:
172 case PassBuilder::O1:
173 case PassBuilder::O2:
174 case PassBuilder::O3:
175 return false;
176
177 case PassBuilder::Os:
178 case PassBuilder::Oz:
179 return true;
180 }
181 llvm_unreachable("Invalid optimization level!");
182}
183
Chandler Carruth66445382014-01-11 08:16:35 +0000184namespace {
185
Chandler Carruthd8330982014-01-12 09:34:22 +0000186/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000187struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000188 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000189 return PreservedAnalyses::all();
190 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000191 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000192};
193
Chandler Carruth0b576b32015-01-06 02:50:06 +0000194/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000195class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
196 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000197 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000198
199public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000200 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000201 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000202 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000203};
204
Chandler Carruth572e3402014-04-21 11:12:00 +0000205/// \brief No-op CGSCC pass which does nothing.
206struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000207 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
208 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000209 return PreservedAnalyses::all();
210 }
211 static StringRef name() { return "NoOpCGSCCPass"; }
212};
213
Chandler Carruth0b576b32015-01-06 02:50:06 +0000214/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000215class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
216 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000217 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000218
219public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000220 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000221 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000222 return Result();
223 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000224 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000225};
226
Chandler Carruthd8330982014-01-12 09:34:22 +0000227/// \brief No-op function pass which does nothing.
228struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000229 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000230 return PreservedAnalyses::all();
231 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000232 static StringRef name() { return "NoOpFunctionPass"; }
233};
234
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000236class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
237 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000238 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000239
240public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000241 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000242 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000243 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000244};
245
Justin Bognereecc3c82016-02-25 07:23:08 +0000246/// \brief No-op loop pass which does nothing.
247struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000248 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
249 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000250 return PreservedAnalyses::all();
251 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000252 static StringRef name() { return "NoOpLoopPass"; }
253};
254
255/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000256class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
257 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000258 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000259
260public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000261 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000262 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
263 return Result();
264 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000265 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000266};
267
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000268AnalysisKey NoOpModuleAnalysis::Key;
269AnalysisKey NoOpCGSCCAnalysis::Key;
270AnalysisKey NoOpFunctionAnalysis::Key;
271AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000272
Chandler Carruth66445382014-01-11 08:16:35 +0000273} // End anonymous namespace.
274
Chandler Carruth1ff77242015-03-07 09:02:36 +0000275void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000276#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000277 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000278#include "PassRegistry.def"
279}
280
Chandler Carruth1ff77242015-03-07 09:02:36 +0000281void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000282#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000283 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000284#include "PassRegistry.def"
285}
286
Chandler Carruth1ff77242015-03-07 09:02:36 +0000287void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000288#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000289 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000290#include "PassRegistry.def"
291}
292
Justin Bognereecc3c82016-02-25 07:23:08 +0000293void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000294#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000295 LAM.registerPass([&] { return CREATE_PASS; });
296#include "PassRegistry.def"
297}
298
Chandler Carruthe3f50642016-12-22 06:59:15 +0000299FunctionPassManager
300PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
301 bool DebugLogging) {
302 assert(Level != O0 && "Must request optimizations!");
303 FunctionPassManager FPM(DebugLogging);
304
305 // Form SSA out of local memory accesses after breaking apart aggregates into
306 // scalars.
307 FPM.addPass(SROA());
308
309 // Catch trivial redundancies
310 FPM.addPass(EarlyCSEPass());
311
312 // Speculative execution if the target has divergent branches; otherwise nop.
313 FPM.addPass(SpeculativeExecutionPass());
314
315 // Optimize based on known information about branches, and cleanup afterward.
316 FPM.addPass(JumpThreadingPass());
317 FPM.addPass(CorrelatedValuePropagationPass());
318 FPM.addPass(SimplifyCFGPass());
319 FPM.addPass(InstCombinePass());
320
321 if (!isOptimizingForSize(Level))
322 FPM.addPass(LibCallsShrinkWrapPass());
323
324 FPM.addPass(TailCallElimPass());
325 FPM.addPass(SimplifyCFGPass());
326
327 // Form canonically associated expression trees, and simplify the trees using
328 // basic mathematical properties. For example, this will form (nearly)
329 // minimal multiplication trees.
330 FPM.addPass(ReassociatePass());
331
332 // Add the primary loop simplification pipeline.
333 // FIXME: Currently this is split into two loop pass pipelines because we run
334 // some function passes in between them. These can and should be replaced by
335 // loop pass equivalenst but those aren't ready yet. Specifically,
336 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
337 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
338 // the other we have is `LoopInstSimplify`.
339 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
340
341 // Rotate Loop - disable header duplication at -Oz
342 LPM1.addPass(LoopRotatePass(Level != Oz));
343 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000344#if 0
345 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000346 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000347#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000348 LPM2.addPass(IndVarSimplifyPass());
349 LPM2.addPass(LoopIdiomRecognizePass());
350 LPM2.addPass(LoopDeletionPass());
Dehao Chen7d230322017-02-18 03:46:51 +0000351 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000352
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000353 // We provide the opt remark emitter pass for LICM to use. We only need to do
354 // this once as it is immutable.
355 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000356 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
357 FPM.addPass(SimplifyCFGPass());
358 FPM.addPass(InstCombinePass());
359 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
360
361 // Eliminate redundancies.
362 if (Level != O1) {
363 // These passes add substantial compile time so skip them at O1.
364 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000365 if (RunNewGVN)
366 FPM.addPass(NewGVNPass());
367 else
368 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000369 }
370
371 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
372 FPM.addPass(MemCpyOptPass());
373
374 // Sparse conditional constant propagation.
375 // FIXME: It isn't clear why we do this *after* loop passes rather than
376 // before...
377 FPM.addPass(SCCPPass());
378
379 // Delete dead bit computations (instcombine runs after to fold away the dead
380 // computations, and then ADCE will run later to exploit any new DCE
381 // opportunities that creates).
382 FPM.addPass(BDCEPass());
383
384 // Run instcombine after redundancy and dead bit elimination to exploit
385 // opportunities opened up by them.
386 FPM.addPass(InstCombinePass());
387
388 // Re-consider control flow based optimizations after redundancy elimination,
389 // redo DCE, etc.
390 FPM.addPass(JumpThreadingPass());
391 FPM.addPass(CorrelatedValuePropagationPass());
392 FPM.addPass(DSEPass());
393 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
394
395 // Finally, do an expensive DCE pass to catch all the dead code exposed by
396 // the simplifications and basic cleanup after all the simplifications.
397 FPM.addPass(ADCEPass());
398 FPM.addPass(SimplifyCFGPass());
399 FPM.addPass(InstCombinePass());
400
401 return FPM;
402}
403
Davide Italiano513dfaa2017-02-13 15:26:22 +0000404static void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
405 PassBuilder::OptimizationLevel Level,
406 bool RunProfileGen, std::string ProfileGenFile,
407 std::string ProfileUseFile) {
408 // Generally running simplification passes and the inliner with an high
409 // threshold results in smaller executables, but there may be cases where
410 // the size grows, so let's be conservative here and skip this simplification
411 // at -Os/Oz.
412 if (!isOptimizingForSize(Level)) {
413 InlineParams IP;
414
415 // In the old pass manager, this is a cl::opt. Should still this be one?
416 IP.DefaultThreshold = 75;
417
418 // FIXME: The hint threshold has the same value used by the regular inliner.
419 // This should probably be lowered after performance testing.
420 // FIXME: this comment is cargo culted from the old pass manager, revisit).
421 IP.HintThreshold = 325;
422
423 CGSCCPassManager CGPipeline(DebugLogging);
424
425 CGPipeline.addPass(InlinerPass(IP));
426
427 FunctionPassManager FPM;
428 FPM.addPass(SROA());
429 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
430 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
431 FPM.addPass(InstCombinePass()); // Combine silly sequences.
432
433 // FIXME: Here the old pass manager inserts peephole extensions.
434 // Add them when they're supported.
435 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
436
437 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
438 }
439
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000440 // Delete anything that is now dead to make sure that we don't instrument
441 // dead code. Instrumentation can end up keeping dead code around and
442 // dramatically increase code size.
443 MPM.addPass(GlobalDCEPass());
444
Davide Italiano513dfaa2017-02-13 15:26:22 +0000445 if (RunProfileGen) {
446 MPM.addPass(PGOInstrumentationGen());
447
448 // Add the profile lowering pass.
449 InstrProfOptions Options;
450 if (!ProfileGenFile.empty())
451 Options.InstrProfileOutput = ProfileGenFile;
452 MPM.addPass(InstrProfiling(Options));
453 }
454
455 if (!ProfileUseFile.empty())
456 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
457}
458
Chandler Carruthe3f50642016-12-22 06:59:15 +0000459ModulePassManager
460PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
461 bool DebugLogging) {
462 assert(Level != O0 && "Must request optimizations for the default pipeline!");
463 ModulePassManager MPM(DebugLogging);
464
465 // Force any function attributes we want the rest of the pipeline te observe.
466 MPM.addPass(ForceFunctionAttrsPass());
467
468 // Do basic inference of function attributes from known properties of system
469 // libraries and other oracles.
470 MPM.addPass(InferFunctionAttrsPass());
471
472 // Create an early function pass manager to cleanup the output of the
473 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000474 FunctionPassManager EarlyFPM(DebugLogging);
475 EarlyFPM.addPass(SimplifyCFGPass());
476 EarlyFPM.addPass(SROA());
477 EarlyFPM.addPass(EarlyCSEPass());
478 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000479 if (EnableGVNHoist)
480 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000481 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000482
483 // Interprocedural constant propagation now that basic cleanup has occured
484 // and prior to optimizing globals.
485 // FIXME: This position in the pipeline hasn't been carefully considered in
486 // years, it should be re-analyzed.
487 MPM.addPass(IPSCCPPass());
488
489 // Optimize globals to try and fold them into constants.
490 MPM.addPass(GlobalOptPass());
491
492 // Promote any localized globals to SSA registers.
493 // FIXME: Should this instead by a run of SROA?
494 // FIXME: We should probably run instcombine and simplify-cfg afterward to
495 // delete control flows that are dead once globals have been folded to
496 // constants.
497 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
498
499 // Remove any dead arguments exposed by cleanups and constand folding
500 // globals.
501 MPM.addPass(DeadArgumentEliminationPass());
502
503 // Create a small function pass pipeline to cleanup after all the global
504 // optimizations.
505 FunctionPassManager GlobalCleanupPM(DebugLogging);
506 GlobalCleanupPM.addPass(InstCombinePass());
507 GlobalCleanupPM.addPass(SimplifyCFGPass());
508 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
509
Davide Italiano513dfaa2017-02-13 15:26:22 +0000510 // Add all the requested passes for PGO Instrumentation, if requested.
511 if (PGOOpt) {
Dehao Chencc75d242017-02-23 22:15:18 +0000512 assert(PGOOpt->RunProfileGen || PGOOpt->SamplePGO ||
513 !PGOOpt->ProfileUseFile.empty());
Davide Italiano513dfaa2017-02-13 15:26:22 +0000514 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
515 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
516 }
517
518 // Indirect call promotion that promotes intra-module targes only.
Dehao Chencc75d242017-02-23 22:15:18 +0000519 MPM.addPass(PGOIndirectCallPromotion(false, PGOOpt && PGOOpt->SamplePGO));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000520
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000521 // Require the GlobalsAA analysis for the module so we can query it within
522 // the CGSCC pipeline.
523 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000524
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000525 // Require the ProfileSummaryAnalysis for the module so we can query it within
526 // the inliner pass.
527 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
528
Chandler Carruthe3f50642016-12-22 06:59:15 +0000529 // Now begin the main postorder CGSCC pipeline.
530 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
531 // manager and trying to emulate its precise behavior. Much of this doesn't
532 // make a lot of sense and we should revisit the core CGSCC structure.
533 CGSCCPassManager MainCGPipeline(DebugLogging);
534
535 // Note: historically, the PruneEH pass was run first to deduce nounwind and
536 // generally clean up exception handling overhead. It isn't clear this is
537 // valuable as the inliner doesn't currently care whether it is inlining an
538 // invoke or a call.
539
540 // Run the inliner first. The theory is that we are walking bottom-up and so
541 // the callees have already been fully optimized, and we want to inline them
542 // into the callers so that our optimizations can reflect that.
543 // FIXME; Customize the threshold based on optimization level.
544 MainCGPipeline.addPass(InlinerPass());
545
546 // Now deduce any function attributes based in the current code.
547 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
548
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000549 // When at O3 add argument promotion to the pass pipeline.
550 // FIXME: It isn't at all clear why this should be limited to O3.
551 if (Level == O3)
552 MainCGPipeline.addPass(ArgumentPromotionPass());
553
Chandler Carruthe3f50642016-12-22 06:59:15 +0000554 // Lastly, add the core function simplification pipeline nested inside the
555 // CGSCC walk.
556 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
557 buildFunctionSimplificationPipeline(Level, DebugLogging)));
558
Chandler Carruth719ffe12017-02-12 05:38:04 +0000559 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
560 // to detect when we devirtualize indirect calls and iterate the SCC passes
561 // in that case to try and catch knock-on inlining or function attrs
562 // opportunities. Then we add it to the module pipeline by walking the SCCs
563 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000564 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000565 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
566 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000567
568 // This ends the canonicalization and simplification phase of the pipeline.
569 // At this point, we expect to have canonical and simple IR which we begin
570 // *optimizing* for efficient execution going forward.
571
Xinliang David Li126157c2017-05-22 16:41:57 +0000572 // Run partial inlining pass to partially inline functions that have
573 // large bodies.
574 if (RunPartialInlining)
575 MPM.addPass(PartialInlinerPass());
576
Chandler Carruthe3f50642016-12-22 06:59:15 +0000577 // Eliminate externally available functions now that inlining is over -- we
578 // won't emit these anyways.
579 MPM.addPass(EliminateAvailableExternallyPass());
580
581 // Do RPO function attribute inference across the module to forward-propagate
582 // attributes where applicable.
583 // FIXME: Is this really an optimization rather than a canonicalization?
584 MPM.addPass(ReversePostOrderFunctionAttrsPass());
585
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000586 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000587 // useful as the above will have inlined, DCE'ed, and function-attr
588 // propagated everything. We should at this point have a reasonably minimal
589 // and richly annotated call graph. By computing aliasing and mod/ref
590 // information for all local globals here, the late loop passes and notably
591 // the vectorizer will be able to use them to help recognize vectorizable
592 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000593 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000594
595 FunctionPassManager OptimizePM(DebugLogging);
596 OptimizePM.addPass(Float2IntPass());
597 // FIXME: We need to run some loop optimizations to re-rotate loops after
598 // simplify-cfg and others undo their rotation.
599
600 // Optimize the loop execution. These passes operate on entire loop nests
601 // rather than on each loop in an inside-out manner, and so they are actually
602 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000603
604 // First rotate loops that may have been un-rotated by prior passes.
605 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
606
607 // Distribute loops to allow partial vectorization. I.e. isolate dependences
608 // into separate loop that would otherwise inhibit vectorization. This is
609 // currently only performed for loops marked with the metadata
610 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000611 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000612
613 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000614 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000615
Chandler Carruthbaabda92017-01-27 01:32:26 +0000616 // Eliminate loads by forwarding stores from the previous iteration to loads
617 // of the current iteration.
618 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000619
620 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000621 OptimizePM.addPass(InstCombinePass());
622
Chandler Carrutha95ff382017-01-27 00:50:21 +0000623
624 // Now that we've formed fast to execute loop structures, we do further
625 // optimizations. These are run afterward as they might block doing complex
626 // analyses and transforms such as what are needed for loop vectorization.
627
Chandler Carruthe3f50642016-12-22 06:59:15 +0000628 // Optimize parallel scalar instruction chains into SIMD instructions.
629 OptimizePM.addPass(SLPVectorizerPass());
630
Chandler Carrutha95ff382017-01-27 00:50:21 +0000631 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000632 OptimizePM.addPass(SimplifyCFGPass());
633 OptimizePM.addPass(InstCombinePass());
634
635 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000636 // execution resources of an out-of-order processor. We also then need to
637 // clean up redundancies and loop invariant code.
638 // FIXME: It would be really good to use a loop-integrated instruction
639 // combiner for cleanup here so that the unrolling and LICM can be pipelined
640 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000641 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000642 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000643 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000644 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000645
646 // Now that we've vectorized and unrolled loops, we may have more refined
647 // alignment information, try to re-derive it here.
648 OptimizePM.addPass(AlignmentFromAssumptionsPass());
649
Chandler Carrutha95ff382017-01-27 00:50:21 +0000650 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
651 // canonicalization pass that enables other optimizations. As a result,
652 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
653 // result too early.
654 OptimizePM.addPass(LoopSinkPass());
655
656 // And finally clean up LCSSA form before generating code.
657 OptimizePM.addPass(InstSimplifierPass());
658
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000659 // LoopSink (and other loop passes since the last simplifyCFG) might have
660 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
661 OptimizePM.addPass(SimplifyCFGPass());
662
Chandler Carrutha95ff382017-01-27 00:50:21 +0000663 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000664 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
665
666 // Now we need to do some global optimization transforms.
667 // FIXME: It would seem like these should come first in the optimization
668 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
669 // ordering here.
670 MPM.addPass(GlobalDCEPass());
671 MPM.addPass(ConstantMergePass());
672
673 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000674}
675
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676ModulePassManager
677PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
678 bool DebugLogging) {
679 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000680 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000681 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000682}
683
Chandler Carruthe3f50642016-12-22 06:59:15 +0000684ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
685 bool DebugLogging) {
686 assert(Level != O0 && "Must request optimizations for the default pipeline!");
687 ModulePassManager MPM(DebugLogging);
688
Davide Italiano089a9122017-01-24 00:57:39 +0000689 // Remove unused virtual tables to improve the quality of code generated by
690 // whole-program devirtualization and bitset lowering.
691 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000692
Davide Italiano089a9122017-01-24 00:57:39 +0000693 // Force any function attributes we want the rest of the pipeline to observe.
694 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000695
Davide Italiano089a9122017-01-24 00:57:39 +0000696 // Do basic inference of function attributes from known properties of system
697 // libraries and other oracles.
698 MPM.addPass(InferFunctionAttrsPass());
699
700 if (Level > 1) {
701 // Indirect call promotion. This should promote all the targets that are
702 // left by the earlier promotion pass that promotes intra-module targets.
703 // This two-step promotion is to save the compile time. For LTO, it should
704 // produce the same result as if we only do promotion here.
Dehao Chencc75d242017-02-23 22:15:18 +0000705 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
706 PGOOpt && PGOOpt->SamplePGO));
Davide Italiano089a9122017-01-24 00:57:39 +0000707
708 // Propagate constants at call sites into the functions they call. This
709 // opens opportunities for globalopt (and inlining) by substituting function
710 // pointers passed as arguments to direct uses of functions.
711 MPM.addPass(IPSCCPPass());
712 }
713
714 // Now deduce any function attributes based in the current code.
715 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
716 PostOrderFunctionAttrsPass()));
717
718 // Do RPO function attribute inference across the module to forward-propagate
719 // attributes where applicable.
720 // FIXME: Is this really an optimization rather than a canonicalization?
721 MPM.addPass(ReversePostOrderFunctionAttrsPass());
722
723 // Use inragne annotations on GEP indices to split globals where beneficial.
724 MPM.addPass(GlobalSplitPass());
725
726 // Run whole program optimization of virtual call when the list of callees
727 // is fixed.
728 MPM.addPass(WholeProgramDevirtPass());
729
730 // Stop here at -O1.
731 if (Level == 1)
732 return MPM;
733
734 // Optimize globals to try and fold them into constants.
735 MPM.addPass(GlobalOptPass());
736
737 // Promote any localized globals to SSA registers.
738 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
739
740 // Linking modules together can lead to duplicate global constant, only
741 // keep one copy of each constant.
742 MPM.addPass(ConstantMergePass());
743
744 // Remove unused arguments from functions.
745 MPM.addPass(DeadArgumentEliminationPass());
746
747 // Reduce the code after globalopt and ipsccp. Both can open up significant
748 // simplification opportunities, and both can propagate functions through
749 // function pointers. When this happens, we often have to resolve varargs
750 // calls, etc, so let instcombine do this.
751 // FIXME: add peephole extensions here as the legacy PM does.
752 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
753
754 // Note: historically, the PruneEH pass was run first to deduce nounwind and
755 // generally clean up exception handling overhead. It isn't clear this is
756 // valuable as the inliner doesn't currently care whether it is inlining an
757 // invoke or a call.
758 // Run the inliner now.
759 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
760
761 // Optimize globals again after we ran the inliner.
762 MPM.addPass(GlobalOptPass());
763
764 // Garbage collect dead functions.
765 // FIXME: Add ArgumentPromotion pass after once it's ported.
766 MPM.addPass(GlobalDCEPass());
767
768 FunctionPassManager FPM(DebugLogging);
769
770 // The IPO Passes may leave cruft around. Clean up after them.
771 // FIXME: add peephole extensions here as the legacy PM does.
772 FPM.addPass(InstCombinePass());
773 FPM.addPass(JumpThreadingPass());
774
775 // Break up allocas
776 FPM.addPass(SROA());
777
778 // Run a few AA driver optimizations here and now to cleanup the code.
779 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
780
781 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
782 PostOrderFunctionAttrsPass()));
783 // FIXME: here we run IP alias analysis in the legacy PM.
784
785 FunctionPassManager MainFPM;
786
787 // FIXME: once we fix LoopPass Manager, add LICM here.
788 // FIXME: once we provide support for enabling MLSM, add it here.
789 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000790 if (RunNewGVN)
791 MainFPM.addPass(NewGVNPass());
792 else
793 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +0000794
795 // Remove dead memcpy()'s.
796 MainFPM.addPass(MemCpyOptPass());
797
798 // Nuke dead stores.
799 MainFPM.addPass(DSEPass());
800
801 // FIXME: at this point, we run a bunch of loop passes:
802 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
803 // loopVectorize. Enable them once the remaining issue with LPM
804 // are sorted out.
805
806 MainFPM.addPass(InstCombinePass());
807 MainFPM.addPass(SimplifyCFGPass());
808 MainFPM.addPass(SCCPPass());
809 MainFPM.addPass(InstCombinePass());
810 MainFPM.addPass(BDCEPass());
811
812 // FIXME: We may want to run SLPVectorizer here.
813 // After vectorization, assume intrinsics may tell us more
814 // about pointer alignments.
815#if 0
816 MainFPM.add(AlignmentFromAssumptionsPass());
817#endif
818
819 // FIXME: Conditionally run LoadCombine here, after it's ported
820 // (in case we still have this pass, given its questionable usefulness).
821
822 // FIXME: add peephole extensions to the PM here.
823 MainFPM.addPass(InstCombinePass());
824 MainFPM.addPass(JumpThreadingPass());
825 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
826
827 // Create a function that performs CFI checks for cross-DSO calls with
828 // targets in the current module.
829 MPM.addPass(CrossDSOCFIPass());
830
831 // Lower type metadata and the type.test intrinsic. This pass supports
832 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
833 // to be run at link time if CFI is enabled. This pass does nothing if
834 // CFI is disabled.
835 // Enable once we add support for the summary in the new PM.
836#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000837 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
838 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000839 Summary));
840#endif
841
842 // Add late LTO optimization passes.
843 // Delete basic blocks, which optimization passes may have killed.
844 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
845
846 // Drop bodies of available eternally objects to improve GlobalDCE.
847 MPM.addPass(EliminateAvailableExternallyPass());
848
849 // Now that we have optimized the program, discard unreachable functions.
850 MPM.addPass(GlobalDCEPass());
851
852 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000853 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000854}
855
Chandler Carruth060ad612016-12-23 20:38:19 +0000856AAManager PassBuilder::buildDefaultAAPipeline() {
857 AAManager AA;
858
859 // The order in which these are registered determines their priority when
860 // being queried.
861
862 // First we register the basic alias analysis that provides the majority of
863 // per-function local AA logic. This is a stateless, on-demand local set of
864 // AA techniques.
865 AA.registerFunctionAnalysis<BasicAA>();
866
867 // Next we query fast, specialized alias analyses that wrap IR-embedded
868 // information about aliasing.
869 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
870 AA.registerFunctionAnalysis<TypeBasedAA>();
871
872 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000873 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
874 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000875 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +0000876 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +0000877
878 return AA;
879}
880
Chandler Carruth241bf242016-08-03 07:44:48 +0000881static Optional<int> parseRepeatPassName(StringRef Name) {
882 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
883 return None;
884 int Count;
885 if (Name.getAsInteger(0, Count) || Count <= 0)
886 return None;
887 return Count;
888}
889
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000890static Optional<int> parseDevirtPassName(StringRef Name) {
891 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
892 return None;
893 int Count;
894 if (Name.getAsInteger(0, Count) || Count <= 0)
895 return None;
896 return Count;
897}
898
Chandler Carruth66445382014-01-11 08:16:35 +0000899static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000900 // Manually handle aliases for pre-configured pipeline fragments.
901 if (Name.startswith("default") || Name.startswith("lto"))
902 return DefaultAliasRegex.match(Name);
903
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000904 // Explicitly handle pass manager names.
905 if (Name == "module")
906 return true;
907 if (Name == "cgscc")
908 return true;
909 if (Name == "function")
910 return true;
911
Chandler Carruth241bf242016-08-03 07:44:48 +0000912 // Explicitly handle custom-parsed pass names.
913 if (parseRepeatPassName(Name))
914 return true;
915
Chandler Carruth74a8a222016-06-17 07:15:29 +0000916#define MODULE_PASS(NAME, CREATE_PASS) \
917 if (Name == NAME) \
918 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000919#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000920 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000921 return true;
922#include "PassRegistry.def"
923
Chandler Carruth66445382014-01-11 08:16:35 +0000924 return false;
925}
926
Chandler Carruth572e3402014-04-21 11:12:00 +0000927static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000928 // Explicitly handle pass manager names.
929 if (Name == "cgscc")
930 return true;
931 if (Name == "function")
932 return true;
933
Chandler Carruth241bf242016-08-03 07:44:48 +0000934 // Explicitly handle custom-parsed pass names.
935 if (parseRepeatPassName(Name))
936 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000937 if (parseDevirtPassName(Name))
938 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000939
Chandler Carruth74a8a222016-06-17 07:15:29 +0000940#define CGSCC_PASS(NAME, CREATE_PASS) \
941 if (Name == NAME) \
942 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000943#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000944 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000945 return true;
946#include "PassRegistry.def"
947
Chandler Carruth572e3402014-04-21 11:12:00 +0000948 return false;
949}
950
Chandler Carruthd8330982014-01-12 09:34:22 +0000951static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000952 // Explicitly handle pass manager names.
953 if (Name == "function")
954 return true;
955 if (Name == "loop")
956 return true;
957
Chandler Carruth241bf242016-08-03 07:44:48 +0000958 // Explicitly handle custom-parsed pass names.
959 if (parseRepeatPassName(Name))
960 return true;
961
Chandler Carruth74a8a222016-06-17 07:15:29 +0000962#define FUNCTION_PASS(NAME, CREATE_PASS) \
963 if (Name == NAME) \
964 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000965#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000966 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000967 return true;
968#include "PassRegistry.def"
969
Chandler Carruthd8330982014-01-12 09:34:22 +0000970 return false;
971}
972
Justin Bognereecc3c82016-02-25 07:23:08 +0000973static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000974 // Explicitly handle pass manager names.
975 if (Name == "loop")
976 return true;
977
Chandler Carruth241bf242016-08-03 07:44:48 +0000978 // Explicitly handle custom-parsed pass names.
979 if (parseRepeatPassName(Name))
980 return true;
981
Chandler Carruth74a8a222016-06-17 07:15:29 +0000982#define LOOP_PASS(NAME, CREATE_PASS) \
983 if (Name == NAME) \
984 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000985#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
986 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
987 return true;
988#include "PassRegistry.def"
989
990 return false;
991}
992
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000993Optional<std::vector<PassBuilder::PipelineElement>>
994PassBuilder::parsePipelineText(StringRef Text) {
995 std::vector<PipelineElement> ResultPipeline;
996
997 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
998 &ResultPipeline};
999 for (;;) {
1000 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1001 size_t Pos = Text.find_first_of(",()");
1002 Pipeline.push_back({Text.substr(0, Pos), {}});
1003
1004 // If we have a single terminating name, we're done.
1005 if (Pos == Text.npos)
1006 break;
1007
1008 char Sep = Text[Pos];
1009 Text = Text.substr(Pos + 1);
1010 if (Sep == ',')
1011 // Just a name ending in a comma, continue.
1012 continue;
1013
1014 if (Sep == '(') {
1015 // Push the inner pipeline onto the stack to continue processing.
1016 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1017 continue;
1018 }
1019
1020 assert(Sep == ')' && "Bogus separator!");
1021 // When handling the close parenthesis, we greedily consume them to avoid
1022 // empty strings in the pipeline.
1023 do {
1024 // If we try to pop the outer pipeline we have unbalanced parentheses.
1025 if (PipelineStack.size() == 1)
1026 return None;
1027
1028 PipelineStack.pop_back();
1029 } while (Text.consume_front(")"));
1030
1031 // Check if we've finished parsing.
1032 if (Text.empty())
1033 break;
1034
1035 // Otherwise, the end of an inner pipeline always has to be followed by
1036 // a comma, and then we can continue.
1037 if (!Text.consume_front(","))
1038 return None;
1039 }
1040
1041 if (PipelineStack.size() > 1)
1042 // Unbalanced paretheses.
1043 return None;
1044
1045 assert(PipelineStack.back() == &ResultPipeline &&
1046 "Wrong pipeline at the bottom of the stack!");
1047 return {std::move(ResultPipeline)};
1048}
1049
1050bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1051 const PipelineElement &E, bool VerifyEachPass,
1052 bool DebugLogging) {
1053 auto &Name = E.Name;
1054 auto &InnerPipeline = E.InnerPipeline;
1055
1056 // First handle complex passes like the pass managers which carry pipelines.
1057 if (!InnerPipeline.empty()) {
1058 if (Name == "module") {
1059 ModulePassManager NestedMPM(DebugLogging);
1060 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1061 DebugLogging))
1062 return false;
1063 MPM.addPass(std::move(NestedMPM));
1064 return true;
1065 }
1066 if (Name == "cgscc") {
1067 CGSCCPassManager CGPM(DebugLogging);
1068 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1069 DebugLogging))
1070 return false;
1071 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1072 DebugLogging));
1073 return true;
1074 }
1075 if (Name == "function") {
1076 FunctionPassManager FPM(DebugLogging);
1077 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1078 DebugLogging))
1079 return false;
1080 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1081 return true;
1082 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001083 if (auto Count = parseRepeatPassName(Name)) {
1084 ModulePassManager NestedMPM(DebugLogging);
1085 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1086 DebugLogging))
1087 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001088 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001089 return true;
1090 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001091 // Normal passes can't have pipelines.
1092 return false;
1093 }
1094
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001095 // Manually handle aliases for pre-configured pipeline fragments.
1096 if (Name.startswith("default") || Name.startswith("lto")) {
1097 SmallVector<StringRef, 3> Matches;
1098 if (!DefaultAliasRegex.match(Name, &Matches))
1099 return false;
1100 assert(Matches.size() == 3 && "Must capture two matched strings!");
1101
Jordan Rosef85a95f2016-07-25 18:34:51 +00001102 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1103 .Case("O0", O0)
1104 .Case("O1", O1)
1105 .Case("O2", O2)
1106 .Case("O3", O3)
1107 .Case("Os", Os)
1108 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001109 if (L == O0)
1110 // At O0 we do nothing at all!
1111 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001112
1113 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001114 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001115 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001116 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001117 } else {
1118 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001119 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001120 }
1121 return true;
1122 }
1123
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001124 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001125#define MODULE_PASS(NAME, CREATE_PASS) \
1126 if (Name == NAME) { \
1127 MPM.addPass(CREATE_PASS); \
1128 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001129 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001130#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1131 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001132 MPM.addPass( \
1133 RequireAnalysisPass< \
1134 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001135 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001136 } \
1137 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001138 MPM.addPass(InvalidateAnalysisPass< \
1139 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001140 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001141 }
1142#include "PassRegistry.def"
1143
Chandler Carruth66445382014-01-11 08:16:35 +00001144 return false;
1145}
1146
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001147bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1148 const PipelineElement &E, bool VerifyEachPass,
1149 bool DebugLogging) {
1150 auto &Name = E.Name;
1151 auto &InnerPipeline = E.InnerPipeline;
1152
1153 // First handle complex passes like the pass managers which carry pipelines.
1154 if (!InnerPipeline.empty()) {
1155 if (Name == "cgscc") {
1156 CGSCCPassManager NestedCGPM(DebugLogging);
1157 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1158 DebugLogging))
1159 return false;
1160 // Add the nested pass manager with the appropriate adaptor.
1161 CGPM.addPass(std::move(NestedCGPM));
1162 return true;
1163 }
1164 if (Name == "function") {
1165 FunctionPassManager FPM(DebugLogging);
1166 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1167 DebugLogging))
1168 return false;
1169 // Add the nested pass manager with the appropriate adaptor.
1170 CGPM.addPass(
1171 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1172 return true;
1173 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001174 if (auto Count = parseRepeatPassName(Name)) {
1175 CGSCCPassManager NestedCGPM(DebugLogging);
1176 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1177 DebugLogging))
1178 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001179 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001180 return true;
1181 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001182 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1183 CGSCCPassManager NestedCGPM(DebugLogging);
1184 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1185 DebugLogging))
1186 return false;
1187 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1188 *MaxRepetitions, DebugLogging));
1189 return true;
1190 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001191 // Normal passes can't have pipelines.
1192 return false;
1193 }
1194
1195 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001196#define CGSCC_PASS(NAME, CREATE_PASS) \
1197 if (Name == NAME) { \
1198 CGPM.addPass(CREATE_PASS); \
1199 return true; \
1200 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001201#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1202 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001203 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001204 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001205 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1206 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001207 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001208 } \
1209 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001210 CGPM.addPass(InvalidateAnalysisPass< \
1211 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001212 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001213 }
1214#include "PassRegistry.def"
1215
Chandler Carruth572e3402014-04-21 11:12:00 +00001216 return false;
1217}
1218
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001219bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1220 const PipelineElement &E,
1221 bool VerifyEachPass, bool DebugLogging) {
1222 auto &Name = E.Name;
1223 auto &InnerPipeline = E.InnerPipeline;
1224
1225 // First handle complex passes like the pass managers which carry pipelines.
1226 if (!InnerPipeline.empty()) {
1227 if (Name == "function") {
1228 FunctionPassManager NestedFPM(DebugLogging);
1229 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1230 DebugLogging))
1231 return false;
1232 // Add the nested pass manager with the appropriate adaptor.
1233 FPM.addPass(std::move(NestedFPM));
1234 return true;
1235 }
1236 if (Name == "loop") {
1237 LoopPassManager LPM(DebugLogging);
1238 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1239 DebugLogging))
1240 return false;
1241 // Add the nested pass manager with the appropriate adaptor.
1242 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1243 return true;
1244 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001245 if (auto Count = parseRepeatPassName(Name)) {
1246 FunctionPassManager NestedFPM(DebugLogging);
1247 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1248 DebugLogging))
1249 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001250 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001251 return true;
1252 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001253 // Normal passes can't have pipelines.
1254 return false;
1255 }
1256
1257 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001258#define FUNCTION_PASS(NAME, CREATE_PASS) \
1259 if (Name == NAME) { \
1260 FPM.addPass(CREATE_PASS); \
1261 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001262 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001263#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1264 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001265 FPM.addPass( \
1266 RequireAnalysisPass< \
1267 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001268 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001269 } \
1270 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001271 FPM.addPass(InvalidateAnalysisPass< \
1272 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001273 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001274 }
1275#include "PassRegistry.def"
1276
Chandler Carruthd8330982014-01-12 09:34:22 +00001277 return false;
1278}
1279
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001280bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001281 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001282 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001283 auto &InnerPipeline = E.InnerPipeline;
1284
1285 // First handle complex passes like the pass managers which carry pipelines.
1286 if (!InnerPipeline.empty()) {
1287 if (Name == "loop") {
1288 LoopPassManager NestedLPM(DebugLogging);
1289 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1290 DebugLogging))
1291 return false;
1292 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001293 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001294 return true;
1295 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001296 if (auto Count = parseRepeatPassName(Name)) {
1297 LoopPassManager NestedLPM(DebugLogging);
1298 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1299 DebugLogging))
1300 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001301 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001302 return true;
1303 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001304 // Normal passes can't have pipelines.
1305 return false;
1306 }
1307
1308 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001309#define LOOP_PASS(NAME, CREATE_PASS) \
1310 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001311 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001312 return true; \
1313 }
1314#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1315 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001316 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001317 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1318 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1319 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001320 return true; \
1321 } \
1322 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001323 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001324 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001325 return true; \
1326 }
1327#include "PassRegistry.def"
1328
1329 return false;
1330}
1331
Chandler Carruthedf59962016-02-18 09:45:17 +00001332bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001333#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1334 if (Name == NAME) { \
1335 AA.registerModuleAnalysis< \
1336 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1337 return true; \
1338 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001339#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1340 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001341 AA.registerFunctionAnalysis< \
1342 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001343 return true; \
1344 }
1345#include "PassRegistry.def"
1346
1347 return false;
1348}
1349
Justin Bognereecc3c82016-02-25 07:23:08 +00001350bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001351 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001352 bool VerifyEachPass,
1353 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001354 for (const auto &Element : Pipeline) {
1355 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1356 return false;
1357 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001358 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001359 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001360}
1361
Chandler Carruth1ff77242015-03-07 09:02:36 +00001362bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001363 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001364 bool VerifyEachPass,
1365 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001366 for (const auto &Element : Pipeline) {
1367 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1368 return false;
1369 if (VerifyEachPass)
1370 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001371 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001372 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001373}
1374
Chandler Carruth1ff77242015-03-07 09:02:36 +00001375bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
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 (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1381 return false;
1382 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001383 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001384 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001385}
1386
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001387void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1388 FunctionAnalysisManager &FAM,
1389 CGSCCAnalysisManager &CGAM,
1390 ModuleAnalysisManager &MAM) {
1391 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1392 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001393 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1394 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1395 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1396 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1397 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1398}
1399
Chandler Carruth1ff77242015-03-07 09:02:36 +00001400bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001401 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001402 bool VerifyEachPass,
1403 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001404 for (const auto &Element : Pipeline) {
1405 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1406 return false;
1407 if (VerifyEachPass)
1408 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001409 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001410 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001411}
1412
1413// Primary pass pipeline description parsing routine.
1414// FIXME: Should this routine accept a TargetMachine or require the caller to
1415// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001416bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1417 StringRef PipelineText, bool VerifyEachPass,
1418 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001419 auto Pipeline = parsePipelineText(PipelineText);
1420 if (!Pipeline || Pipeline->empty())
1421 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001422
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001423 // If the first name isn't at the module layer, wrap the pipeline up
1424 // automatically.
1425 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001426
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001427 if (!isModulePassName(FirstName)) {
1428 if (isCGSCCPassName(FirstName))
1429 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1430 else if (isFunctionPassName(FirstName))
1431 Pipeline = {{"function", std::move(*Pipeline)}};
1432 else if (isLoopPassName(FirstName))
1433 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1434 else
1435 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001436 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001437 }
1438
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001439 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001440}
Chandler Carruthedf59962016-02-18 09:45:17 +00001441
1442bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001443 // If the pipeline just consists of the word 'default' just replace the AA
1444 // manager with our default one.
1445 if (PipelineText == "default") {
1446 AA = buildDefaultAAPipeline();
1447 return true;
1448 }
1449
Chandler Carruthedf59962016-02-18 09:45:17 +00001450 while (!PipelineText.empty()) {
1451 StringRef Name;
1452 std::tie(Name, PipelineText) = PipelineText.split(',');
1453 if (!parseAAPassName(AA, Name))
1454 return false;
1455 }
1456
1457 return true;
1458}