blob: 9264cf11e5049e96254b5e9ec87ba34b99733c32 [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 Carruth8b5a74192016-02-28 22:16:03 +0000150static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
151
Chandler Carruthe3f50642016-12-22 06:59:15 +0000152static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
153 switch (Level) {
154 case PassBuilder::O0:
155 case PassBuilder::O1:
156 case PassBuilder::O2:
157 case PassBuilder::O3:
158 return false;
159
160 case PassBuilder::Os:
161 case PassBuilder::Oz:
162 return true;
163 }
164 llvm_unreachable("Invalid optimization level!");
165}
166
Chandler Carruth66445382014-01-11 08:16:35 +0000167namespace {
168
Chandler Carruthd8330982014-01-12 09:34:22 +0000169/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000170struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000171 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000172 return PreservedAnalyses::all();
173 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000174 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000175};
176
Chandler Carruth0b576b32015-01-06 02:50:06 +0000177/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000178class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
179 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000180 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000181
182public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000183 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000184 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000185 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000186};
187
Chandler Carruth572e3402014-04-21 11:12:00 +0000188/// \brief No-op CGSCC pass which does nothing.
189struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000190 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
191 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000192 return PreservedAnalyses::all();
193 }
194 static StringRef name() { return "NoOpCGSCCPass"; }
195};
196
Chandler Carruth0b576b32015-01-06 02:50:06 +0000197/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000198class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
199 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000200 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000201
202public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000203 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000204 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000205 return Result();
206 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000207 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000208};
209
Chandler Carruthd8330982014-01-12 09:34:22 +0000210/// \brief No-op function pass which does nothing.
211struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000212 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000213 return PreservedAnalyses::all();
214 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000215 static StringRef name() { return "NoOpFunctionPass"; }
216};
217
Chandler Carruth0b576b32015-01-06 02:50:06 +0000218/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000219class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
220 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000221 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000222
223public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000224 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000225 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000226 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000227};
228
Justin Bognereecc3c82016-02-25 07:23:08 +0000229/// \brief No-op loop pass which does nothing.
230struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000231 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
232 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000233 return PreservedAnalyses::all();
234 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000235 static StringRef name() { return "NoOpLoopPass"; }
236};
237
238/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000239class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
240 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000241 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000242
243public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000244 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000245 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
246 return Result();
247 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000248 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000249};
250
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000251AnalysisKey NoOpModuleAnalysis::Key;
252AnalysisKey NoOpCGSCCAnalysis::Key;
253AnalysisKey NoOpFunctionAnalysis::Key;
254AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000255
Chandler Carruth66445382014-01-11 08:16:35 +0000256} // End anonymous namespace.
257
Chandler Carruth1ff77242015-03-07 09:02:36 +0000258void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000259#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000260 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000261#include "PassRegistry.def"
262}
263
Chandler Carruth1ff77242015-03-07 09:02:36 +0000264void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000265#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000266 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000267#include "PassRegistry.def"
268}
269
Chandler Carruth1ff77242015-03-07 09:02:36 +0000270void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000271#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000272 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000273#include "PassRegistry.def"
274}
275
Justin Bognereecc3c82016-02-25 07:23:08 +0000276void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000277#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000278 LAM.registerPass([&] { return CREATE_PASS; });
279#include "PassRegistry.def"
280}
281
Chandler Carruthe3f50642016-12-22 06:59:15 +0000282FunctionPassManager
283PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
284 bool DebugLogging) {
285 assert(Level != O0 && "Must request optimizations!");
286 FunctionPassManager FPM(DebugLogging);
287
288 // Form SSA out of local memory accesses after breaking apart aggregates into
289 // scalars.
290 FPM.addPass(SROA());
291
292 // Catch trivial redundancies
293 FPM.addPass(EarlyCSEPass());
294
295 // Speculative execution if the target has divergent branches; otherwise nop.
296 FPM.addPass(SpeculativeExecutionPass());
297
298 // Optimize based on known information about branches, and cleanup afterward.
299 FPM.addPass(JumpThreadingPass());
300 FPM.addPass(CorrelatedValuePropagationPass());
301 FPM.addPass(SimplifyCFGPass());
302 FPM.addPass(InstCombinePass());
303
304 if (!isOptimizingForSize(Level))
305 FPM.addPass(LibCallsShrinkWrapPass());
306
307 FPM.addPass(TailCallElimPass());
308 FPM.addPass(SimplifyCFGPass());
309
310 // Form canonically associated expression trees, and simplify the trees using
311 // basic mathematical properties. For example, this will form (nearly)
312 // minimal multiplication trees.
313 FPM.addPass(ReassociatePass());
314
315 // Add the primary loop simplification pipeline.
316 // FIXME: Currently this is split into two loop pass pipelines because we run
317 // some function passes in between them. These can and should be replaced by
318 // loop pass equivalenst but those aren't ready yet. Specifically,
319 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
320 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
321 // the other we have is `LoopInstSimplify`.
322 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
323
324 // Rotate Loop - disable header duplication at -Oz
325 LPM1.addPass(LoopRotatePass(Level != Oz));
326 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000327#if 0
328 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000329 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000330#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000331 LPM2.addPass(IndVarSimplifyPass());
332 LPM2.addPass(LoopIdiomRecognizePass());
333 LPM2.addPass(LoopDeletionPass());
334 LPM2.addPass(LoopUnrollPass::createFull());
335
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000336 // We provide the opt remark emitter pass for LICM to use. We only need to do
337 // this once as it is immutable.
338 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000339 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
340 FPM.addPass(SimplifyCFGPass());
341 FPM.addPass(InstCombinePass());
342 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
343
344 // Eliminate redundancies.
345 if (Level != O1) {
346 // These passes add substantial compile time so skip them at O1.
347 FPM.addPass(MergedLoadStoreMotionPass());
348 FPM.addPass(GVN());
349 }
350
351 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
352 FPM.addPass(MemCpyOptPass());
353
354 // Sparse conditional constant propagation.
355 // FIXME: It isn't clear why we do this *after* loop passes rather than
356 // before...
357 FPM.addPass(SCCPPass());
358
359 // Delete dead bit computations (instcombine runs after to fold away the dead
360 // computations, and then ADCE will run later to exploit any new DCE
361 // opportunities that creates).
362 FPM.addPass(BDCEPass());
363
364 // Run instcombine after redundancy and dead bit elimination to exploit
365 // opportunities opened up by them.
366 FPM.addPass(InstCombinePass());
367
368 // Re-consider control flow based optimizations after redundancy elimination,
369 // redo DCE, etc.
370 FPM.addPass(JumpThreadingPass());
371 FPM.addPass(CorrelatedValuePropagationPass());
372 FPM.addPass(DSEPass());
373 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
374
375 // Finally, do an expensive DCE pass to catch all the dead code exposed by
376 // the simplifications and basic cleanup after all the simplifications.
377 FPM.addPass(ADCEPass());
378 FPM.addPass(SimplifyCFGPass());
379 FPM.addPass(InstCombinePass());
380
381 return FPM;
382}
383
384ModulePassManager
385PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
386 bool DebugLogging) {
387 assert(Level != O0 && "Must request optimizations for the default pipeline!");
388 ModulePassManager MPM(DebugLogging);
389
390 // Force any function attributes we want the rest of the pipeline te observe.
391 MPM.addPass(ForceFunctionAttrsPass());
392
393 // Do basic inference of function attributes from known properties of system
394 // libraries and other oracles.
395 MPM.addPass(InferFunctionAttrsPass());
396
397 // Create an early function pass manager to cleanup the output of the
398 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000399 FunctionPassManager EarlyFPM(DebugLogging);
400 EarlyFPM.addPass(SimplifyCFGPass());
401 EarlyFPM.addPass(SROA());
402 EarlyFPM.addPass(EarlyCSEPass());
403 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000404 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000405 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000406
407 // Interprocedural constant propagation now that basic cleanup has occured
408 // and prior to optimizing globals.
409 // FIXME: This position in the pipeline hasn't been carefully considered in
410 // years, it should be re-analyzed.
411 MPM.addPass(IPSCCPPass());
412
413 // Optimize globals to try and fold them into constants.
414 MPM.addPass(GlobalOptPass());
415
416 // Promote any localized globals to SSA registers.
417 // FIXME: Should this instead by a run of SROA?
418 // FIXME: We should probably run instcombine and simplify-cfg afterward to
419 // delete control flows that are dead once globals have been folded to
420 // constants.
421 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
422
423 // Remove any dead arguments exposed by cleanups and constand folding
424 // globals.
425 MPM.addPass(DeadArgumentEliminationPass());
426
427 // Create a small function pass pipeline to cleanup after all the global
428 // optimizations.
429 FunctionPassManager GlobalCleanupPM(DebugLogging);
430 GlobalCleanupPM.addPass(InstCombinePass());
431 GlobalCleanupPM.addPass(SimplifyCFGPass());
432 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
433
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000434 // Require the GlobalsAA analysis for the module so we can query it within
435 // the CGSCC pipeline.
436 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000437
438 // Now begin the main postorder CGSCC pipeline.
439 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
440 // manager and trying to emulate its precise behavior. Much of this doesn't
441 // make a lot of sense and we should revisit the core CGSCC structure.
442 CGSCCPassManager MainCGPipeline(DebugLogging);
443
444 // Note: historically, the PruneEH pass was run first to deduce nounwind and
445 // generally clean up exception handling overhead. It isn't clear this is
446 // valuable as the inliner doesn't currently care whether it is inlining an
447 // invoke or a call.
448
449 // Run the inliner first. The theory is that we are walking bottom-up and so
450 // the callees have already been fully optimized, and we want to inline them
451 // into the callers so that our optimizations can reflect that.
452 // FIXME; Customize the threshold based on optimization level.
453 MainCGPipeline.addPass(InlinerPass());
454
455 // Now deduce any function attributes based in the current code.
456 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
457
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000458 // When at O3 add argument promotion to the pass pipeline.
459 // FIXME: It isn't at all clear why this should be limited to O3.
460 if (Level == O3)
461 MainCGPipeline.addPass(ArgumentPromotionPass());
462
Chandler Carruthe3f50642016-12-22 06:59:15 +0000463 // Lastly, add the core function simplification pipeline nested inside the
464 // CGSCC walk.
465 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
466 buildFunctionSimplificationPipeline(Level, DebugLogging)));
467
468 MPM.addPass(
469 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
470
471 // This ends the canonicalization and simplification phase of the pipeline.
472 // At this point, we expect to have canonical and simple IR which we begin
473 // *optimizing* for efficient execution going forward.
474
475 // Eliminate externally available functions now that inlining is over -- we
476 // won't emit these anyways.
477 MPM.addPass(EliminateAvailableExternallyPass());
478
479 // Do RPO function attribute inference across the module to forward-propagate
480 // attributes where applicable.
481 // FIXME: Is this really an optimization rather than a canonicalization?
482 MPM.addPass(ReversePostOrderFunctionAttrsPass());
483
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000484 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000485 // useful as the above will have inlined, DCE'ed, and function-attr
486 // propagated everything. We should at this point have a reasonably minimal
487 // and richly annotated call graph. By computing aliasing and mod/ref
488 // information for all local globals here, the late loop passes and notably
489 // the vectorizer will be able to use them to help recognize vectorizable
490 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000491 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000492
493 FunctionPassManager OptimizePM(DebugLogging);
494 OptimizePM.addPass(Float2IntPass());
495 // FIXME: We need to run some loop optimizations to re-rotate loops after
496 // simplify-cfg and others undo their rotation.
497
498 // Optimize the loop execution. These passes operate on entire loop nests
499 // rather than on each loop in an inside-out manner, and so they are actually
500 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000501
502 // First rotate loops that may have been un-rotated by prior passes.
503 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
504
505 // Distribute loops to allow partial vectorization. I.e. isolate dependences
506 // into separate loop that would otherwise inhibit vectorization. This is
507 // currently only performed for loops marked with the metadata
508 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000509 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000510
511 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000512 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000513
Chandler Carruthbaabda92017-01-27 01:32:26 +0000514 // Eliminate loads by forwarding stores from the previous iteration to loads
515 // of the current iteration.
516 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000517
518 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000519 OptimizePM.addPass(InstCombinePass());
520
Chandler Carrutha95ff382017-01-27 00:50:21 +0000521
522 // Now that we've formed fast to execute loop structures, we do further
523 // optimizations. These are run afterward as they might block doing complex
524 // analyses and transforms such as what are needed for loop vectorization.
525
Chandler Carruthe3f50642016-12-22 06:59:15 +0000526 // Optimize parallel scalar instruction chains into SIMD instructions.
527 OptimizePM.addPass(SLPVectorizerPass());
528
Chandler Carrutha95ff382017-01-27 00:50:21 +0000529 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000530 OptimizePM.addPass(SimplifyCFGPass());
531 OptimizePM.addPass(InstCombinePass());
532
533 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000534 // execution resources of an out-of-order processor. We also then need to
535 // clean up redundancies and loop invariant code.
536 // FIXME: It would be really good to use a loop-integrated instruction
537 // combiner for cleanup here so that the unrolling and LICM can be pipelined
538 // across the loop nests.
539 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create()));
540 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000541 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000542 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000543
544 // Now that we've vectorized and unrolled loops, we may have more refined
545 // alignment information, try to re-derive it here.
546 OptimizePM.addPass(AlignmentFromAssumptionsPass());
547
Chandler Carrutha95ff382017-01-27 00:50:21 +0000548 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
549 // canonicalization pass that enables other optimizations. As a result,
550 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
551 // result too early.
552 OptimizePM.addPass(LoopSinkPass());
553
554 // And finally clean up LCSSA form before generating code.
555 OptimizePM.addPass(InstSimplifierPass());
556
557 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000558 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
559
560 // Now we need to do some global optimization transforms.
561 // FIXME: It would seem like these should come first in the optimization
562 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
563 // ordering here.
564 MPM.addPass(GlobalDCEPass());
565 MPM.addPass(ConstantMergePass());
566
567 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000568}
569
Chandler Carruthe3f50642016-12-22 06:59:15 +0000570ModulePassManager
571PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
572 bool DebugLogging) {
573 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000574 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000575 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000576}
577
Chandler Carruthe3f50642016-12-22 06:59:15 +0000578ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
579 bool DebugLogging) {
580 assert(Level != O0 && "Must request optimizations for the default pipeline!");
581 ModulePassManager MPM(DebugLogging);
582
Davide Italiano089a9122017-01-24 00:57:39 +0000583 // Remove unused virtual tables to improve the quality of code generated by
584 // whole-program devirtualization and bitset lowering.
585 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000586
Davide Italiano089a9122017-01-24 00:57:39 +0000587 // Force any function attributes we want the rest of the pipeline to observe.
588 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000589
Davide Italiano089a9122017-01-24 00:57:39 +0000590 // Do basic inference of function attributes from known properties of system
591 // libraries and other oracles.
592 MPM.addPass(InferFunctionAttrsPass());
593
594 if (Level > 1) {
595 // Indirect call promotion. This should promote all the targets that are
596 // left by the earlier promotion pass that promotes intra-module targets.
597 // This two-step promotion is to save the compile time. For LTO, it should
598 // produce the same result as if we only do promotion here.
599 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */));
600
601 // Propagate constants at call sites into the functions they call. This
602 // opens opportunities for globalopt (and inlining) by substituting function
603 // pointers passed as arguments to direct uses of functions.
604 MPM.addPass(IPSCCPPass());
605 }
606
607 // Now deduce any function attributes based in the current code.
608 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
609 PostOrderFunctionAttrsPass()));
610
611 // Do RPO function attribute inference across the module to forward-propagate
612 // attributes where applicable.
613 // FIXME: Is this really an optimization rather than a canonicalization?
614 MPM.addPass(ReversePostOrderFunctionAttrsPass());
615
616 // Use inragne annotations on GEP indices to split globals where beneficial.
617 MPM.addPass(GlobalSplitPass());
618
619 // Run whole program optimization of virtual call when the list of callees
620 // is fixed.
621 MPM.addPass(WholeProgramDevirtPass());
622
623 // Stop here at -O1.
624 if (Level == 1)
625 return MPM;
626
627 // Optimize globals to try and fold them into constants.
628 MPM.addPass(GlobalOptPass());
629
630 // Promote any localized globals to SSA registers.
631 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
632
633 // Linking modules together can lead to duplicate global constant, only
634 // keep one copy of each constant.
635 MPM.addPass(ConstantMergePass());
636
637 // Remove unused arguments from functions.
638 MPM.addPass(DeadArgumentEliminationPass());
639
640 // Reduce the code after globalopt and ipsccp. Both can open up significant
641 // simplification opportunities, and both can propagate functions through
642 // function pointers. When this happens, we often have to resolve varargs
643 // calls, etc, so let instcombine do this.
644 // FIXME: add peephole extensions here as the legacy PM does.
645 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
646
647 // Note: historically, the PruneEH pass was run first to deduce nounwind and
648 // generally clean up exception handling overhead. It isn't clear this is
649 // valuable as the inliner doesn't currently care whether it is inlining an
650 // invoke or a call.
651 // Run the inliner now.
652 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
653
654 // Optimize globals again after we ran the inliner.
655 MPM.addPass(GlobalOptPass());
656
657 // Garbage collect dead functions.
658 // FIXME: Add ArgumentPromotion pass after once it's ported.
659 MPM.addPass(GlobalDCEPass());
660
661 FunctionPassManager FPM(DebugLogging);
662
663 // The IPO Passes may leave cruft around. Clean up after them.
664 // FIXME: add peephole extensions here as the legacy PM does.
665 FPM.addPass(InstCombinePass());
666 FPM.addPass(JumpThreadingPass());
667
668 // Break up allocas
669 FPM.addPass(SROA());
670
671 // Run a few AA driver optimizations here and now to cleanup the code.
672 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
673
674 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
675 PostOrderFunctionAttrsPass()));
676 // FIXME: here we run IP alias analysis in the legacy PM.
677
678 FunctionPassManager MainFPM;
679
680 // FIXME: once we fix LoopPass Manager, add LICM here.
681 // FIXME: once we provide support for enabling MLSM, add it here.
682 // FIXME: once we provide support for enabling NewGVN, add it here.
683 MainFPM.addPass(GVN());
684
685 // Remove dead memcpy()'s.
686 MainFPM.addPass(MemCpyOptPass());
687
688 // Nuke dead stores.
689 MainFPM.addPass(DSEPass());
690
691 // FIXME: at this point, we run a bunch of loop passes:
692 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
693 // loopVectorize. Enable them once the remaining issue with LPM
694 // are sorted out.
695
696 MainFPM.addPass(InstCombinePass());
697 MainFPM.addPass(SimplifyCFGPass());
698 MainFPM.addPass(SCCPPass());
699 MainFPM.addPass(InstCombinePass());
700 MainFPM.addPass(BDCEPass());
701
702 // FIXME: We may want to run SLPVectorizer here.
703 // After vectorization, assume intrinsics may tell us more
704 // about pointer alignments.
705#if 0
706 MainFPM.add(AlignmentFromAssumptionsPass());
707#endif
708
709 // FIXME: Conditionally run LoadCombine here, after it's ported
710 // (in case we still have this pass, given its questionable usefulness).
711
712 // FIXME: add peephole extensions to the PM here.
713 MainFPM.addPass(InstCombinePass());
714 MainFPM.addPass(JumpThreadingPass());
715 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
716
717 // Create a function that performs CFI checks for cross-DSO calls with
718 // targets in the current module.
719 MPM.addPass(CrossDSOCFIPass());
720
721 // Lower type metadata and the type.test intrinsic. This pass supports
722 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
723 // to be run at link time if CFI is enabled. This pass does nothing if
724 // CFI is disabled.
725 // Enable once we add support for the summary in the new PM.
726#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000727 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
728 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000729 Summary));
730#endif
731
732 // Add late LTO optimization passes.
733 // Delete basic blocks, which optimization passes may have killed.
734 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
735
736 // Drop bodies of available eternally objects to improve GlobalDCE.
737 MPM.addPass(EliminateAvailableExternallyPass());
738
739 // Now that we have optimized the program, discard unreachable functions.
740 MPM.addPass(GlobalDCEPass());
741
742 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000743 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000744}
745
Chandler Carruth060ad612016-12-23 20:38:19 +0000746AAManager PassBuilder::buildDefaultAAPipeline() {
747 AAManager AA;
748
749 // The order in which these are registered determines their priority when
750 // being queried.
751
752 // First we register the basic alias analysis that provides the majority of
753 // per-function local AA logic. This is a stateless, on-demand local set of
754 // AA techniques.
755 AA.registerFunctionAnalysis<BasicAA>();
756
757 // Next we query fast, specialized alias analyses that wrap IR-embedded
758 // information about aliasing.
759 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
760 AA.registerFunctionAnalysis<TypeBasedAA>();
761
762 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000763 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
764 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000765 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +0000766 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +0000767
768 return AA;
769}
770
Chandler Carruth241bf242016-08-03 07:44:48 +0000771static Optional<int> parseRepeatPassName(StringRef Name) {
772 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
773 return None;
774 int Count;
775 if (Name.getAsInteger(0, Count) || Count <= 0)
776 return None;
777 return Count;
778}
779
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000780static Optional<int> parseDevirtPassName(StringRef Name) {
781 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
782 return None;
783 int Count;
784 if (Name.getAsInteger(0, Count) || Count <= 0)
785 return None;
786 return Count;
787}
788
Chandler Carruth66445382014-01-11 08:16:35 +0000789static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000790 // Manually handle aliases for pre-configured pipeline fragments.
791 if (Name.startswith("default") || Name.startswith("lto"))
792 return DefaultAliasRegex.match(Name);
793
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000794 // Explicitly handle pass manager names.
795 if (Name == "module")
796 return true;
797 if (Name == "cgscc")
798 return true;
799 if (Name == "function")
800 return true;
801
Chandler Carruth241bf242016-08-03 07:44:48 +0000802 // Explicitly handle custom-parsed pass names.
803 if (parseRepeatPassName(Name))
804 return true;
805
Chandler Carruth74a8a222016-06-17 07:15:29 +0000806#define MODULE_PASS(NAME, CREATE_PASS) \
807 if (Name == NAME) \
808 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000809#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000810 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000811 return true;
812#include "PassRegistry.def"
813
Chandler Carruth66445382014-01-11 08:16:35 +0000814 return false;
815}
816
Chandler Carruth572e3402014-04-21 11:12:00 +0000817static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000818 // Explicitly handle pass manager names.
819 if (Name == "cgscc")
820 return true;
821 if (Name == "function")
822 return true;
823
Chandler Carruth241bf242016-08-03 07:44:48 +0000824 // Explicitly handle custom-parsed pass names.
825 if (parseRepeatPassName(Name))
826 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000827 if (parseDevirtPassName(Name))
828 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000829
Chandler Carruth74a8a222016-06-17 07:15:29 +0000830#define CGSCC_PASS(NAME, CREATE_PASS) \
831 if (Name == NAME) \
832 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000833#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000834 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000835 return true;
836#include "PassRegistry.def"
837
Chandler Carruth572e3402014-04-21 11:12:00 +0000838 return false;
839}
840
Chandler Carruthd8330982014-01-12 09:34:22 +0000841static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000842 // Explicitly handle pass manager names.
843 if (Name == "function")
844 return true;
845 if (Name == "loop")
846 return true;
847
Chandler Carruth241bf242016-08-03 07:44:48 +0000848 // Explicitly handle custom-parsed pass names.
849 if (parseRepeatPassName(Name))
850 return true;
851
Chandler Carruth74a8a222016-06-17 07:15:29 +0000852#define FUNCTION_PASS(NAME, CREATE_PASS) \
853 if (Name == NAME) \
854 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000855#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000856 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000857 return true;
858#include "PassRegistry.def"
859
Chandler Carruthd8330982014-01-12 09:34:22 +0000860 return false;
861}
862
Justin Bognereecc3c82016-02-25 07:23:08 +0000863static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000864 // Explicitly handle pass manager names.
865 if (Name == "loop")
866 return true;
867
Chandler Carruth241bf242016-08-03 07:44:48 +0000868 // Explicitly handle custom-parsed pass names.
869 if (parseRepeatPassName(Name))
870 return true;
871
Chandler Carruth74a8a222016-06-17 07:15:29 +0000872#define LOOP_PASS(NAME, CREATE_PASS) \
873 if (Name == NAME) \
874 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000875#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
876 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
877 return true;
878#include "PassRegistry.def"
879
880 return false;
881}
882
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000883Optional<std::vector<PassBuilder::PipelineElement>>
884PassBuilder::parsePipelineText(StringRef Text) {
885 std::vector<PipelineElement> ResultPipeline;
886
887 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
888 &ResultPipeline};
889 for (;;) {
890 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
891 size_t Pos = Text.find_first_of(",()");
892 Pipeline.push_back({Text.substr(0, Pos), {}});
893
894 // If we have a single terminating name, we're done.
895 if (Pos == Text.npos)
896 break;
897
898 char Sep = Text[Pos];
899 Text = Text.substr(Pos + 1);
900 if (Sep == ',')
901 // Just a name ending in a comma, continue.
902 continue;
903
904 if (Sep == '(') {
905 // Push the inner pipeline onto the stack to continue processing.
906 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
907 continue;
908 }
909
910 assert(Sep == ')' && "Bogus separator!");
911 // When handling the close parenthesis, we greedily consume them to avoid
912 // empty strings in the pipeline.
913 do {
914 // If we try to pop the outer pipeline we have unbalanced parentheses.
915 if (PipelineStack.size() == 1)
916 return None;
917
918 PipelineStack.pop_back();
919 } while (Text.consume_front(")"));
920
921 // Check if we've finished parsing.
922 if (Text.empty())
923 break;
924
925 // Otherwise, the end of an inner pipeline always has to be followed by
926 // a comma, and then we can continue.
927 if (!Text.consume_front(","))
928 return None;
929 }
930
931 if (PipelineStack.size() > 1)
932 // Unbalanced paretheses.
933 return None;
934
935 assert(PipelineStack.back() == &ResultPipeline &&
936 "Wrong pipeline at the bottom of the stack!");
937 return {std::move(ResultPipeline)};
938}
939
940bool PassBuilder::parseModulePass(ModulePassManager &MPM,
941 const PipelineElement &E, bool VerifyEachPass,
942 bool DebugLogging) {
943 auto &Name = E.Name;
944 auto &InnerPipeline = E.InnerPipeline;
945
946 // First handle complex passes like the pass managers which carry pipelines.
947 if (!InnerPipeline.empty()) {
948 if (Name == "module") {
949 ModulePassManager NestedMPM(DebugLogging);
950 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
951 DebugLogging))
952 return false;
953 MPM.addPass(std::move(NestedMPM));
954 return true;
955 }
956 if (Name == "cgscc") {
957 CGSCCPassManager CGPM(DebugLogging);
958 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
959 DebugLogging))
960 return false;
961 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
962 DebugLogging));
963 return true;
964 }
965 if (Name == "function") {
966 FunctionPassManager FPM(DebugLogging);
967 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
968 DebugLogging))
969 return false;
970 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
971 return true;
972 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000973 if (auto Count = parseRepeatPassName(Name)) {
974 ModulePassManager NestedMPM(DebugLogging);
975 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
976 DebugLogging))
977 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000978 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000979 return true;
980 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000981 // Normal passes can't have pipelines.
982 return false;
983 }
984
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000985 // Manually handle aliases for pre-configured pipeline fragments.
986 if (Name.startswith("default") || Name.startswith("lto")) {
987 SmallVector<StringRef, 3> Matches;
988 if (!DefaultAliasRegex.match(Name, &Matches))
989 return false;
990 assert(Matches.size() == 3 && "Must capture two matched strings!");
991
Jordan Rosef85a95f2016-07-25 18:34:51 +0000992 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
993 .Case("O0", O0)
994 .Case("O1", O1)
995 .Case("O2", O2)
996 .Case("O3", O3)
997 .Case("Os", Os)
998 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000999 if (L == O0)
1000 // At O0 we do nothing at all!
1001 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001002
1003 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001004 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001005 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001006 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001007 } else {
1008 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001009 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001010 }
1011 return true;
1012 }
1013
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001014 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001015#define MODULE_PASS(NAME, CREATE_PASS) \
1016 if (Name == NAME) { \
1017 MPM.addPass(CREATE_PASS); \
1018 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001019 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001020#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1021 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001022 MPM.addPass( \
1023 RequireAnalysisPass< \
1024 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001025 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001026 } \
1027 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001028 MPM.addPass(InvalidateAnalysisPass< \
1029 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001030 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001031 }
1032#include "PassRegistry.def"
1033
Chandler Carruth66445382014-01-11 08:16:35 +00001034 return false;
1035}
1036
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001037bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1038 const PipelineElement &E, bool VerifyEachPass,
1039 bool DebugLogging) {
1040 auto &Name = E.Name;
1041 auto &InnerPipeline = E.InnerPipeline;
1042
1043 // First handle complex passes like the pass managers which carry pipelines.
1044 if (!InnerPipeline.empty()) {
1045 if (Name == "cgscc") {
1046 CGSCCPassManager NestedCGPM(DebugLogging);
1047 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1048 DebugLogging))
1049 return false;
1050 // Add the nested pass manager with the appropriate adaptor.
1051 CGPM.addPass(std::move(NestedCGPM));
1052 return true;
1053 }
1054 if (Name == "function") {
1055 FunctionPassManager FPM(DebugLogging);
1056 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1057 DebugLogging))
1058 return false;
1059 // Add the nested pass manager with the appropriate adaptor.
1060 CGPM.addPass(
1061 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1062 return true;
1063 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001064 if (auto Count = parseRepeatPassName(Name)) {
1065 CGSCCPassManager NestedCGPM(DebugLogging);
1066 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1067 DebugLogging))
1068 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001069 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001070 return true;
1071 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001072 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1073 CGSCCPassManager NestedCGPM(DebugLogging);
1074 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1075 DebugLogging))
1076 return false;
1077 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1078 *MaxRepetitions, DebugLogging));
1079 return true;
1080 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001081 // Normal passes can't have pipelines.
1082 return false;
1083 }
1084
1085 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001086#define CGSCC_PASS(NAME, CREATE_PASS) \
1087 if (Name == NAME) { \
1088 CGPM.addPass(CREATE_PASS); \
1089 return true; \
1090 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001091#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1092 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001093 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001094 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001095 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1096 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001097 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001098 } \
1099 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001100 CGPM.addPass(InvalidateAnalysisPass< \
1101 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001102 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001103 }
1104#include "PassRegistry.def"
1105
Chandler Carruth572e3402014-04-21 11:12:00 +00001106 return false;
1107}
1108
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001109bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1110 const PipelineElement &E,
1111 bool VerifyEachPass, bool DebugLogging) {
1112 auto &Name = E.Name;
1113 auto &InnerPipeline = E.InnerPipeline;
1114
1115 // First handle complex passes like the pass managers which carry pipelines.
1116 if (!InnerPipeline.empty()) {
1117 if (Name == "function") {
1118 FunctionPassManager NestedFPM(DebugLogging);
1119 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1120 DebugLogging))
1121 return false;
1122 // Add the nested pass manager with the appropriate adaptor.
1123 FPM.addPass(std::move(NestedFPM));
1124 return true;
1125 }
1126 if (Name == "loop") {
1127 LoopPassManager LPM(DebugLogging);
1128 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1129 DebugLogging))
1130 return false;
1131 // Add the nested pass manager with the appropriate adaptor.
1132 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1133 return true;
1134 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001135 if (auto Count = parseRepeatPassName(Name)) {
1136 FunctionPassManager NestedFPM(DebugLogging);
1137 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1138 DebugLogging))
1139 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001140 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001141 return true;
1142 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001143 // Normal passes can't have pipelines.
1144 return false;
1145 }
1146
1147 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001148#define FUNCTION_PASS(NAME, CREATE_PASS) \
1149 if (Name == NAME) { \
1150 FPM.addPass(CREATE_PASS); \
1151 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001152 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001153#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1154 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001155 FPM.addPass( \
1156 RequireAnalysisPass< \
1157 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001158 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001159 } \
1160 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001161 FPM.addPass(InvalidateAnalysisPass< \
1162 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001163 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001164 }
1165#include "PassRegistry.def"
1166
Chandler Carruthd8330982014-01-12 09:34:22 +00001167 return false;
1168}
1169
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001170bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001171 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001172 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001173 auto &InnerPipeline = E.InnerPipeline;
1174
1175 // First handle complex passes like the pass managers which carry pipelines.
1176 if (!InnerPipeline.empty()) {
1177 if (Name == "loop") {
1178 LoopPassManager NestedLPM(DebugLogging);
1179 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1180 DebugLogging))
1181 return false;
1182 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001183 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001184 return true;
1185 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001186 if (auto Count = parseRepeatPassName(Name)) {
1187 LoopPassManager NestedLPM(DebugLogging);
1188 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1189 DebugLogging))
1190 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001191 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001192 return true;
1193 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001194 // Normal passes can't have pipelines.
1195 return false;
1196 }
1197
1198 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001199#define LOOP_PASS(NAME, CREATE_PASS) \
1200 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001201 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001202 return true; \
1203 }
1204#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1205 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001206 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001207 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1208 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1209 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001210 return true; \
1211 } \
1212 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001213 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001214 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001215 return true; \
1216 }
1217#include "PassRegistry.def"
1218
1219 return false;
1220}
1221
Chandler Carruthedf59962016-02-18 09:45:17 +00001222bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001223#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1224 if (Name == NAME) { \
1225 AA.registerModuleAnalysis< \
1226 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1227 return true; \
1228 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001229#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1230 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001231 AA.registerFunctionAnalysis< \
1232 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001233 return true; \
1234 }
1235#include "PassRegistry.def"
1236
1237 return false;
1238}
1239
Justin Bognereecc3c82016-02-25 07:23:08 +00001240bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001241 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001242 bool VerifyEachPass,
1243 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001244 for (const auto &Element : Pipeline) {
1245 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1246 return false;
1247 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001248 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001249 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001250}
1251
Chandler Carruth1ff77242015-03-07 09:02:36 +00001252bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001253 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001254 bool VerifyEachPass,
1255 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001256 for (const auto &Element : Pipeline) {
1257 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1258 return false;
1259 if (VerifyEachPass)
1260 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001261 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001262 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001263}
1264
Chandler Carruth1ff77242015-03-07 09:02:36 +00001265bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001266 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001267 bool VerifyEachPass,
1268 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001269 for (const auto &Element : Pipeline) {
1270 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1271 return false;
1272 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001273 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001274 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001275}
1276
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001277void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1278 FunctionAnalysisManager &FAM,
1279 CGSCCAnalysisManager &CGAM,
1280 ModuleAnalysisManager &MAM) {
1281 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1282 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001283 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1284 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1285 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1286 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1287 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1288}
1289
Chandler Carruth1ff77242015-03-07 09:02:36 +00001290bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001291 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001292 bool VerifyEachPass,
1293 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001294 for (const auto &Element : Pipeline) {
1295 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1296 return false;
1297 if (VerifyEachPass)
1298 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001299 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001300 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001301}
1302
1303// Primary pass pipeline description parsing routine.
1304// FIXME: Should this routine accept a TargetMachine or require the caller to
1305// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001306bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1307 StringRef PipelineText, bool VerifyEachPass,
1308 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001309 auto Pipeline = parsePipelineText(PipelineText);
1310 if (!Pipeline || Pipeline->empty())
1311 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001312
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001313 // If the first name isn't at the module layer, wrap the pipeline up
1314 // automatically.
1315 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001316
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001317 if (!isModulePassName(FirstName)) {
1318 if (isCGSCCPassName(FirstName))
1319 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1320 else if (isFunctionPassName(FirstName))
1321 Pipeline = {{"function", std::move(*Pipeline)}};
1322 else if (isLoopPassName(FirstName))
1323 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1324 else
1325 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001326 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001327 }
1328
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001329 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001330}
Chandler Carruthedf59962016-02-18 09:45:17 +00001331
1332bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001333 // If the pipeline just consists of the word 'default' just replace the AA
1334 // manager with our default one.
1335 if (PipelineText == "default") {
1336 AA = buildDefaultAAPipeline();
1337 return true;
1338 }
1339
Chandler Carruthedf59962016-02-18 09:45:17 +00001340 while (!PipelineText.empty()) {
1341 StringRef Name;
1342 std::tie(Name, PipelineText) = PipelineText.split(',');
1343 if (!parseAAPassName(AA, Name))
1344 return false;
1345 }
1346
1347 return true;
1348}