blob: 7ae9e97e5cdcf75adee90612a5ced07291585bf9 [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"
Sanjoy Das4d4339d2016-06-05 18:01:19 +000097#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +000098#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +000099#include "llvm/Transforms/Scalar/LICM.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000100#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000101#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000102#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000103#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000104#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000105#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000106#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000107#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000108#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000109#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000110#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000111#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000112#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000113#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000114#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000115#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000116#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000117#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000118#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000119#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000120#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000121#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000122#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000123#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000124#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000125#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000126#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000127#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000128#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000129#include "llvm/Transforms/Utils/Mem2Reg.h"
Geoff Berryb96d3b22016-06-01 21:30:40 +0000130#include "llvm/Transforms/Utils/MemorySSA.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000131#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000132#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000133#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000134#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000135#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000136
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000137#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000138
139using namespace llvm;
140
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000141static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$");
142
Chandler Carruthe3f50642016-12-22 06:59:15 +0000143static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
144 switch (Level) {
145 case PassBuilder::O0:
146 case PassBuilder::O1:
147 case PassBuilder::O2:
148 case PassBuilder::O3:
149 return false;
150
151 case PassBuilder::Os:
152 case PassBuilder::Oz:
153 return true;
154 }
155 llvm_unreachable("Invalid optimization level!");
156}
157
Chandler Carruth66445382014-01-11 08:16:35 +0000158namespace {
159
Chandler Carruthd8330982014-01-12 09:34:22 +0000160/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000161struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000162 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000163 return PreservedAnalyses::all();
164 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000165 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000166};
167
Chandler Carruth0b576b32015-01-06 02:50:06 +0000168/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000169class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
170 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000171 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000172
173public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000174 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000175 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000176 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000177};
178
Chandler Carruth572e3402014-04-21 11:12:00 +0000179/// \brief No-op CGSCC pass which does nothing.
180struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000181 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
182 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000183 return PreservedAnalyses::all();
184 }
185 static StringRef name() { return "NoOpCGSCCPass"; }
186};
187
Chandler Carruth0b576b32015-01-06 02:50:06 +0000188/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000189class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
190 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000191 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000192
193public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000194 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000195 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000196 return Result();
197 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000198 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000199};
200
Chandler Carruthd8330982014-01-12 09:34:22 +0000201/// \brief No-op function pass which does nothing.
202struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000203 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000204 return PreservedAnalyses::all();
205 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000206 static StringRef name() { return "NoOpFunctionPass"; }
207};
208
Chandler Carruth0b576b32015-01-06 02:50:06 +0000209/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000210class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
211 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000212 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000213
214public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000215 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000216 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000217 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000218};
219
Justin Bognereecc3c82016-02-25 07:23:08 +0000220/// \brief No-op loop pass which does nothing.
221struct NoOpLoopPass {
Sean Silva0746f3b2016-08-09 00:28:52 +0000222 PreservedAnalyses run(Loop &L, LoopAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000223 return PreservedAnalyses::all();
224 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000225 static StringRef name() { return "NoOpLoopPass"; }
226};
227
228/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000229class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
230 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000231 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000232
233public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000234 struct Result {};
Sean Silva0746f3b2016-08-09 00:28:52 +0000235 Result run(Loop &, LoopAnalysisManager &) { return Result(); }
Justin Bognereecc3c82016-02-25 07:23:08 +0000236 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000237};
238
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000239AnalysisKey NoOpModuleAnalysis::Key;
240AnalysisKey NoOpCGSCCAnalysis::Key;
241AnalysisKey NoOpFunctionAnalysis::Key;
242AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000243
Chandler Carruth66445382014-01-11 08:16:35 +0000244} // End anonymous namespace.
245
Chandler Carruth1ff77242015-03-07 09:02:36 +0000246void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000247#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000248 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000249#include "PassRegistry.def"
250}
251
Chandler Carruth1ff77242015-03-07 09:02:36 +0000252void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000253#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000254 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000255#include "PassRegistry.def"
256}
257
Chandler Carruth1ff77242015-03-07 09:02:36 +0000258void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000259#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000260 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000261#include "PassRegistry.def"
262}
263
Justin Bognereecc3c82016-02-25 07:23:08 +0000264void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000265#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000266 LAM.registerPass([&] { return CREATE_PASS; });
267#include "PassRegistry.def"
268}
269
Chandler Carruthe3f50642016-12-22 06:59:15 +0000270FunctionPassManager
271PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
272 bool DebugLogging) {
273 assert(Level != O0 && "Must request optimizations!");
274 FunctionPassManager FPM(DebugLogging);
275
276 // Form SSA out of local memory accesses after breaking apart aggregates into
277 // scalars.
278 FPM.addPass(SROA());
279
280 // Catch trivial redundancies
281 FPM.addPass(EarlyCSEPass());
282
283 // Speculative execution if the target has divergent branches; otherwise nop.
284 FPM.addPass(SpeculativeExecutionPass());
285
286 // Optimize based on known information about branches, and cleanup afterward.
287 FPM.addPass(JumpThreadingPass());
288 FPM.addPass(CorrelatedValuePropagationPass());
289 FPM.addPass(SimplifyCFGPass());
290 FPM.addPass(InstCombinePass());
291
292 if (!isOptimizingForSize(Level))
293 FPM.addPass(LibCallsShrinkWrapPass());
294
295 FPM.addPass(TailCallElimPass());
296 FPM.addPass(SimplifyCFGPass());
297
298 // Form canonically associated expression trees, and simplify the trees using
299 // basic mathematical properties. For example, this will form (nearly)
300 // minimal multiplication trees.
301 FPM.addPass(ReassociatePass());
302
303 // Add the primary loop simplification pipeline.
304 // FIXME: Currently this is split into two loop pass pipelines because we run
305 // some function passes in between them. These can and should be replaced by
306 // loop pass equivalenst but those aren't ready yet. Specifically,
307 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
308 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
309 // the other we have is `LoopInstSimplify`.
310 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
311
312 // Rotate Loop - disable header duplication at -Oz
313 LPM1.addPass(LoopRotatePass(Level != Oz));
314 LPM1.addPass(LICMPass());
315 // FIXME: Enable these when the loop pass manager can support updating the
316 // loop nest after transformations and we finsih porting the loop passes.
317#if 0
318 LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3));
319 LPM2.addPass(IndVarSimplifyPass());
320 LPM2.addPass(LoopIdiomPass());
321 LPM2.addPass(LoopDeletionPass());
322 LPM2.addPass(SimpleLoopUnrollPass());
323#endif
324 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
325 FPM.addPass(SimplifyCFGPass());
326 FPM.addPass(InstCombinePass());
327 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
328
329 // Eliminate redundancies.
330 if (Level != O1) {
331 // These passes add substantial compile time so skip them at O1.
332 FPM.addPass(MergedLoadStoreMotionPass());
333 FPM.addPass(GVN());
334 }
335
336 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
337 FPM.addPass(MemCpyOptPass());
338
339 // Sparse conditional constant propagation.
340 // FIXME: It isn't clear why we do this *after* loop passes rather than
341 // before...
342 FPM.addPass(SCCPPass());
343
344 // Delete dead bit computations (instcombine runs after to fold away the dead
345 // computations, and then ADCE will run later to exploit any new DCE
346 // opportunities that creates).
347 FPM.addPass(BDCEPass());
348
349 // Run instcombine after redundancy and dead bit elimination to exploit
350 // opportunities opened up by them.
351 FPM.addPass(InstCombinePass());
352
353 // Re-consider control flow based optimizations after redundancy elimination,
354 // redo DCE, etc.
355 FPM.addPass(JumpThreadingPass());
356 FPM.addPass(CorrelatedValuePropagationPass());
357 FPM.addPass(DSEPass());
358 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
359
360 // Finally, do an expensive DCE pass to catch all the dead code exposed by
361 // the simplifications and basic cleanup after all the simplifications.
362 FPM.addPass(ADCEPass());
363 FPM.addPass(SimplifyCFGPass());
364 FPM.addPass(InstCombinePass());
365
366 return FPM;
367}
368
369ModulePassManager
370PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
371 bool DebugLogging) {
372 assert(Level != O0 && "Must request optimizations for the default pipeline!");
373 ModulePassManager MPM(DebugLogging);
374
375 // Force any function attributes we want the rest of the pipeline te observe.
376 MPM.addPass(ForceFunctionAttrsPass());
377
378 // Do basic inference of function attributes from known properties of system
379 // libraries and other oracles.
380 MPM.addPass(InferFunctionAttrsPass());
381
382 // Create an early function pass manager to cleanup the output of the
383 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000384 FunctionPassManager EarlyFPM(DebugLogging);
385 EarlyFPM.addPass(SimplifyCFGPass());
386 EarlyFPM.addPass(SROA());
387 EarlyFPM.addPass(EarlyCSEPass());
388 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000389 EarlyFPM.addPass(GVNHoistPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000390 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000391
392 // Interprocedural constant propagation now that basic cleanup has occured
393 // and prior to optimizing globals.
394 // FIXME: This position in the pipeline hasn't been carefully considered in
395 // years, it should be re-analyzed.
396 MPM.addPass(IPSCCPPass());
397
398 // Optimize globals to try and fold them into constants.
399 MPM.addPass(GlobalOptPass());
400
401 // Promote any localized globals to SSA registers.
402 // FIXME: Should this instead by a run of SROA?
403 // FIXME: We should probably run instcombine and simplify-cfg afterward to
404 // delete control flows that are dead once globals have been folded to
405 // constants.
406 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
407
408 // Remove any dead arguments exposed by cleanups and constand folding
409 // globals.
410 MPM.addPass(DeadArgumentEliminationPass());
411
412 // Create a small function pass pipeline to cleanup after all the global
413 // optimizations.
414 FunctionPassManager GlobalCleanupPM(DebugLogging);
415 GlobalCleanupPM.addPass(InstCombinePass());
416 GlobalCleanupPM.addPass(SimplifyCFGPass());
417 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
418
419 // FIXME: Enable this when cross-IR-unit analysis invalidation is working.
420#if 0
421 MPM.addPass(RequireAnalysisPass<GlobalsAA>());
422#endif
423
424 // Now begin the main postorder CGSCC pipeline.
425 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
426 // manager and trying to emulate its precise behavior. Much of this doesn't
427 // make a lot of sense and we should revisit the core CGSCC structure.
428 CGSCCPassManager MainCGPipeline(DebugLogging);
429
430 // Note: historically, the PruneEH pass was run first to deduce nounwind and
431 // generally clean up exception handling overhead. It isn't clear this is
432 // valuable as the inliner doesn't currently care whether it is inlining an
433 // invoke or a call.
434
435 // Run the inliner first. The theory is that we are walking bottom-up and so
436 // the callees have already been fully optimized, and we want to inline them
437 // into the callers so that our optimizations can reflect that.
438 // FIXME; Customize the threshold based on optimization level.
439 MainCGPipeline.addPass(InlinerPass());
440
441 // Now deduce any function attributes based in the current code.
442 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
443
444 // Lastly, add the core function simplification pipeline nested inside the
445 // CGSCC walk.
446 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
447 buildFunctionSimplificationPipeline(Level, DebugLogging)));
448
449 MPM.addPass(
450 createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline)));
451
452 // This ends the canonicalization and simplification phase of the pipeline.
453 // At this point, we expect to have canonical and simple IR which we begin
454 // *optimizing* for efficient execution going forward.
455
456 // Eliminate externally available functions now that inlining is over -- we
457 // won't emit these anyways.
458 MPM.addPass(EliminateAvailableExternallyPass());
459
460 // Do RPO function attribute inference across the module to forward-propagate
461 // attributes where applicable.
462 // FIXME: Is this really an optimization rather than a canonicalization?
463 MPM.addPass(ReversePostOrderFunctionAttrsPass());
464
465 // Recompute GloblasAA here prior to function passes. This is particularly
466 // useful as the above will have inlined, DCE'ed, and function-attr
467 // propagated everything. We should at this point have a reasonably minimal
468 // and richly annotated call graph. By computing aliasing and mod/ref
469 // information for all local globals here, the late loop passes and notably
470 // the vectorizer will be able to use them to help recognize vectorizable
471 // memory operations.
472 // FIXME: Enable this once analysis invalidation is fully supported.
473#if 0
474 MPM.addPass(Require<GlobalsAA>());
475#endif
476
477 FunctionPassManager OptimizePM(DebugLogging);
478 OptimizePM.addPass(Float2IntPass());
479 // FIXME: We need to run some loop optimizations to re-rotate loops after
480 // simplify-cfg and others undo their rotation.
481
482 // Optimize the loop execution. These passes operate on entire loop nests
483 // rather than on each loop in an inside-out manner, and so they are actually
484 // function passes.
485 OptimizePM.addPass(LoopDistributePass());
486 OptimizePM.addPass(LoopVectorizePass());
487 // FIXME: Need to port Loop Load Elimination and add it here.
488 OptimizePM.addPass(InstCombinePass());
489
490 // Optimize parallel scalar instruction chains into SIMD instructions.
491 OptimizePM.addPass(SLPVectorizerPass());
492
493 // Cleanup after vectorizers.
494 OptimizePM.addPass(SimplifyCFGPass());
495 OptimizePM.addPass(InstCombinePass());
496
497 // Unroll small loops to hide loop backedge latency and saturate any parallel
498 // execution resources of an out-of-order processor.
499 // FIXME: Need to add once loop pass pipeline is available.
500
501 // FIXME: Add the loop sink pass when ported.
502
503 // FIXME: Add cleanup from the loop pass manager when we're forming LCSSA
504 // here.
505
506 // Now that we've vectorized and unrolled loops, we may have more refined
507 // alignment information, try to re-derive it here.
508 OptimizePM.addPass(AlignmentFromAssumptionsPass());
509
510 // ADd the core optimizing pipeline.
511 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
512
513 // Now we need to do some global optimization transforms.
514 // FIXME: It would seem like these should come first in the optimization
515 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
516 // ordering here.
517 MPM.addPass(GlobalDCEPass());
518 MPM.addPass(ConstantMergePass());
519
520 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000521}
522
Chandler Carruthe3f50642016-12-22 06:59:15 +0000523ModulePassManager
524PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
525 bool DebugLogging) {
526 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000527 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000528 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000529}
530
Chandler Carruthe3f50642016-12-22 06:59:15 +0000531ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
532 bool DebugLogging) {
533 assert(Level != O0 && "Must request optimizations for the default pipeline!");
534 ModulePassManager MPM(DebugLogging);
535
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000536 // FIXME: Finish fleshing this out to match the legacy LTO pipelines.
537 FunctionPassManager LateFPM(DebugLogging);
538 LateFPM.addPass(InstCombinePass());
539 LateFPM.addPass(SimplifyCFGPass());
540
541 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(LateFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000542
543 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000544}
545
Chandler Carruth241bf242016-08-03 07:44:48 +0000546static Optional<int> parseRepeatPassName(StringRef Name) {
547 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
548 return None;
549 int Count;
550 if (Name.getAsInteger(0, Count) || Count <= 0)
551 return None;
552 return Count;
553}
554
Chandler Carruth66445382014-01-11 08:16:35 +0000555static bool isModulePassName(StringRef Name) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000556 // Manually handle aliases for pre-configured pipeline fragments.
557 if (Name.startswith("default") || Name.startswith("lto"))
558 return DefaultAliasRegex.match(Name);
559
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000560 // Explicitly handle pass manager names.
561 if (Name == "module")
562 return true;
563 if (Name == "cgscc")
564 return true;
565 if (Name == "function")
566 return true;
567
Chandler Carruth241bf242016-08-03 07:44:48 +0000568 // Explicitly handle custom-parsed pass names.
569 if (parseRepeatPassName(Name))
570 return true;
571
Chandler Carruth74a8a222016-06-17 07:15:29 +0000572#define MODULE_PASS(NAME, CREATE_PASS) \
573 if (Name == NAME) \
574 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000575#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000576 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000577 return true;
578#include "PassRegistry.def"
579
Chandler Carruth66445382014-01-11 08:16:35 +0000580 return false;
581}
582
Chandler Carruth572e3402014-04-21 11:12:00 +0000583static bool isCGSCCPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000584 // Explicitly handle pass manager names.
585 if (Name == "cgscc")
586 return true;
587 if (Name == "function")
588 return true;
589
Chandler Carruth241bf242016-08-03 07:44:48 +0000590 // Explicitly handle custom-parsed pass names.
591 if (parseRepeatPassName(Name))
592 return true;
593
Chandler Carruth74a8a222016-06-17 07:15:29 +0000594#define CGSCC_PASS(NAME, CREATE_PASS) \
595 if (Name == NAME) \
596 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000597#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000598 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000599 return true;
600#include "PassRegistry.def"
601
Chandler Carruth572e3402014-04-21 11:12:00 +0000602 return false;
603}
604
Chandler Carruthd8330982014-01-12 09:34:22 +0000605static bool isFunctionPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000606 // Explicitly handle pass manager names.
607 if (Name == "function")
608 return true;
609 if (Name == "loop")
610 return true;
611
Chandler Carruth241bf242016-08-03 07:44:48 +0000612 // Explicitly handle custom-parsed pass names.
613 if (parseRepeatPassName(Name))
614 return true;
615
Chandler Carruth74a8a222016-06-17 07:15:29 +0000616#define FUNCTION_PASS(NAME, CREATE_PASS) \
617 if (Name == NAME) \
618 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +0000619#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000620 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +0000621 return true;
622#include "PassRegistry.def"
623
Chandler Carruthd8330982014-01-12 09:34:22 +0000624 return false;
625}
626
Justin Bognereecc3c82016-02-25 07:23:08 +0000627static bool isLoopPassName(StringRef Name) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000628 // Explicitly handle pass manager names.
629 if (Name == "loop")
630 return true;
631
Chandler Carruth241bf242016-08-03 07:44:48 +0000632 // Explicitly handle custom-parsed pass names.
633 if (parseRepeatPassName(Name))
634 return true;
635
Chandler Carruth74a8a222016-06-17 07:15:29 +0000636#define LOOP_PASS(NAME, CREATE_PASS) \
637 if (Name == NAME) \
638 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +0000639#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
640 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
641 return true;
642#include "PassRegistry.def"
643
644 return false;
645}
646
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000647Optional<std::vector<PassBuilder::PipelineElement>>
648PassBuilder::parsePipelineText(StringRef Text) {
649 std::vector<PipelineElement> ResultPipeline;
650
651 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
652 &ResultPipeline};
653 for (;;) {
654 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
655 size_t Pos = Text.find_first_of(",()");
656 Pipeline.push_back({Text.substr(0, Pos), {}});
657
658 // If we have a single terminating name, we're done.
659 if (Pos == Text.npos)
660 break;
661
662 char Sep = Text[Pos];
663 Text = Text.substr(Pos + 1);
664 if (Sep == ',')
665 // Just a name ending in a comma, continue.
666 continue;
667
668 if (Sep == '(') {
669 // Push the inner pipeline onto the stack to continue processing.
670 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
671 continue;
672 }
673
674 assert(Sep == ')' && "Bogus separator!");
675 // When handling the close parenthesis, we greedily consume them to avoid
676 // empty strings in the pipeline.
677 do {
678 // If we try to pop the outer pipeline we have unbalanced parentheses.
679 if (PipelineStack.size() == 1)
680 return None;
681
682 PipelineStack.pop_back();
683 } while (Text.consume_front(")"));
684
685 // Check if we've finished parsing.
686 if (Text.empty())
687 break;
688
689 // Otherwise, the end of an inner pipeline always has to be followed by
690 // a comma, and then we can continue.
691 if (!Text.consume_front(","))
692 return None;
693 }
694
695 if (PipelineStack.size() > 1)
696 // Unbalanced paretheses.
697 return None;
698
699 assert(PipelineStack.back() == &ResultPipeline &&
700 "Wrong pipeline at the bottom of the stack!");
701 return {std::move(ResultPipeline)};
702}
703
704bool PassBuilder::parseModulePass(ModulePassManager &MPM,
705 const PipelineElement &E, bool VerifyEachPass,
706 bool DebugLogging) {
707 auto &Name = E.Name;
708 auto &InnerPipeline = E.InnerPipeline;
709
710 // First handle complex passes like the pass managers which carry pipelines.
711 if (!InnerPipeline.empty()) {
712 if (Name == "module") {
713 ModulePassManager NestedMPM(DebugLogging);
714 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
715 DebugLogging))
716 return false;
717 MPM.addPass(std::move(NestedMPM));
718 return true;
719 }
720 if (Name == "cgscc") {
721 CGSCCPassManager CGPM(DebugLogging);
722 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
723 DebugLogging))
724 return false;
725 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM),
726 DebugLogging));
727 return true;
728 }
729 if (Name == "function") {
730 FunctionPassManager FPM(DebugLogging);
731 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
732 DebugLogging))
733 return false;
734 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
735 return true;
736 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000737 if (auto Count = parseRepeatPassName(Name)) {
738 ModulePassManager NestedMPM(DebugLogging);
739 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
740 DebugLogging))
741 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000742 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000743 return true;
744 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000745 // Normal passes can't have pipelines.
746 return false;
747 }
748
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000749 // Manually handle aliases for pre-configured pipeline fragments.
750 if (Name.startswith("default") || Name.startswith("lto")) {
751 SmallVector<StringRef, 3> Matches;
752 if (!DefaultAliasRegex.match(Name, &Matches))
753 return false;
754 assert(Matches.size() == 3 && "Must capture two matched strings!");
755
Jordan Rosef85a95f2016-07-25 18:34:51 +0000756 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
757 .Case("O0", O0)
758 .Case("O1", O1)
759 .Case("O2", O2)
760 .Case("O3", O3)
761 .Case("Os", Os)
762 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000763 if (L == O0)
764 // At O0 we do nothing at all!
765 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000766
767 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000768 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000769 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000770 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000771 } else {
772 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +0000773 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000774 }
775 return true;
776 }
777
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000778 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +0000779#define MODULE_PASS(NAME, CREATE_PASS) \
780 if (Name == NAME) { \
781 MPM.addPass(CREATE_PASS); \
782 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000783 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000784#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
785 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +0000786 MPM.addPass( \
787 RequireAnalysisPass< \
788 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000789 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000790 } \
791 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000792 MPM.addPass(InvalidateAnalysisPass< \
793 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000794 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000795 }
796#include "PassRegistry.def"
797
Chandler Carruth66445382014-01-11 08:16:35 +0000798 return false;
799}
800
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000801bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
802 const PipelineElement &E, bool VerifyEachPass,
803 bool DebugLogging) {
804 auto &Name = E.Name;
805 auto &InnerPipeline = E.InnerPipeline;
806
807 // First handle complex passes like the pass managers which carry pipelines.
808 if (!InnerPipeline.empty()) {
809 if (Name == "cgscc") {
810 CGSCCPassManager NestedCGPM(DebugLogging);
811 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
812 DebugLogging))
813 return false;
814 // Add the nested pass manager with the appropriate adaptor.
815 CGPM.addPass(std::move(NestedCGPM));
816 return true;
817 }
818 if (Name == "function") {
819 FunctionPassManager FPM(DebugLogging);
820 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
821 DebugLogging))
822 return false;
823 // Add the nested pass manager with the appropriate adaptor.
824 CGPM.addPass(
825 createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging));
826 return true;
827 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000828 if (auto Count = parseRepeatPassName(Name)) {
829 CGSCCPassManager NestedCGPM(DebugLogging);
830 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
831 DebugLogging))
832 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000833 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000834 return true;
835 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000836 // Normal passes can't have pipelines.
837 return false;
838 }
839
840 // Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +0000841#define CGSCC_PASS(NAME, CREATE_PASS) \
842 if (Name == NAME) { \
843 CGPM.addPass(CREATE_PASS); \
844 return true; \
845 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000846#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
847 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000848 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +0000849 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +0000850 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
851 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000852 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000853 } \
854 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000855 CGPM.addPass(InvalidateAnalysisPass< \
856 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000857 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000858 }
859#include "PassRegistry.def"
860
Chandler Carruth572e3402014-04-21 11:12:00 +0000861 return false;
862}
863
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000864bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
865 const PipelineElement &E,
866 bool VerifyEachPass, bool DebugLogging) {
867 auto &Name = E.Name;
868 auto &InnerPipeline = E.InnerPipeline;
869
870 // First handle complex passes like the pass managers which carry pipelines.
871 if (!InnerPipeline.empty()) {
872 if (Name == "function") {
873 FunctionPassManager NestedFPM(DebugLogging);
874 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
875 DebugLogging))
876 return false;
877 // Add the nested pass manager with the appropriate adaptor.
878 FPM.addPass(std::move(NestedFPM));
879 return true;
880 }
881 if (Name == "loop") {
882 LoopPassManager LPM(DebugLogging);
883 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
884 DebugLogging))
885 return false;
886 // Add the nested pass manager with the appropriate adaptor.
887 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
888 return true;
889 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000890 if (auto Count = parseRepeatPassName(Name)) {
891 FunctionPassManager NestedFPM(DebugLogging);
892 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
893 DebugLogging))
894 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000895 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000896 return true;
897 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000898 // Normal passes can't have pipelines.
899 return false;
900 }
901
902 // Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +0000903#define FUNCTION_PASS(NAME, CREATE_PASS) \
904 if (Name == NAME) { \
905 FPM.addPass(CREATE_PASS); \
906 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +0000907 }
Chandler Carruth628503e2015-01-06 02:10:51 +0000908#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
909 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +0000910 FPM.addPass( \
911 RequireAnalysisPass< \
912 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +0000913 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000914 } \
915 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +0000916 FPM.addPass(InvalidateAnalysisPass< \
917 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +0000918 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +0000919 }
920#include "PassRegistry.def"
921
Chandler Carruthd8330982014-01-12 09:34:22 +0000922 return false;
923}
924
Chandler Carruthfdc6ba12016-08-03 09:14:03 +0000925bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000926 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +0000927 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000928 auto &InnerPipeline = E.InnerPipeline;
929
930 // First handle complex passes like the pass managers which carry pipelines.
931 if (!InnerPipeline.empty()) {
932 if (Name == "loop") {
933 LoopPassManager NestedLPM(DebugLogging);
934 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
935 DebugLogging))
936 return false;
937 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +0000938 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000939 return true;
940 }
Chandler Carruth241bf242016-08-03 07:44:48 +0000941 if (auto Count = parseRepeatPassName(Name)) {
942 LoopPassManager NestedLPM(DebugLogging);
943 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
944 DebugLogging))
945 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +0000946 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +0000947 return true;
948 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000949 // Normal passes can't have pipelines.
950 return false;
951 }
952
953 // Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +0000954#define LOOP_PASS(NAME, CREATE_PASS) \
955 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +0000956 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000957 return true; \
958 }
959#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
960 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +0000961 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +0000962 std::remove_reference<decltype(CREATE_PASS)>::type, Loop>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000963 return true; \
964 } \
965 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +0000966 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +0000967 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +0000968 return true; \
969 }
970#include "PassRegistry.def"
971
972 return false;
973}
974
Chandler Carruthedf59962016-02-18 09:45:17 +0000975bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +0000976#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
977 if (Name == NAME) { \
978 AA.registerModuleAnalysis< \
979 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
980 return true; \
981 }
Chandler Carruthedf59962016-02-18 09:45:17 +0000982#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
983 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000984 AA.registerFunctionAnalysis< \
985 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +0000986 return true; \
987 }
988#include "PassRegistry.def"
989
990 return false;
991}
992
Justin Bognereecc3c82016-02-25 07:23:08 +0000993bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000994 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +0000995 bool VerifyEachPass,
996 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +0000997 for (const auto &Element : Pipeline) {
998 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
999 return false;
1000 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001001 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001002 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001003}
1004
Chandler Carruth1ff77242015-03-07 09:02:36 +00001005bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001006 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001007 bool VerifyEachPass,
1008 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001009 for (const auto &Element : Pipeline) {
1010 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1011 return false;
1012 if (VerifyEachPass)
1013 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001014 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001015 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001016}
1017
Chandler Carruth1ff77242015-03-07 09:02:36 +00001018bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001019 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001020 bool VerifyEachPass,
1021 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001022 for (const auto &Element : Pipeline) {
1023 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1024 return false;
1025 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001026 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001027 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001028}
1029
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001030void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1031 FunctionAnalysisManager &FAM,
1032 CGSCCAnalysisManager &CGAM,
1033 ModuleAnalysisManager &MAM) {
1034 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1035 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001036 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1037 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1038 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1039 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1040 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1041}
1042
Chandler Carruth1ff77242015-03-07 09:02:36 +00001043bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001044 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001045 bool VerifyEachPass,
1046 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001047 for (const auto &Element : Pipeline) {
1048 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1049 return false;
1050 if (VerifyEachPass)
1051 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001052 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001053 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001054}
1055
1056// Primary pass pipeline description parsing routine.
1057// FIXME: Should this routine accept a TargetMachine or require the caller to
1058// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001059bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1060 StringRef PipelineText, bool VerifyEachPass,
1061 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001062 auto Pipeline = parsePipelineText(PipelineText);
1063 if (!Pipeline || Pipeline->empty())
1064 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001065
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001066 // If the first name isn't at the module layer, wrap the pipeline up
1067 // automatically.
1068 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001069
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001070 if (!isModulePassName(FirstName)) {
1071 if (isCGSCCPassName(FirstName))
1072 Pipeline = {{"cgscc", std::move(*Pipeline)}};
1073 else if (isFunctionPassName(FirstName))
1074 Pipeline = {{"function", std::move(*Pipeline)}};
1075 else if (isLoopPassName(FirstName))
1076 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
1077 else
1078 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001079 return false;
Chandler Carruth572e3402014-04-21 11:12:00 +00001080 }
1081
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001082 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001083}
Chandler Carruthedf59962016-02-18 09:45:17 +00001084
1085bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
1086 while (!PipelineText.empty()) {
1087 StringRef Name;
1088 std::tie(Name, PipelineText) = PipelineText.split(',');
1089 if (!parseAAPassName(AA, Name))
1090 return false;
1091 }
1092
1093 return true;
1094}