blob: 0feb51fa9dc7af8e8627d966f9456d6920e19c1e [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 Carruthf49f1a872015-12-27 08:13:45 +0000128#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000129#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000130#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000131#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000132#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000133#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000134#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000135#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000136#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000137#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000138#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000139#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000140#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000141#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000142#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000143#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000144#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000145
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000146#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000147
148using namespace llvm;
149
Chandler Carruth719ffe12017-02-12 05:38:04 +0000150static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
151 cl::ReallyHidden, cl::init(4));
152
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000153static cl::opt<bool> EnableGVNHoist(
154 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
155 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
156
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000157static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
158
Chandler Carruthe3f50642016-12-22 06:59:15 +0000159static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
160 switch (Level) {
161 case PassBuilder::O0:
162 case PassBuilder::O1:
163 case PassBuilder::O2:
164 case PassBuilder::O3:
165 return false;
166
167 case PassBuilder::Os:
168 case PassBuilder::Oz:
169 return true;
170 }
171 llvm_unreachable("Invalid optimization level!");
172}
173
Chandler Carruth66445382014-01-11 08:16:35 +0000174namespace {
175
Chandler Carruthd8330982014-01-12 09:34:22 +0000176/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000177struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000178 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000179 return PreservedAnalyses::all();
180 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000181 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000182};
183
Chandler Carruth0b576b32015-01-06 02:50:06 +0000184/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000185class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
186 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000187 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000188
189public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000190 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000191 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000192 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000193};
194
Chandler Carruth572e3402014-04-21 11:12:00 +0000195/// \brief No-op CGSCC pass which does nothing.
196struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000197 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
198 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000199 return PreservedAnalyses::all();
200 }
201 static StringRef name() { return "NoOpCGSCCPass"; }
202};
203
Chandler Carruth0b576b32015-01-06 02:50:06 +0000204/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000205class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
206 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000207 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000208
209public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000210 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000211 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000212 return Result();
213 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000214 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000215};
216
Chandler Carruthd8330982014-01-12 09:34:22 +0000217/// \brief No-op function pass which does nothing.
218struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000219 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000220 return PreservedAnalyses::all();
221 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000222 static StringRef name() { return "NoOpFunctionPass"; }
223};
224
Chandler Carruth0b576b32015-01-06 02:50:06 +0000225/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000226class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
227 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000228 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000229
230public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000231 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000232 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000233 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234};
235
Justin Bognereecc3c82016-02-25 07:23:08 +0000236/// \brief No-op loop pass which does nothing.
237struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000238 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
239 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000240 return PreservedAnalyses::all();
241 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000242 static StringRef name() { return "NoOpLoopPass"; }
243};
244
245/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000246class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
247 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000248 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000249
250public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000251 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000252 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
253 return Result();
254 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000255 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000256};
257
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000258AnalysisKey NoOpModuleAnalysis::Key;
259AnalysisKey NoOpCGSCCAnalysis::Key;
260AnalysisKey NoOpFunctionAnalysis::Key;
261AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000262
Chandler Carruth66445382014-01-11 08:16:35 +0000263} // End anonymous namespace.
264
Chandler Carruth1ff77242015-03-07 09:02:36 +0000265void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000266#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000267 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000268#include "PassRegistry.def"
269}
270
Chandler Carruth1ff77242015-03-07 09:02:36 +0000271void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000272#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000273 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000274#include "PassRegistry.def"
275}
276
Chandler Carruth1ff77242015-03-07 09:02:36 +0000277void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000278#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000279 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000280#include "PassRegistry.def"
281}
282
Justin Bognereecc3c82016-02-25 07:23:08 +0000283void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000284#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000285 LAM.registerPass([&] { return CREATE_PASS; });
286#include "PassRegistry.def"
287}
288
Chandler Carruthe3f50642016-12-22 06:59:15 +0000289FunctionPassManager
290PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
291 bool DebugLogging) {
292 assert(Level != O0 && "Must request optimizations!");
293 FunctionPassManager FPM(DebugLogging);
294
295 // Form SSA out of local memory accesses after breaking apart aggregates into
296 // scalars.
297 FPM.addPass(SROA());
298
299 // Catch trivial redundancies
300 FPM.addPass(EarlyCSEPass());
301
302 // Speculative execution if the target has divergent branches; otherwise nop.
303 FPM.addPass(SpeculativeExecutionPass());
304
305 // Optimize based on known information about branches, and cleanup afterward.
306 FPM.addPass(JumpThreadingPass());
307 FPM.addPass(CorrelatedValuePropagationPass());
308 FPM.addPass(SimplifyCFGPass());
309 FPM.addPass(InstCombinePass());
310
311 if (!isOptimizingForSize(Level))
312 FPM.addPass(LibCallsShrinkWrapPass());
313
314 FPM.addPass(TailCallElimPass());
315 FPM.addPass(SimplifyCFGPass());
316
317 // Form canonically associated expression trees, and simplify the trees using
318 // basic mathematical properties. For example, this will form (nearly)
319 // minimal multiplication trees.
320 FPM.addPass(ReassociatePass());
321
322 // Add the primary loop simplification pipeline.
323 // FIXME: Currently this is split into two loop pass pipelines because we run
324 // some function passes in between them. These can and should be replaced by
325 // loop pass equivalenst but those aren't ready yet. Specifically,
326 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
327 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
328 // the other we have is `LoopInstSimplify`.
329 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
330
331 // Rotate Loop - disable header duplication at -Oz
332 LPM1.addPass(LoopRotatePass(Level != Oz));
333 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000334#if 0
335 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000336 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000337#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000338 LPM2.addPass(IndVarSimplifyPass());
339 LPM2.addPass(LoopIdiomRecognizePass());
340 LPM2.addPass(LoopDeletionPass());
Dehao Chen7d230322017-02-18 03:46:51 +0000341 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000342
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000343 // We provide the opt remark emitter pass for LICM to use. We only need to do
344 // this once as it is immutable.
345 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000346 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
347 FPM.addPass(SimplifyCFGPass());
348 FPM.addPass(InstCombinePass());
349 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
350
351 // Eliminate redundancies.
352 if (Level != O1) {
353 // These passes add substantial compile time so skip them at O1.
354 FPM.addPass(MergedLoadStoreMotionPass());
355 FPM.addPass(GVN());
356 }
357
358 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
359 FPM.addPass(MemCpyOptPass());
360
361 // Sparse conditional constant propagation.
362 // FIXME: It isn't clear why we do this *after* loop passes rather than
363 // before...
364 FPM.addPass(SCCPPass());
365
366 // Delete dead bit computations (instcombine runs after to fold away the dead
367 // computations, and then ADCE will run later to exploit any new DCE
368 // opportunities that creates).
369 FPM.addPass(BDCEPass());
370
371 // Run instcombine after redundancy and dead bit elimination to exploit
372 // opportunities opened up by them.
373 FPM.addPass(InstCombinePass());
374
375 // Re-consider control flow based optimizations after redundancy elimination,
376 // redo DCE, etc.
377 FPM.addPass(JumpThreadingPass());
378 FPM.addPass(CorrelatedValuePropagationPass());
379 FPM.addPass(DSEPass());
380 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
381
382 // Finally, do an expensive DCE pass to catch all the dead code exposed by
383 // the simplifications and basic cleanup after all the simplifications.
384 FPM.addPass(ADCEPass());
385 FPM.addPass(SimplifyCFGPass());
386 FPM.addPass(InstCombinePass());
387
388 return FPM;
389}
390
Davide Italiano513dfaa2017-02-13 15:26:22 +0000391static void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
392 PassBuilder::OptimizationLevel Level,
393 bool RunProfileGen, std::string ProfileGenFile,
394 std::string ProfileUseFile) {
395 // Generally running simplification passes and the inliner with an high
396 // threshold results in smaller executables, but there may be cases where
397 // the size grows, so let's be conservative here and skip this simplification
398 // at -Os/Oz.
399 if (!isOptimizingForSize(Level)) {
400 InlineParams IP;
401
402 // In the old pass manager, this is a cl::opt. Should still this be one?
403 IP.DefaultThreshold = 75;
404
405 // FIXME: The hint threshold has the same value used by the regular inliner.
406 // This should probably be lowered after performance testing.
407 // FIXME: this comment is cargo culted from the old pass manager, revisit).
408 IP.HintThreshold = 325;
409
410 CGSCCPassManager CGPipeline(DebugLogging);
411
412 CGPipeline.addPass(InlinerPass(IP));
413
414 FunctionPassManager FPM;
415 FPM.addPass(SROA());
416 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
417 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
418 FPM.addPass(InstCombinePass()); // Combine silly sequences.
419
420 // FIXME: Here the old pass manager inserts peephole extensions.
421 // Add them when they're supported.
422 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
423
424 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
425 }
426
427 if (RunProfileGen) {
428 MPM.addPass(PGOInstrumentationGen());
429
430 // Add the profile lowering pass.
431 InstrProfOptions Options;
432 if (!ProfileGenFile.empty())
433 Options.InstrProfileOutput = ProfileGenFile;
434 MPM.addPass(InstrProfiling(Options));
435 }
436
437 if (!ProfileUseFile.empty())
438 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
439}
440
Chandler Carruthe3f50642016-12-22 06:59:15 +0000441ModulePassManager
442PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
443 bool DebugLogging) {
444 assert(Level != O0 && "Must request optimizations for the default pipeline!");
445 ModulePassManager MPM(DebugLogging);
446
447 // Force any function attributes we want the rest of the pipeline te observe.
448 MPM.addPass(ForceFunctionAttrsPass());
449
450 // Do basic inference of function attributes from known properties of system
451 // libraries and other oracles.
452 MPM.addPass(InferFunctionAttrsPass());
453
454 // Create an early function pass manager to cleanup the output of the
455 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000456 FunctionPassManager EarlyFPM(DebugLogging);
457 EarlyFPM.addPass(SimplifyCFGPass());
458 EarlyFPM.addPass(SROA());
459 EarlyFPM.addPass(EarlyCSEPass());
460 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000461 if (EnableGVNHoist)
462 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000463 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000464
465 // Interprocedural constant propagation now that basic cleanup has occured
466 // and prior to optimizing globals.
467 // FIXME: This position in the pipeline hasn't been carefully considered in
468 // years, it should be re-analyzed.
469 MPM.addPass(IPSCCPPass());
470
471 // Optimize globals to try and fold them into constants.
472 MPM.addPass(GlobalOptPass());
473
474 // Promote any localized globals to SSA registers.
475 // FIXME: Should this instead by a run of SROA?
476 // FIXME: We should probably run instcombine and simplify-cfg afterward to
477 // delete control flows that are dead once globals have been folded to
478 // constants.
479 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
480
481 // Remove any dead arguments exposed by cleanups and constand folding
482 // globals.
483 MPM.addPass(DeadArgumentEliminationPass());
484
485 // Create a small function pass pipeline to cleanup after all the global
486 // optimizations.
487 FunctionPassManager GlobalCleanupPM(DebugLogging);
488 GlobalCleanupPM.addPass(InstCombinePass());
489 GlobalCleanupPM.addPass(SimplifyCFGPass());
490 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
491
Davide Italiano513dfaa2017-02-13 15:26:22 +0000492 // Add all the requested passes for PGO Instrumentation, if requested.
493 if (PGOOpt) {
Dehao Chencc75d242017-02-23 22:15:18 +0000494 assert(PGOOpt->RunProfileGen || PGOOpt->SamplePGO ||
495 !PGOOpt->ProfileUseFile.empty());
Davide Italiano513dfaa2017-02-13 15:26:22 +0000496 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
497 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
498 }
499
500 // Indirect call promotion that promotes intra-module targes only.
Dehao Chencc75d242017-02-23 22:15:18 +0000501 MPM.addPass(PGOIndirectCallPromotion(false, PGOOpt && PGOOpt->SamplePGO));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000502
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000503 // Require the GlobalsAA analysis for the module so we can query it within
504 // the CGSCC pipeline.
505 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000506
507 // Now begin the main postorder CGSCC pipeline.
508 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
509 // manager and trying to emulate its precise behavior. Much of this doesn't
510 // make a lot of sense and we should revisit the core CGSCC structure.
511 CGSCCPassManager MainCGPipeline(DebugLogging);
512
513 // Note: historically, the PruneEH pass was run first to deduce nounwind and
514 // generally clean up exception handling overhead. It isn't clear this is
515 // valuable as the inliner doesn't currently care whether it is inlining an
516 // invoke or a call.
517
518 // Run the inliner first. The theory is that we are walking bottom-up and so
519 // the callees have already been fully optimized, and we want to inline them
520 // into the callers so that our optimizations can reflect that.
521 // FIXME; Customize the threshold based on optimization level.
522 MainCGPipeline.addPass(InlinerPass());
523
524 // Now deduce any function attributes based in the current code.
525 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
526
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000527 // When at O3 add argument promotion to the pass pipeline.
528 // FIXME: It isn't at all clear why this should be limited to O3.
529 if (Level == O3)
530 MainCGPipeline.addPass(ArgumentPromotionPass());
531
Chandler Carruthe3f50642016-12-22 06:59:15 +0000532 // Lastly, add the core function simplification pipeline nested inside the
533 // CGSCC walk.
534 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
535 buildFunctionSimplificationPipeline(Level, DebugLogging)));
536
Chandler Carruth719ffe12017-02-12 05:38:04 +0000537 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
538 // to detect when we devirtualize indirect calls and iterate the SCC passes
539 // in that case to try and catch knock-on inlining or function attrs
540 // opportunities. Then we add it to the module pipeline by walking the SCCs
541 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000542 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000543 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
544 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000545
546 // This ends the canonicalization and simplification phase of the pipeline.
547 // At this point, we expect to have canonical and simple IR which we begin
548 // *optimizing* for efficient execution going forward.
549
550 // Eliminate externally available functions now that inlining is over -- we
551 // won't emit these anyways.
552 MPM.addPass(EliminateAvailableExternallyPass());
553
554 // Do RPO function attribute inference across the module to forward-propagate
555 // attributes where applicable.
556 // FIXME: Is this really an optimization rather than a canonicalization?
557 MPM.addPass(ReversePostOrderFunctionAttrsPass());
558
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000559 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000560 // useful as the above will have inlined, DCE'ed, and function-attr
561 // propagated everything. We should at this point have a reasonably minimal
562 // and richly annotated call graph. By computing aliasing and mod/ref
563 // information for all local globals here, the late loop passes and notably
564 // the vectorizer will be able to use them to help recognize vectorizable
565 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000566 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000567
568 FunctionPassManager OptimizePM(DebugLogging);
569 OptimizePM.addPass(Float2IntPass());
570 // FIXME: We need to run some loop optimizations to re-rotate loops after
571 // simplify-cfg and others undo their rotation.
572
573 // Optimize the loop execution. These passes operate on entire loop nests
574 // rather than on each loop in an inside-out manner, and so they are actually
575 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000576
577 // First rotate loops that may have been un-rotated by prior passes.
578 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
579
580 // Distribute loops to allow partial vectorization. I.e. isolate dependences
581 // into separate loop that would otherwise inhibit vectorization. This is
582 // currently only performed for loops marked with the metadata
583 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000584 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000585
586 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000587 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000588
Chandler Carruthbaabda92017-01-27 01:32:26 +0000589 // Eliminate loads by forwarding stores from the previous iteration to loads
590 // of the current iteration.
591 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000592
593 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000594 OptimizePM.addPass(InstCombinePass());
595
Chandler Carrutha95ff382017-01-27 00:50:21 +0000596
597 // Now that we've formed fast to execute loop structures, we do further
598 // optimizations. These are run afterward as they might block doing complex
599 // analyses and transforms such as what are needed for loop vectorization.
600
Chandler Carruthe3f50642016-12-22 06:59:15 +0000601 // Optimize parallel scalar instruction chains into SIMD instructions.
602 OptimizePM.addPass(SLPVectorizerPass());
603
Chandler Carrutha95ff382017-01-27 00:50:21 +0000604 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000605 OptimizePM.addPass(SimplifyCFGPass());
606 OptimizePM.addPass(InstCombinePass());
607
608 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000609 // execution resources of an out-of-order processor. We also then need to
610 // clean up redundancies and loop invariant code.
611 // FIXME: It would be really good to use a loop-integrated instruction
612 // combiner for cleanup here so that the unrolling and LICM can be pipelined
613 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000614 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000615 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000616 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000617 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000618
619 // Now that we've vectorized and unrolled loops, we may have more refined
620 // alignment information, try to re-derive it here.
621 OptimizePM.addPass(AlignmentFromAssumptionsPass());
622
Chandler Carrutha95ff382017-01-27 00:50:21 +0000623 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
624 // canonicalization pass that enables other optimizations. As a result,
625 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
626 // result too early.
627 OptimizePM.addPass(LoopSinkPass());
628
629 // And finally clean up LCSSA form before generating code.
630 OptimizePM.addPass(InstSimplifierPass());
631
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000632 // LoopSink (and other loop passes since the last simplifyCFG) might have
633 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
634 OptimizePM.addPass(SimplifyCFGPass());
635
Chandler Carrutha95ff382017-01-27 00:50:21 +0000636 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000637 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
638
639 // Now we need to do some global optimization transforms.
640 // FIXME: It would seem like these should come first in the optimization
641 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
642 // ordering here.
643 MPM.addPass(GlobalDCEPass());
644 MPM.addPass(ConstantMergePass());
645
646 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000647}
648
Chandler Carruthe3f50642016-12-22 06:59:15 +0000649ModulePassManager
650PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
651 bool DebugLogging) {
652 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000653 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000654 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000655}
656
Chandler Carruthe3f50642016-12-22 06:59:15 +0000657ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
658 bool DebugLogging) {
659 assert(Level != O0 && "Must request optimizations for the default pipeline!");
660 ModulePassManager MPM(DebugLogging);
661
Davide Italiano089a9122017-01-24 00:57:39 +0000662 // Remove unused virtual tables to improve the quality of code generated by
663 // whole-program devirtualization and bitset lowering.
664 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000665
Davide Italiano089a9122017-01-24 00:57:39 +0000666 // Force any function attributes we want the rest of the pipeline to observe.
667 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000668
Davide Italiano089a9122017-01-24 00:57:39 +0000669 // Do basic inference of function attributes from known properties of system
670 // libraries and other oracles.
671 MPM.addPass(InferFunctionAttrsPass());
672
673 if (Level > 1) {
674 // Indirect call promotion. This should promote all the targets that are
675 // left by the earlier promotion pass that promotes intra-module targets.
676 // This two-step promotion is to save the compile time. For LTO, it should
677 // produce the same result as if we only do promotion here.
Dehao Chencc75d242017-02-23 22:15:18 +0000678 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
679 PGOOpt && PGOOpt->SamplePGO));
Davide Italiano089a9122017-01-24 00:57:39 +0000680
681 // Propagate constants at call sites into the functions they call. This
682 // opens opportunities for globalopt (and inlining) by substituting function
683 // pointers passed as arguments to direct uses of functions.
684 MPM.addPass(IPSCCPPass());
685 }
686
687 // Now deduce any function attributes based in the current code.
688 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
689 PostOrderFunctionAttrsPass()));
690
691 // Do RPO function attribute inference across the module to forward-propagate
692 // attributes where applicable.
693 // FIXME: Is this really an optimization rather than a canonicalization?
694 MPM.addPass(ReversePostOrderFunctionAttrsPass());
695
696 // Use inragne annotations on GEP indices to split globals where beneficial.
697 MPM.addPass(GlobalSplitPass());
698
699 // Run whole program optimization of virtual call when the list of callees
700 // is fixed.
701 MPM.addPass(WholeProgramDevirtPass());
702
703 // Stop here at -O1.
704 if (Level == 1)
705 return MPM;
706
707 // Optimize globals to try and fold them into constants.
708 MPM.addPass(GlobalOptPass());
709
710 // Promote any localized globals to SSA registers.
711 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
712
713 // Linking modules together can lead to duplicate global constant, only
714 // keep one copy of each constant.
715 MPM.addPass(ConstantMergePass());
716
717 // Remove unused arguments from functions.
718 MPM.addPass(DeadArgumentEliminationPass());
719
720 // Reduce the code after globalopt and ipsccp. Both can open up significant
721 // simplification opportunities, and both can propagate functions through
722 // function pointers. When this happens, we often have to resolve varargs
723 // calls, etc, so let instcombine do this.
724 // FIXME: add peephole extensions here as the legacy PM does.
725 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
726
727 // Note: historically, the PruneEH pass was run first to deduce nounwind and
728 // generally clean up exception handling overhead. It isn't clear this is
729 // valuable as the inliner doesn't currently care whether it is inlining an
730 // invoke or a call.
731 // Run the inliner now.
732 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
733
734 // Optimize globals again after we ran the inliner.
735 MPM.addPass(GlobalOptPass());
736
737 // Garbage collect dead functions.
738 // FIXME: Add ArgumentPromotion pass after once it's ported.
739 MPM.addPass(GlobalDCEPass());
740
741 FunctionPassManager FPM(DebugLogging);
742
743 // The IPO Passes may leave cruft around. Clean up after them.
744 // FIXME: add peephole extensions here as the legacy PM does.
745 FPM.addPass(InstCombinePass());
746 FPM.addPass(JumpThreadingPass());
747
748 // Break up allocas
749 FPM.addPass(SROA());
750
751 // Run a few AA driver optimizations here and now to cleanup the code.
752 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
753
754 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
755 PostOrderFunctionAttrsPass()));
756 // FIXME: here we run IP alias analysis in the legacy PM.
757
758 FunctionPassManager MainFPM;
759
760 // FIXME: once we fix LoopPass Manager, add LICM here.
761 // FIXME: once we provide support for enabling MLSM, add it here.
762 // FIXME: once we provide support for enabling NewGVN, add it here.
763 MainFPM.addPass(GVN());
764
765 // Remove dead memcpy()'s.
766 MainFPM.addPass(MemCpyOptPass());
767
768 // Nuke dead stores.
769 MainFPM.addPass(DSEPass());
770
771 // FIXME: at this point, we run a bunch of loop passes:
772 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
773 // loopVectorize. Enable them once the remaining issue with LPM
774 // are sorted out.
775
776 MainFPM.addPass(InstCombinePass());
777 MainFPM.addPass(SimplifyCFGPass());
778 MainFPM.addPass(SCCPPass());
779 MainFPM.addPass(InstCombinePass());
780 MainFPM.addPass(BDCEPass());
781
782 // FIXME: We may want to run SLPVectorizer here.
783 // After vectorization, assume intrinsics may tell us more
784 // about pointer alignments.
785#if 0
786 MainFPM.add(AlignmentFromAssumptionsPass());
787#endif
788
789 // FIXME: Conditionally run LoadCombine here, after it's ported
790 // (in case we still have this pass, given its questionable usefulness).
791
792 // FIXME: add peephole extensions to the PM here.
793 MainFPM.addPass(InstCombinePass());
794 MainFPM.addPass(JumpThreadingPass());
795 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
796
797 // Create a function that performs CFI checks for cross-DSO calls with
798 // targets in the current module.
799 MPM.addPass(CrossDSOCFIPass());
800
801 // Lower type metadata and the type.test intrinsic. This pass supports
802 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
803 // to be run at link time if CFI is enabled. This pass does nothing if
804 // CFI is disabled.
805 // Enable once we add support for the summary in the new PM.
806#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000807 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
808 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000809 Summary));
810#endif
811
812 // Add late LTO optimization passes.
813 // Delete basic blocks, which optimization passes may have killed.
814 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
815
816 // Drop bodies of available eternally objects to improve GlobalDCE.
817 MPM.addPass(EliminateAvailableExternallyPass());
818
819 // Now that we have optimized the program, discard unreachable functions.
820 MPM.addPass(GlobalDCEPass());
821
822 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000823 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000824}
825
Chandler Carruth060ad612016-12-23 20:38:19 +0000826AAManager PassBuilder::buildDefaultAAPipeline() {
827 AAManager AA;
828
829 // The order in which these are registered determines their priority when
830 // being queried.
831
832 // First we register the basic alias analysis that provides the majority of
833 // per-function local AA logic. This is a stateless, on-demand local set of
834 // AA techniques.
835 AA.registerFunctionAnalysis<BasicAA>();
836
837 // Next we query fast, specialized alias analyses that wrap IR-embedded
838 // information about aliasing.
839 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
840 AA.registerFunctionAnalysis<TypeBasedAA>();
841
842 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000843 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
844 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000845 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +0000846 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +0000847
848 return AA;
849}
850
Chandler Carruth241bf242016-08-03 07:44:48 +0000851static Optional<int> parseRepeatPassName(StringRef Name) {
852 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
853 return None;
854 int Count;
855 if (Name.getAsInteger(0, Count) || Count <= 0)
856 return None;
857 return Count;
858}
859
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000860static Optional<int> parseDevirtPassName(StringRef Name) {
861 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
862 return None;
863 int Count;
864 if (Name.getAsInteger(0, Count) || Count <= 0)
865 return None;
866 return Count;
867}
868
Chandler Carruth66445382014-01-11 08:16:35 +0000869static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000870 // Manually handle aliases for pre-configured pipeline fragments.
871 if (Name.startswith("default") || Name.startswith("lto"))
872 return DefaultAliasRegex.match(Name);
873
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000874 // Explicitly handle pass manager names.
875 if (Name == "module")
876 return true;
877 if (Name == "cgscc")
878 return true;
879 if (Name == "function")
880 return true;
881
Chandler Carruth241bf242016-08-03 07:44:48 +0000882 // Explicitly handle custom-parsed pass names.
883 if (parseRepeatPassName(Name))
884 return true;
885
Chandler Carruth74a8a222016-06-17 07:15:29 +0000886#define MODULE_PASS(NAME, CREATE_PASS) \
887 if (Name == NAME) \
888 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000889#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000890 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000891 return true;
892#include "PassRegistry.def"
893
Chandler Carruth66445382014-01-11 08:16:35 +0000894 return false;
895}
896
Chandler Carruth572e3402014-04-21 11:12:00 +0000897static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000898 // Explicitly handle pass manager names.
899 if (Name == "cgscc")
900 return true;
901 if (Name == "function")
902 return true;
903
Chandler Carruth241bf242016-08-03 07:44:48 +0000904 // Explicitly handle custom-parsed pass names.
905 if (parseRepeatPassName(Name))
906 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000907 if (parseDevirtPassName(Name))
908 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000909
Chandler Carruth74a8a222016-06-17 07:15:29 +0000910#define CGSCC_PASS(NAME, CREATE_PASS) \
911 if (Name == NAME) \
912 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000913#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000914 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000915 return true;
916#include "PassRegistry.def"
917
Chandler Carruth572e3402014-04-21 11:12:00 +0000918 return false;
919}
920
Chandler Carruthd8330982014-01-12 09:34:22 +0000921static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000922 // Explicitly handle pass manager names.
923 if (Name == "function")
924 return true;
925 if (Name == "loop")
926 return true;
927
Chandler Carruth241bf242016-08-03 07:44:48 +0000928 // Explicitly handle custom-parsed pass names.
929 if (parseRepeatPassName(Name))
930 return true;
931
Chandler Carruth74a8a222016-06-17 07:15:29 +0000932#define FUNCTION_PASS(NAME, CREATE_PASS) \
933 if (Name == NAME) \
934 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000935#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000936 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000937 return true;
938#include "PassRegistry.def"
939
Chandler Carruthd8330982014-01-12 09:34:22 +0000940 return false;
941}
942
Justin Bognereecc3c82016-02-25 07:23:08 +0000943static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000944 // Explicitly handle pass manager names.
945 if (Name == "loop")
946 return true;
947
Chandler Carruth241bf242016-08-03 07:44:48 +0000948 // Explicitly handle custom-parsed pass names.
949 if (parseRepeatPassName(Name))
950 return true;
951
Chandler Carruth74a8a222016-06-17 07:15:29 +0000952#define LOOP_PASS(NAME, CREATE_PASS) \
953 if (Name == NAME) \
954 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000955#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
956 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
957 return true;
958#include "PassRegistry.def"
959
960 return false;
961}
962
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000963Optional<std::vector<PassBuilder::PipelineElement>>
964PassBuilder::parsePipelineText(StringRef Text) {
965 std::vector<PipelineElement> ResultPipeline;
966
967 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
968 &ResultPipeline};
969 for (;;) {
970 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
971 size_t Pos = Text.find_first_of(",()");
972 Pipeline.push_back({Text.substr(0, Pos), {}});
973
974 // If we have a single terminating name, we're done.
975 if (Pos == Text.npos)
976 break;
977
978 char Sep = Text[Pos];
979 Text = Text.substr(Pos + 1);
980 if (Sep == ',')
981 // Just a name ending in a comma, continue.
982 continue;
983
984 if (Sep == '(') {
985 // Push the inner pipeline onto the stack to continue processing.
986 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
987 continue;
988 }
989
990 assert(Sep == ')' && "Bogus separator!");
991 // When handling the close parenthesis, we greedily consume them to avoid
992 // empty strings in the pipeline.
993 do {
994 // If we try to pop the outer pipeline we have unbalanced parentheses.
995 if (PipelineStack.size() == 1)
996 return None;
997
998 PipelineStack.pop_back();
999 } while (Text.consume_front(")"));
1000
1001 // Check if we've finished parsing.
1002 if (Text.empty())
1003 break;
1004
1005 // Otherwise, the end of an inner pipeline always has to be followed by
1006 // a comma, and then we can continue.
1007 if (!Text.consume_front(","))
1008 return None;
1009 }
1010
1011 if (PipelineStack.size() > 1)
1012 // Unbalanced paretheses.
1013 return None;
1014
1015 assert(PipelineStack.back() == &ResultPipeline &&
1016 "Wrong pipeline at the bottom of the stack!");
1017 return {std::move(ResultPipeline)};
1018}
1019
1020bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1021 const PipelineElement &E, bool VerifyEachPass,
1022 bool DebugLogging) {
1023 auto &Name = E.Name;
1024 auto &InnerPipeline = E.InnerPipeline;
1025
1026 // First handle complex passes like the pass managers which carry pipelines.
1027 if (!InnerPipeline.empty()) {
1028 if (Name == "module") {
1029 ModulePassManager NestedMPM(DebugLogging);
1030 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1031 DebugLogging))
1032 return false;
1033 MPM.addPass(std::move(NestedMPM));
1034 return true;
1035 }
1036 if (Name == "cgscc") {
1037 CGSCCPassManager CGPM(DebugLogging);
1038 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1039 DebugLogging))
1040 return false;
1041 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1042 DebugLogging));
1043 return true;
1044 }
1045 if (Name == "function") {
1046 FunctionPassManager FPM(DebugLogging);
1047 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1048 DebugLogging))
1049 return false;
1050 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1051 return true;
1052 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001053 if (auto Count = parseRepeatPassName(Name)) {
1054 ModulePassManager NestedMPM(DebugLogging);
1055 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1056 DebugLogging))
1057 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001058 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001059 return true;
1060 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001061 // Normal passes can't have pipelines.
1062 return false;
1063 }
1064
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001065 // Manually handle aliases for pre-configured pipeline fragments.
1066 if (Name.startswith("default") || Name.startswith("lto")) {
1067 SmallVector<StringRef, 3> Matches;
1068 if (!DefaultAliasRegex.match(Name, &Matches))
1069 return false;
1070 assert(Matches.size() == 3 && "Must capture two matched strings!");
1071
Jordan Rosef85a95f2016-07-25 18:34:51 +00001072 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1073 .Case("O0", O0)
1074 .Case("O1", O1)
1075 .Case("O2", O2)
1076 .Case("O3", O3)
1077 .Case("Os", Os)
1078 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001079 if (L == O0)
1080 // At O0 we do nothing at all!
1081 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001082
1083 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001084 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001085 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001086 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001087 } else {
1088 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001089 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001090 }
1091 return true;
1092 }
1093
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001094 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001095#define MODULE_PASS(NAME, CREATE_PASS) \
1096 if (Name == NAME) { \
1097 MPM.addPass(CREATE_PASS); \
1098 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001099 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001100#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1101 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001102 MPM.addPass( \
1103 RequireAnalysisPass< \
1104 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001105 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001106 } \
1107 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001108 MPM.addPass(InvalidateAnalysisPass< \
1109 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001110 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001111 }
1112#include "PassRegistry.def"
1113
Chandler Carruth66445382014-01-11 08:16:35 +00001114 return false;
1115}
1116
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001117bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1118 const PipelineElement &E, bool VerifyEachPass,
1119 bool DebugLogging) {
1120 auto &Name = E.Name;
1121 auto &InnerPipeline = E.InnerPipeline;
1122
1123 // First handle complex passes like the pass managers which carry pipelines.
1124 if (!InnerPipeline.empty()) {
1125 if (Name == "cgscc") {
1126 CGSCCPassManager NestedCGPM(DebugLogging);
1127 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1128 DebugLogging))
1129 return false;
1130 // Add the nested pass manager with the appropriate adaptor.
1131 CGPM.addPass(std::move(NestedCGPM));
1132 return true;
1133 }
1134 if (Name == "function") {
1135 FunctionPassManager FPM(DebugLogging);
1136 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1137 DebugLogging))
1138 return false;
1139 // Add the nested pass manager with the appropriate adaptor.
1140 CGPM.addPass(
1141 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1142 return true;
1143 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001144 if (auto Count = parseRepeatPassName(Name)) {
1145 CGSCCPassManager NestedCGPM(DebugLogging);
1146 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1147 DebugLogging))
1148 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001149 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001150 return true;
1151 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001152 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1153 CGSCCPassManager NestedCGPM(DebugLogging);
1154 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1155 DebugLogging))
1156 return false;
1157 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1158 *MaxRepetitions, DebugLogging));
1159 return true;
1160 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001161 // Normal passes can't have pipelines.
1162 return false;
1163 }
1164
1165 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001166#define CGSCC_PASS(NAME, CREATE_PASS) \
1167 if (Name == NAME) { \
1168 CGPM.addPass(CREATE_PASS); \
1169 return true; \
1170 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001171#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1172 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001173 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001174 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001175 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1176 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001177 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001178 } \
1179 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001180 CGPM.addPass(InvalidateAnalysisPass< \
1181 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001182 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001183 }
1184#include "PassRegistry.def"
1185
Chandler Carruth572e3402014-04-21 11:12:00 +00001186 return false;
1187}
1188
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001189bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1190 const PipelineElement &E,
1191 bool VerifyEachPass, bool DebugLogging) {
1192 auto &Name = E.Name;
1193 auto &InnerPipeline = E.InnerPipeline;
1194
1195 // First handle complex passes like the pass managers which carry pipelines.
1196 if (!InnerPipeline.empty()) {
1197 if (Name == "function") {
1198 FunctionPassManager NestedFPM(DebugLogging);
1199 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1200 DebugLogging))
1201 return false;
1202 // Add the nested pass manager with the appropriate adaptor.
1203 FPM.addPass(std::move(NestedFPM));
1204 return true;
1205 }
1206 if (Name == "loop") {
1207 LoopPassManager LPM(DebugLogging);
1208 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1209 DebugLogging))
1210 return false;
1211 // Add the nested pass manager with the appropriate adaptor.
1212 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1213 return true;
1214 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001215 if (auto Count = parseRepeatPassName(Name)) {
1216 FunctionPassManager NestedFPM(DebugLogging);
1217 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1218 DebugLogging))
1219 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001220 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001221 return true;
1222 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001223 // Normal passes can't have pipelines.
1224 return false;
1225 }
1226
1227 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001228#define FUNCTION_PASS(NAME, CREATE_PASS) \
1229 if (Name == NAME) { \
1230 FPM.addPass(CREATE_PASS); \
1231 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001232 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001233#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1234 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001235 FPM.addPass( \
1236 RequireAnalysisPass< \
1237 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001238 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001239 } \
1240 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001241 FPM.addPass(InvalidateAnalysisPass< \
1242 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001243 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001244 }
1245#include "PassRegistry.def"
1246
Chandler Carruthd8330982014-01-12 09:34:22 +00001247 return false;
1248}
1249
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001250bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001251 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001252 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001253 auto &InnerPipeline = E.InnerPipeline;
1254
1255 // First handle complex passes like the pass managers which carry pipelines.
1256 if (!InnerPipeline.empty()) {
1257 if (Name == "loop") {
1258 LoopPassManager NestedLPM(DebugLogging);
1259 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1260 DebugLogging))
1261 return false;
1262 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001263 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001264 return true;
1265 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001266 if (auto Count = parseRepeatPassName(Name)) {
1267 LoopPassManager NestedLPM(DebugLogging);
1268 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1269 DebugLogging))
1270 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001271 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001272 return true;
1273 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001274 // Normal passes can't have pipelines.
1275 return false;
1276 }
1277
1278 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001279#define LOOP_PASS(NAME, CREATE_PASS) \
1280 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001281 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001282 return true; \
1283 }
1284#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1285 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001286 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001287 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1288 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1289 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001290 return true; \
1291 } \
1292 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001293 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001294 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001295 return true; \
1296 }
1297#include "PassRegistry.def"
1298
1299 return false;
1300}
1301
Chandler Carruthedf59962016-02-18 09:45:17 +00001302bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001303#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1304 if (Name == NAME) { \
1305 AA.registerModuleAnalysis< \
1306 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1307 return true; \
1308 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001309#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1310 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001311 AA.registerFunctionAnalysis< \
1312 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001313 return true; \
1314 }
1315#include "PassRegistry.def"
1316
1317 return false;
1318}
1319
Justin Bognereecc3c82016-02-25 07:23:08 +00001320bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001321 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001322 bool VerifyEachPass,
1323 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001324 for (const auto &Element : Pipeline) {
1325 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1326 return false;
1327 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001328 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001329 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001330}
1331
Chandler Carruth1ff77242015-03-07 09:02:36 +00001332bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001333 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001334 bool VerifyEachPass,
1335 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001336 for (const auto &Element : Pipeline) {
1337 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1338 return false;
1339 if (VerifyEachPass)
1340 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001341 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001342 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001343}
1344
Chandler Carruth1ff77242015-03-07 09:02:36 +00001345bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001346 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001347 bool VerifyEachPass,
1348 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001349 for (const auto &Element : Pipeline) {
1350 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1351 return false;
1352 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001353 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001354 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001355}
1356
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001357void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1358 FunctionAnalysisManager &FAM,
1359 CGSCCAnalysisManager &CGAM,
1360 ModuleAnalysisManager &MAM) {
1361 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1362 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001363 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1364 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1365 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1366 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1367 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1368}
1369
Chandler Carruth1ff77242015-03-07 09:02:36 +00001370bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001371 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001372 bool VerifyEachPass,
1373 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001374 for (const auto &Element : Pipeline) {
1375 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1376 return false;
1377 if (VerifyEachPass)
1378 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001379 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001380 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001381}
1382
1383// Primary pass pipeline description parsing routine.
1384// FIXME: Should this routine accept a TargetMachine or require the caller to
1385// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001386bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1387 StringRef PipelineText, bool VerifyEachPass,
1388 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001389 auto Pipeline = parsePipelineText(PipelineText);
1390 if (!Pipeline || Pipeline->empty())
1391 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001392
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001393 // If the first name isn't at the module layer, wrap the pipeline up
1394 // automatically.
1395 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001396
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001397 if (!isModulePassName(FirstName)) {
1398 if (isCGSCCPassName(FirstName))
1399 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1400 else if (isFunctionPassName(FirstName))
1401 Pipeline = {{"function", std::move(*Pipeline)}};
1402 else if (isLoopPassName(FirstName))
1403 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1404 else
1405 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001406 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001407 }
1408
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001409 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001410}
Chandler Carruthedf59962016-02-18 09:45:17 +00001411
1412bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001413 // If the pipeline just consists of the word 'default' just replace the AA
1414 // manager with our default one.
1415 if (PipelineText == "default") {
1416 AA = buildDefaultAAPipeline();
1417 return true;
1418 }
1419
Chandler Carruthedf59962016-02-18 09:45:17 +00001420 while (!PipelineText.empty()) {
1421 StringRef Name;
1422 std::tie(Name, PipelineText) = PipelineText.split(',');
1423 if (!parseAAPassName(AA, Name))
1424 return false;
1425 }
1426
1427 return true;
1428}