blob: ac77b1d511bf13de2ef354d8ba6f9d37b4accd9d [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"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet79ac42a2016-07-18 16:29:21 +000043#include "llvm/Analysis/OptimizationDiagnosticInfo.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000044#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000045#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000046#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000047#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000048#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000049#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000050#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000051#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000052#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000053#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000054#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000055#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000056#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000057#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000058#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000059#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000060#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000061#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000062#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000063#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000064#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000065#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000066#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000067#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000068#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000069#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000070#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000071#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000072#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000073#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000074#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000075#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000076#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000077#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000078#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000079#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000080#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000081#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000082#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000083#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000084#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000085#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000086#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000087#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000088#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000089#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000090#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000091#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000092#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000093#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000094#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000095#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000096#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000097#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000098#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +000099#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000100#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000101#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000102#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000103#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000104#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000105#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000106#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000107#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000108#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000109#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000110#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000111#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000112#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000113#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000114#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000115#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000116#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000117#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000118#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000119#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000120#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000121#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000122#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000123#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000124#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000125#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000126#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000127#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000128#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000129#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000130#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000131#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000132#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000133#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000134#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000135#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000136#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000137#include "llvm/Transforms/Utils/Mem2Reg.h"
Geoff Berryb96d3b22016-06-01 21:30:40 +0000138#include "llvm/Transforms/Utils/MemorySSA.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 Carruth8b5a74192016-02-28 22:16:03 +0000153static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
154
Chandler Carruthe3f50642016-12-22 06:59:15 +0000155static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
156 switch (Level) {
157 case PassBuilder::O0:
158 case PassBuilder::O1:
159 case PassBuilder::O2:
160 case PassBuilder::O3:
161 return false;
162
163 case PassBuilder::Os:
164 case PassBuilder::Oz:
165 return true;
166 }
167 llvm_unreachable("Invalid optimization level!");
168}
169
Chandler Carruth66445382014-01-11 08:16:35 +0000170namespace {
171
Chandler Carruthd8330982014-01-12 09:34:22 +0000172/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000173struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000174 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000175 return PreservedAnalyses::all();
176 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000177 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000178};
179
Chandler Carruth0b576b32015-01-06 02:50:06 +0000180/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000181class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
182 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000183 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000184
185public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000186 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000187 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000188 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000189};
190
Chandler Carruth572e3402014-04-21 11:12:00 +0000191/// \brief No-op CGSCC pass which does nothing.
192struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000193 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
194 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000195 return PreservedAnalyses::all();
196 }
197 static StringRef name() { return "NoOpCGSCCPass"; }
198};
199
Chandler Carruth0b576b32015-01-06 02:50:06 +0000200/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000201class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
202 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000203 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000204
205public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000206 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000207 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000208 return Result();
209 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000210 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000211};
212
Chandler Carruthd8330982014-01-12 09:34:22 +0000213/// \brief No-op function pass which does nothing.
214struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000215 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000216 return PreservedAnalyses::all();
217 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000218 static StringRef name() { return "NoOpFunctionPass"; }
219};
220
Chandler Carruth0b576b32015-01-06 02:50:06 +0000221/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000222class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
223 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000224 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000225
226public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000227 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000228 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000229 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000230};
231
Justin Bognereecc3c82016-02-25 07:23:08 +0000232/// \brief No-op loop pass which does nothing.
233struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000234 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
235 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000236 return PreservedAnalyses::all();
237 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000238 static StringRef name() { return "NoOpLoopPass"; }
239};
240
241/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000242class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
243 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000244 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000245
246public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000247 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000248 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
249 return Result();
250 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000251 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000252};
253
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000254AnalysisKey NoOpModuleAnalysis::Key;
255AnalysisKey NoOpCGSCCAnalysis::Key;
256AnalysisKey NoOpFunctionAnalysis::Key;
257AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000258
Chandler Carruth66445382014-01-11 08:16:35 +0000259} // End anonymous namespace.
260
Chandler Carruth1ff77242015-03-07 09:02:36 +0000261void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000262#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000263 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000264#include "PassRegistry.def"
265}
266
Chandler Carruth1ff77242015-03-07 09:02:36 +0000267void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000268#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000269 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000270#include "PassRegistry.def"
271}
272
Chandler Carruth1ff77242015-03-07 09:02:36 +0000273void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000274#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000275 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000276#include "PassRegistry.def"
277}
278
Justin Bognereecc3c82016-02-25 07:23:08 +0000279void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000280#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000281 LAM.registerPass([&] { return CREATE_PASS; });
282#include "PassRegistry.def"
283}
284
Chandler Carruthe3f50642016-12-22 06:59:15 +0000285FunctionPassManager
286PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
287 bool DebugLogging) {
288 assert(Level != O0 && "Must request optimizations!");
289 FunctionPassManager FPM(DebugLogging);
290
291 // Form SSA out of local memory accesses after breaking apart aggregates into
292 // scalars.
293 FPM.addPass(SROA());
294
295 // Catch trivial redundancies
296 FPM.addPass(EarlyCSEPass());
297
298 // Speculative execution if the target has divergent branches; otherwise nop.
299 FPM.addPass(SpeculativeExecutionPass());
300
301 // Optimize based on known information about branches, and cleanup afterward.
302 FPM.addPass(JumpThreadingPass());
303 FPM.addPass(CorrelatedValuePropagationPass());
304 FPM.addPass(SimplifyCFGPass());
305 FPM.addPass(InstCombinePass());
306
307 if (!isOptimizingForSize(Level))
308 FPM.addPass(LibCallsShrinkWrapPass());
309
310 FPM.addPass(TailCallElimPass());
311 FPM.addPass(SimplifyCFGPass());
312
313 // Form canonically associated expression trees, and simplify the trees using
314 // basic mathematical properties. For example, this will form (nearly)
315 // minimal multiplication trees.
316 FPM.addPass(ReassociatePass());
317
318 // Add the primary loop simplification pipeline.
319 // FIXME: Currently this is split into two loop pass pipelines because we run
320 // some function passes in between them. These can and should be replaced by
321 // loop pass equivalenst but those aren't ready yet. Specifically,
322 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
323 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
324 // the other we have is `LoopInstSimplify`.
325 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
326
327 // Rotate Loop - disable header duplication at -Oz
328 LPM1.addPass(LoopRotatePass(Level != Oz));
329 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000330#if 0
331 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000332 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000333#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000334 LPM2.addPass(IndVarSimplifyPass());
335 LPM2.addPass(LoopIdiomRecognizePass());
336 LPM2.addPass(LoopDeletionPass());
Dehao Chen7d230322017-02-18 03:46:51 +0000337 LPM2.addPass(LoopUnrollPass::createFull(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000338
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000339 // We provide the opt remark emitter pass for LICM to use. We only need to do
340 // this once as it is immutable.
341 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000342 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
343 FPM.addPass(SimplifyCFGPass());
344 FPM.addPass(InstCombinePass());
345 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
346
347 // Eliminate redundancies.
348 if (Level != O1) {
349 // These passes add substantial compile time so skip them at O1.
350 FPM.addPass(MergedLoadStoreMotionPass());
351 FPM.addPass(GVN());
352 }
353
354 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
355 FPM.addPass(MemCpyOptPass());
356
357 // Sparse conditional constant propagation.
358 // FIXME: It isn't clear why we do this *after* loop passes rather than
359 // before...
360 FPM.addPass(SCCPPass());
361
362 // Delete dead bit computations (instcombine runs after to fold away the dead
363 // computations, and then ADCE will run later to exploit any new DCE
364 // opportunities that creates).
365 FPM.addPass(BDCEPass());
366
367 // Run instcombine after redundancy and dead bit elimination to exploit
368 // opportunities opened up by them.
369 FPM.addPass(InstCombinePass());
370
371 // Re-consider control flow based optimizations after redundancy elimination,
372 // redo DCE, etc.
373 FPM.addPass(JumpThreadingPass());
374 FPM.addPass(CorrelatedValuePropagationPass());
375 FPM.addPass(DSEPass());
376 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
377
378 // Finally, do an expensive DCE pass to catch all the dead code exposed by
379 // the simplifications and basic cleanup after all the simplifications.
380 FPM.addPass(ADCEPass());
381 FPM.addPass(SimplifyCFGPass());
382 FPM.addPass(InstCombinePass());
383
384 return FPM;
385}
386
Davide Italiano513dfaa2017-02-13 15:26:22 +0000387static void addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
388 PassBuilder::OptimizationLevel Level,
389 bool RunProfileGen, std::string ProfileGenFile,
390 std::string ProfileUseFile) {
391 // Generally running simplification passes and the inliner with an high
392 // threshold results in smaller executables, but there may be cases where
393 // the size grows, so let's be conservative here and skip this simplification
394 // at -Os/Oz.
395 if (!isOptimizingForSize(Level)) {
396 InlineParams IP;
397
398 // In the old pass manager, this is a cl::opt. Should still this be one?
399 IP.DefaultThreshold = 75;
400
401 // FIXME: The hint threshold has the same value used by the regular inliner.
402 // This should probably be lowered after performance testing.
403 // FIXME: this comment is cargo culted from the old pass manager, revisit).
404 IP.HintThreshold = 325;
405
406 CGSCCPassManager CGPipeline(DebugLogging);
407
408 CGPipeline.addPass(InlinerPass(IP));
409
410 FunctionPassManager FPM;
411 FPM.addPass(SROA());
412 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
413 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
414 FPM.addPass(InstCombinePass()); // Combine silly sequences.
415
416 // FIXME: Here the old pass manager inserts peephole extensions.
417 // Add them when they're supported.
418 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
419
420 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
421 }
422
423 if (RunProfileGen) {
424 MPM.addPass(PGOInstrumentationGen());
425
426 // Add the profile lowering pass.
427 InstrProfOptions Options;
428 if (!ProfileGenFile.empty())
429 Options.InstrProfileOutput = ProfileGenFile;
430 MPM.addPass(InstrProfiling(Options));
431 }
432
433 if (!ProfileUseFile.empty())
434 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
435}
436
Chandler Carruthe3f50642016-12-22 06:59:15 +0000437ModulePassManager
438PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
439 bool DebugLogging) {
440 assert(Level != O0 && "Must request optimizations for the default pipeline!");
441 ModulePassManager MPM(DebugLogging);
442
443 // Force any function attributes we want the rest of the pipeline te observe.
444 MPM.addPass(ForceFunctionAttrsPass());
445
446 // Do basic inference of function attributes from known properties of system
447 // libraries and other oracles.
448 MPM.addPass(InferFunctionAttrsPass());
449
450 // Create an early function pass manager to cleanup the output of the
451 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000452 FunctionPassManager EarlyFPM(DebugLogging);
453 EarlyFPM.addPass(SimplifyCFGPass());
454 EarlyFPM.addPass(SROA());
455 EarlyFPM.addPass(EarlyCSEPass());
456 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000457 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000458 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000459
460 // Interprocedural constant propagation now that basic cleanup has occured
461 // and prior to optimizing globals.
462 // FIXME: This position in the pipeline hasn't been carefully considered in
463 // years, it should be re-analyzed.
464 MPM.addPass(IPSCCPPass());
465
466 // Optimize globals to try and fold them into constants.
467 MPM.addPass(GlobalOptPass());
468
469 // Promote any localized globals to SSA registers.
470 // FIXME: Should this instead by a run of SROA?
471 // FIXME: We should probably run instcombine and simplify-cfg afterward to
472 // delete control flows that are dead once globals have been folded to
473 // constants.
474 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
475
476 // Remove any dead arguments exposed by cleanups and constand folding
477 // globals.
478 MPM.addPass(DeadArgumentEliminationPass());
479
480 // Create a small function pass pipeline to cleanup after all the global
481 // optimizations.
482 FunctionPassManager GlobalCleanupPM(DebugLogging);
483 GlobalCleanupPM.addPass(InstCombinePass());
484 GlobalCleanupPM.addPass(SimplifyCFGPass());
485 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
486
Davide Italiano513dfaa2017-02-13 15:26:22 +0000487 // Add all the requested passes for PGO Instrumentation, if requested.
488 if (PGOOpt) {
Dehao Chencc75d242017-02-23 22:15:18 +0000489 assert(PGOOpt->RunProfileGen || PGOOpt->SamplePGO ||
490 !PGOOpt->ProfileUseFile.empty());
Davide Italiano513dfaa2017-02-13 15:26:22 +0000491 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
492 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
493 }
494
495 // Indirect call promotion that promotes intra-module targes only.
Dehao Chencc75d242017-02-23 22:15:18 +0000496 MPM.addPass(PGOIndirectCallPromotion(false, PGOOpt && PGOOpt->SamplePGO));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000497
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000498 // Require the GlobalsAA analysis for the module so we can query it within
499 // the CGSCC pipeline.
500 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000501
502 // Now begin the main postorder CGSCC pipeline.
503 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
504 // manager and trying to emulate its precise behavior. Much of this doesn't
505 // make a lot of sense and we should revisit the core CGSCC structure.
506 CGSCCPassManager MainCGPipeline(DebugLogging);
507
508 // Note: historically, the PruneEH pass was run first to deduce nounwind and
509 // generally clean up exception handling overhead. It isn't clear this is
510 // valuable as the inliner doesn't currently care whether it is inlining an
511 // invoke or a call.
512
513 // Run the inliner first. The theory is that we are walking bottom-up and so
514 // the callees have already been fully optimized, and we want to inline them
515 // into the callers so that our optimizations can reflect that.
516 // FIXME; Customize the threshold based on optimization level.
517 MainCGPipeline.addPass(InlinerPass());
518
519 // Now deduce any function attributes based in the current code.
520 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
521
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000522 // When at O3 add argument promotion to the pass pipeline.
523 // FIXME: It isn't at all clear why this should be limited to O3.
524 if (Level == O3)
525 MainCGPipeline.addPass(ArgumentPromotionPass());
526
Chandler Carruthe3f50642016-12-22 06:59:15 +0000527 // Lastly, add the core function simplification pipeline nested inside the
528 // CGSCC walk.
529 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
530 buildFunctionSimplificationPipeline(Level, DebugLogging)));
531
Chandler Carruth719ffe12017-02-12 05:38:04 +0000532 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
533 // to detect when we devirtualize indirect calls and iterate the SCC passes
534 // in that case to try and catch knock-on inlining or function attrs
535 // opportunities. Then we add it to the module pipeline by walking the SCCs
536 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000537 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000538 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
539 std::move(MainCGPipeline), MaxDevirtIterations, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000540
541 // This ends the canonicalization and simplification phase of the pipeline.
542 // At this point, we expect to have canonical and simple IR which we begin
543 // *optimizing* for efficient execution going forward.
544
545 // Eliminate externally available functions now that inlining is over -- we
546 // won't emit these anyways.
547 MPM.addPass(EliminateAvailableExternallyPass());
548
549 // Do RPO function attribute inference across the module to forward-propagate
550 // attributes where applicable.
551 // FIXME: Is this really an optimization rather than a canonicalization?
552 MPM.addPass(ReversePostOrderFunctionAttrsPass());
553
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000554 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000555 // useful as the above will have inlined, DCE'ed, and function-attr
556 // propagated everything. We should at this point have a reasonably minimal
557 // and richly annotated call graph. By computing aliasing and mod/ref
558 // information for all local globals here, the late loop passes and notably
559 // the vectorizer will be able to use them to help recognize vectorizable
560 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000561 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000562
563 FunctionPassManager OptimizePM(DebugLogging);
564 OptimizePM.addPass(Float2IntPass());
565 // FIXME: We need to run some loop optimizations to re-rotate loops after
566 // simplify-cfg and others undo their rotation.
567
568 // Optimize the loop execution. These passes operate on entire loop nests
569 // rather than on each loop in an inside-out manner, and so they are actually
570 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000571
572 // First rotate loops that may have been un-rotated by prior passes.
573 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
574
575 // Distribute loops to allow partial vectorization. I.e. isolate dependences
576 // into separate loop that would otherwise inhibit vectorization. This is
577 // currently only performed for loops marked with the metadata
578 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000579 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000580
581 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000582 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000583
Chandler Carruthbaabda92017-01-27 01:32:26 +0000584 // Eliminate loads by forwarding stores from the previous iteration to loads
585 // of the current iteration.
586 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000587
588 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000589 OptimizePM.addPass(InstCombinePass());
590
Chandler Carrutha95ff382017-01-27 00:50:21 +0000591
592 // Now that we've formed fast to execute loop structures, we do further
593 // optimizations. These are run afterward as they might block doing complex
594 // analyses and transforms such as what are needed for loop vectorization.
595
Chandler Carruthe3f50642016-12-22 06:59:15 +0000596 // Optimize parallel scalar instruction chains into SIMD instructions.
597 OptimizePM.addPass(SLPVectorizerPass());
598
Chandler Carrutha95ff382017-01-27 00:50:21 +0000599 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000600 OptimizePM.addPass(SimplifyCFGPass());
601 OptimizePM.addPass(InstCombinePass());
602
603 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000604 // execution resources of an out-of-order processor. We also then need to
605 // clean up redundancies and loop invariant code.
606 // FIXME: It would be really good to use a loop-integrated instruction
607 // combiner for cleanup here so that the unrolling and LICM can be pipelined
608 // across the loop nests.
Dehao Chen7d230322017-02-18 03:46:51 +0000609 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create(Level)));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000610 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000611 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000612 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000613
614 // Now that we've vectorized and unrolled loops, we may have more refined
615 // alignment information, try to re-derive it here.
616 OptimizePM.addPass(AlignmentFromAssumptionsPass());
617
Chandler Carrutha95ff382017-01-27 00:50:21 +0000618 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
619 // canonicalization pass that enables other optimizations. As a result,
620 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
621 // result too early.
622 OptimizePM.addPass(LoopSinkPass());
623
624 // And finally clean up LCSSA form before generating code.
625 OptimizePM.addPass(InstSimplifierPass());
626
627 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000628 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
629
630 // Now we need to do some global optimization transforms.
631 // FIXME: It would seem like these should come first in the optimization
632 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
633 // ordering here.
634 MPM.addPass(GlobalDCEPass());
635 MPM.addPass(ConstantMergePass());
636
637 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000638}
639
Chandler Carruthe3f50642016-12-22 06:59:15 +0000640ModulePassManager
641PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
642 bool DebugLogging) {
643 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000644 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000645 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000646}
647
Chandler Carruthe3f50642016-12-22 06:59:15 +0000648ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
649 bool DebugLogging) {
650 assert(Level != O0 && "Must request optimizations for the default pipeline!");
651 ModulePassManager MPM(DebugLogging);
652
Davide Italiano089a9122017-01-24 00:57:39 +0000653 // Remove unused virtual tables to improve the quality of code generated by
654 // whole-program devirtualization and bitset lowering.
655 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000656
Davide Italiano089a9122017-01-24 00:57:39 +0000657 // Force any function attributes we want the rest of the pipeline to observe.
658 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000659
Davide Italiano089a9122017-01-24 00:57:39 +0000660 // Do basic inference of function attributes from known properties of system
661 // libraries and other oracles.
662 MPM.addPass(InferFunctionAttrsPass());
663
664 if (Level > 1) {
665 // Indirect call promotion. This should promote all the targets that are
666 // left by the earlier promotion pass that promotes intra-module targets.
667 // This two-step promotion is to save the compile time. For LTO, it should
668 // produce the same result as if we only do promotion here.
Dehao Chencc75d242017-02-23 22:15:18 +0000669 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
670 PGOOpt && PGOOpt->SamplePGO));
Davide Italiano089a9122017-01-24 00:57:39 +0000671
672 // Propagate constants at call sites into the functions they call. This
673 // opens opportunities for globalopt (and inlining) by substituting function
674 // pointers passed as arguments to direct uses of functions.
675 MPM.addPass(IPSCCPPass());
676 }
677
678 // Now deduce any function attributes based in the current code.
679 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
680 PostOrderFunctionAttrsPass()));
681
682 // Do RPO function attribute inference across the module to forward-propagate
683 // attributes where applicable.
684 // FIXME: Is this really an optimization rather than a canonicalization?
685 MPM.addPass(ReversePostOrderFunctionAttrsPass());
686
687 // Use inragne annotations on GEP indices to split globals where beneficial.
688 MPM.addPass(GlobalSplitPass());
689
690 // Run whole program optimization of virtual call when the list of callees
691 // is fixed.
692 MPM.addPass(WholeProgramDevirtPass());
693
694 // Stop here at -O1.
695 if (Level == 1)
696 return MPM;
697
698 // Optimize globals to try and fold them into constants.
699 MPM.addPass(GlobalOptPass());
700
701 // Promote any localized globals to SSA registers.
702 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
703
704 // Linking modules together can lead to duplicate global constant, only
705 // keep one copy of each constant.
706 MPM.addPass(ConstantMergePass());
707
708 // Remove unused arguments from functions.
709 MPM.addPass(DeadArgumentEliminationPass());
710
711 // Reduce the code after globalopt and ipsccp. Both can open up significant
712 // simplification opportunities, and both can propagate functions through
713 // function pointers. When this happens, we often have to resolve varargs
714 // calls, etc, so let instcombine do this.
715 // FIXME: add peephole extensions here as the legacy PM does.
716 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
717
718 // Note: historically, the PruneEH pass was run first to deduce nounwind and
719 // generally clean up exception handling overhead. It isn't clear this is
720 // valuable as the inliner doesn't currently care whether it is inlining an
721 // invoke or a call.
722 // Run the inliner now.
723 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
724
725 // Optimize globals again after we ran the inliner.
726 MPM.addPass(GlobalOptPass());
727
728 // Garbage collect dead functions.
729 // FIXME: Add ArgumentPromotion pass after once it's ported.
730 MPM.addPass(GlobalDCEPass());
731
732 FunctionPassManager FPM(DebugLogging);
733
734 // The IPO Passes may leave cruft around. Clean up after them.
735 // FIXME: add peephole extensions here as the legacy PM does.
736 FPM.addPass(InstCombinePass());
737 FPM.addPass(JumpThreadingPass());
738
739 // Break up allocas
740 FPM.addPass(SROA());
741
742 // Run a few AA driver optimizations here and now to cleanup the code.
743 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
744
745 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
746 PostOrderFunctionAttrsPass()));
747 // FIXME: here we run IP alias analysis in the legacy PM.
748
749 FunctionPassManager MainFPM;
750
751 // FIXME: once we fix LoopPass Manager, add LICM here.
752 // FIXME: once we provide support for enabling MLSM, add it here.
753 // FIXME: once we provide support for enabling NewGVN, add it here.
754 MainFPM.addPass(GVN());
755
756 // Remove dead memcpy()'s.
757 MainFPM.addPass(MemCpyOptPass());
758
759 // Nuke dead stores.
760 MainFPM.addPass(DSEPass());
761
762 // FIXME: at this point, we run a bunch of loop passes:
763 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
764 // loopVectorize. Enable them once the remaining issue with LPM
765 // are sorted out.
766
767 MainFPM.addPass(InstCombinePass());
768 MainFPM.addPass(SimplifyCFGPass());
769 MainFPM.addPass(SCCPPass());
770 MainFPM.addPass(InstCombinePass());
771 MainFPM.addPass(BDCEPass());
772
773 // FIXME: We may want to run SLPVectorizer here.
774 // After vectorization, assume intrinsics may tell us more
775 // about pointer alignments.
776#if 0
777 MainFPM.add(AlignmentFromAssumptionsPass());
778#endif
779
780 // FIXME: Conditionally run LoadCombine here, after it's ported
781 // (in case we still have this pass, given its questionable usefulness).
782
783 // FIXME: add peephole extensions to the PM here.
784 MainFPM.addPass(InstCombinePass());
785 MainFPM.addPass(JumpThreadingPass());
786 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
787
788 // Create a function that performs CFI checks for cross-DSO calls with
789 // targets in the current module.
790 MPM.addPass(CrossDSOCFIPass());
791
792 // Lower type metadata and the type.test intrinsic. This pass supports
793 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
794 // to be run at link time if CFI is enabled. This pass does nothing if
795 // CFI is disabled.
796 // Enable once we add support for the summary in the new PM.
797#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000798 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
799 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000800 Summary));
801#endif
802
803 // Add late LTO optimization passes.
804 // Delete basic blocks, which optimization passes may have killed.
805 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
806
807 // Drop bodies of available eternally objects to improve GlobalDCE.
808 MPM.addPass(EliminateAvailableExternallyPass());
809
810 // Now that we have optimized the program, discard unreachable functions.
811 MPM.addPass(GlobalDCEPass());
812
813 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000814 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000815}
816
Chandler Carruth060ad612016-12-23 20:38:19 +0000817AAManager PassBuilder::buildDefaultAAPipeline() {
818 AAManager AA;
819
820 // The order in which these are registered determines their priority when
821 // being queried.
822
823 // First we register the basic alias analysis that provides the majority of
824 // per-function local AA logic. This is a stateless, on-demand local set of
825 // AA techniques.
826 AA.registerFunctionAnalysis<BasicAA>();
827
828 // Next we query fast, specialized alias analyses that wrap IR-embedded
829 // information about aliasing.
830 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
831 AA.registerFunctionAnalysis<TypeBasedAA>();
832
833 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000834 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
835 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000836 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +0000837 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +0000838
839 return AA;
840}
841
Chandler Carruth241bf242016-08-03 07:44:48 +0000842static Optional<int> parseRepeatPassName(StringRef Name) {
843 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
844 return None;
845 int Count;
846 if (Name.getAsInteger(0, Count) || Count <= 0)
847 return None;
848 return Count;
849}
850
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000851static Optional<int> parseDevirtPassName(StringRef Name) {
852 if (!Name.consume_front("devirt<") || !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 Carruth66445382014-01-11 08:16:35 +0000860static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000861 // Manually handle aliases for pre-configured pipeline fragments.
862 if (Name.startswith("default") || Name.startswith("lto"))
863 return DefaultAliasRegex.match(Name);
864
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000865 // Explicitly handle pass manager names.
866 if (Name == "module")
867 return true;
868 if (Name == "cgscc")
869 return true;
870 if (Name == "function")
871 return true;
872
Chandler Carruth241bf242016-08-03 07:44:48 +0000873 // Explicitly handle custom-parsed pass names.
874 if (parseRepeatPassName(Name))
875 return true;
876
Chandler Carruth74a8a222016-06-17 07:15:29 +0000877#define MODULE_PASS(NAME, CREATE_PASS) \
878 if (Name == NAME) \
879 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000880#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000881 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000882 return true;
883#include "PassRegistry.def"
884
Chandler Carruth66445382014-01-11 08:16:35 +0000885 return false;
886}
887
Chandler Carruth572e3402014-04-21 11:12:00 +0000888static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000889 // Explicitly handle pass manager names.
890 if (Name == "cgscc")
891 return true;
892 if (Name == "function")
893 return true;
894
Chandler Carruth241bf242016-08-03 07:44:48 +0000895 // Explicitly handle custom-parsed pass names.
896 if (parseRepeatPassName(Name))
897 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000898 if (parseDevirtPassName(Name))
899 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000900
Chandler Carruth74a8a222016-06-17 07:15:29 +0000901#define CGSCC_PASS(NAME, CREATE_PASS) \
902 if (Name == NAME) \
903 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000904#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000905 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000906 return true;
907#include "PassRegistry.def"
908
Chandler Carruth572e3402014-04-21 11:12:00 +0000909 return false;
910}
911
Chandler Carruthd8330982014-01-12 09:34:22 +0000912static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000913 // Explicitly handle pass manager names.
914 if (Name == "function")
915 return true;
916 if (Name == "loop")
917 return true;
918
Chandler Carruth241bf242016-08-03 07:44:48 +0000919 // Explicitly handle custom-parsed pass names.
920 if (parseRepeatPassName(Name))
921 return true;
922
Chandler Carruth74a8a222016-06-17 07:15:29 +0000923#define FUNCTION_PASS(NAME, CREATE_PASS) \
924 if (Name == NAME) \
925 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000926#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000927 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000928 return true;
929#include "PassRegistry.def"
930
Chandler Carruthd8330982014-01-12 09:34:22 +0000931 return false;
932}
933
Justin Bognereecc3c82016-02-25 07:23:08 +0000934static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000935 // Explicitly handle pass manager names.
936 if (Name == "loop")
937 return true;
938
Chandler Carruth241bf242016-08-03 07:44:48 +0000939 // Explicitly handle custom-parsed pass names.
940 if (parseRepeatPassName(Name))
941 return true;
942
Chandler Carruth74a8a222016-06-17 07:15:29 +0000943#define LOOP_PASS(NAME, CREATE_PASS) \
944 if (Name == NAME) \
945 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000946#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
947 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
948 return true;
949#include "PassRegistry.def"
950
951 return false;
952}
953
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000954Optional<std::vector<PassBuilder::PipelineElement>>
955PassBuilder::parsePipelineText(StringRef Text) {
956 std::vector<PipelineElement> ResultPipeline;
957
958 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
959 &ResultPipeline};
960 for (;;) {
961 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
962 size_t Pos = Text.find_first_of(",()");
963 Pipeline.push_back({Text.substr(0, Pos), {}});
964
965 // If we have a single terminating name, we're done.
966 if (Pos == Text.npos)
967 break;
968
969 char Sep = Text[Pos];
970 Text = Text.substr(Pos + 1);
971 if (Sep == ',')
972 // Just a name ending in a comma, continue.
973 continue;
974
975 if (Sep == '(') {
976 // Push the inner pipeline onto the stack to continue processing.
977 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
978 continue;
979 }
980
981 assert(Sep == ')' && "Bogus separator!");
982 // When handling the close parenthesis, we greedily consume them to avoid
983 // empty strings in the pipeline.
984 do {
985 // If we try to pop the outer pipeline we have unbalanced parentheses.
986 if (PipelineStack.size() == 1)
987 return None;
988
989 PipelineStack.pop_back();
990 } while (Text.consume_front(")"));
991
992 // Check if we've finished parsing.
993 if (Text.empty())
994 break;
995
996 // Otherwise, the end of an inner pipeline always has to be followed by
997 // a comma, and then we can continue.
998 if (!Text.consume_front(","))
999 return None;
1000 }
1001
1002 if (PipelineStack.size() > 1)
1003 // Unbalanced paretheses.
1004 return None;
1005
1006 assert(PipelineStack.back() == &ResultPipeline &&
1007 "Wrong pipeline at the bottom of the stack!");
1008 return {std::move(ResultPipeline)};
1009}
1010
1011bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1012 const PipelineElement &E, bool VerifyEachPass,
1013 bool DebugLogging) {
1014 auto &Name = E.Name;
1015 auto &InnerPipeline = E.InnerPipeline;
1016
1017 // First handle complex passes like the pass managers which carry pipelines.
1018 if (!InnerPipeline.empty()) {
1019 if (Name == "module") {
1020 ModulePassManager NestedMPM(DebugLogging);
1021 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1022 DebugLogging))
1023 return false;
1024 MPM.addPass(std::move(NestedMPM));
1025 return true;
1026 }
1027 if (Name == "cgscc") {
1028 CGSCCPassManager CGPM(DebugLogging);
1029 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1030 DebugLogging))
1031 return false;
1032 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
1033 DebugLogging));
1034 return true;
1035 }
1036 if (Name == "function") {
1037 FunctionPassManager FPM(DebugLogging);
1038 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1039 DebugLogging))
1040 return false;
1041 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1042 return true;
1043 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001044 if (auto Count = parseRepeatPassName(Name)) {
1045 ModulePassManager NestedMPM(DebugLogging);
1046 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1047 DebugLogging))
1048 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001049 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001050 return true;
1051 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001052 // Normal passes can't have pipelines.
1053 return false;
1054 }
1055
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001056 // Manually handle aliases for pre-configured pipeline fragments.
1057 if (Name.startswith("default") || Name.startswith("lto")) {
1058 SmallVector<StringRef, 3> Matches;
1059 if (!DefaultAliasRegex.match(Name, &Matches))
1060 return false;
1061 assert(Matches.size() == 3 && "Must capture two matched strings!");
1062
Jordan Rosef85a95f2016-07-25 18:34:51 +00001063 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1064 .Case("O0", O0)
1065 .Case("O1", O1)
1066 .Case("O2", O2)
1067 .Case("O3", O3)
1068 .Case("Os", Os)
1069 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001070 if (L == O0)
1071 // At O0 we do nothing at all!
1072 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001073
1074 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001075 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001076 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001077 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001078 } else {
1079 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001080 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001081 }
1082 return true;
1083 }
1084
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001085 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001086#define MODULE_PASS(NAME, CREATE_PASS) \
1087 if (Name == NAME) { \
1088 MPM.addPass(CREATE_PASS); \
1089 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001090 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001091#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1092 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001093 MPM.addPass( \
1094 RequireAnalysisPass< \
1095 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001096 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001097 } \
1098 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001099 MPM.addPass(InvalidateAnalysisPass< \
1100 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001101 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001102 }
1103#include "PassRegistry.def"
1104
Chandler Carruth66445382014-01-11 08:16:35 +00001105 return false;
1106}
1107
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001108bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1109 const PipelineElement &E, bool VerifyEachPass,
1110 bool DebugLogging) {
1111 auto &Name = E.Name;
1112 auto &InnerPipeline = E.InnerPipeline;
1113
1114 // First handle complex passes like the pass managers which carry pipelines.
1115 if (!InnerPipeline.empty()) {
1116 if (Name == "cgscc") {
1117 CGSCCPassManager NestedCGPM(DebugLogging);
1118 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1119 DebugLogging))
1120 return false;
1121 // Add the nested pass manager with the appropriate adaptor.
1122 CGPM.addPass(std::move(NestedCGPM));
1123 return true;
1124 }
1125 if (Name == "function") {
1126 FunctionPassManager FPM(DebugLogging);
1127 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1128 DebugLogging))
1129 return false;
1130 // Add the nested pass manager with the appropriate adaptor.
1131 CGPM.addPass(
1132 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1133 return true;
1134 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001135 if (auto Count = parseRepeatPassName(Name)) {
1136 CGSCCPassManager NestedCGPM(DebugLogging);
1137 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1138 DebugLogging))
1139 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001140 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001141 return true;
1142 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001143 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1144 CGSCCPassManager NestedCGPM(DebugLogging);
1145 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1146 DebugLogging))
1147 return false;
1148 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1149 *MaxRepetitions, DebugLogging));
1150 return true;
1151 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001152 // Normal passes can't have pipelines.
1153 return false;
1154 }
1155
1156 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001157#define CGSCC_PASS(NAME, CREATE_PASS) \
1158 if (Name == NAME) { \
1159 CGPM.addPass(CREATE_PASS); \
1160 return true; \
1161 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001162#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1163 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001164 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001165 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001166 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1167 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001168 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001169 } \
1170 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001171 CGPM.addPass(InvalidateAnalysisPass< \
1172 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001173 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001174 }
1175#include "PassRegistry.def"
1176
Chandler Carruth572e3402014-04-21 11:12:00 +00001177 return false;
1178}
1179
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001180bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1181 const PipelineElement &E,
1182 bool VerifyEachPass, bool DebugLogging) {
1183 auto &Name = E.Name;
1184 auto &InnerPipeline = E.InnerPipeline;
1185
1186 // First handle complex passes like the pass managers which carry pipelines.
1187 if (!InnerPipeline.empty()) {
1188 if (Name == "function") {
1189 FunctionPassManager NestedFPM(DebugLogging);
1190 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1191 DebugLogging))
1192 return false;
1193 // Add the nested pass manager with the appropriate adaptor.
1194 FPM.addPass(std::move(NestedFPM));
1195 return true;
1196 }
1197 if (Name == "loop") {
1198 LoopPassManager LPM(DebugLogging);
1199 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1200 DebugLogging))
1201 return false;
1202 // Add the nested pass manager with the appropriate adaptor.
1203 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1204 return true;
1205 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001206 if (auto Count = parseRepeatPassName(Name)) {
1207 FunctionPassManager NestedFPM(DebugLogging);
1208 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1209 DebugLogging))
1210 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001211 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001212 return true;
1213 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001214 // Normal passes can't have pipelines.
1215 return false;
1216 }
1217
1218 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001219#define FUNCTION_PASS(NAME, CREATE_PASS) \
1220 if (Name == NAME) { \
1221 FPM.addPass(CREATE_PASS); \
1222 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001223 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001224#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1225 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001226 FPM.addPass( \
1227 RequireAnalysisPass< \
1228 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001229 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001230 } \
1231 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001232 FPM.addPass(InvalidateAnalysisPass< \
1233 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001234 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001235 }
1236#include "PassRegistry.def"
1237
Chandler Carruthd8330982014-01-12 09:34:22 +00001238 return false;
1239}
1240
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001241bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001242 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001243 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001244 auto &InnerPipeline = E.InnerPipeline;
1245
1246 // First handle complex passes like the pass managers which carry pipelines.
1247 if (!InnerPipeline.empty()) {
1248 if (Name == "loop") {
1249 LoopPassManager NestedLPM(DebugLogging);
1250 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1251 DebugLogging))
1252 return false;
1253 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001254 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001255 return true;
1256 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001257 if (auto Count = parseRepeatPassName(Name)) {
1258 LoopPassManager NestedLPM(DebugLogging);
1259 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1260 DebugLogging))
1261 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001262 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001263 return true;
1264 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001265 // Normal passes can't have pipelines.
1266 return false;
1267 }
1268
1269 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001270#define LOOP_PASS(NAME, CREATE_PASS) \
1271 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001272 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001273 return true; \
1274 }
1275#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1276 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001277 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001278 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1279 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1280 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001281 return true; \
1282 } \
1283 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001284 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001285 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001286 return true; \
1287 }
1288#include "PassRegistry.def"
1289
1290 return false;
1291}
1292
Chandler Carruthedf59962016-02-18 09:45:17 +00001293bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001294#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1295 if (Name == NAME) { \
1296 AA.registerModuleAnalysis< \
1297 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1298 return true; \
1299 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001300#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1301 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001302 AA.registerFunctionAnalysis< \
1303 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001304 return true; \
1305 }
1306#include "PassRegistry.def"
1307
1308 return false;
1309}
1310
Justin Bognereecc3c82016-02-25 07:23:08 +00001311bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001312 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001313 bool VerifyEachPass,
1314 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001315 for (const auto &Element : Pipeline) {
1316 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1317 return false;
1318 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001319 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001320 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001321}
1322
Chandler Carruth1ff77242015-03-07 09:02:36 +00001323bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001324 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001325 bool VerifyEachPass,
1326 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001327 for (const auto &Element : Pipeline) {
1328 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1329 return false;
1330 if (VerifyEachPass)
1331 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001332 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001333 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001334}
1335
Chandler Carruth1ff77242015-03-07 09:02:36 +00001336bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001337 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001338 bool VerifyEachPass,
1339 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001340 for (const auto &Element : Pipeline) {
1341 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1342 return false;
1343 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001344 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001345 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001346}
1347
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001348void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1349 FunctionAnalysisManager &FAM,
1350 CGSCCAnalysisManager &CGAM,
1351 ModuleAnalysisManager &MAM) {
1352 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1353 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001354 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1355 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1356 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1357 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1358 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1359}
1360
Chandler Carruth1ff77242015-03-07 09:02:36 +00001361bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001362 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001363 bool VerifyEachPass,
1364 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001365 for (const auto &Element : Pipeline) {
1366 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1367 return false;
1368 if (VerifyEachPass)
1369 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001370 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001371 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001372}
1373
1374// Primary pass pipeline description parsing routine.
1375// FIXME: Should this routine accept a TargetMachine or require the caller to
1376// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001377bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1378 StringRef PipelineText, bool VerifyEachPass,
1379 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001380 auto Pipeline = parsePipelineText(PipelineText);
1381 if (!Pipeline || Pipeline->empty())
1382 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001383
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001384 // If the first name isn't at the module layer, wrap the pipeline up
1385 // automatically.
1386 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001387
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001388 if (!isModulePassName(FirstName)) {
1389 if (isCGSCCPassName(FirstName))
1390 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1391 else if (isFunctionPassName(FirstName))
1392 Pipeline = {{"function", std::move(*Pipeline)}};
1393 else if (isLoopPassName(FirstName))
1394 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1395 else
1396 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001397 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001398 }
1399
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001400 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001401}
Chandler Carruthedf59962016-02-18 09:45:17 +00001402
1403bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001404 // If the pipeline just consists of the word 'default' just replace the AA
1405 // manager with our default one.
1406 if (PipelineText == "default") {
1407 AA = buildDefaultAAPipeline();
1408 return true;
1409 }
1410
Chandler Carruthedf59962016-02-18 09:45:17 +00001411 while (!PipelineText.empty()) {
1412 StringRef Name;
1413 std::tie(Name, PipelineText) = PipelineText.split(',');
1414 if (!parseAAPassName(AA, Name))
1415 return false;
1416 }
1417
1418 return true;
1419}