blob: 21d95a07125c37b94d16cdf15fefb902ede49866 [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"
Daniel Berlin554dcd82017-04-11 20:06:36 +000042#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000043#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000044#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000063#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000066#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000067#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000068#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000069#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000070#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000071#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000072#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000073#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000074#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000075#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000076#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000077#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000078#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000079#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000080#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000081#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000082#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000083#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000084#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000085#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000086#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000087#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000088#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000089#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000090#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000091#include "llvm/Transforms/Scalar/BDCE.h"
Adam Nemet3beef412016-07-18 16:29:17 +000092#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000093#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000094#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000095#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000096#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000097#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000098#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000099#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000100#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000101#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000102#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000103#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000104#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000105#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000106#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000107#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000108#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000109#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000110#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000111#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000112#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000113#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000114#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000115#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000116#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000117#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000118#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000119#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000120#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000121#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000122#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000123#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000124#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000125#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000126#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000127#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000128#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000129#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000130#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000131#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000132#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000133#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000134#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000135#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000136#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000137#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000138#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000139#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000140#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000141#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000142#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000143#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000144#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000145#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000146#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000147#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000148
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000149#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000150
151using namespace llvm;
152
Chandler Carruth719ffe12017-02-12 05:38:04 +0000153static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
154 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000155static cl::opt<bool>
156 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
157 cl::Hidden, cl::ZeroOrMore,
158 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000159
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000160static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000161 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000162 cl::Hidden, cl::ZeroOrMore,
163 cl::desc("Run NewGVN instead of GVN"));
164
Geoff Berry3cca1da2017-06-10 15:20:03 +0000165static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000166 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
167 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000168
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000169static cl::opt<bool> EnableGVNHoist(
170 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
171 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
172
Davide Italianobe1b6a92017-06-03 23:18:29 +0000173static cl::opt<bool> EnableGVNSink(
174 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
175 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
176
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000177static Regex DefaultAliasRegex(
178 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000179
Chandler Carruthe3f50642016-12-22 06:59:15 +0000180static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
181 switch (Level) {
182 case PassBuilder::O0:
183 case PassBuilder::O1:
184 case PassBuilder::O2:
185 case PassBuilder::O3:
186 return false;
187
188 case PassBuilder::Os:
189 case PassBuilder::Oz:
190 return true;
191 }
192 llvm_unreachable("Invalid optimization level!");
193}
194
Chandler Carruth66445382014-01-11 08:16:35 +0000195namespace {
196
Chandler Carruthd8330982014-01-12 09:34:22 +0000197/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000198struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000199 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000200 return PreservedAnalyses::all();
201 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000202 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000203};
204
Chandler Carruth0b576b32015-01-06 02:50:06 +0000205/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000206class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
207 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000208 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000209
210public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000211 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000212 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000213 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000214};
215
Chandler Carruth572e3402014-04-21 11:12:00 +0000216/// \brief No-op CGSCC pass which does nothing.
217struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000218 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
219 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000220 return PreservedAnalyses::all();
221 }
222 static StringRef name() { return "NoOpCGSCCPass"; }
223};
224
Chandler Carruth0b576b32015-01-06 02:50:06 +0000225/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000226class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
227 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000228 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000229
230public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000231 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000232 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000233 return Result();
234 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000236};
237
Chandler Carruthd8330982014-01-12 09:34:22 +0000238/// \brief No-op function pass which does nothing.
239struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000240 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000241 return PreservedAnalyses::all();
242 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000243 static StringRef name() { return "NoOpFunctionPass"; }
244};
245
Chandler Carruth0b576b32015-01-06 02:50:06 +0000246/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000247class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
248 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000249 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000250
251public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000252 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000253 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000254 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000255};
256
Justin Bognereecc3c82016-02-25 07:23:08 +0000257/// \brief No-op loop pass which does nothing.
258struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000259 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
260 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000261 return PreservedAnalyses::all();
262 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000263 static StringRef name() { return "NoOpLoopPass"; }
264};
265
266/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000267class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
268 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000269 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000270
271public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000272 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000273 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
274 return Result();
275 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000276 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000277};
278
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000279AnalysisKey NoOpModuleAnalysis::Key;
280AnalysisKey NoOpCGSCCAnalysis::Key;
281AnalysisKey NoOpFunctionAnalysis::Key;
282AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000283
Chandler Carruth66445382014-01-11 08:16:35 +0000284} // End anonymous namespace.
285
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000286void PassBuilder::invokePeepholeEPCallbacks(
287 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
288 for (auto &C : PeepholeEPCallbacks)
289 C(FPM, Level);
290}
291
Chandler Carruth1ff77242015-03-07 09:02:36 +0000292void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000293#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000294 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000295#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000296
297 for (auto &C : ModuleAnalysisRegistrationCallbacks)
298 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000299}
300
Chandler Carruth1ff77242015-03-07 09:02:36 +0000301void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000302#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000303 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000304#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000305
306 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
307 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000308}
309
Chandler Carruth1ff77242015-03-07 09:02:36 +0000310void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000311#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000312 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000313#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000314
315 for (auto &C : FunctionAnalysisRegistrationCallbacks)
316 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000317}
318
Justin Bognereecc3c82016-02-25 07:23:08 +0000319void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000320#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000321 LAM.registerPass([&] { return CREATE_PASS; });
322#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000323
324 for (auto &C : LoopAnalysisRegistrationCallbacks)
325 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000326}
327
Chandler Carruthe3f50642016-12-22 06:59:15 +0000328FunctionPassManager
329PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000330 ThinLTOPhase Phase,
331 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000332 assert(Level != O0 && "Must request optimizations!");
333 FunctionPassManager FPM(DebugLogging);
334
335 // Form SSA out of local memory accesses after breaking apart aggregates into
336 // scalars.
337 FPM.addPass(SROA());
338
339 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000340 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000341
Davide Italianoc3688312017-06-01 23:08:14 +0000342 // Hoisting of scalars and load expressions.
343 if (EnableGVNHoist)
344 FPM.addPass(GVNHoistPass());
345
Davide Italianobe1b6a92017-06-03 23:18:29 +0000346 // Global value numbering based sinking.
347 if (EnableGVNSink) {
348 FPM.addPass(GVNSinkPass());
349 FPM.addPass(SimplifyCFGPass());
350 }
351
Chandler Carruthe3f50642016-12-22 06:59:15 +0000352 // Speculative execution if the target has divergent branches; otherwise nop.
353 FPM.addPass(SpeculativeExecutionPass());
354
355 // Optimize based on known information about branches, and cleanup afterward.
356 FPM.addPass(JumpThreadingPass());
357 FPM.addPass(CorrelatedValuePropagationPass());
358 FPM.addPass(SimplifyCFGPass());
359 FPM.addPass(InstCombinePass());
360
361 if (!isOptimizingForSize(Level))
362 FPM.addPass(LibCallsShrinkWrapPass());
363
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000364 invokePeepholeEPCallbacks(FPM, Level);
365
Rong Xue1f42452017-10-23 22:21:29 +0000366 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
367 // using the size value profile. Don't perform this when optimizing for size.
368 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
369 !isOptimizingForSize(Level))
370 FPM.addPass(PGOMemOPSizeOpt());
371
Chandler Carruthe3f50642016-12-22 06:59:15 +0000372 FPM.addPass(TailCallElimPass());
373 FPM.addPass(SimplifyCFGPass());
374
375 // Form canonically associated expression trees, and simplify the trees using
376 // basic mathematical properties. For example, this will form (nearly)
377 // minimal multiplication trees.
378 FPM.addPass(ReassociatePass());
379
380 // Add the primary loop simplification pipeline.
381 // FIXME: Currently this is split into two loop pass pipelines because we run
382 // some function passes in between them. These can and should be replaced by
383 // loop pass equivalenst but those aren't ready yet. Specifically,
384 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
385 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
386 // the other we have is `LoopInstSimplify`.
387 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
388
389 // Rotate Loop - disable header duplication at -Oz
390 LPM1.addPass(LoopRotatePass(Level != Oz));
391 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000392 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000393 LPM2.addPass(IndVarSimplifyPass());
394 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000395
396 for (auto &C : LateLoopOptimizationsEPCallbacks)
397 C(LPM2, Level);
398
Chandler Carruth79b733b2017-01-26 23:21:17 +0000399 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000400 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000401 // because it changes IR to makes profile annotation in back compile
402 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000403 if (Phase != ThinLTOPhase::PreLink ||
404 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000405 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000406
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000407 for (auto &C : LoopOptimizerEndEPCallbacks)
408 C(LPM2, Level);
409
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000410 // We provide the opt remark emitter pass for LICM to use. We only need to do
411 // this once as it is immutable.
412 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000413 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
414 FPM.addPass(SimplifyCFGPass());
415 FPM.addPass(InstCombinePass());
416 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
417
418 // Eliminate redundancies.
419 if (Level != O1) {
420 // These passes add substantial compile time so skip them at O1.
421 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000422 if (RunNewGVN)
423 FPM.addPass(NewGVNPass());
424 else
425 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000426 }
427
428 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
429 FPM.addPass(MemCpyOptPass());
430
431 // Sparse conditional constant propagation.
432 // FIXME: It isn't clear why we do this *after* loop passes rather than
433 // before...
434 FPM.addPass(SCCPPass());
435
436 // Delete dead bit computations (instcombine runs after to fold away the dead
437 // computations, and then ADCE will run later to exploit any new DCE
438 // opportunities that creates).
439 FPM.addPass(BDCEPass());
440
441 // Run instcombine after redundancy and dead bit elimination to exploit
442 // opportunities opened up by them.
443 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000444 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000445
446 // Re-consider control flow based optimizations after redundancy elimination,
447 // redo DCE, etc.
448 FPM.addPass(JumpThreadingPass());
449 FPM.addPass(CorrelatedValuePropagationPass());
450 FPM.addPass(DSEPass());
451 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
452
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000453 for (auto &C : ScalarOptimizerLateEPCallbacks)
454 C(FPM, Level);
455
Chandler Carruthe3f50642016-12-22 06:59:15 +0000456 // Finally, do an expensive DCE pass to catch all the dead code exposed by
457 // the simplifications and basic cleanup after all the simplifications.
458 FPM.addPass(ADCEPass());
459 FPM.addPass(SimplifyCFGPass());
460 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000461 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000462
463 return FPM;
464}
465
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000466void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
467 PassBuilder::OptimizationLevel Level,
468 bool RunProfileGen,
469 std::string ProfileGenFile,
470 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000471 // Generally running simplification passes and the inliner with an high
472 // threshold results in smaller executables, but there may be cases where
473 // the size grows, so let's be conservative here and skip this simplification
474 // at -Os/Oz.
475 if (!isOptimizingForSize(Level)) {
476 InlineParams IP;
477
478 // In the old pass manager, this is a cl::opt. Should still this be one?
479 IP.DefaultThreshold = 75;
480
481 // FIXME: The hint threshold has the same value used by the regular inliner.
482 // This should probably be lowered after performance testing.
483 // FIXME: this comment is cargo culted from the old pass manager, revisit).
484 IP.HintThreshold = 325;
485
486 CGSCCPassManager CGPipeline(DebugLogging);
487
488 CGPipeline.addPass(InlinerPass(IP));
489
490 FunctionPassManager FPM;
491 FPM.addPass(SROA());
492 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
493 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
494 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000495 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000496
Davide Italiano513dfaa2017-02-13 15:26:22 +0000497 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
498
499 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
500 }
501
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000502 // Delete anything that is now dead to make sure that we don't instrument
503 // dead code. Instrumentation can end up keeping dead code around and
504 // dramatically increase code size.
505 MPM.addPass(GlobalDCEPass());
506
Davide Italiano513dfaa2017-02-13 15:26:22 +0000507 if (RunProfileGen) {
508 MPM.addPass(PGOInstrumentationGen());
509
Xinliang David Lib67530e2017-06-25 00:26:43 +0000510 FunctionPassManager FPM;
511 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
512 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
513
Davide Italiano513dfaa2017-02-13 15:26:22 +0000514 // Add the profile lowering pass.
515 InstrProfOptions Options;
516 if (!ProfileGenFile.empty())
517 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000518 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000519 MPM.addPass(InstrProfiling(Options));
520 }
521
522 if (!ProfileUseFile.empty())
523 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
524}
525
Easwaran Raman8249fac2017-06-28 13:33:49 +0000526static InlineParams
527getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
528 auto O3 = PassBuilder::O3;
529 unsigned OptLevel = Level > O3 ? 2 : Level;
530 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
531 return getInlineParams(OptLevel, SizeLevel);
532}
533
Chandler Carruthe3f50642016-12-22 06:59:15 +0000534ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000535PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000536 ThinLTOPhase Phase,
537 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000538 ModulePassManager MPM(DebugLogging);
539
Chandler Carruthe3f50642016-12-22 06:59:15 +0000540 // Do basic inference of function attributes from known properties of system
541 // libraries and other oracles.
542 MPM.addPass(InferFunctionAttrsPass());
543
544 // Create an early function pass manager to cleanup the output of the
545 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000546 FunctionPassManager EarlyFPM(DebugLogging);
547 EarlyFPM.addPass(SimplifyCFGPass());
548 EarlyFPM.addPass(SROA());
549 EarlyFPM.addPass(EarlyCSEPass());
550 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Dehao Chen08f88312017-08-07 20:23:20 +0000551 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
552 // to convert bitcast to direct calls so that they can be inlined during the
553 // profile annotation prepration step.
554 // More details about SamplePGO design can be found in:
555 // https://research.google.com/pubs/pub45290.html
556 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
557 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
558 Phase == ThinLTOPhase::PostLink)
559 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000560 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000561
Dehao Chen08f88312017-08-07 20:23:20 +0000562 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
563 // Annotate sample profile right after early FPM to ensure freshness of
564 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000565 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
566 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000567 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
568 // for the profile annotation to be accurate in the ThinLTO backend.
569 if (Phase != ThinLTOPhase::PreLink)
570 // We perform early indirect call promotion here, before globalopt.
571 // This is important for the ThinLTO backend phase because otherwise
572 // imported available_externally functions look unreferenced and are
573 // removed.
574 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
575 true));
576 }
577
Chandler Carruthe3f50642016-12-22 06:59:15 +0000578 // Interprocedural constant propagation now that basic cleanup has occured
579 // and prior to optimizing globals.
580 // FIXME: This position in the pipeline hasn't been carefully considered in
581 // years, it should be re-analyzed.
582 MPM.addPass(IPSCCPPass());
583
Matthew Simpsoncb585582017-10-25 13:40:08 +0000584 // Attach metadata to indirect call sites indicating the set of functions
585 // they may target at run-time. This should follow IPSCCP.
586 MPM.addPass(CalledValuePropagationPass());
587
Chandler Carruthe3f50642016-12-22 06:59:15 +0000588 // Optimize globals to try and fold them into constants.
589 MPM.addPass(GlobalOptPass());
590
591 // Promote any localized globals to SSA registers.
592 // FIXME: Should this instead by a run of SROA?
593 // FIXME: We should probably run instcombine and simplify-cfg afterward to
594 // delete control flows that are dead once globals have been folded to
595 // constants.
596 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
597
598 // Remove any dead arguments exposed by cleanups and constand folding
599 // globals.
600 MPM.addPass(DeadArgumentEliminationPass());
601
602 // Create a small function pass pipeline to cleanup after all the global
603 // optimizations.
604 FunctionPassManager GlobalCleanupPM(DebugLogging);
605 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000606 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
607
Chandler Carruthe3f50642016-12-22 06:59:15 +0000608 GlobalCleanupPM.addPass(SimplifyCFGPass());
609 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
610
Dehao Chen89d32262017-08-02 01:28:31 +0000611 // Add all the requested passes for instrumentation PGO, if requested.
612 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
613 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
614 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
615 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
616 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000617 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000618
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000619 // Require the GlobalsAA analysis for the module so we can query it within
620 // the CGSCC pipeline.
621 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000622
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000623 // Require the ProfileSummaryAnalysis for the module so we can query it within
624 // the inliner pass.
625 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
626
Chandler Carruthe3f50642016-12-22 06:59:15 +0000627 // Now begin the main postorder CGSCC pipeline.
628 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
629 // manager and trying to emulate its precise behavior. Much of this doesn't
630 // make a lot of sense and we should revisit the core CGSCC structure.
631 CGSCCPassManager MainCGPipeline(DebugLogging);
632
633 // Note: historically, the PruneEH pass was run first to deduce nounwind and
634 // generally clean up exception handling overhead. It isn't clear this is
635 // valuable as the inliner doesn't currently care whether it is inlining an
636 // invoke or a call.
637
638 // Run the inliner first. The theory is that we are walking bottom-up and so
639 // the callees have already been fully optimized, and we want to inline them
640 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000641 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000642 // because it makes profile annotation in the backend inaccurate.
643 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000644 if (Phase == ThinLTOPhase::PreLink &&
645 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000646 IP.HotCallSiteThreshold = 0;
647 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000648
649 // Now deduce any function attributes based in the current code.
650 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
651
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000652 // When at O3 add argument promotion to the pass pipeline.
653 // FIXME: It isn't at all clear why this should be limited to O3.
654 if (Level == O3)
655 MainCGPipeline.addPass(ArgumentPromotionPass());
656
Chandler Carruthe3f50642016-12-22 06:59:15 +0000657 // Lastly, add the core function simplification pipeline nested inside the
658 // CGSCC walk.
659 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000660 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000661
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000662 for (auto &C : CGSCCOptimizerLateEPCallbacks)
663 C(MainCGPipeline, Level);
664
Chandler Carruth719ffe12017-02-12 05:38:04 +0000665 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
666 // to detect when we devirtualize indirect calls and iterate the SCC passes
667 // in that case to try and catch knock-on inlining or function attrs
668 // opportunities. Then we add it to the module pipeline by walking the SCCs
669 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000670 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000671 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000672 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000673
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000674 return MPM;
675}
676
677ModulePassManager
678PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
679 bool DebugLogging) {
680 ModulePassManager MPM(DebugLogging);
681
682 // Optimize globals now that the module is fully simplified.
683 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000684 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000685
Xinliang David Li126157c2017-05-22 16:41:57 +0000686 // Run partial inlining pass to partially inline functions that have
687 // large bodies.
688 if (RunPartialInlining)
689 MPM.addPass(PartialInlinerPass());
690
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000691 // Remove avail extern fns and globals definitions since we aren't compiling
692 // an object file for later LTO. For LTO we want to preserve these so they
693 // are eligible for inlining at link-time. Note if they are unreferenced they
694 // will be removed by GlobalDCE later, so this only impacts referenced
695 // available externally globals. Eventually they will be suppressed during
696 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
697 // may make globals referenced by available external functions dead and saves
698 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000699 MPM.addPass(EliminateAvailableExternallyPass());
700
701 // Do RPO function attribute inference across the module to forward-propagate
702 // attributes where applicable.
703 // FIXME: Is this really an optimization rather than a canonicalization?
704 MPM.addPass(ReversePostOrderFunctionAttrsPass());
705
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000706 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000707 // useful as the above will have inlined, DCE'ed, and function-attr
708 // propagated everything. We should at this point have a reasonably minimal
709 // and richly annotated call graph. By computing aliasing and mod/ref
710 // information for all local globals here, the late loop passes and notably
711 // the vectorizer will be able to use them to help recognize vectorizable
712 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000713 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000714
715 FunctionPassManager OptimizePM(DebugLogging);
716 OptimizePM.addPass(Float2IntPass());
717 // FIXME: We need to run some loop optimizations to re-rotate loops after
718 // simplify-cfg and others undo their rotation.
719
720 // Optimize the loop execution. These passes operate on entire loop nests
721 // rather than on each loop in an inside-out manner, and so they are actually
722 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000723
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000724 for (auto &C : VectorizerStartEPCallbacks)
725 C(OptimizePM, Level);
726
Chandler Carrutha95ff382017-01-27 00:50:21 +0000727 // First rotate loops that may have been un-rotated by prior passes.
728 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
729
730 // Distribute loops to allow partial vectorization. I.e. isolate dependences
731 // into separate loop that would otherwise inhibit vectorization. This is
732 // currently only performed for loops marked with the metadata
733 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000734 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000735
736 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000737 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000738
Chandler Carruthbaabda92017-01-27 01:32:26 +0000739 // Eliminate loads by forwarding stores from the previous iteration to loads
740 // of the current iteration.
741 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000742
743 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000744 OptimizePM.addPass(InstCombinePass());
745
Chandler Carrutha95ff382017-01-27 00:50:21 +0000746
747 // Now that we've formed fast to execute loop structures, we do further
748 // optimizations. These are run afterward as they might block doing complex
749 // analyses and transforms such as what are needed for loop vectorization.
750
Chandler Carruthe3f50642016-12-22 06:59:15 +0000751 // Optimize parallel scalar instruction chains into SIMD instructions.
752 OptimizePM.addPass(SLPVectorizerPass());
753
Sanjay Pateladf38912017-10-29 20:49:31 +0000754 // Cleanup after all of the vectorizers. Simplification passes like CVP and
755 // GVN, loop transforms, and others have already run, so it's now better to
756 // convert to more optimized IR using more aggressive simplify CFG options.
757 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
758 forwardSwitchCondToPhi(true).
759 convertSwitchToLookupTable(true).
760 needCanonicalLoops(false)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000761 OptimizePM.addPass(InstCombinePass());
762
763 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000764 // execution resources of an out-of-order processor. We also then need to
765 // clean up redundancies and loop invariant code.
766 // FIXME: It would be really good to use a loop-integrated instruction
767 // combiner for cleanup here so that the unrolling and LICM can be pipelined
768 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000769 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000770 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000771 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000772 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000773
774 // Now that we've vectorized and unrolled loops, we may have more refined
775 // alignment information, try to re-derive it here.
776 OptimizePM.addPass(AlignmentFromAssumptionsPass());
777
Chandler Carrutha95ff382017-01-27 00:50:21 +0000778 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
779 // canonicalization pass that enables other optimizations. As a result,
780 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
781 // result too early.
782 OptimizePM.addPass(LoopSinkPass());
783
784 // And finally clean up LCSSA form before generating code.
785 OptimizePM.addPass(InstSimplifierPass());
786
Sanjay Patel6fd43912017-09-09 13:38:18 +0000787 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
788 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
789 // flattening of blocks.
790 OptimizePM.addPass(DivRemPairsPass());
791
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000792 // LoopSink (and other loop passes since the last simplifyCFG) might have
793 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
794 OptimizePM.addPass(SimplifyCFGPass());
795
Chandler Carrutha95ff382017-01-27 00:50:21 +0000796 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000797 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
798
799 // Now we need to do some global optimization transforms.
800 // FIXME: It would seem like these should come first in the optimization
801 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
802 // ordering here.
803 MPM.addPass(GlobalDCEPass());
804 MPM.addPass(ConstantMergePass());
805
806 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000807}
808
Chandler Carruthe3f50642016-12-22 06:59:15 +0000809ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000810PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
811 bool DebugLogging) {
812 assert(Level != O0 && "Must request optimizations for the default pipeline!");
813
814 ModulePassManager MPM(DebugLogging);
815
816 // Force any function attributes we want the rest of the pipeline to observe.
817 MPM.addPass(ForceFunctionAttrsPass());
818
Dehao Chen08f88312017-08-07 20:23:20 +0000819 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000820 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
821
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000822 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000823 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
824 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000825
826 // Now add the optimization pipeline.
827 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
828
829 return MPM;
830}
831
832ModulePassManager
833PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
834 bool DebugLogging) {
835 assert(Level != O0 && "Must request optimizations for the default pipeline!");
836
837 ModulePassManager MPM(DebugLogging);
838
839 // Force any function attributes we want the rest of the pipeline to observe.
840 MPM.addPass(ForceFunctionAttrsPass());
841
Dehao Chen08f88312017-08-07 20:23:20 +0000842 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000843 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
844
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000845 // If we are planning to perform ThinLTO later, we don't bloat the code with
846 // unrolling/vectorization/... now. Just simplify the module as much as we
847 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000848 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
849 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000850
851 // Run partial inlining pass to partially inline functions that have
852 // large bodies.
853 // FIXME: It isn't clear whether this is really the right place to run this
854 // in ThinLTO. Because there is another canonicalization and simplification
855 // phase that will run after the thin link, running this here ends up with
856 // less information than will be available later and it may grow functions in
857 // ways that aren't beneficial.
858 if (RunPartialInlining)
859 MPM.addPass(PartialInlinerPass());
860
861 // Reduce the size of the IR as much as possible.
862 MPM.addPass(GlobalOptPass());
863
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000864 return MPM;
865}
866
867ModulePassManager
868PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
869 bool DebugLogging) {
870 // FIXME: The summary index is not hooked in the new pass manager yet.
871 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
872 // here.
873
874 ModulePassManager MPM(DebugLogging);
875
876 // Force any function attributes we want the rest of the pipeline to observe.
877 MPM.addPass(ForceFunctionAttrsPass());
878
879 // During the ThinLTO backend phase we perform early indirect call promotion
880 // here, before globalopt. Otherwise imported available_externally functions
881 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000882 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
883 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000884 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000885 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
886 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000887
888 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000889 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
890 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000891
892 // Now add the optimization pipeline.
893 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
894
895 return MPM;
896}
897
898ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000899PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
900 bool DebugLogging) {
901 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000902 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000903 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000904}
905
Chandler Carruthe3f50642016-12-22 06:59:15 +0000906ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
907 bool DebugLogging) {
908 assert(Level != O0 && "Must request optimizations for the default pipeline!");
909 ModulePassManager MPM(DebugLogging);
910
Davide Italiano089a9122017-01-24 00:57:39 +0000911 // Remove unused virtual tables to improve the quality of code generated by
912 // whole-program devirtualization and bitset lowering.
913 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000914
Davide Italiano089a9122017-01-24 00:57:39 +0000915 // Force any function attributes we want the rest of the pipeline to observe.
916 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000917
Davide Italiano089a9122017-01-24 00:57:39 +0000918 // Do basic inference of function attributes from known properties of system
919 // libraries and other oracles.
920 MPM.addPass(InferFunctionAttrsPass());
921
922 if (Level > 1) {
923 // Indirect call promotion. This should promote all the targets that are
924 // left by the earlier promotion pass that promotes intra-module targets.
925 // This two-step promotion is to save the compile time. For LTO, it should
926 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000927 MPM.addPass(PGOIndirectCallPromotion(
928 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000929
930 // Propagate constants at call sites into the functions they call. This
931 // opens opportunities for globalopt (and inlining) by substituting function
932 // pointers passed as arguments to direct uses of functions.
933 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000934
935 // Attach metadata to indirect call sites indicating the set of functions
936 // they may target at run-time. This should follow IPSCCP.
937 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000938 }
939
940 // Now deduce any function attributes based in the current code.
941 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
942 PostOrderFunctionAttrsPass()));
943
944 // Do RPO function attribute inference across the module to forward-propagate
945 // attributes where applicable.
946 // FIXME: Is this really an optimization rather than a canonicalization?
947 MPM.addPass(ReversePostOrderFunctionAttrsPass());
948
949 // Use inragne annotations on GEP indices to split globals where beneficial.
950 MPM.addPass(GlobalSplitPass());
951
952 // Run whole program optimization of virtual call when the list of callees
953 // is fixed.
954 MPM.addPass(WholeProgramDevirtPass());
955
956 // Stop here at -O1.
957 if (Level == 1)
958 return MPM;
959
960 // Optimize globals to try and fold them into constants.
961 MPM.addPass(GlobalOptPass());
962
963 // Promote any localized globals to SSA registers.
964 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
965
966 // Linking modules together can lead to duplicate global constant, only
967 // keep one copy of each constant.
968 MPM.addPass(ConstantMergePass());
969
970 // Remove unused arguments from functions.
971 MPM.addPass(DeadArgumentEliminationPass());
972
973 // Reduce the code after globalopt and ipsccp. Both can open up significant
974 // simplification opportunities, and both can propagate functions through
975 // function pointers. When this happens, we often have to resolve varargs
976 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000977 FunctionPassManager PeepholeFPM(DebugLogging);
978 PeepholeFPM.addPass(InstCombinePass());
979 invokePeepholeEPCallbacks(PeepholeFPM, Level);
980
981 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000982
983 // Note: historically, the PruneEH pass was run first to deduce nounwind and
984 // generally clean up exception handling overhead. It isn't clear this is
985 // valuable as the inliner doesn't currently care whether it is inlining an
986 // invoke or a call.
987 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000988 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
989 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000990
991 // Optimize globals again after we ran the inliner.
992 MPM.addPass(GlobalOptPass());
993
994 // Garbage collect dead functions.
995 // FIXME: Add ArgumentPromotion pass after once it's ported.
996 MPM.addPass(GlobalDCEPass());
997
998 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +0000999 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001000 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001001 invokePeepholeEPCallbacks(FPM, Level);
1002
Davide Italiano089a9122017-01-24 00:57:39 +00001003 FPM.addPass(JumpThreadingPass());
1004
1005 // Break up allocas
1006 FPM.addPass(SROA());
1007
1008 // Run a few AA driver optimizations here and now to cleanup the code.
1009 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1010
1011 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1012 PostOrderFunctionAttrsPass()));
1013 // FIXME: here we run IP alias analysis in the legacy PM.
1014
1015 FunctionPassManager MainFPM;
1016
1017 // FIXME: once we fix LoopPass Manager, add LICM here.
1018 // FIXME: once we provide support for enabling MLSM, add it here.
1019 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001020 if (RunNewGVN)
1021 MainFPM.addPass(NewGVNPass());
1022 else
1023 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001024
1025 // Remove dead memcpy()'s.
1026 MainFPM.addPass(MemCpyOptPass());
1027
1028 // Nuke dead stores.
1029 MainFPM.addPass(DSEPass());
1030
1031 // FIXME: at this point, we run a bunch of loop passes:
1032 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1033 // loopVectorize. Enable them once the remaining issue with LPM
1034 // are sorted out.
1035
1036 MainFPM.addPass(InstCombinePass());
1037 MainFPM.addPass(SimplifyCFGPass());
1038 MainFPM.addPass(SCCPPass());
1039 MainFPM.addPass(InstCombinePass());
1040 MainFPM.addPass(BDCEPass());
1041
1042 // FIXME: We may want to run SLPVectorizer here.
1043 // After vectorization, assume intrinsics may tell us more
1044 // about pointer alignments.
1045#if 0
1046 MainFPM.add(AlignmentFromAssumptionsPass());
1047#endif
1048
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001049 // FIXME: Conditionally run LoadCombine here, after it's ported
1050 // (in case we still have this pass, given its questionable usefulness).
1051
Davide Italiano089a9122017-01-24 00:57:39 +00001052 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001053 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001054 MainFPM.addPass(JumpThreadingPass());
1055 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1056
1057 // Create a function that performs CFI checks for cross-DSO calls with
1058 // targets in the current module.
1059 MPM.addPass(CrossDSOCFIPass());
1060
1061 // Lower type metadata and the type.test intrinsic. This pass supports
1062 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1063 // to be run at link time if CFI is enabled. This pass does nothing if
1064 // CFI is disabled.
1065 // Enable once we add support for the summary in the new PM.
1066#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001067 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1068 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001069 Summary));
1070#endif
1071
1072 // Add late LTO optimization passes.
1073 // Delete basic blocks, which optimization passes may have killed.
1074 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1075
1076 // Drop bodies of available eternally objects to improve GlobalDCE.
1077 MPM.addPass(EliminateAvailableExternallyPass());
1078
1079 // Now that we have optimized the program, discard unreachable functions.
1080 MPM.addPass(GlobalDCEPass());
1081
1082 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001083 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001084}
1085
Chandler Carruth060ad612016-12-23 20:38:19 +00001086AAManager PassBuilder::buildDefaultAAPipeline() {
1087 AAManager AA;
1088
1089 // The order in which these are registered determines their priority when
1090 // being queried.
1091
1092 // First we register the basic alias analysis that provides the majority of
1093 // per-function local AA logic. This is a stateless, on-demand local set of
1094 // AA techniques.
1095 AA.registerFunctionAnalysis<BasicAA>();
1096
1097 // Next we query fast, specialized alias analyses that wrap IR-embedded
1098 // information about aliasing.
1099 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1100 AA.registerFunctionAnalysis<TypeBasedAA>();
1101
1102 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001103 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1104 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001105 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001106 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001107
1108 return AA;
1109}
1110
Chandler Carruth241bf242016-08-03 07:44:48 +00001111static Optional<int> parseRepeatPassName(StringRef Name) {
1112 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1113 return None;
1114 int Count;
1115 if (Name.getAsInteger(0, Count) || Count <= 0)
1116 return None;
1117 return Count;
1118}
1119
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001120static Optional<int> parseDevirtPassName(StringRef Name) {
1121 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1122 return None;
1123 int Count;
1124 if (Name.getAsInteger(0, Count) || Count <= 0)
1125 return None;
1126 return Count;
1127}
1128
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001129/// Tests whether a pass name starts with a valid prefix for a default pipeline
1130/// alias.
1131static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1132 return Name.startswith("default") || Name.startswith("thinlto") ||
1133 Name.startswith("lto");
1134}
1135
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001136/// Tests whether registered callbacks will accept a given pass name.
1137///
1138/// When parsing a pipeline text, the type of the outermost pipeline may be
1139/// omitted, in which case the type is automatically determined from the first
1140/// pass name in the text. This may be a name that is handled through one of the
1141/// callbacks. We check this through the oridinary parsing callbacks by setting
1142/// up a dummy PassManager in order to not force the client to also handle this
1143/// type of query.
1144template <typename PassManagerT, typename CallbacksT>
1145static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1146 if (!Callbacks.empty()) {
1147 PassManagerT DummyPM;
1148 for (auto &CB : Callbacks)
1149 if (CB(Name, DummyPM, {}))
1150 return true;
1151 }
1152 return false;
1153}
1154
1155template <typename CallbacksT>
1156static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001157 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001158 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001159 return DefaultAliasRegex.match(Name);
1160
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001161 // Explicitly handle pass manager names.
1162 if (Name == "module")
1163 return true;
1164 if (Name == "cgscc")
1165 return true;
1166 if (Name == "function")
1167 return true;
1168
Chandler Carruth241bf242016-08-03 07:44:48 +00001169 // Explicitly handle custom-parsed pass names.
1170 if (parseRepeatPassName(Name))
1171 return true;
1172
Chandler Carruth74a8a222016-06-17 07:15:29 +00001173#define MODULE_PASS(NAME, CREATE_PASS) \
1174 if (Name == NAME) \
1175 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001176#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001177 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001178 return true;
1179#include "PassRegistry.def"
1180
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001181 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001182}
1183
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001184template <typename CallbacksT>
1185static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001186 // Explicitly handle pass manager names.
1187 if (Name == "cgscc")
1188 return true;
1189 if (Name == "function")
1190 return true;
1191
Chandler Carruth241bf242016-08-03 07:44:48 +00001192 // Explicitly handle custom-parsed pass names.
1193 if (parseRepeatPassName(Name))
1194 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001195 if (parseDevirtPassName(Name))
1196 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001197
Chandler Carruth74a8a222016-06-17 07:15:29 +00001198#define CGSCC_PASS(NAME, CREATE_PASS) \
1199 if (Name == NAME) \
1200 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001201#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001202 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001203 return true;
1204#include "PassRegistry.def"
1205
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001206 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001207}
1208
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001209template <typename CallbacksT>
1210static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001211 // Explicitly handle pass manager names.
1212 if (Name == "function")
1213 return true;
1214 if (Name == "loop")
1215 return true;
1216
Chandler Carruth241bf242016-08-03 07:44:48 +00001217 // Explicitly handle custom-parsed pass names.
1218 if (parseRepeatPassName(Name))
1219 return true;
1220
Chandler Carruth74a8a222016-06-17 07:15:29 +00001221#define FUNCTION_PASS(NAME, CREATE_PASS) \
1222 if (Name == NAME) \
1223 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001224#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001225 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001226 return true;
1227#include "PassRegistry.def"
1228
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001229 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001230}
1231
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001232template <typename CallbacksT>
1233static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001234 // Explicitly handle pass manager names.
1235 if (Name == "loop")
1236 return true;
1237
Chandler Carruth241bf242016-08-03 07:44:48 +00001238 // Explicitly handle custom-parsed pass names.
1239 if (parseRepeatPassName(Name))
1240 return true;
1241
Chandler Carruth74a8a222016-06-17 07:15:29 +00001242#define LOOP_PASS(NAME, CREATE_PASS) \
1243 if (Name == NAME) \
1244 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001245#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1246 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1247 return true;
1248#include "PassRegistry.def"
1249
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001250 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001251}
1252
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001253Optional<std::vector<PassBuilder::PipelineElement>>
1254PassBuilder::parsePipelineText(StringRef Text) {
1255 std::vector<PipelineElement> ResultPipeline;
1256
1257 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1258 &ResultPipeline};
1259 for (;;) {
1260 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1261 size_t Pos = Text.find_first_of(",()");
1262 Pipeline.push_back({Text.substr(0, Pos), {}});
1263
1264 // If we have a single terminating name, we're done.
1265 if (Pos == Text.npos)
1266 break;
1267
1268 char Sep = Text[Pos];
1269 Text = Text.substr(Pos + 1);
1270 if (Sep == ',')
1271 // Just a name ending in a comma, continue.
1272 continue;
1273
1274 if (Sep == '(') {
1275 // Push the inner pipeline onto the stack to continue processing.
1276 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1277 continue;
1278 }
1279
1280 assert(Sep == ')' && "Bogus separator!");
1281 // When handling the close parenthesis, we greedily consume them to avoid
1282 // empty strings in the pipeline.
1283 do {
1284 // If we try to pop the outer pipeline we have unbalanced parentheses.
1285 if (PipelineStack.size() == 1)
1286 return None;
1287
1288 PipelineStack.pop_back();
1289 } while (Text.consume_front(")"));
1290
1291 // Check if we've finished parsing.
1292 if (Text.empty())
1293 break;
1294
1295 // Otherwise, the end of an inner pipeline always has to be followed by
1296 // a comma, and then we can continue.
1297 if (!Text.consume_front(","))
1298 return None;
1299 }
1300
1301 if (PipelineStack.size() > 1)
1302 // Unbalanced paretheses.
1303 return None;
1304
1305 assert(PipelineStack.back() == &ResultPipeline &&
1306 "Wrong pipeline at the bottom of the stack!");
1307 return {std::move(ResultPipeline)};
1308}
1309
1310bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1311 const PipelineElement &E, bool VerifyEachPass,
1312 bool DebugLogging) {
1313 auto &Name = E.Name;
1314 auto &InnerPipeline = E.InnerPipeline;
1315
1316 // First handle complex passes like the pass managers which carry pipelines.
1317 if (!InnerPipeline.empty()) {
1318 if (Name == "module") {
1319 ModulePassManager NestedMPM(DebugLogging);
1320 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1321 DebugLogging))
1322 return false;
1323 MPM.addPass(std::move(NestedMPM));
1324 return true;
1325 }
1326 if (Name == "cgscc") {
1327 CGSCCPassManager CGPM(DebugLogging);
1328 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1329 DebugLogging))
1330 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001331 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001332 return true;
1333 }
1334 if (Name == "function") {
1335 FunctionPassManager FPM(DebugLogging);
1336 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1337 DebugLogging))
1338 return false;
1339 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1340 return true;
1341 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001342 if (auto Count = parseRepeatPassName(Name)) {
1343 ModulePassManager NestedMPM(DebugLogging);
1344 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1345 DebugLogging))
1346 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001347 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001348 return true;
1349 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001350
1351 for (auto &C : ModulePipelineParsingCallbacks)
1352 if (C(Name, MPM, InnerPipeline))
1353 return true;
1354
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001355 // Normal passes can't have pipelines.
1356 return false;
1357 }
1358
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001359 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001360 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001361 SmallVector<StringRef, 3> Matches;
1362 if (!DefaultAliasRegex.match(Name, &Matches))
1363 return false;
1364 assert(Matches.size() == 3 && "Must capture two matched strings!");
1365
Jordan Rosef85a95f2016-07-25 18:34:51 +00001366 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001367 .Case("O0", O0)
1368 .Case("O1", O1)
1369 .Case("O2", O2)
1370 .Case("O3", O3)
1371 .Case("Os", Os)
1372 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001373 if (L == O0)
1374 // At O0 we do nothing at all!
1375 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001376
1377 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001378 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001379 } else if (Matches[1] == "thinlto-pre-link") {
1380 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1381 } else if (Matches[1] == "thinlto") {
1382 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001383 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001384 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001385 } else {
1386 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001387 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001388 }
1389 return true;
1390 }
1391
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001392 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001393#define MODULE_PASS(NAME, CREATE_PASS) \
1394 if (Name == NAME) { \
1395 MPM.addPass(CREATE_PASS); \
1396 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001397 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001398#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1399 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001400 MPM.addPass( \
1401 RequireAnalysisPass< \
1402 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001403 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001404 } \
1405 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001406 MPM.addPass(InvalidateAnalysisPass< \
1407 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001408 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001409 }
1410#include "PassRegistry.def"
1411
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001412 for (auto &C : ModulePipelineParsingCallbacks)
1413 if (C(Name, MPM, InnerPipeline))
1414 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001415 return false;
1416}
1417
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001418bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1419 const PipelineElement &E, bool VerifyEachPass,
1420 bool DebugLogging) {
1421 auto &Name = E.Name;
1422 auto &InnerPipeline = E.InnerPipeline;
1423
1424 // First handle complex passes like the pass managers which carry pipelines.
1425 if (!InnerPipeline.empty()) {
1426 if (Name == "cgscc") {
1427 CGSCCPassManager NestedCGPM(DebugLogging);
1428 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1429 DebugLogging))
1430 return false;
1431 // Add the nested pass manager with the appropriate adaptor.
1432 CGPM.addPass(std::move(NestedCGPM));
1433 return true;
1434 }
1435 if (Name == "function") {
1436 FunctionPassManager FPM(DebugLogging);
1437 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1438 DebugLogging))
1439 return false;
1440 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001441 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001442 return true;
1443 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001444 if (auto Count = parseRepeatPassName(Name)) {
1445 CGSCCPassManager NestedCGPM(DebugLogging);
1446 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1447 DebugLogging))
1448 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001449 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001450 return true;
1451 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001452 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1453 CGSCCPassManager NestedCGPM(DebugLogging);
1454 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1455 DebugLogging))
1456 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001457 CGPM.addPass(
1458 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001459 return true;
1460 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001461
1462 for (auto &C : CGSCCPipelineParsingCallbacks)
1463 if (C(Name, CGPM, InnerPipeline))
1464 return true;
1465
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001466 // Normal passes can't have pipelines.
1467 return false;
1468 }
1469
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001470// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001471#define CGSCC_PASS(NAME, CREATE_PASS) \
1472 if (Name == NAME) { \
1473 CGPM.addPass(CREATE_PASS); \
1474 return true; \
1475 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001476#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1477 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001478 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001479 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001480 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1481 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001482 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001483 } \
1484 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001485 CGPM.addPass(InvalidateAnalysisPass< \
1486 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001487 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001488 }
1489#include "PassRegistry.def"
1490
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001491 for (auto &C : CGSCCPipelineParsingCallbacks)
1492 if (C(Name, CGPM, InnerPipeline))
1493 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001494 return false;
1495}
1496
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001497bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1498 const PipelineElement &E,
1499 bool VerifyEachPass, bool DebugLogging) {
1500 auto &Name = E.Name;
1501 auto &InnerPipeline = E.InnerPipeline;
1502
1503 // First handle complex passes like the pass managers which carry pipelines.
1504 if (!InnerPipeline.empty()) {
1505 if (Name == "function") {
1506 FunctionPassManager NestedFPM(DebugLogging);
1507 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1508 DebugLogging))
1509 return false;
1510 // Add the nested pass manager with the appropriate adaptor.
1511 FPM.addPass(std::move(NestedFPM));
1512 return true;
1513 }
1514 if (Name == "loop") {
1515 LoopPassManager LPM(DebugLogging);
1516 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1517 DebugLogging))
1518 return false;
1519 // Add the nested pass manager with the appropriate adaptor.
1520 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1521 return true;
1522 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001523 if (auto Count = parseRepeatPassName(Name)) {
1524 FunctionPassManager NestedFPM(DebugLogging);
1525 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1526 DebugLogging))
1527 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001528 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001529 return true;
1530 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001531
1532 for (auto &C : FunctionPipelineParsingCallbacks)
1533 if (C(Name, FPM, InnerPipeline))
1534 return true;
1535
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001536 // Normal passes can't have pipelines.
1537 return false;
1538 }
1539
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001540// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001541#define FUNCTION_PASS(NAME, CREATE_PASS) \
1542 if (Name == NAME) { \
1543 FPM.addPass(CREATE_PASS); \
1544 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001545 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001546#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1547 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001548 FPM.addPass( \
1549 RequireAnalysisPass< \
1550 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001551 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001552 } \
1553 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001554 FPM.addPass(InvalidateAnalysisPass< \
1555 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001556 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001557 }
1558#include "PassRegistry.def"
1559
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001560 for (auto &C : FunctionPipelineParsingCallbacks)
1561 if (C(Name, FPM, InnerPipeline))
1562 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001563 return false;
1564}
1565
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001566bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001567 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001568 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001569 auto &InnerPipeline = E.InnerPipeline;
1570
1571 // First handle complex passes like the pass managers which carry pipelines.
1572 if (!InnerPipeline.empty()) {
1573 if (Name == "loop") {
1574 LoopPassManager NestedLPM(DebugLogging);
1575 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1576 DebugLogging))
1577 return false;
1578 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001579 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001580 return true;
1581 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001582 if (auto Count = parseRepeatPassName(Name)) {
1583 LoopPassManager NestedLPM(DebugLogging);
1584 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1585 DebugLogging))
1586 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001587 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001588 return true;
1589 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001590
1591 for (auto &C : LoopPipelineParsingCallbacks)
1592 if (C(Name, LPM, InnerPipeline))
1593 return true;
1594
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001595 // Normal passes can't have pipelines.
1596 return false;
1597 }
1598
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001599// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001600#define LOOP_PASS(NAME, CREATE_PASS) \
1601 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001602 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001603 return true; \
1604 }
1605#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1606 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001607 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001608 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1609 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1610 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001611 return true; \
1612 } \
1613 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001614 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001615 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001616 return true; \
1617 }
1618#include "PassRegistry.def"
1619
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001620 for (auto &C : LoopPipelineParsingCallbacks)
1621 if (C(Name, LPM, InnerPipeline))
1622 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001623 return false;
1624}
1625
Chandler Carruthedf59962016-02-18 09:45:17 +00001626bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001627#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1628 if (Name == NAME) { \
1629 AA.registerModuleAnalysis< \
1630 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1631 return true; \
1632 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001633#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1634 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001635 AA.registerFunctionAnalysis< \
1636 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001637 return true; \
1638 }
1639#include "PassRegistry.def"
1640
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001641 for (auto &C : AAParsingCallbacks)
1642 if (C(Name, AA))
1643 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001644 return false;
1645}
1646
Justin Bognereecc3c82016-02-25 07:23:08 +00001647bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001648 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001649 bool VerifyEachPass,
1650 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001651 for (const auto &Element : Pipeline) {
1652 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1653 return false;
1654 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001655 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001656 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001657}
1658
Chandler Carruth1ff77242015-03-07 09:02:36 +00001659bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001660 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001661 bool VerifyEachPass,
1662 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001663 for (const auto &Element : Pipeline) {
1664 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1665 return false;
1666 if (VerifyEachPass)
1667 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001668 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001669 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001670}
1671
Chandler Carruth1ff77242015-03-07 09:02:36 +00001672bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001673 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001674 bool VerifyEachPass,
1675 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001676 for (const auto &Element : Pipeline) {
1677 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1678 return false;
1679 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001680 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001681 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001682}
1683
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001684void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1685 FunctionAnalysisManager &FAM,
1686 CGSCCAnalysisManager &CGAM,
1687 ModuleAnalysisManager &MAM) {
1688 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1689 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001690 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1691 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1692 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1693 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1694 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1695}
1696
Chandler Carruth1ff77242015-03-07 09:02:36 +00001697bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001698 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001699 bool VerifyEachPass,
1700 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001701 for (const auto &Element : Pipeline) {
1702 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1703 return false;
1704 if (VerifyEachPass)
1705 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001706 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001707 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001708}
1709
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001710// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001711// FIXME: Should this routine accept a TargetMachine or require the caller to
1712// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001713bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1714 StringRef PipelineText, bool VerifyEachPass,
1715 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001716 auto Pipeline = parsePipelineText(PipelineText);
1717 if (!Pipeline || Pipeline->empty())
1718 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001719
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001720 // If the first name isn't at the module layer, wrap the pipeline up
1721 // automatically.
1722 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001723
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001724 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1725 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001726 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001727 } else if (isFunctionPassName(FirstName,
1728 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001729 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001730 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001731 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001732 } else {
1733 for (auto &C : TopLevelPipelineParsingCallbacks)
1734 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1735 return true;
1736
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001737 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001738 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001739 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001740 }
1741
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001742 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001743}
Chandler Carruthedf59962016-02-18 09:45:17 +00001744
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001745// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1746bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1747 StringRef PipelineText, bool VerifyEachPass,
1748 bool DebugLogging) {
1749 auto Pipeline = parsePipelineText(PipelineText);
1750 if (!Pipeline || Pipeline->empty())
1751 return false;
1752
1753 StringRef FirstName = Pipeline->front().Name;
1754 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1755 return false;
1756
1757 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1758}
1759
1760// Primary pass pipeline description parsing routine for a \c
1761// FunctionPassManager
1762bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1763 StringRef PipelineText, bool VerifyEachPass,
1764 bool DebugLogging) {
1765 auto Pipeline = parsePipelineText(PipelineText);
1766 if (!Pipeline || Pipeline->empty())
1767 return false;
1768
1769 StringRef FirstName = Pipeline->front().Name;
1770 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1771 return false;
1772
1773 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1774 DebugLogging);
1775}
1776
1777// Primary pass pipeline description parsing routine for a \c LoopPassManager
1778bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1779 StringRef PipelineText, bool VerifyEachPass,
1780 bool DebugLogging) {
1781 auto Pipeline = parsePipelineText(PipelineText);
1782 if (!Pipeline || Pipeline->empty())
1783 return false;
1784
1785 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1786}
1787
Chandler Carruthedf59962016-02-18 09:45:17 +00001788bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001789 // If the pipeline just consists of the word 'default' just replace the AA
1790 // manager with our default one.
1791 if (PipelineText == "default") {
1792 AA = buildDefaultAAPipeline();
1793 return true;
1794 }
1795
Chandler Carruthedf59962016-02-18 09:45:17 +00001796 while (!PipelineText.empty()) {
1797 StringRef Name;
1798 std::tie(Name, PipelineText) = PipelineText.split(',');
1799 if (!parseAAPassName(AA, Name))
1800 return false;
1801 }
1802
1803 return true;
1804}