blob: 520c9df30283dff430e43d3d2a4565a18c2b1a80 [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"
Davide Italiano164b9bc2016-05-05 00:51:09 +000064#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000065#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000066#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000067#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000068#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000069#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000070#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000071#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000072#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000073#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000074#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000075#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000076#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000077#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000078#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000079#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000080#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000081#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000082#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000083#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000084#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000085#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000086#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000087#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000088#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000089#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000090#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000091#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000092#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000093#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000094#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000095#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +000096#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +000097#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +000098#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +000099#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000100#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000101#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000102#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000103#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000104#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000105#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000106#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000107#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000108#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000109#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000110#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000111#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000112#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000113#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000114#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000115#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000116#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000117#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000118#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000119#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000120#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000121#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000122#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000123#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000124#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000125#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000126#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000127#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000128#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000129#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000130#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000131#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000132#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000133#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000134#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000135#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000136#include "llvm/Transforms/Utils/Mem2Reg.h"
Geoff Berryb96d3b22016-06-01 21:30:40 +0000137#include "llvm/Transforms/Utils/MemorySSA.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000138#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000139#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000140#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000141#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000142#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000143#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000144
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000145#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000146
147using namespace llvm;
148
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000149static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
150
Chandler Carruthe3f50642016-12-22 06:59:15 +0000151static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
152 switch (Level) {
153 case PassBuilder::O0:
154 case PassBuilder::O1:
155 case PassBuilder::O2:
156 case PassBuilder::O3:
157 return false;
158
159 case PassBuilder::Os:
160 case PassBuilder::Oz:
161 return true;
162 }
163 llvm_unreachable("Invalid optimization level!");
164}
165
Chandler Carruth66445382014-01-11 08:16:35 +0000166namespace {
167
Chandler Carruthd8330982014-01-12 09:34:22 +0000168/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000169struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000170 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000171 return PreservedAnalyses::all();
172 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000173 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000174};
175
Chandler Carruth0b576b32015-01-06 02:50:06 +0000176/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000177class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
178 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000179 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000180
181public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000182 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000183 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000184 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000185};
186
Chandler Carruth572e3402014-04-21 11:12:00 +0000187/// \brief No-op CGSCC pass which does nothing.
188struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000189 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
190 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000191 return PreservedAnalyses::all();
192 }
193 static StringRef name() { return "NoOpCGSCCPass"; }
194};
195
Chandler Carruth0b576b32015-01-06 02:50:06 +0000196/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000197class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
198 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000199 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000200
201public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000202 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000203 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000204 return Result();
205 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000206 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000207};
208
Chandler Carruthd8330982014-01-12 09:34:22 +0000209/// \brief No-op function pass which does nothing.
210struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000211 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000212 return PreservedAnalyses::all();
213 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000214 static StringRef name() { return "NoOpFunctionPass"; }
215};
216
Chandler Carruth0b576b32015-01-06 02:50:06 +0000217/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000218class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
219 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000220 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000221
222public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000223 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000224 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000225 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000226};
227
Justin Bognereecc3c82016-02-25 07:23:08 +0000228/// \brief No-op loop pass which does nothing.
229struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000230 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
231 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000232 return PreservedAnalyses::all();
233 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000234 static StringRef name() { return "NoOpLoopPass"; }
235};
236
237/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000238class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
239 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000240 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000241
242public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000243 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000244 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
245 return Result();
246 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000247 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000248};
249
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000250AnalysisKey NoOpModuleAnalysis::Key;
251AnalysisKey NoOpCGSCCAnalysis::Key;
252AnalysisKey NoOpFunctionAnalysis::Key;
253AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000254
Chandler Carruth66445382014-01-11 08:16:35 +0000255} // End anonymous namespace.
256
Chandler Carruth1ff77242015-03-07 09:02:36 +0000257void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000258#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000259 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000260#include "PassRegistry.def"
261}
262
Chandler Carruth1ff77242015-03-07 09:02:36 +0000263void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000264#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000265 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000266#include "PassRegistry.def"
267}
268
Chandler Carruth1ff77242015-03-07 09:02:36 +0000269void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000270#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000271 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000272#include "PassRegistry.def"
273}
274
Justin Bognereecc3c82016-02-25 07:23:08 +0000275void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000276#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000277 LAM.registerPass([&] { return CREATE_PASS; });
278#include "PassRegistry.def"
279}
280
Chandler Carruthe3f50642016-12-22 06:59:15 +0000281FunctionPassManager
282PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
283 bool DebugLogging) {
284 assert(Level != O0 && "Must request optimizations!");
285 FunctionPassManager FPM(DebugLogging);
286
287 // Form SSA out of local memory accesses after breaking apart aggregates into
288 // scalars.
289 FPM.addPass(SROA());
290
291 // Catch trivial redundancies
292 FPM.addPass(EarlyCSEPass());
293
294 // Speculative execution if the target has divergent branches; otherwise nop.
295 FPM.addPass(SpeculativeExecutionPass());
296
297 // Optimize based on known information about branches, and cleanup afterward.
298 FPM.addPass(JumpThreadingPass());
299 FPM.addPass(CorrelatedValuePropagationPass());
300 FPM.addPass(SimplifyCFGPass());
301 FPM.addPass(InstCombinePass());
302
303 if (!isOptimizingForSize(Level))
304 FPM.addPass(LibCallsShrinkWrapPass());
305
306 FPM.addPass(TailCallElimPass());
307 FPM.addPass(SimplifyCFGPass());
308
309 // Form canonically associated expression trees, and simplify the trees using
310 // basic mathematical properties. For example, this will form (nearly)
311 // minimal multiplication trees.
312 FPM.addPass(ReassociatePass());
313
314 // Add the primary loop simplification pipeline.
315 // FIXME: Currently this is split into two loop pass pipelines because we run
316 // some function passes in between them. These can and should be replaced by
317 // loop pass equivalenst but those aren't ready yet. Specifically,
318 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
319 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
320 // the other we have is `LoopInstSimplify`.
321 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
322
323 // Rotate Loop - disable header duplication at -Oz
324 LPM1.addPass(LoopRotatePass(Level != Oz));
325 LPM1.addPass(LICMPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000326#if 0
327 // The LoopUnswitch pass isn't yet ported to the new pass manager.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000328 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000329#endif
Chandler Carruth79b733b2017-01-26 23:21:17 +0000330 LPM2.addPass(IndVarSimplifyPass());
331 LPM2.addPass(LoopIdiomRecognizePass());
332 LPM2.addPass(LoopDeletionPass());
333 LPM2.addPass(LoopUnrollPass::createFull());
334
Chandler Carruthe3f50642016-12-22 06:59:15 +0000335 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
336 FPM.addPass(SimplifyCFGPass());
337 FPM.addPass(InstCombinePass());
338 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
339
340 // Eliminate redundancies.
341 if (Level != O1) {
342 // These passes add substantial compile time so skip them at O1.
343 FPM.addPass(MergedLoadStoreMotionPass());
344 FPM.addPass(GVN());
345 }
346
347 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
348 FPM.addPass(MemCpyOptPass());
349
350 // Sparse conditional constant propagation.
351 // FIXME: It isn't clear why we do this *after* loop passes rather than
352 // before...
353 FPM.addPass(SCCPPass());
354
355 // Delete dead bit computations (instcombine runs after to fold away the dead
356 // computations, and then ADCE will run later to exploit any new DCE
357 // opportunities that creates).
358 FPM.addPass(BDCEPass());
359
360 // Run instcombine after redundancy and dead bit elimination to exploit
361 // opportunities opened up by them.
362 FPM.addPass(InstCombinePass());
363
364 // Re-consider control flow based optimizations after redundancy elimination,
365 // redo DCE, etc.
366 FPM.addPass(JumpThreadingPass());
367 FPM.addPass(CorrelatedValuePropagationPass());
368 FPM.addPass(DSEPass());
369 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
370
371 // Finally, do an expensive DCE pass to catch all the dead code exposed by
372 // the simplifications and basic cleanup after all the simplifications.
373 FPM.addPass(ADCEPass());
374 FPM.addPass(SimplifyCFGPass());
375 FPM.addPass(InstCombinePass());
376
377 return FPM;
378}
379
380ModulePassManager
381PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
382 bool DebugLogging) {
383 assert(Level != O0 && "Must request optimizations for the default pipeline!");
384 ModulePassManager MPM(DebugLogging);
385
386 // Force any function attributes we want the rest of the pipeline te observe.
387 MPM.addPass(ForceFunctionAttrsPass());
388
389 // Do basic inference of function attributes from known properties of system
390 // libraries and other oracles.
391 MPM.addPass(InferFunctionAttrsPass());
392
393 // Create an early function pass manager to cleanup the output of the
394 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000395 FunctionPassManager EarlyFPM(DebugLogging);
396 EarlyFPM.addPass(SimplifyCFGPass());
397 EarlyFPM.addPass(SROA());
398 EarlyFPM.addPass(EarlyCSEPass());
399 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000400 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000401 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000402
403 // Interprocedural constant propagation now that basic cleanup has occured
404 // and prior to optimizing globals.
405 // FIXME: This position in the pipeline hasn't been carefully considered in
406 // years, it should be re-analyzed.
407 MPM.addPass(IPSCCPPass());
408
409 // Optimize globals to try and fold them into constants.
410 MPM.addPass(GlobalOptPass());
411
412 // Promote any localized globals to SSA registers.
413 // FIXME: Should this instead by a run of SROA?
414 // FIXME: We should probably run instcombine and simplify-cfg afterward to
415 // delete control flows that are dead once globals have been folded to
416 // constants.
417 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
418
419 // Remove any dead arguments exposed by cleanups and constand folding
420 // globals.
421 MPM.addPass(DeadArgumentEliminationPass());
422
423 // Create a small function pass pipeline to cleanup after all the global
424 // optimizations.
425 FunctionPassManager GlobalCleanupPM(DebugLogging);
426 GlobalCleanupPM.addPass(InstCombinePass());
427 GlobalCleanupPM.addPass(SimplifyCFGPass());
428 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
429
430 // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
431#if 0
432 MPM.addPass(RequireAnalysisPass<GlobalsAA>());
433#endif
434
435 // Now begin the main postorder CGSCC pipeline.
436 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
437 // manager and trying to emulate its precise behavior. Much of this doesn't
438 // make a lot of sense and we should revisit the core CGSCC structure.
439 CGSCCPassManager MainCGPipeline(DebugLogging);
440
441 // Note: historically, the PruneEH pass was run first to deduce nounwind and
442 // generally clean up exception handling overhead. It isn't clear this is
443 // valuable as the inliner doesn't currently care whether it is inlining an
444 // invoke or a call.
445
446 // Run the inliner first. The theory is that we are walking bottom-up and so
447 // the callees have already been fully optimized, and we want to inline them
448 // into the callers so that our optimizations can reflect that.
449 // FIXME; Customize the threshold based on optimization level.
450 MainCGPipeline.addPass(InlinerPass());
451
452 // Now deduce any function attributes based in the current code.
453 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
454
455 // Lastly, add the core function simplification pipeline nested inside the
456 // CGSCC walk.
457 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
458 buildFunctionSimplificationPipeline(Level, DebugLogging)));
459
460 MPM.addPass(
461 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
462
463 // This ends the canonicalization and simplification phase of the pipeline.
464 // At this point, we expect to have canonical and simple IR which we begin
465 // *optimizing* for efficient execution going forward.
466
467 // Eliminate externally available functions now that inlining is over -- we
468 // won't emit these anyways.
469 MPM.addPass(EliminateAvailableExternallyPass());
470
471 // Do RPO function attribute inference across the module to forward-propagate
472 // attributes where applicable.
473 // FIXME: Is this really an optimization rather than a canonicalization?
474 MPM.addPass(ReversePostOrderFunctionAttrsPass());
475
476 // Recompute GloblasAA here prior to function passes. This is particularly
477 // useful as the above will have inlined, DCE'ed, and function-attr
478 // propagated everything. We should at this point have a reasonably minimal
479 // and richly annotated call graph. By computing aliasing and mod/ref
480 // information for all local globals here, the late loop passes and notably
481 // the vectorizer will be able to use them to help recognize vectorizable
482 // memory operations.
483 // FIXME: Enable this once analysis invalidation is fully supported.
484#if 0
485 MPM.addPass(Require<GlobalsAA>());
486#endif
487
488 FunctionPassManager OptimizePM(DebugLogging);
489 OptimizePM.addPass(Float2IntPass());
490 // FIXME: We need to run some loop optimizations to re-rotate loops after
491 // simplify-cfg and others undo their rotation.
492
493 // Optimize the loop execution. These passes operate on entire loop nests
494 // rather than on each loop in an inside-out manner, and so they are actually
495 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000496
497 // First rotate loops that may have been un-rotated by prior passes.
498 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
499
500 // Distribute loops to allow partial vectorization. I.e. isolate dependences
501 // into separate loop that would otherwise inhibit vectorization. This is
502 // currently only performed for loops marked with the metadata
503 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000504 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000505
506 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000507 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000508
Chandler Carruthbaabda92017-01-27 01:32:26 +0000509 // Eliminate loads by forwarding stores from the previous iteration to loads
510 // of the current iteration.
511 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000512
513 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000514 OptimizePM.addPass(InstCombinePass());
515
Chandler Carrutha95ff382017-01-27 00:50:21 +0000516
517 // Now that we've formed fast to execute loop structures, we do further
518 // optimizations. These are run afterward as they might block doing complex
519 // analyses and transforms such as what are needed for loop vectorization.
520
Chandler Carruthe3f50642016-12-22 06:59:15 +0000521 // Optimize parallel scalar instruction chains into SIMD instructions.
522 OptimizePM.addPass(SLPVectorizerPass());
523
Chandler Carrutha95ff382017-01-27 00:50:21 +0000524 // Cleanup after all of the vectorizers.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000525 OptimizePM.addPass(SimplifyCFGPass());
526 OptimizePM.addPass(InstCombinePass());
527
528 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000529 // execution resources of an out-of-order processor. We also then need to
530 // clean up redundancies and loop invariant code.
531 // FIXME: It would be really good to use a loop-integrated instruction
532 // combiner for cleanup here so that the unrolling and LICM can be pipelined
533 // across the loop nests.
534 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create()));
535 OptimizePM.addPass(InstCombinePass());
536 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000537
538 // Now that we've vectorized and unrolled loops, we may have more refined
539 // alignment information, try to re-derive it here.
540 OptimizePM.addPass(AlignmentFromAssumptionsPass());
541
Chandler Carrutha95ff382017-01-27 00:50:21 +0000542 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
543 // canonicalization pass that enables other optimizations. As a result,
544 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
545 // result too early.
546 OptimizePM.addPass(LoopSinkPass());
547
548 // And finally clean up LCSSA form before generating code.
549 OptimizePM.addPass(InstSimplifierPass());
550
551 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000552 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
553
554 // Now we need to do some global optimization transforms.
555 // FIXME: It would seem like these should come first in the optimization
556 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
557 // ordering here.
558 MPM.addPass(GlobalDCEPass());
559 MPM.addPass(ConstantMergePass());
560
561 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000562}
563
Chandler Carruthe3f50642016-12-22 06:59:15 +0000564ModulePassManager
565PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
566 bool DebugLogging) {
567 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000568 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000569 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000570}
571
Chandler Carruthe3f50642016-12-22 06:59:15 +0000572ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
573 bool DebugLogging) {
574 assert(Level != O0 && "Must request optimizations for the default pipeline!");
575 ModulePassManager MPM(DebugLogging);
576
Davide Italiano089a9122017-01-24 00:57:39 +0000577 // Remove unused virtual tables to improve the quality of code generated by
578 // whole-program devirtualization and bitset lowering.
579 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000580
Davide Italiano089a9122017-01-24 00:57:39 +0000581 // Force any function attributes we want the rest of the pipeline to observe.
582 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000583
Davide Italiano089a9122017-01-24 00:57:39 +0000584 // Do basic inference of function attributes from known properties of system
585 // libraries and other oracles.
586 MPM.addPass(InferFunctionAttrsPass());
587
588 if (Level > 1) {
589 // Indirect call promotion. This should promote all the targets that are
590 // left by the earlier promotion pass that promotes intra-module targets.
591 // This two-step promotion is to save the compile time. For LTO, it should
592 // produce the same result as if we only do promotion here.
593 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */));
594
595 // Propagate constants at call sites into the functions they call. This
596 // opens opportunities for globalopt (and inlining) by substituting function
597 // pointers passed as arguments to direct uses of functions.
598 MPM.addPass(IPSCCPPass());
599 }
600
601 // Now deduce any function attributes based in the current code.
602 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
603 PostOrderFunctionAttrsPass()));
604
605 // Do RPO function attribute inference across the module to forward-propagate
606 // attributes where applicable.
607 // FIXME: Is this really an optimization rather than a canonicalization?
608 MPM.addPass(ReversePostOrderFunctionAttrsPass());
609
610 // Use inragne annotations on GEP indices to split globals where beneficial.
611 MPM.addPass(GlobalSplitPass());
612
613 // Run whole program optimization of virtual call when the list of callees
614 // is fixed.
615 MPM.addPass(WholeProgramDevirtPass());
616
617 // Stop here at -O1.
618 if (Level == 1)
619 return MPM;
620
621 // Optimize globals to try and fold them into constants.
622 MPM.addPass(GlobalOptPass());
623
624 // Promote any localized globals to SSA registers.
625 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
626
627 // Linking modules together can lead to duplicate global constant, only
628 // keep one copy of each constant.
629 MPM.addPass(ConstantMergePass());
630
631 // Remove unused arguments from functions.
632 MPM.addPass(DeadArgumentEliminationPass());
633
634 // Reduce the code after globalopt and ipsccp. Both can open up significant
635 // simplification opportunities, and both can propagate functions through
636 // function pointers. When this happens, we often have to resolve varargs
637 // calls, etc, so let instcombine do this.
638 // FIXME: add peephole extensions here as the legacy PM does.
639 MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass()));
640
641 // Note: historically, the PruneEH pass was run first to deduce nounwind and
642 // generally clean up exception handling overhead. It isn't clear this is
643 // valuable as the inliner doesn't currently care whether it is inlining an
644 // invoke or a call.
645 // Run the inliner now.
646 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass()));
647
648 // Optimize globals again after we ran the inliner.
649 MPM.addPass(GlobalOptPass());
650
651 // Garbage collect dead functions.
652 // FIXME: Add ArgumentPromotion pass after once it's ported.
653 MPM.addPass(GlobalDCEPass());
654
655 FunctionPassManager FPM(DebugLogging);
656
657 // The IPO Passes may leave cruft around. Clean up after them.
658 // FIXME: add peephole extensions here as the legacy PM does.
659 FPM.addPass(InstCombinePass());
660 FPM.addPass(JumpThreadingPass());
661
662 // Break up allocas
663 FPM.addPass(SROA());
664
665 // Run a few AA driver optimizations here and now to cleanup the code.
666 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
667
668 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
669 PostOrderFunctionAttrsPass()));
670 // FIXME: here we run IP alias analysis in the legacy PM.
671
672 FunctionPassManager MainFPM;
673
674 // FIXME: once we fix LoopPass Manager, add LICM here.
675 // FIXME: once we provide support for enabling MLSM, add it here.
676 // FIXME: once we provide support for enabling NewGVN, add it here.
677 MainFPM.addPass(GVN());
678
679 // Remove dead memcpy()'s.
680 MainFPM.addPass(MemCpyOptPass());
681
682 // Nuke dead stores.
683 MainFPM.addPass(DSEPass());
684
685 // FIXME: at this point, we run a bunch of loop passes:
686 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
687 // loopVectorize. Enable them once the remaining issue with LPM
688 // are sorted out.
689
690 MainFPM.addPass(InstCombinePass());
691 MainFPM.addPass(SimplifyCFGPass());
692 MainFPM.addPass(SCCPPass());
693 MainFPM.addPass(InstCombinePass());
694 MainFPM.addPass(BDCEPass());
695
696 // FIXME: We may want to run SLPVectorizer here.
697 // After vectorization, assume intrinsics may tell us more
698 // about pointer alignments.
699#if 0
700 MainFPM.add(AlignmentFromAssumptionsPass());
701#endif
702
703 // FIXME: Conditionally run LoadCombine here, after it's ported
704 // (in case we still have this pass, given its questionable usefulness).
705
706 // FIXME: add peephole extensions to the PM here.
707 MainFPM.addPass(InstCombinePass());
708 MainFPM.addPass(JumpThreadingPass());
709 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
710
711 // Create a function that performs CFI checks for cross-DSO calls with
712 // targets in the current module.
713 MPM.addPass(CrossDSOCFIPass());
714
715 // Lower type metadata and the type.test intrinsic. This pass supports
716 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
717 // to be run at link time if CFI is enabled. This pass does nothing if
718 // CFI is disabled.
719 // Enable once we add support for the summary in the new PM.
720#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +0000721 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
722 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +0000723 Summary));
724#endif
725
726 // Add late LTO optimization passes.
727 // Delete basic blocks, which optimization passes may have killed.
728 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
729
730 // Drop bodies of available eternally objects to improve GlobalDCE.
731 MPM.addPass(EliminateAvailableExternallyPass());
732
733 // Now that we have optimized the program, discard unreachable functions.
734 MPM.addPass(GlobalDCEPass());
735
736 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000737 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000738}
739
Chandler Carruth060ad612016-12-23 20:38:19 +0000740AAManager PassBuilder::buildDefaultAAPipeline() {
741 AAManager AA;
742
743 // The order in which these are registered determines their priority when
744 // being queried.
745
746 // First we register the basic alias analysis that provides the majority of
747 // per-function local AA logic. This is a stateless, on-demand local set of
748 // AA techniques.
749 AA.registerFunctionAnalysis<BasicAA>();
750
751 // Next we query fast, specialized alias analyses that wrap IR-embedded
752 // information about aliasing.
753 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
754 AA.registerFunctionAnalysis<TypeBasedAA>();
755
756 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +0000757 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
758 // analysis, all that the `AAManager` can do is query for any *cached*
759 // results from `GlobalsAA` through a readonly proxy..
Chandler Carruth060ad612016-12-23 20:38:19 +0000760#if 0
Chandler Carruth534d6442016-12-24 05:11:17 +0000761 // FIXME: Enable once the invalidation logic supports this. Currently, the
762 // `AAManager` will hold stale references to the module analyses.
Chandler Carruth060ad612016-12-23 20:38:19 +0000763 AA.registerModuleAnalysis<GlobalsAA>();
764#endif
765
766 return AA;
767}
768
Chandler Carruth241bf242016-08-03 07:44:48 +0000769static Optional<int> parseRepeatPassName(StringRef Name) {
770 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
771 return None;
772 int Count;
773 if (Name.getAsInteger(0, Count) || Count <= 0)
774 return None;
775 return Count;
776}
777
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000778static Optional<int> parseDevirtPassName(StringRef Name) {
779 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
780 return None;
781 int Count;
782 if (Name.getAsInteger(0, Count) || Count <= 0)
783 return None;
784 return Count;
785}
786
Chandler Carruth66445382014-01-11 08:16:35 +0000787static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000788 // Manually handle aliases for pre-configured pipeline fragments.
789 if (Name.startswith("default") || Name.startswith("lto"))
790 return DefaultAliasRegex.match(Name);
791
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000792 // Explicitly handle pass manager names.
793 if (Name == "module")
794 return true;
795 if (Name == "cgscc")
796 return true;
797 if (Name == "function")
798 return true;
799
Chandler Carruth241bf242016-08-03 07:44:48 +0000800 // Explicitly handle custom-parsed pass names.
801 if (parseRepeatPassName(Name))
802 return true;
803
Chandler Carruth74a8a222016-06-17 07:15:29 +0000804#define MODULE_PASS(NAME, CREATE_PASS) \
805 if (Name == NAME) \
806 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000807#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000808 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000809 return true;
810#include "PassRegistry.def"
811
Chandler Carruth66445382014-01-11 08:16:35 +0000812 return false;
813}
814
Chandler Carruth572e3402014-04-21 11:12:00 +0000815static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000816 // Explicitly handle pass manager names.
817 if (Name == "cgscc")
818 return true;
819 if (Name == "function")
820 return true;
821
Chandler Carruth241bf242016-08-03 07:44:48 +0000822 // Explicitly handle custom-parsed pass names.
823 if (parseRepeatPassName(Name))
824 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +0000825 if (parseDevirtPassName(Name))
826 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +0000827
Chandler Carruth74a8a222016-06-17 07:15:29 +0000828#define CGSCC_PASS(NAME, CREATE_PASS) \
829 if (Name == NAME) \
830 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000831#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000832 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000833 return true;
834#include "PassRegistry.def"
835
Chandler Carruth572e3402014-04-21 11:12:00 +0000836 return false;
837}
838
Chandler Carruthd8330982014-01-12 09:34:22 +0000839static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000840 // Explicitly handle pass manager names.
841 if (Name == "function")
842 return true;
843 if (Name == "loop")
844 return true;
845
Chandler Carruth241bf242016-08-03 07:44:48 +0000846 // Explicitly handle custom-parsed pass names.
847 if (parseRepeatPassName(Name))
848 return true;
849
Chandler Carruth74a8a222016-06-17 07:15:29 +0000850#define FUNCTION_PASS(NAME, CREATE_PASS) \
851 if (Name == NAME) \
852 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000853#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000854 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000855 return true;
856#include "PassRegistry.def"
857
Chandler Carruthd8330982014-01-12 09:34:22 +0000858 return false;
859}
860
Justin Bognereecc3c82016-02-25 07:23:08 +0000861static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000862 // Explicitly handle pass manager names.
863 if (Name == "loop")
864 return true;
865
Chandler Carruth241bf242016-08-03 07:44:48 +0000866 // Explicitly handle custom-parsed pass names.
867 if (parseRepeatPassName(Name))
868 return true;
869
Chandler Carruth74a8a222016-06-17 07:15:29 +0000870#define LOOP_PASS(NAME, CREATE_PASS) \
871 if (Name == NAME) \
872 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000873#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
874 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
875 return true;
876#include "PassRegistry.def"
877
878 return false;
879}
880
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000881Optional<std::vector<PassBuilder::PipelineElement>>
882PassBuilder::parsePipelineText(StringRef Text) {
883 std::vector<PipelineElement> ResultPipeline;
884
885 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
886 &ResultPipeline};
887 for (;;) {
888 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
889 size_t Pos = Text.find_first_of(",()");
890 Pipeline.push_back({Text.substr(0, Pos), {}});
891
892 // If we have a single terminating name, we're done.
893 if (Pos == Text.npos)
894 break;
895
896 char Sep = Text[Pos];
897 Text = Text.substr(Pos + 1);
898 if (Sep == ',')
899 // Just a name ending in a comma, continue.
900 continue;
901
902 if (Sep == '(') {
903 // Push the inner pipeline onto the stack to continue processing.
904 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
905 continue;
906 }
907
908 assert(Sep == ')' && "Bogus separator!");
909 // When handling the close parenthesis, we greedily consume them to avoid
910 // empty strings in the pipeline.
911 do {
912 // If we try to pop the outer pipeline we have unbalanced parentheses.
913 if (PipelineStack.size() == 1)
914 return None;
915
916 PipelineStack.pop_back();
917 } while (Text.consume_front(")"));
918
919 // Check if we've finished parsing.
920 if (Text.empty())
921 break;
922
923 // Otherwise, the end of an inner pipeline always has to be followed by
924 // a comma, and then we can continue.
925 if (!Text.consume_front(","))
926 return None;
927 }
928
929 if (PipelineStack.size() > 1)
930 // Unbalanced paretheses.
931 return None;
932
933 assert(PipelineStack.back() == &ResultPipeline &&
934 "Wrong pipeline at the bottom of the stack!");
935 return {std::move(ResultPipeline)};
936}
937
938bool PassBuilder::parseModulePass(ModulePassManager &MPM,
939 const PipelineElement &E, bool VerifyEachPass,
940 bool DebugLogging) {
941 auto &Name = E.Name;
942 auto &InnerPipeline = E.InnerPipeline;
943
944 // First handle complex passes like the pass managers which carry pipelines.
945 if (!InnerPipeline.empty()) {
946 if (Name == "module") {
947 ModulePassManager NestedMPM(DebugLogging);
948 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
949 DebugLogging))
950 return false;
951 MPM.addPass(std::move(NestedMPM));
952 return true;
953 }
954 if (Name == "cgscc") {
955 CGSCCPassManager CGPM(DebugLogging);
956 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
957 DebugLogging))
958 return false;
959 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
960 DebugLogging));
961 return true;
962 }
963 if (Name == "function") {
964 FunctionPassManager FPM(DebugLogging);
965 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
966 DebugLogging))
967 return false;
968 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
969 return true;
970 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000971 if (auto Count = parseRepeatPassName(Name)) {
972 ModulePassManager NestedMPM(DebugLogging);
973 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
974 DebugLogging))
975 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000976 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000977 return true;
978 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000979 // Normal passes can't have pipelines.
980 return false;
981 }
982
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000983 // Manually handle aliases for pre-configured pipeline fragments.
984 if (Name.startswith("default") || Name.startswith("lto")) {
985 SmallVector<StringRef, 3> Matches;
986 if (!DefaultAliasRegex.match(Name, &Matches))
987 return false;
988 assert(Matches.size() == 3 && "Must capture two matched strings!");
989
Jordan Rosef85a95f2016-07-25 18:34:51 +0000990 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
991 .Case("O0", O0)
992 .Case("O1", O1)
993 .Case("O2", O2)
994 .Case("O3", O3)
995 .Case("Os", Os)
996 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000997 if (L == O0)
998 // At O0 we do nothing at all!
999 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001000
1001 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001002 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001003 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001004 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001005 } else {
1006 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001007 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001008 }
1009 return true;
1010 }
1011
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001012 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001013#define MODULE_PASS(NAME, CREATE_PASS) \
1014 if (Name == NAME) { \
1015 MPM.addPass(CREATE_PASS); \
1016 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001017 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001018#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1019 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001020 MPM.addPass( \
1021 RequireAnalysisPass< \
1022 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001023 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001024 } \
1025 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001026 MPM.addPass(InvalidateAnalysisPass< \
1027 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001028 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001029 }
1030#include "PassRegistry.def"
1031
Chandler Carruth66445382014-01-11 08:16:35 +00001032 return false;
1033}
1034
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001035bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1036 const PipelineElement &E, bool VerifyEachPass,
1037 bool DebugLogging) {
1038 auto &Name = E.Name;
1039 auto &InnerPipeline = E.InnerPipeline;
1040
1041 // First handle complex passes like the pass managers which carry pipelines.
1042 if (!InnerPipeline.empty()) {
1043 if (Name == "cgscc") {
1044 CGSCCPassManager NestedCGPM(DebugLogging);
1045 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1046 DebugLogging))
1047 return false;
1048 // Add the nested pass manager with the appropriate adaptor.
1049 CGPM.addPass(std::move(NestedCGPM));
1050 return true;
1051 }
1052 if (Name == "function") {
1053 FunctionPassManager FPM(DebugLogging);
1054 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1055 DebugLogging))
1056 return false;
1057 // Add the nested pass manager with the appropriate adaptor.
1058 CGPM.addPass(
1059 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
1060 return true;
1061 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001062 if (auto Count = parseRepeatPassName(Name)) {
1063 CGSCCPassManager NestedCGPM(DebugLogging);
1064 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1065 DebugLogging))
1066 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001067 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001068 return true;
1069 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001070 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1071 CGSCCPassManager NestedCGPM(DebugLogging);
1072 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1073 DebugLogging))
1074 return false;
1075 CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM),
1076 *MaxRepetitions, DebugLogging));
1077 return true;
1078 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001079 // Normal passes can't have pipelines.
1080 return false;
1081 }
1082
1083 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001084#define CGSCC_PASS(NAME, CREATE_PASS) \
1085 if (Name == NAME) { \
1086 CGPM.addPass(CREATE_PASS); \
1087 return true; \
1088 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001089#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1090 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001091 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001092 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001093 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1094 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001095 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001096 } \
1097 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001098 CGPM.addPass(InvalidateAnalysisPass< \
1099 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001100 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001101 }
1102#include "PassRegistry.def"
1103
Chandler Carruth572e3402014-04-21 11:12:00 +00001104 return false;
1105}
1106
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001107bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1108 const PipelineElement &E,
1109 bool VerifyEachPass, bool DebugLogging) {
1110 auto &Name = E.Name;
1111 auto &InnerPipeline = E.InnerPipeline;
1112
1113 // First handle complex passes like the pass managers which carry pipelines.
1114 if (!InnerPipeline.empty()) {
1115 if (Name == "function") {
1116 FunctionPassManager NestedFPM(DebugLogging);
1117 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1118 DebugLogging))
1119 return false;
1120 // Add the nested pass manager with the appropriate adaptor.
1121 FPM.addPass(std::move(NestedFPM));
1122 return true;
1123 }
1124 if (Name == "loop") {
1125 LoopPassManager LPM(DebugLogging);
1126 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1127 DebugLogging))
1128 return false;
1129 // Add the nested pass manager with the appropriate adaptor.
1130 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1131 return true;
1132 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001133 if (auto Count = parseRepeatPassName(Name)) {
1134 FunctionPassManager NestedFPM(DebugLogging);
1135 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1136 DebugLogging))
1137 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001138 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001139 return true;
1140 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001141 // Normal passes can't have pipelines.
1142 return false;
1143 }
1144
1145 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001146#define FUNCTION_PASS(NAME, CREATE_PASS) \
1147 if (Name == NAME) { \
1148 FPM.addPass(CREATE_PASS); \
1149 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001150 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001151#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1152 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001153 FPM.addPass( \
1154 RequireAnalysisPass< \
1155 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001156 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001157 } \
1158 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001159 FPM.addPass(InvalidateAnalysisPass< \
1160 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001161 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001162 }
1163#include "PassRegistry.def"
1164
Chandler Carruthd8330982014-01-12 09:34:22 +00001165 return false;
1166}
1167
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001168bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001169 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001170 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001171 auto &InnerPipeline = E.InnerPipeline;
1172
1173 // First handle complex passes like the pass managers which carry pipelines.
1174 if (!InnerPipeline.empty()) {
1175 if (Name == "loop") {
1176 LoopPassManager NestedLPM(DebugLogging);
1177 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1178 DebugLogging))
1179 return false;
1180 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001181 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001182 return true;
1183 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001184 if (auto Count = parseRepeatPassName(Name)) {
1185 LoopPassManager NestedLPM(DebugLogging);
1186 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1187 DebugLogging))
1188 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001189 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001190 return true;
1191 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001192 // Normal passes can't have pipelines.
1193 return false;
1194 }
1195
1196 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001197#define LOOP_PASS(NAME, CREATE_PASS) \
1198 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001199 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001200 return true; \
1201 }
1202#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1203 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001204 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001205 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1206 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1207 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001208 return true; \
1209 } \
1210 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001211 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001212 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001213 return true; \
1214 }
1215#include "PassRegistry.def"
1216
1217 return false;
1218}
1219
Chandler Carruthedf59962016-02-18 09:45:17 +00001220bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001221#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1222 if (Name == NAME) { \
1223 AA.registerModuleAnalysis< \
1224 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1225 return true; \
1226 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001227#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1228 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001229 AA.registerFunctionAnalysis< \
1230 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001231 return true; \
1232 }
1233#include "PassRegistry.def"
1234
1235 return false;
1236}
1237
Justin Bognereecc3c82016-02-25 07:23:08 +00001238bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001239 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001240 bool VerifyEachPass,
1241 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001242 for (const auto &Element : Pipeline) {
1243 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1244 return false;
1245 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001246 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001247 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001248}
1249
Chandler Carruth1ff77242015-03-07 09:02:36 +00001250bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001251 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001252 bool VerifyEachPass,
1253 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001254 for (const auto &Element : Pipeline) {
1255 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1256 return false;
1257 if (VerifyEachPass)
1258 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001259 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001260 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001261}
1262
Chandler Carruth1ff77242015-03-07 09:02:36 +00001263bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001264 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001265 bool VerifyEachPass,
1266 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001267 for (const auto &Element : Pipeline) {
1268 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1269 return false;
1270 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001271 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001272 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001273}
1274
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001275void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1276 FunctionAnalysisManager &FAM,
1277 CGSCCAnalysisManager &CGAM,
1278 ModuleAnalysisManager &MAM) {
1279 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1280 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001281 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1282 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1283 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1284 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1285 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1286}
1287
Chandler Carruth1ff77242015-03-07 09:02:36 +00001288bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001289 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001290 bool VerifyEachPass,
1291 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001292 for (const auto &Element : Pipeline) {
1293 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1294 return false;
1295 if (VerifyEachPass)
1296 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001297 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001298 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001299}
1300
1301// Primary pass pipeline description parsing routine.
1302// FIXME: Should this routine accept a TargetMachine or require the caller to
1303// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001304bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1305 StringRef PipelineText, bool VerifyEachPass,
1306 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001307 auto Pipeline = parsePipelineText(PipelineText);
1308 if (!Pipeline || Pipeline->empty())
1309 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001310
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001311 // If the first name isn't at the module layer, wrap the pipeline up
1312 // automatically.
1313 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001314
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001315 if (!isModulePassName(FirstName)) {
1316 if (isCGSCCPassName(FirstName))
1317 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1318 else if (isFunctionPassName(FirstName))
1319 Pipeline = {{"function", std::move(*Pipeline)}};
1320 else if (isLoopPassName(FirstName))
1321 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1322 else
1323 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001324 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001325 }
1326
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001327 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001328}
Chandler Carruthedf59962016-02-18 09:45:17 +00001329
1330bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001331 // If the pipeline just consists of the word 'default' just replace the AA
1332 // manager with our default one.
1333 if (PipelineText == "default") {
1334 AA = buildDefaultAAPipeline();
1335 return true;
1336 }
1337
Chandler Carruthedf59962016-02-18 09:45:17 +00001338 while (!PipelineText.empty()) {
1339 StringRef Name;
1340 std::tie(Name, PipelineText) = PipelineText.split(',');
1341 if (!parseAAPassName(AA, Name))
1342 return false;
1343 }
1344
1345 return true;
1346}