blob: 06190682f32cf044bc2bf9ff2e2ca5f2d7375598 [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
434 // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
435#if 0
436 MPM.addPass(RequireAnalysisPass<GlobalsAA>());
437#endif
438
439 // Now begin the main postorder CGSCC pipeline.
440 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
441 // manager and trying to emulate its precise behavior. Much of this doesn't
442 // make a lot of sense and we should revisit the core CGSCC structure.
443 CGSCCPassManager MainCGPipeline(DebugLogging);
444
445 // Note: historically, the PruneEH pass was run first to deduce nounwind and
446 // generally clean up exception handling overhead. It isn't clear this is
447 // valuable as the inliner doesn't currently care whether it is inlining an
448 // invoke or a call.
449
450 // Run the inliner first. The theory is that we are walking bottom-up and so
451 // the callees have already been fully optimized, and we want to inline them
452 // into the callers so that our optimizations can reflect that.
453 // FIXME; Customize the threshold based on optimization level.
454 MainCGPipeline.addPass(InlinerPass());
455
456 // Now deduce any function attributes based in the current code.
457 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
458
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000459 // When at O3 add argument promotion to the pass pipeline.
460 // FIXME: It isn't at all clear why this should be limited to O3.
461 if (Level == O3)
462 MainCGPipeline.addPass(ArgumentPromotionPass());
463
Chandler Carruthe3f50642016-12-22 06:59:15 +0000464 // Lastly, add the core function simplification pipeline nested inside the
465 // CGSCC walk.
466 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
467 buildFunctionSimplificationPipeline(Level, DebugLogging)));
468
469 MPM.addPass(
470 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
471
472 // This ends the canonicalization and simplification phase of the pipeline.
473 // At this point, we expect to have canonical and simple IR which we begin
474 // *optimizing* for efficient execution going forward.
475
476 // Eliminate externally available functions now that inlining is over -- we
477 // won't emit these anyways.
478 MPM.addPass(EliminateAvailableExternallyPass());
479
480 // Do RPO function attribute inference across the module to forward-propagate
481 // attributes where applicable.
482 // FIXME: Is this really an optimization rather than a canonicalization?
483 MPM.addPass(ReversePostOrderFunctionAttrsPass());
484
485 // Recompute GloblasAA here prior to function passes. This is particularly
486 // useful as the above will have inlined, DCE'ed, and function-attr
487 // propagated everything. We should at this point have a reasonably minimal
488 // and richly annotated call graph. By computing aliasing and mod/ref
489 // information for all local globals here, the late loop passes and notably
490 // the vectorizer will be able to use them to help recognize vectorizable
491 // memory operations.
492 // FIXME: Enable this once analysis invalidation is fully supported.
493#if 0
494 MPM.addPass(Require<GlobalsAA>());
495#endif
496
497 FunctionPassManager OptimizePM(DebugLogging);
498 OptimizePM.addPass(Float2IntPass());
499 // FIXME: We need to run some loop optimizations to re-rotate loops after
500 // simplify-cfg and others undo their rotation.
501
502 // Optimize the loop execution. These passes operate on entire loop nests
503 // rather than on each loop in an inside-out manner, and so they are actually
504 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000505
506 // First rotate loops that may have been un-rotated by prior passes.
507 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
508
509 // Distribute loops to allow partial vectorization. I.e. isolate dependences
510 // into separate loop that would otherwise inhibit vectorization. This is
511 // currently only performed for loops marked with the metadata
512 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000513 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000514
515 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000516 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000517
Chandler Carruthbaabda92017-01-27 01:32:26 +0000518 // Eliminate loads by forwarding stores from the previous iteration to loads
519 // of the current iteration.
520 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000521
522 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000523 OptimizePM.addPass(InstCombinePass());
524
Chandler Carrutha95ff382017-01-27 00:50:21 +0000525
526 // Now that we've formed fast to execute loop structures, we do further
527 // optimizations. These are run afterward as they might block doing complex
528 // analyses and transforms such as what are needed for loop vectorization.
529
Chandler Carruthe3f50642016-12-22 06:59:15 +0000530 // Optimize parallel scalar instruction chains into SIMD instructions.
531 OptimizePM.addPass(SLPVectorizerPass());
532
Chandler Carrutha95ff382017-01-27 00:50:21 +0000533 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000534 OptimizePM.addPass(SimplifyCFGPass());
535 OptimizePM.addPass(InstCombinePass());
536
537 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000538 // execution resources of an out-of-order processor. We also then need to
539 // clean up redundancies and loop invariant code.
540 // FIXME: It would be really good to use a loop-integrated instruction
541 // combiner for cleanup here so that the unrolling and LICM can be pipelined
542 // across the loop nests.
543 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create()));
544 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000545 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000546 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000547
548 // Now that we've vectorized and unrolled loops, we may have more refined
549 // alignment information, try to re-derive it here.
550 OptimizePM.addPass(AlignmentFromAssumptionsPass());
551
Chandler Carrutha95ff382017-01-27 00:50:21 +0000552 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
553 // canonicalization pass that enables other optimizations. As a result,
554 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
555 // result too early.
556 OptimizePM.addPass(LoopSinkPass());
557
558 // And finally clean up LCSSA form before generating code.
559 OptimizePM.addPass(InstSimplifierPass());
560
561 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000562 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
563
564 // Now we need to do some global optimization transforms.
565 // FIXME: It would seem like these should come first in the optimization
566 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
567 // ordering here.
568 MPM.addPass(GlobalDCEPass());
569 MPM.addPass(ConstantMergePass());
570
571 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000572}
573
Chandler Carruthe3f50642016-12-22 06:59:15 +0000574ModulePassManager
575PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
576 bool DebugLogging) {
577 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000578 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000579 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000580}
581
Chandler Carruthe3f50642016-12-22 06:59:15 +0000582ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
583 bool DebugLogging) {
584 assert(Level != O0 && "Must request optimizations for the default pipeline!");
585 ModulePassManager MPM(DebugLogging);
586
Davide Italiano089a9122017-01-24 00:57:39 +0000587 // Remove unused virtual tables to improve the quality of code generated by
588 // whole-program devirtualization and bitset lowering.
589 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000590
Davide Italiano089a9122017-01-24 00:57:39 +0000591 // Force any function attributes we want the rest of the pipeline to observe.
592 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000593
Davide Italiano089a9122017-01-24 00:57:39 +0000594 // Do basic inference of function attributes from known properties of system
595 // libraries and other oracles.
596 MPM.addPass(InferFunctionAttrsPass());
597
598 if (Level > 1) {
599 // Indirect call promotion. This should promote all the targets that are
600 // left by the earlier promotion pass that promotes intra-module targets.
601 // This two-step promotion is to save the compile time. For LTO, it should
602 // produce the same result as if we only do promotion here.
603 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */));
604
605 // Propagate constants at call sites into the functions they call. This
606 // opens opportunities for globalopt (and inlining) by substituting function
607 // pointers passed as arguments to direct uses of functions.
608 MPM.addPass(IPSCCPPass());
609 }
610
611 // Now deduce any function attributes based in the current code.
612 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
613 PostOrderFunctionAttrsPass()));
614
615 // Do RPO function attribute inference across the module to forward-propagate
616 // attributes where applicable.
617 // FIXME: Is this really an optimization rather than a canonicalization?
618 MPM.addPass(ReversePostOrderFunctionAttrsPass());
619
620 // Use inragne annotations on GEP indices to split globals where beneficial.
621 MPM.addPass(GlobalSplitPass());
622
623 // Run whole program optimization of virtual call when the list of callees
624 // is fixed.
625 MPM.addPass(WholeProgramDevirtPass());
626
627 // Stop here at -O1.
628 if (Level == 1)
629 return MPM;
630
631 // Optimize globals to try and fold them into constants.
632 MPM.addPass(GlobalOptPass());
633
634 // Promote any localized globals to SSA registers.
635 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
636
637 // Linking modules together can lead to duplicate global constant, only
638 // keep one copy of each constant.
639 MPM.addPass(ConstantMergePass());
640
641 // Remove unused arguments from functions.
642 MPM.addPass(DeadArgumentEliminationPass());
643
644 // Reduce the code after globalopt and ipsccp. Both can open up significant
645 // simplification opportunities, and both can propagate functions through
646 // function pointers. When this happens, we often have to resolve varargs
647 // calls, etc, so let instcombine do this.
648 // FIXME: add peephole extensions here as the legacy PM does.
649 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
650
651 // Note: historically, the PruneEH pass was run first to deduce nounwind and
652 // generally clean up exception handling overhead. It isn't clear this is
653 // valuable as the inliner doesn't currently care whether it is inlining an
654 // invoke or a call.
655 // Run the inliner now.
656 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
657
658 // Optimize globals again after we ran the inliner.
659 MPM.addPass(GlobalOptPass());
660
661 // Garbage collect dead functions.
662 // FIXME: Add ArgumentPromotion pass after once it's ported.
663 MPM.addPass(GlobalDCEPass());
664
665 FunctionPassManager FPM(DebugLogging);
666
667 // The IPO Passes may leave cruft around. Clean up after them.
668 // FIXME: add peephole extensions here as the legacy PM does.
669 FPM.addPass(InstCombinePass());
670 FPM.addPass(JumpThreadingPass());
671
672 // Break up allocas
673 FPM.addPass(SROA());
674
675 // Run a few AA driver optimizations here and now to cleanup the code.
676 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
677
678 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
679 PostOrderFunctionAttrsPass()));
680 // FIXME: here we run IP alias analysis in the legacy PM.
681
682 FunctionPassManager MainFPM;
683
684 // FIXME: once we fix LoopPass Manager, add LICM here.
685 // FIXME: once we provide support for enabling MLSM, add it here.
686 // FIXME: once we provide support for enabling NewGVN, add it here.
687 MainFPM.addPass(GVN());
688
689 // Remove dead memcpy()'s.
690 MainFPM.addPass(MemCpyOptPass());
691
692 // Nuke dead stores.
693 MainFPM.addPass(DSEPass());
694
695 // FIXME: at this point, we run a bunch of loop passes:
696 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
697 // loopVectorize. Enable them once the remaining issue with LPM
698 // are sorted out.
699
700 MainFPM.addPass(InstCombinePass());
701 MainFPM.addPass(SimplifyCFGPass());
702 MainFPM.addPass(SCCPPass());
703 MainFPM.addPass(InstCombinePass());
704 MainFPM.addPass(BDCEPass());
705
706 // FIXME: We may want to run SLPVectorizer here.
707 // After vectorization, assume intrinsics may tell us more
708 // about pointer alignments.
709#if 0
710 MainFPM.add(AlignmentFromAssumptionsPass());
711#endif
712
713 // FIXME: Conditionally run LoadCombine here, after it's ported
714 // (in case we still have this pass, given its questionable usefulness).
715
716 // FIXME: add peephole extensions to the PM here.
717 MainFPM.addPass(InstCombinePass());
718 MainFPM.addPass(JumpThreadingPass());
719 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
720
721 // Create a function that performs CFI checks for cross-DSO calls with
722 // targets in the current module.
723 MPM.addPass(CrossDSOCFIPass());
724
725 // Lower type metadata and the type.test intrinsic. This pass supports
726 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
727 // to be run at link time if CFI is enabled. This pass does nothing if
728 // CFI is disabled.
729 // Enable once we add support for the summary in the new PM.
730#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000731 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
732 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000733 Summary));
734#endif
735
736 // Add late LTO optimization passes.
737 // Delete basic blocks, which optimization passes may have killed.
738 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
739
740 // Drop bodies of available eternally objects to improve GlobalDCE.
741 MPM.addPass(EliminateAvailableExternallyPass());
742
743 // Now that we have optimized the program, discard unreachable functions.
744 MPM.addPass(GlobalDCEPass());
745
746 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000747 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000748}
749
Chandler Carruth060ad612016-12-23 20:38:19 +0000750AAManager PassBuilder::buildDefaultAAPipeline() {
751 AAManager AA;
752
753 // The order in which these are registered determines their priority when
754 // being queried.
755
756 // First we register the basic alias analysis that provides the majority of
757 // per-function local AA logic. This is a stateless, on-demand local set of
758 // AA techniques.
759 AA.registerFunctionAnalysis<BasicAA>();
760
761 // Next we query fast, specialized alias analyses that wrap IR-embedded
762 // information about aliasing.
763 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
764 AA.registerFunctionAnalysis<TypeBasedAA>();
765
766 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000767 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
768 // analysis, all that the `AAManager` can do is query for any *cached*
769 // results from `GlobalsAA` through a readonly proxy..
Chandler Carruth060ad612016-12-23 20:38:19 +0000770#if 0
Chandler Carruth534d6442016-12-24 05:11:17 +0000771 // FIXME: Enable once the invalidation logic supports this. Currently, the
772 // `AAManager` will hold stale references to the module analyses.
Chandler Carruth060ad612016-12-23 20:38:19 +0000773 AA.registerModuleAnalysis<GlobalsAA>();
774#endif
775
776 return AA;
777}
778
Chandler Carruth241bf242016-08-03 07:44:48 +0000779static Optional<int> parseRepeatPassName(StringRef Name) {
780 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
781 return None;
782 int Count;
783 if (Name.getAsInteger(0, Count) || Count <= 0)
784 return None;
785 return Count;
786}
787
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000788static Optional<int> parseDevirtPassName(StringRef Name) {
789 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
790 return None;
791 int Count;
792 if (Name.getAsInteger(0, Count) || Count <= 0)
793 return None;
794 return Count;
795}
796
Chandler Carruth66445382014-01-11 08:16:35 +0000797static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000798 // Manually handle aliases for pre-configured pipeline fragments.
799 if (Name.startswith("default") || Name.startswith("lto"))
800 return DefaultAliasRegex.match(Name);
801
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000802 // Explicitly handle pass manager names.
803 if (Name == "module")
804 return true;
805 if (Name == "cgscc")
806 return true;
807 if (Name == "function")
808 return true;
809
Chandler Carruth241bf242016-08-03 07:44:48 +0000810 // Explicitly handle custom-parsed pass names.
811 if (parseRepeatPassName(Name))
812 return true;
813
Chandler Carruth74a8a222016-06-17 07:15:29 +0000814#define MODULE_PASS(NAME, CREATE_PASS) \
815 if (Name == NAME) \
816 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000817#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000818 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000819 return true;
820#include "PassRegistry.def"
821
Chandler Carruth66445382014-01-11 08:16:35 +0000822 return false;
823}
824
Chandler Carruth572e3402014-04-21 11:12:00 +0000825static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000826 // Explicitly handle pass manager names.
827 if (Name == "cgscc")
828 return true;
829 if (Name == "function")
830 return true;
831
Chandler Carruth241bf242016-08-03 07:44:48 +0000832 // Explicitly handle custom-parsed pass names.
833 if (parseRepeatPassName(Name))
834 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000835 if (parseDevirtPassName(Name))
836 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000837
Chandler Carruth74a8a222016-06-17 07:15:29 +0000838#define CGSCC_PASS(NAME, CREATE_PASS) \
839 if (Name == NAME) \
840 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000841#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000842 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000843 return true;
844#include "PassRegistry.def"
845
Chandler Carruth572e3402014-04-21 11:12:00 +0000846 return false;
847}
848
Chandler Carruthd8330982014-01-12 09:34:22 +0000849static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000850 // Explicitly handle pass manager names.
851 if (Name == "function")
852 return true;
853 if (Name == "loop")
854 return true;
855
Chandler Carruth241bf242016-08-03 07:44:48 +0000856 // Explicitly handle custom-parsed pass names.
857 if (parseRepeatPassName(Name))
858 return true;
859
Chandler Carruth74a8a222016-06-17 07:15:29 +0000860#define FUNCTION_PASS(NAME, CREATE_PASS) \
861 if (Name == NAME) \
862 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000863#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000864 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000865 return true;
866#include "PassRegistry.def"
867
Chandler Carruthd8330982014-01-12 09:34:22 +0000868 return false;
869}
870
Justin Bognereecc3c82016-02-25 07:23:08 +0000871static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000872 // Explicitly handle pass manager names.
873 if (Name == "loop")
874 return true;
875
Chandler Carruth241bf242016-08-03 07:44:48 +0000876 // Explicitly handle custom-parsed pass names.
877 if (parseRepeatPassName(Name))
878 return true;
879
Chandler Carruth74a8a222016-06-17 07:15:29 +0000880#define LOOP_PASS(NAME, CREATE_PASS) \
881 if (Name == NAME) \
882 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000883#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
884 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
885 return true;
886#include "PassRegistry.def"
887
888 return false;
889}
890
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000891Optional<std::vector<PassBuilder::PipelineElement>>
892PassBuilder::parsePipelineText(StringRef Text) {
893 std::vector<PipelineElement> ResultPipeline;
894
895 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
896 &ResultPipeline};
897 for (;;) {
898 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
899 size_t Pos = Text.find_first_of(",()");
900 Pipeline.push_back({Text.substr(0, Pos), {}});
901
902 // If we have a single terminating name, we're done.
903 if (Pos == Text.npos)
904 break;
905
906 char Sep = Text[Pos];
907 Text = Text.substr(Pos + 1);
908 if (Sep == ',')
909 // Just a name ending in a comma, continue.
910 continue;
911
912 if (Sep == '(') {
913 // Push the inner pipeline onto the stack to continue processing.
914 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
915 continue;
916 }
917
918 assert(Sep == ')' && "Bogus separator!");
919 // When handling the close parenthesis, we greedily consume them to avoid
920 // empty strings in the pipeline.
921 do {
922 // If we try to pop the outer pipeline we have unbalanced parentheses.
923 if (PipelineStack.size() == 1)
924 return None;
925
926 PipelineStack.pop_back();
927 } while (Text.consume_front(")"));
928
929 // Check if we've finished parsing.
930 if (Text.empty())
931 break;
932
933 // Otherwise, the end of an inner pipeline always has to be followed by
934 // a comma, and then we can continue.
935 if (!Text.consume_front(","))
936 return None;
937 }
938
939 if (PipelineStack.size() > 1)
940 // Unbalanced paretheses.
941 return None;
942
943 assert(PipelineStack.back() == &ResultPipeline &&
944 "Wrong pipeline at the bottom of the stack!");
945 return {std::move(ResultPipeline)};
946}
947
948bool PassBuilder::parseModulePass(ModulePassManager &MPM,
949 const PipelineElement &E, bool VerifyEachPass,
950 bool DebugLogging) {
951 auto &Name = E.Name;
952 auto &InnerPipeline = E.InnerPipeline;
953
954 // First handle complex passes like the pass managers which carry pipelines.
955 if (!InnerPipeline.empty()) {
956 if (Name == "module") {
957 ModulePassManager NestedMPM(DebugLogging);
958 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
959 DebugLogging))
960 return false;
961 MPM.addPass(std::move(NestedMPM));
962 return true;
963 }
964 if (Name == "cgscc") {
965 CGSCCPassManager CGPM(DebugLogging);
966 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
967 DebugLogging))
968 return false;
969 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
970 DebugLogging));
971 return true;
972 }
973 if (Name == "function") {
974 FunctionPassManager FPM(DebugLogging);
975 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
976 DebugLogging))
977 return false;
978 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
979 return true;
980 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000981 if (auto Count = parseRepeatPassName(Name)) {
982 ModulePassManager NestedMPM(DebugLogging);
983 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
984 DebugLogging))
985 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000986 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000987 return true;
988 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000989 // Normal passes can't have pipelines.
990 return false;
991 }
992
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000993 // Manually handle aliases for pre-configured pipeline fragments.
994 if (Name.startswith("default") || Name.startswith("lto")) {
995 SmallVector<StringRef, 3> Matches;
996 if (!DefaultAliasRegex.match(Name, &Matches))
997 return false;
998 assert(Matches.size() == 3 && "Must capture two matched strings!");
999
Jordan Rosef85a95f2016-07-25 18:34:51 +00001000 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
1001 .Case("O0", O0)
1002 .Case("O1", O1)
1003 .Case("O2", O2)
1004 .Case("O3", O3)
1005 .Case("Os", Os)
1006 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001007 if (L == O0)
1008 // At O0 we do nothing at all!
1009 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001010
1011 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001012 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001013 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001014 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001015 } else {
1016 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001017 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001018 }
1019 return true;
1020 }
1021
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001022 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001023#define MODULE_PASS(NAME, CREATE_PASS) \
1024 if (Name == NAME) { \
1025 MPM.addPass(CREATE_PASS); \
1026 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001027 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001028#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1029 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001030 MPM.addPass( \
1031 RequireAnalysisPass< \
1032 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001033 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001034 } \
1035 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001036 MPM.addPass(InvalidateAnalysisPass< \
1037 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001038 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001039 }
1040#include "PassRegistry.def"
1041
Chandler Carruth66445382014-01-11 08:16:35 +00001042 return false;
1043}
1044
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001045bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1046 const PipelineElement &E, bool VerifyEachPass,
1047 bool DebugLogging) {
1048 auto &Name = E.Name;
1049 auto &InnerPipeline = E.InnerPipeline;
1050
1051 // First handle complex passes like the pass managers which carry pipelines.
1052 if (!InnerPipeline.empty()) {
1053 if (Name == "cgscc") {
1054 CGSCCPassManager NestedCGPM(DebugLogging);
1055 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1056 DebugLogging))
1057 return false;
1058 // Add the nested pass manager with the appropriate adaptor.
1059 CGPM.addPass(std::move(NestedCGPM));
1060 return true;
1061 }
1062 if (Name == "function") {
1063 FunctionPassManager FPM(DebugLogging);
1064 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1065 DebugLogging))
1066 return false;
1067 // Add the nested pass manager with the appropriate adaptor.
1068 CGPM.addPass(
1069 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1070 return true;
1071 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001072 if (auto Count = parseRepeatPassName(Name)) {
1073 CGSCCPassManager NestedCGPM(DebugLogging);
1074 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1075 DebugLogging))
1076 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001077 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001078 return true;
1079 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001080 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1081 CGSCCPassManager NestedCGPM(DebugLogging);
1082 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1083 DebugLogging))
1084 return false;
1085 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1086 *MaxRepetitions, DebugLogging));
1087 return true;
1088 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001089 // Normal passes can't have pipelines.
1090 return false;
1091 }
1092
1093 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001094#define CGSCC_PASS(NAME, CREATE_PASS) \
1095 if (Name == NAME) { \
1096 CGPM.addPass(CREATE_PASS); \
1097 return true; \
1098 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001099#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1100 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001101 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001102 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001103 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1104 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001105 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001106 } \
1107 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001108 CGPM.addPass(InvalidateAnalysisPass< \
1109 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001110 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001111 }
1112#include "PassRegistry.def"
1113
Chandler Carruth572e3402014-04-21 11:12:00 +00001114 return false;
1115}
1116
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001117bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1118 const PipelineElement &E,
1119 bool VerifyEachPass, bool DebugLogging) {
1120 auto &Name = E.Name;
1121 auto &InnerPipeline = E.InnerPipeline;
1122
1123 // First handle complex passes like the pass managers which carry pipelines.
1124 if (!InnerPipeline.empty()) {
1125 if (Name == "function") {
1126 FunctionPassManager NestedFPM(DebugLogging);
1127 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1128 DebugLogging))
1129 return false;
1130 // Add the nested pass manager with the appropriate adaptor.
1131 FPM.addPass(std::move(NestedFPM));
1132 return true;
1133 }
1134 if (Name == "loop") {
1135 LoopPassManager LPM(DebugLogging);
1136 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1137 DebugLogging))
1138 return false;
1139 // Add the nested pass manager with the appropriate adaptor.
1140 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1141 return true;
1142 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001143 if (auto Count = parseRepeatPassName(Name)) {
1144 FunctionPassManager NestedFPM(DebugLogging);
1145 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1146 DebugLogging))
1147 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001148 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001149 return true;
1150 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001151 // Normal passes can't have pipelines.
1152 return false;
1153 }
1154
1155 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001156#define FUNCTION_PASS(NAME, CREATE_PASS) \
1157 if (Name == NAME) { \
1158 FPM.addPass(CREATE_PASS); \
1159 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001160 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001161#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1162 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001163 FPM.addPass( \
1164 RequireAnalysisPass< \
1165 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001166 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001167 } \
1168 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001169 FPM.addPass(InvalidateAnalysisPass< \
1170 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001171 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001172 }
1173#include "PassRegistry.def"
1174
Chandler Carruthd8330982014-01-12 09:34:22 +00001175 return false;
1176}
1177
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001178bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001179 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001180 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001181 auto &InnerPipeline = E.InnerPipeline;
1182
1183 // First handle complex passes like the pass managers which carry pipelines.
1184 if (!InnerPipeline.empty()) {
1185 if (Name == "loop") {
1186 LoopPassManager NestedLPM(DebugLogging);
1187 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1188 DebugLogging))
1189 return false;
1190 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001191 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001192 return true;
1193 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001194 if (auto Count = parseRepeatPassName(Name)) {
1195 LoopPassManager NestedLPM(DebugLogging);
1196 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1197 DebugLogging))
1198 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001199 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001200 return true;
1201 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001202 // Normal passes can't have pipelines.
1203 return false;
1204 }
1205
1206 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001207#define LOOP_PASS(NAME, CREATE_PASS) \
1208 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001209 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001210 return true; \
1211 }
1212#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1213 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001214 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001215 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1216 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1217 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001218 return true; \
1219 } \
1220 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001221 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001222 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001223 return true; \
1224 }
1225#include "PassRegistry.def"
1226
1227 return false;
1228}
1229
Chandler Carruthedf59962016-02-18 09:45:17 +00001230bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001231#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1232 if (Name == NAME) { \
1233 AA.registerModuleAnalysis< \
1234 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1235 return true; \
1236 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001237#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1238 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001239 AA.registerFunctionAnalysis< \
1240 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001241 return true; \
1242 }
1243#include "PassRegistry.def"
1244
1245 return false;
1246}
1247
Justin Bognereecc3c82016-02-25 07:23:08 +00001248bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001249 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001250 bool VerifyEachPass,
1251 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001252 for (const auto &Element : Pipeline) {
1253 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1254 return false;
1255 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001256 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001257 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001258}
1259
Chandler Carruth1ff77242015-03-07 09:02:36 +00001260bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001261 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001262 bool VerifyEachPass,
1263 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001264 for (const auto &Element : Pipeline) {
1265 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1266 return false;
1267 if (VerifyEachPass)
1268 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001269 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001270 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001271}
1272
Chandler Carruth1ff77242015-03-07 09:02:36 +00001273bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001274 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001275 bool VerifyEachPass,
1276 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001277 for (const auto &Element : Pipeline) {
1278 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1279 return false;
1280 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001281 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001282 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001283}
1284
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001285void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1286 FunctionAnalysisManager &FAM,
1287 CGSCCAnalysisManager &CGAM,
1288 ModuleAnalysisManager &MAM) {
1289 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1290 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001291 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1292 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1293 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1294 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1295 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1296}
1297
Chandler Carruth1ff77242015-03-07 09:02:36 +00001298bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001299 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001300 bool VerifyEachPass,
1301 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001302 for (const auto &Element : Pipeline) {
1303 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1304 return false;
1305 if (VerifyEachPass)
1306 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001307 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001308 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001309}
1310
1311// Primary pass pipeline description parsing routine.
1312// FIXME: Should this routine accept a TargetMachine or require the caller to
1313// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001314bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1315 StringRef PipelineText, bool VerifyEachPass,
1316 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001317 auto Pipeline = parsePipelineText(PipelineText);
1318 if (!Pipeline || Pipeline->empty())
1319 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001320
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001321 // If the first name isn't at the module layer, wrap the pipeline up
1322 // automatically.
1323 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001324
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001325 if (!isModulePassName(FirstName)) {
1326 if (isCGSCCPassName(FirstName))
1327 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1328 else if (isFunctionPassName(FirstName))
1329 Pipeline = {{"function", std::move(*Pipeline)}};
1330 else if (isLoopPassName(FirstName))
1331 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1332 else
1333 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001334 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001335 }
1336
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001337 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001338}
Chandler Carruthedf59962016-02-18 09:45:17 +00001339
1340bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001341 // If the pipeline just consists of the word 'default' just replace the AA
1342 // manager with our default one.
1343 if (PipelineText == "default") {
1344 AA = buildDefaultAAPipeline();
1345 return true;
1346 }
1347
Chandler Carruthedf59962016-02-18 09:45:17 +00001348 while (!PipelineText.empty()) {
1349 StringRef Name;
1350 std::tie(Name, PipelineText) = PipelineText.split(',');
1351 if (!parseAAPassName(AA, Name))
1352 return false;
1353 }
1354
1355 return true;
1356}