blob: 84301752853392432915fa4975e12c3291650230 [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"
Chandler Carruth00a301d2017-11-14 01:30:04 +000087#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000088#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000089#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000090#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000091#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000092#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000093#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000094#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000095#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000096#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000097#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000098#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000099#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000100#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000101#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000102#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000103#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000104#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000105#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000106#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000107#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000108#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000109#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000110#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000111#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000112#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000113#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000114#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000115#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000116#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000117#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000118#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000119#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000120#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000121#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000122#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000123#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000124#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000125#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000126#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000127#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000128#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000129#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000130#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000131#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000132#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000133#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000134#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000135#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000136#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000137#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000138#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000139#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000140#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000141#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000142#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000143#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000144#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000145#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000146#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000147#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000148#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000149#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000150#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000151#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000152
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000153#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000154
155using namespace llvm;
156
Chandler Carruth719ffe12017-02-12 05:38:04 +0000157static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
158 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000159static cl::opt<bool>
160 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
161 cl::Hidden, cl::ZeroOrMore,
162 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000163
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000164static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000165 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000166 cl::Hidden, cl::ZeroOrMore,
167 cl::desc("Run NewGVN instead of GVN"));
168
Geoff Berry3cca1da2017-06-10 15:20:03 +0000169static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000170 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
171 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000172
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000173static cl::opt<bool> EnableGVNHoist(
174 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
175 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
176
Davide Italianobe1b6a92017-06-03 23:18:29 +0000177static cl::opt<bool> EnableGVNSink(
178 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
179 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
180
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000181static Regex DefaultAliasRegex(
182 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000183
Chandler Carruthe3f50642016-12-22 06:59:15 +0000184static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
185 switch (Level) {
186 case PassBuilder::O0:
187 case PassBuilder::O1:
188 case PassBuilder::O2:
189 case PassBuilder::O3:
190 return false;
191
192 case PassBuilder::Os:
193 case PassBuilder::Oz:
194 return true;
195 }
196 llvm_unreachable("Invalid optimization level!");
197}
198
Chandler Carruth66445382014-01-11 08:16:35 +0000199namespace {
200
Chandler Carruthd8330982014-01-12 09:34:22 +0000201/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000202struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000203 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000204 return PreservedAnalyses::all();
205 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000206 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000207};
208
Chandler Carruth0b576b32015-01-06 02:50:06 +0000209/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000210class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
211 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000212 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000213
214public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000215 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000216 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000217 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000218};
219
Chandler Carruth572e3402014-04-21 11:12:00 +0000220/// \brief No-op CGSCC pass which does nothing.
221struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000222 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
223 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000224 return PreservedAnalyses::all();
225 }
226 static StringRef name() { return "NoOpCGSCCPass"; }
227};
228
Chandler Carruth0b576b32015-01-06 02:50:06 +0000229/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000230class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
231 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000232 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000233
234public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000235 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000236 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000237 return Result();
238 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000239 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000240};
241
Chandler Carruthd8330982014-01-12 09:34:22 +0000242/// \brief No-op function pass which does nothing.
243struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000244 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000245 return PreservedAnalyses::all();
246 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000247 static StringRef name() { return "NoOpFunctionPass"; }
248};
249
Chandler Carruth0b576b32015-01-06 02:50:06 +0000250/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000251class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
252 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000253 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000254
255public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000256 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000257 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000258 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000259};
260
Justin Bognereecc3c82016-02-25 07:23:08 +0000261/// \brief No-op loop pass which does nothing.
262struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000263 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
264 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000265 return PreservedAnalyses::all();
266 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000267 static StringRef name() { return "NoOpLoopPass"; }
268};
269
270/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000271class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
272 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000273 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000274
275public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000276 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000277 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
278 return Result();
279 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000280 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000281};
282
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000283AnalysisKey NoOpModuleAnalysis::Key;
284AnalysisKey NoOpCGSCCAnalysis::Key;
285AnalysisKey NoOpFunctionAnalysis::Key;
286AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000287
Chandler Carruth66445382014-01-11 08:16:35 +0000288} // End anonymous namespace.
289
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000290void PassBuilder::invokePeepholeEPCallbacks(
291 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
292 for (auto &C : PeepholeEPCallbacks)
293 C(FPM, Level);
294}
295
Chandler Carruth1ff77242015-03-07 09:02:36 +0000296void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000297#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000298 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000299#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000300
301 for (auto &C : ModuleAnalysisRegistrationCallbacks)
302 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000303}
304
Chandler Carruth1ff77242015-03-07 09:02:36 +0000305void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000306#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000307 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000308#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000309
310 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
311 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000312}
313
Chandler Carruth1ff77242015-03-07 09:02:36 +0000314void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000315#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000316 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000317#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000318
319 for (auto &C : FunctionAnalysisRegistrationCallbacks)
320 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000321}
322
Justin Bognereecc3c82016-02-25 07:23:08 +0000323void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000324#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000325 LAM.registerPass([&] { return CREATE_PASS; });
326#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000327
328 for (auto &C : LoopAnalysisRegistrationCallbacks)
329 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000330}
331
Chandler Carruthe3f50642016-12-22 06:59:15 +0000332FunctionPassManager
333PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000334 ThinLTOPhase Phase,
335 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000336 assert(Level != O0 && "Must request optimizations!");
337 FunctionPassManager FPM(DebugLogging);
338
339 // Form SSA out of local memory accesses after breaking apart aggregates into
340 // scalars.
341 FPM.addPass(SROA());
342
343 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000344 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000345
Davide Italianoc3688312017-06-01 23:08:14 +0000346 // Hoisting of scalars and load expressions.
347 if (EnableGVNHoist)
348 FPM.addPass(GVNHoistPass());
349
Davide Italianobe1b6a92017-06-03 23:18:29 +0000350 // Global value numbering based sinking.
351 if (EnableGVNSink) {
352 FPM.addPass(GVNSinkPass());
353 FPM.addPass(SimplifyCFGPass());
354 }
355
Chandler Carruthe3f50642016-12-22 06:59:15 +0000356 // Speculative execution if the target has divergent branches; otherwise nop.
357 FPM.addPass(SpeculativeExecutionPass());
358
359 // Optimize based on known information about branches, and cleanup afterward.
360 FPM.addPass(JumpThreadingPass());
361 FPM.addPass(CorrelatedValuePropagationPass());
362 FPM.addPass(SimplifyCFGPass());
363 FPM.addPass(InstCombinePass());
364
365 if (!isOptimizingForSize(Level))
366 FPM.addPass(LibCallsShrinkWrapPass());
367
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000368 invokePeepholeEPCallbacks(FPM, Level);
369
Rong Xue1f42452017-10-23 22:21:29 +0000370 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
371 // using the size value profile. Don't perform this when optimizing for size.
372 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
373 !isOptimizingForSize(Level))
374 FPM.addPass(PGOMemOPSizeOpt());
375
Chandler Carruthe3f50642016-12-22 06:59:15 +0000376 FPM.addPass(TailCallElimPass());
377 FPM.addPass(SimplifyCFGPass());
378
379 // Form canonically associated expression trees, and simplify the trees using
380 // basic mathematical properties. For example, this will form (nearly)
381 // minimal multiplication trees.
382 FPM.addPass(ReassociatePass());
383
384 // Add the primary loop simplification pipeline.
385 // FIXME: Currently this is split into two loop pass pipelines because we run
386 // some function passes in between them. These can and should be replaced by
387 // loop pass equivalenst but those aren't ready yet. Specifically,
388 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
389 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
390 // the other we have is `LoopInstSimplify`.
391 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
392
393 // Rotate Loop - disable header duplication at -Oz
394 LPM1.addPass(LoopRotatePass(Level != Oz));
395 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000396 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000397 LPM2.addPass(IndVarSimplifyPass());
398 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000399
400 for (auto &C : LateLoopOptimizationsEPCallbacks)
401 C(LPM2, Level);
402
Chandler Carruth79b733b2017-01-26 23:21:17 +0000403 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000404 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000405 // because it changes IR to makes profile annotation in back compile
406 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000407 if (Phase != ThinLTOPhase::PreLink ||
408 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000409 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000410
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000411 for (auto &C : LoopOptimizerEndEPCallbacks)
412 C(LPM2, Level);
413
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000414 // We provide the opt remark emitter pass for LICM to use. We only need to do
415 // this once as it is immutable.
416 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000417 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
418 FPM.addPass(SimplifyCFGPass());
419 FPM.addPass(InstCombinePass());
420 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
421
422 // Eliminate redundancies.
423 if (Level != O1) {
424 // These passes add substantial compile time so skip them at O1.
425 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000426 if (RunNewGVN)
427 FPM.addPass(NewGVNPass());
428 else
429 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000430 }
431
432 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
433 FPM.addPass(MemCpyOptPass());
434
435 // Sparse conditional constant propagation.
436 // FIXME: It isn't clear why we do this *after* loop passes rather than
437 // before...
438 FPM.addPass(SCCPPass());
439
440 // Delete dead bit computations (instcombine runs after to fold away the dead
441 // computations, and then ADCE will run later to exploit any new DCE
442 // opportunities that creates).
443 FPM.addPass(BDCEPass());
444
445 // Run instcombine after redundancy and dead bit elimination to exploit
446 // opportunities opened up by them.
447 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000448 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000449
450 // Re-consider control flow based optimizations after redundancy elimination,
451 // redo DCE, etc.
452 FPM.addPass(JumpThreadingPass());
453 FPM.addPass(CorrelatedValuePropagationPass());
454 FPM.addPass(DSEPass());
455 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
456
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000457 for (auto &C : ScalarOptimizerLateEPCallbacks)
458 C(FPM, Level);
459
Chandler Carruthe3f50642016-12-22 06:59:15 +0000460 // Finally, do an expensive DCE pass to catch all the dead code exposed by
461 // the simplifications and basic cleanup after all the simplifications.
462 FPM.addPass(ADCEPass());
463 FPM.addPass(SimplifyCFGPass());
464 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000465 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000466
467 return FPM;
468}
469
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000470void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
471 PassBuilder::OptimizationLevel Level,
472 bool RunProfileGen,
473 std::string ProfileGenFile,
474 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000475 // Generally running simplification passes and the inliner with an high
476 // threshold results in smaller executables, but there may be cases where
477 // the size grows, so let's be conservative here and skip this simplification
478 // at -Os/Oz.
479 if (!isOptimizingForSize(Level)) {
480 InlineParams IP;
481
482 // In the old pass manager, this is a cl::opt. Should still this be one?
483 IP.DefaultThreshold = 75;
484
485 // FIXME: The hint threshold has the same value used by the regular inliner.
486 // This should probably be lowered after performance testing.
487 // FIXME: this comment is cargo culted from the old pass manager, revisit).
488 IP.HintThreshold = 325;
489
490 CGSCCPassManager CGPipeline(DebugLogging);
491
492 CGPipeline.addPass(InlinerPass(IP));
493
494 FunctionPassManager FPM;
495 FPM.addPass(SROA());
496 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
497 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
498 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000499 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000500
Davide Italiano513dfaa2017-02-13 15:26:22 +0000501 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
502
503 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
504 }
505
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000506 // Delete anything that is now dead to make sure that we don't instrument
507 // dead code. Instrumentation can end up keeping dead code around and
508 // dramatically increase code size.
509 MPM.addPass(GlobalDCEPass());
510
Davide Italiano513dfaa2017-02-13 15:26:22 +0000511 if (RunProfileGen) {
512 MPM.addPass(PGOInstrumentationGen());
513
Xinliang David Lib67530e2017-06-25 00:26:43 +0000514 FunctionPassManager FPM;
515 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
516 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
517
Davide Italiano513dfaa2017-02-13 15:26:22 +0000518 // Add the profile lowering pass.
519 InstrProfOptions Options;
520 if (!ProfileGenFile.empty())
521 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000522 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000523 MPM.addPass(InstrProfiling(Options));
524 }
525
526 if (!ProfileUseFile.empty())
527 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
528}
529
Easwaran Raman8249fac2017-06-28 13:33:49 +0000530static InlineParams
531getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
532 auto O3 = PassBuilder::O3;
533 unsigned OptLevel = Level > O3 ? 2 : Level;
534 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
535 return getInlineParams(OptLevel, SizeLevel);
536}
537
Chandler Carruthe3f50642016-12-22 06:59:15 +0000538ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000539PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000540 ThinLTOPhase Phase,
541 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000542 ModulePassManager MPM(DebugLogging);
543
Chandler Carruthe3f50642016-12-22 06:59:15 +0000544 // Do basic inference of function attributes from known properties of system
545 // libraries and other oracles.
546 MPM.addPass(InferFunctionAttrsPass());
547
548 // Create an early function pass manager to cleanup the output of the
549 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000550 FunctionPassManager EarlyFPM(DebugLogging);
551 EarlyFPM.addPass(SimplifyCFGPass());
552 EarlyFPM.addPass(SROA());
553 EarlyFPM.addPass(EarlyCSEPass());
554 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000555 if (Level == O3)
556 EarlyFPM.addPass(CallSiteSplittingPass());
557
Dehao Chen08f88312017-08-07 20:23:20 +0000558 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
559 // to convert bitcast to direct calls so that they can be inlined during the
560 // profile annotation prepration step.
561 // More details about SamplePGO design can be found in:
562 // https://research.google.com/pubs/pub45290.html
563 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
564 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
565 Phase == ThinLTOPhase::PostLink)
566 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000567 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000568
Dehao Chen08f88312017-08-07 20:23:20 +0000569 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
570 // Annotate sample profile right after early FPM to ensure freshness of
571 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000572 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
573 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000574 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
575 // for the profile annotation to be accurate in the ThinLTO backend.
576 if (Phase != ThinLTOPhase::PreLink)
577 // We perform early indirect call promotion here, before globalopt.
578 // This is important for the ThinLTO backend phase because otherwise
579 // imported available_externally functions look unreferenced and are
580 // removed.
581 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
582 true));
583 }
584
Chandler Carruthe3f50642016-12-22 06:59:15 +0000585 // Interprocedural constant propagation now that basic cleanup has occured
586 // and prior to optimizing globals.
587 // FIXME: This position in the pipeline hasn't been carefully considered in
588 // years, it should be re-analyzed.
589 MPM.addPass(IPSCCPPass());
590
Matthew Simpsoncb585582017-10-25 13:40:08 +0000591 // Attach metadata to indirect call sites indicating the set of functions
592 // they may target at run-time. This should follow IPSCCP.
593 MPM.addPass(CalledValuePropagationPass());
594
Chandler Carruthe3f50642016-12-22 06:59:15 +0000595 // Optimize globals to try and fold them into constants.
596 MPM.addPass(GlobalOptPass());
597
598 // Promote any localized globals to SSA registers.
599 // FIXME: Should this instead by a run of SROA?
600 // FIXME: We should probably run instcombine and simplify-cfg afterward to
601 // delete control flows that are dead once globals have been folded to
602 // constants.
603 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
604
605 // Remove any dead arguments exposed by cleanups and constand folding
606 // globals.
607 MPM.addPass(DeadArgumentEliminationPass());
608
609 // Create a small function pass pipeline to cleanup after all the global
610 // optimizations.
611 FunctionPassManager GlobalCleanupPM(DebugLogging);
612 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000613 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
614
Chandler Carruthe3f50642016-12-22 06:59:15 +0000615 GlobalCleanupPM.addPass(SimplifyCFGPass());
616 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
617
Dehao Chen89d32262017-08-02 01:28:31 +0000618 // Add all the requested passes for instrumentation PGO, if requested.
619 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
620 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
621 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
622 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
623 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000624 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000625
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000626 // Require the GlobalsAA analysis for the module so we can query it within
627 // the CGSCC pipeline.
628 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000629
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000630 // Require the ProfileSummaryAnalysis for the module so we can query it within
631 // the inliner pass.
632 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
633
Chandler Carruthe3f50642016-12-22 06:59:15 +0000634 // Now begin the main postorder CGSCC pipeline.
635 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
636 // manager and trying to emulate its precise behavior. Much of this doesn't
637 // make a lot of sense and we should revisit the core CGSCC structure.
638 CGSCCPassManager MainCGPipeline(DebugLogging);
639
640 // Note: historically, the PruneEH pass was run first to deduce nounwind and
641 // generally clean up exception handling overhead. It isn't clear this is
642 // valuable as the inliner doesn't currently care whether it is inlining an
643 // invoke or a call.
644
645 // Run the inliner first. The theory is that we are walking bottom-up and so
646 // the callees have already been fully optimized, and we want to inline them
647 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000648 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000649 // because it makes profile annotation in the backend inaccurate.
650 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000651 if (Phase == ThinLTOPhase::PreLink &&
652 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000653 IP.HotCallSiteThreshold = 0;
654 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000655
656 // Now deduce any function attributes based in the current code.
657 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
658
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000659 // When at O3 add argument promotion to the pass pipeline.
660 // FIXME: It isn't at all clear why this should be limited to O3.
661 if (Level == O3)
662 MainCGPipeline.addPass(ArgumentPromotionPass());
663
Chandler Carruthe3f50642016-12-22 06:59:15 +0000664 // Lastly, add the core function simplification pipeline nested inside the
665 // CGSCC walk.
666 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000667 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000668
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000669 for (auto &C : CGSCCOptimizerLateEPCallbacks)
670 C(MainCGPipeline, Level);
671
Chandler Carruth719ffe12017-02-12 05:38:04 +0000672 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
673 // to detect when we devirtualize indirect calls and iterate the SCC passes
674 // in that case to try and catch knock-on inlining or function attrs
675 // opportunities. Then we add it to the module pipeline by walking the SCCs
676 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000677 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000678 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000679 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000680
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000681 return MPM;
682}
683
684ModulePassManager
685PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
686 bool DebugLogging) {
687 ModulePassManager MPM(DebugLogging);
688
689 // Optimize globals now that the module is fully simplified.
690 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000691 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000692
Xinliang David Li126157c2017-05-22 16:41:57 +0000693 // Run partial inlining pass to partially inline functions that have
694 // large bodies.
695 if (RunPartialInlining)
696 MPM.addPass(PartialInlinerPass());
697
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000698 // Remove avail extern fns and globals definitions since we aren't compiling
699 // an object file for later LTO. For LTO we want to preserve these so they
700 // are eligible for inlining at link-time. Note if they are unreferenced they
701 // will be removed by GlobalDCE later, so this only impacts referenced
702 // available externally globals. Eventually they will be suppressed during
703 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
704 // may make globals referenced by available external functions dead and saves
705 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000706 MPM.addPass(EliminateAvailableExternallyPass());
707
708 // Do RPO function attribute inference across the module to forward-propagate
709 // attributes where applicable.
710 // FIXME: Is this really an optimization rather than a canonicalization?
711 MPM.addPass(ReversePostOrderFunctionAttrsPass());
712
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000713 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000714 // useful as the above will have inlined, DCE'ed, and function-attr
715 // propagated everything. We should at this point have a reasonably minimal
716 // and richly annotated call graph. By computing aliasing and mod/ref
717 // information for all local globals here, the late loop passes and notably
718 // the vectorizer will be able to use them to help recognize vectorizable
719 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000720 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000721
722 FunctionPassManager OptimizePM(DebugLogging);
723 OptimizePM.addPass(Float2IntPass());
724 // FIXME: We need to run some loop optimizations to re-rotate loops after
725 // simplify-cfg and others undo their rotation.
726
727 // Optimize the loop execution. These passes operate on entire loop nests
728 // rather than on each loop in an inside-out manner, and so they are actually
729 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000730
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000731 for (auto &C : VectorizerStartEPCallbacks)
732 C(OptimizePM, Level);
733
Chandler Carrutha95ff382017-01-27 00:50:21 +0000734 // First rotate loops that may have been un-rotated by prior passes.
735 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
736
737 // Distribute loops to allow partial vectorization. I.e. isolate dependences
738 // into separate loop that would otherwise inhibit vectorization. This is
739 // currently only performed for loops marked with the metadata
740 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000741 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000742
743 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000744 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000745
Chandler Carruthbaabda92017-01-27 01:32:26 +0000746 // Eliminate loads by forwarding stores from the previous iteration to loads
747 // of the current iteration.
748 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000749
750 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000751 OptimizePM.addPass(InstCombinePass());
752
Chandler Carrutha95ff382017-01-27 00:50:21 +0000753
754 // Now that we've formed fast to execute loop structures, we do further
755 // optimizations. These are run afterward as they might block doing complex
756 // analyses and transforms such as what are needed for loop vectorization.
757
Chandler Carruthe3f50642016-12-22 06:59:15 +0000758 // Optimize parallel scalar instruction chains into SIMD instructions.
759 OptimizePM.addPass(SLPVectorizerPass());
760
Sanjay Patel3e298902017-11-15 16:33:11 +0000761 // Cleanup after all of the vectorizers. Simplification passes like CVP and
762 // GVN, loop transforms, and others have already run, so it's now better to
763 // convert to more optimized IR using more aggressive simplify CFG options.
764 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
765 forwardSwitchCondToPhi(true).
766 convertSwitchToLookupTable(true).
767 needCanonicalLoops(false)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000768 OptimizePM.addPass(InstCombinePass());
769
770 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000771 // execution resources of an out-of-order processor. We also then need to
772 // clean up redundancies and loop invariant code.
773 // FIXME: It would be really good to use a loop-integrated instruction
774 // combiner for cleanup here so that the unrolling and LICM can be pipelined
775 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000776 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000777 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000778 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000779 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000780
781 // Now that we've vectorized and unrolled loops, we may have more refined
782 // alignment information, try to re-derive it here.
783 OptimizePM.addPass(AlignmentFromAssumptionsPass());
784
Chandler Carrutha95ff382017-01-27 00:50:21 +0000785 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
786 // canonicalization pass that enables other optimizations. As a result,
787 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
788 // result too early.
789 OptimizePM.addPass(LoopSinkPass());
790
791 // And finally clean up LCSSA form before generating code.
792 OptimizePM.addPass(InstSimplifierPass());
793
Sanjay Patel6fd43912017-09-09 13:38:18 +0000794 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
795 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
796 // flattening of blocks.
797 OptimizePM.addPass(DivRemPairsPass());
798
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000799 // LoopSink (and other loop passes since the last simplifyCFG) might have
800 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
801 OptimizePM.addPass(SimplifyCFGPass());
802
Chandler Carruthc34f7892017-11-28 11:32:31 +0000803 // Optimize PHIs by speculating around them when profitable. Note that this
804 // pass needs to be run after any PRE or similar pass as it is essentially
805 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
806 OptimizePM.addPass(SpeculateAroundPHIsPass());
807
Chandler Carrutha95ff382017-01-27 00:50:21 +0000808 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000809 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
810
811 // Now we need to do some global optimization transforms.
812 // FIXME: It would seem like these should come first in the optimization
813 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
814 // ordering here.
815 MPM.addPass(GlobalDCEPass());
816 MPM.addPass(ConstantMergePass());
817
818 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000819}
820
Chandler Carruthe3f50642016-12-22 06:59:15 +0000821ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000822PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
823 bool DebugLogging) {
824 assert(Level != O0 && "Must request optimizations for the default pipeline!");
825
826 ModulePassManager MPM(DebugLogging);
827
828 // Force any function attributes we want the rest of the pipeline to observe.
829 MPM.addPass(ForceFunctionAttrsPass());
830
Dehao Chen08f88312017-08-07 20:23:20 +0000831 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000832 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
833
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000834 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000835 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
836 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000837
838 // Now add the optimization pipeline.
839 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
840
841 return MPM;
842}
843
844ModulePassManager
845PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
846 bool DebugLogging) {
847 assert(Level != O0 && "Must request optimizations for the default pipeline!");
848
849 ModulePassManager MPM(DebugLogging);
850
851 // Force any function attributes we want the rest of the pipeline to observe.
852 MPM.addPass(ForceFunctionAttrsPass());
853
Dehao Chen08f88312017-08-07 20:23:20 +0000854 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000855 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
856
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000857 // If we are planning to perform ThinLTO later, we don't bloat the code with
858 // unrolling/vectorization/... now. Just simplify the module as much as we
859 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000860 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
861 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000862
863 // Run partial inlining pass to partially inline functions that have
864 // large bodies.
865 // FIXME: It isn't clear whether this is really the right place to run this
866 // in ThinLTO. Because there is another canonicalization and simplification
867 // phase that will run after the thin link, running this here ends up with
868 // less information than will be available later and it may grow functions in
869 // ways that aren't beneficial.
870 if (RunPartialInlining)
871 MPM.addPass(PartialInlinerPass());
872
873 // Reduce the size of the IR as much as possible.
874 MPM.addPass(GlobalOptPass());
875
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000876 return MPM;
877}
878
879ModulePassManager
880PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
881 bool DebugLogging) {
882 // FIXME: The summary index is not hooked in the new pass manager yet.
883 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
884 // here.
885
886 ModulePassManager MPM(DebugLogging);
887
888 // Force any function attributes we want the rest of the pipeline to observe.
889 MPM.addPass(ForceFunctionAttrsPass());
890
891 // During the ThinLTO backend phase we perform early indirect call promotion
892 // here, before globalopt. Otherwise imported available_externally functions
893 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000894 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
895 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000896 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000897 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
898 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000899
900 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000901 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
902 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000903
904 // Now add the optimization pipeline.
905 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
906
907 return MPM;
908}
909
910ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000911PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
912 bool DebugLogging) {
913 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000914 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000915 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000916}
917
Chandler Carruthe3f50642016-12-22 06:59:15 +0000918ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
919 bool DebugLogging) {
920 assert(Level != O0 && "Must request optimizations for the default pipeline!");
921 ModulePassManager MPM(DebugLogging);
922
Davide Italiano089a9122017-01-24 00:57:39 +0000923 // Remove unused virtual tables to improve the quality of code generated by
924 // whole-program devirtualization and bitset lowering.
925 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000926
Davide Italiano089a9122017-01-24 00:57:39 +0000927 // Force any function attributes we want the rest of the pipeline to observe.
928 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000929
Davide Italiano089a9122017-01-24 00:57:39 +0000930 // Do basic inference of function attributes from known properties of system
931 // libraries and other oracles.
932 MPM.addPass(InferFunctionAttrsPass());
933
934 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000935 FunctionPassManager EarlyFPM(DebugLogging);
936 EarlyFPM.addPass(CallSiteSplittingPass());
937 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
938
Davide Italiano089a9122017-01-24 00:57:39 +0000939 // Indirect call promotion. This should promote all the targets that are
940 // left by the earlier promotion pass that promotes intra-module targets.
941 // This two-step promotion is to save the compile time. For LTO, it should
942 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000943 MPM.addPass(PGOIndirectCallPromotion(
944 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000945 // Propagate constants at call sites into the functions they call. This
946 // opens opportunities for globalopt (and inlining) by substituting function
947 // pointers passed as arguments to direct uses of functions.
948 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000949
950 // Attach metadata to indirect call sites indicating the set of functions
951 // they may target at run-time. This should follow IPSCCP.
952 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000953 }
954
955 // Now deduce any function attributes based in the current code.
956 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
957 PostOrderFunctionAttrsPass()));
958
959 // Do RPO function attribute inference across the module to forward-propagate
960 // attributes where applicable.
961 // FIXME: Is this really an optimization rather than a canonicalization?
962 MPM.addPass(ReversePostOrderFunctionAttrsPass());
963
964 // Use inragne annotations on GEP indices to split globals where beneficial.
965 MPM.addPass(GlobalSplitPass());
966
967 // Run whole program optimization of virtual call when the list of callees
968 // is fixed.
969 MPM.addPass(WholeProgramDevirtPass());
970
971 // Stop here at -O1.
972 if (Level == 1)
973 return MPM;
974
975 // Optimize globals to try and fold them into constants.
976 MPM.addPass(GlobalOptPass());
977
978 // Promote any localized globals to SSA registers.
979 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
980
981 // Linking modules together can lead to duplicate global constant, only
982 // keep one copy of each constant.
983 MPM.addPass(ConstantMergePass());
984
985 // Remove unused arguments from functions.
986 MPM.addPass(DeadArgumentEliminationPass());
987
988 // Reduce the code after globalopt and ipsccp. Both can open up significant
989 // simplification opportunities, and both can propagate functions through
990 // function pointers. When this happens, we often have to resolve varargs
991 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000992 FunctionPassManager PeepholeFPM(DebugLogging);
993 PeepholeFPM.addPass(InstCombinePass());
994 invokePeepholeEPCallbacks(PeepholeFPM, Level);
995
996 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000997
998 // Note: historically, the PruneEH pass was run first to deduce nounwind and
999 // generally clean up exception handling overhead. It isn't clear this is
1000 // valuable as the inliner doesn't currently care whether it is inlining an
1001 // invoke or a call.
1002 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001003 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1004 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001005
1006 // Optimize globals again after we ran the inliner.
1007 MPM.addPass(GlobalOptPass());
1008
1009 // Garbage collect dead functions.
1010 // FIXME: Add ArgumentPromotion pass after once it's ported.
1011 MPM.addPass(GlobalDCEPass());
1012
1013 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001014 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001015 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001016 invokePeepholeEPCallbacks(FPM, Level);
1017
Davide Italiano089a9122017-01-24 00:57:39 +00001018 FPM.addPass(JumpThreadingPass());
1019
1020 // Break up allocas
1021 FPM.addPass(SROA());
1022
1023 // Run a few AA driver optimizations here and now to cleanup the code.
1024 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1025
1026 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1027 PostOrderFunctionAttrsPass()));
1028 // FIXME: here we run IP alias analysis in the legacy PM.
1029
1030 FunctionPassManager MainFPM;
1031
1032 // FIXME: once we fix LoopPass Manager, add LICM here.
1033 // FIXME: once we provide support for enabling MLSM, add it here.
1034 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001035 if (RunNewGVN)
1036 MainFPM.addPass(NewGVNPass());
1037 else
1038 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001039
1040 // Remove dead memcpy()'s.
1041 MainFPM.addPass(MemCpyOptPass());
1042
1043 // Nuke dead stores.
1044 MainFPM.addPass(DSEPass());
1045
1046 // FIXME: at this point, we run a bunch of loop passes:
1047 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1048 // loopVectorize. Enable them once the remaining issue with LPM
1049 // are sorted out.
1050
1051 MainFPM.addPass(InstCombinePass());
1052 MainFPM.addPass(SimplifyCFGPass());
1053 MainFPM.addPass(SCCPPass());
1054 MainFPM.addPass(InstCombinePass());
1055 MainFPM.addPass(BDCEPass());
1056
1057 // FIXME: We may want to run SLPVectorizer here.
1058 // After vectorization, assume intrinsics may tell us more
1059 // about pointer alignments.
1060#if 0
1061 MainFPM.add(AlignmentFromAssumptionsPass());
1062#endif
1063
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001064 // FIXME: Conditionally run LoadCombine here, after it's ported
1065 // (in case we still have this pass, given its questionable usefulness).
1066
Davide Italiano089a9122017-01-24 00:57:39 +00001067 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001068 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001069 MainFPM.addPass(JumpThreadingPass());
1070 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1071
1072 // Create a function that performs CFI checks for cross-DSO calls with
1073 // targets in the current module.
1074 MPM.addPass(CrossDSOCFIPass());
1075
1076 // Lower type metadata and the type.test intrinsic. This pass supports
1077 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1078 // to be run at link time if CFI is enabled. This pass does nothing if
1079 // CFI is disabled.
1080 // Enable once we add support for the summary in the new PM.
1081#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001082 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1083 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001084 Summary));
1085#endif
1086
1087 // Add late LTO optimization passes.
1088 // Delete basic blocks, which optimization passes may have killed.
1089 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1090
1091 // Drop bodies of available eternally objects to improve GlobalDCE.
1092 MPM.addPass(EliminateAvailableExternallyPass());
1093
1094 // Now that we have optimized the program, discard unreachable functions.
1095 MPM.addPass(GlobalDCEPass());
1096
1097 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001098 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001099}
1100
Chandler Carruth060ad612016-12-23 20:38:19 +00001101AAManager PassBuilder::buildDefaultAAPipeline() {
1102 AAManager AA;
1103
1104 // The order in which these are registered determines their priority when
1105 // being queried.
1106
1107 // First we register the basic alias analysis that provides the majority of
1108 // per-function local AA logic. This is a stateless, on-demand local set of
1109 // AA techniques.
1110 AA.registerFunctionAnalysis<BasicAA>();
1111
1112 // Next we query fast, specialized alias analyses that wrap IR-embedded
1113 // information about aliasing.
1114 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1115 AA.registerFunctionAnalysis<TypeBasedAA>();
1116
1117 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001118 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1119 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001120 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001121 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001122
1123 return AA;
1124}
1125
Chandler Carruth241bf242016-08-03 07:44:48 +00001126static Optional<int> parseRepeatPassName(StringRef Name) {
1127 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1128 return None;
1129 int Count;
1130 if (Name.getAsInteger(0, Count) || Count <= 0)
1131 return None;
1132 return Count;
1133}
1134
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001135static Optional<int> parseDevirtPassName(StringRef Name) {
1136 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1137 return None;
1138 int Count;
1139 if (Name.getAsInteger(0, Count) || Count <= 0)
1140 return None;
1141 return Count;
1142}
1143
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001144/// Tests whether a pass name starts with a valid prefix for a default pipeline
1145/// alias.
1146static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1147 return Name.startswith("default") || Name.startswith("thinlto") ||
1148 Name.startswith("lto");
1149}
1150
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001151/// Tests whether registered callbacks will accept a given pass name.
1152///
1153/// When parsing a pipeline text, the type of the outermost pipeline may be
1154/// omitted, in which case the type is automatically determined from the first
1155/// pass name in the text. This may be a name that is handled through one of the
1156/// callbacks. We check this through the oridinary parsing callbacks by setting
1157/// up a dummy PassManager in order to not force the client to also handle this
1158/// type of query.
1159template <typename PassManagerT, typename CallbacksT>
1160static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1161 if (!Callbacks.empty()) {
1162 PassManagerT DummyPM;
1163 for (auto &CB : Callbacks)
1164 if (CB(Name, DummyPM, {}))
1165 return true;
1166 }
1167 return false;
1168}
1169
1170template <typename CallbacksT>
1171static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001172 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001173 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001174 return DefaultAliasRegex.match(Name);
1175
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001176 // Explicitly handle pass manager names.
1177 if (Name == "module")
1178 return true;
1179 if (Name == "cgscc")
1180 return true;
1181 if (Name == "function")
1182 return true;
1183
Chandler Carruth241bf242016-08-03 07:44:48 +00001184 // Explicitly handle custom-parsed pass names.
1185 if (parseRepeatPassName(Name))
1186 return true;
1187
Chandler Carruth74a8a222016-06-17 07:15:29 +00001188#define MODULE_PASS(NAME, CREATE_PASS) \
1189 if (Name == NAME) \
1190 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001191#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001192 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001193 return true;
1194#include "PassRegistry.def"
1195
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001196 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001197}
1198
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001199template <typename CallbacksT>
1200static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001201 // Explicitly handle pass manager names.
1202 if (Name == "cgscc")
1203 return true;
1204 if (Name == "function")
1205 return true;
1206
Chandler Carruth241bf242016-08-03 07:44:48 +00001207 // Explicitly handle custom-parsed pass names.
1208 if (parseRepeatPassName(Name))
1209 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001210 if (parseDevirtPassName(Name))
1211 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001212
Chandler Carruth74a8a222016-06-17 07:15:29 +00001213#define CGSCC_PASS(NAME, CREATE_PASS) \
1214 if (Name == NAME) \
1215 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001216#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001217 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001218 return true;
1219#include "PassRegistry.def"
1220
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001221 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001222}
1223
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001224template <typename CallbacksT>
1225static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001226 // Explicitly handle pass manager names.
1227 if (Name == "function")
1228 return true;
1229 if (Name == "loop")
1230 return true;
1231
Chandler Carruth241bf242016-08-03 07:44:48 +00001232 // Explicitly handle custom-parsed pass names.
1233 if (parseRepeatPassName(Name))
1234 return true;
1235
Chandler Carruth74a8a222016-06-17 07:15:29 +00001236#define FUNCTION_PASS(NAME, CREATE_PASS) \
1237 if (Name == NAME) \
1238 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001239#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001240 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001241 return true;
1242#include "PassRegistry.def"
1243
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001244 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001245}
1246
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001247template <typename CallbacksT>
1248static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001249 // Explicitly handle pass manager names.
1250 if (Name == "loop")
1251 return true;
1252
Chandler Carruth241bf242016-08-03 07:44:48 +00001253 // Explicitly handle custom-parsed pass names.
1254 if (parseRepeatPassName(Name))
1255 return true;
1256
Chandler Carruth74a8a222016-06-17 07:15:29 +00001257#define LOOP_PASS(NAME, CREATE_PASS) \
1258 if (Name == NAME) \
1259 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001260#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1261 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1262 return true;
1263#include "PassRegistry.def"
1264
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001265 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001266}
1267
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001268Optional<std::vector<PassBuilder::PipelineElement>>
1269PassBuilder::parsePipelineText(StringRef Text) {
1270 std::vector<PipelineElement> ResultPipeline;
1271
1272 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1273 &ResultPipeline};
1274 for (;;) {
1275 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1276 size_t Pos = Text.find_first_of(",()");
1277 Pipeline.push_back({Text.substr(0, Pos), {}});
1278
1279 // If we have a single terminating name, we're done.
1280 if (Pos == Text.npos)
1281 break;
1282
1283 char Sep = Text[Pos];
1284 Text = Text.substr(Pos + 1);
1285 if (Sep == ',')
1286 // Just a name ending in a comma, continue.
1287 continue;
1288
1289 if (Sep == '(') {
1290 // Push the inner pipeline onto the stack to continue processing.
1291 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1292 continue;
1293 }
1294
1295 assert(Sep == ')' && "Bogus separator!");
1296 // When handling the close parenthesis, we greedily consume them to avoid
1297 // empty strings in the pipeline.
1298 do {
1299 // If we try to pop the outer pipeline we have unbalanced parentheses.
1300 if (PipelineStack.size() == 1)
1301 return None;
1302
1303 PipelineStack.pop_back();
1304 } while (Text.consume_front(")"));
1305
1306 // Check if we've finished parsing.
1307 if (Text.empty())
1308 break;
1309
1310 // Otherwise, the end of an inner pipeline always has to be followed by
1311 // a comma, and then we can continue.
1312 if (!Text.consume_front(","))
1313 return None;
1314 }
1315
1316 if (PipelineStack.size() > 1)
1317 // Unbalanced paretheses.
1318 return None;
1319
1320 assert(PipelineStack.back() == &ResultPipeline &&
1321 "Wrong pipeline at the bottom of the stack!");
1322 return {std::move(ResultPipeline)};
1323}
1324
1325bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1326 const PipelineElement &E, bool VerifyEachPass,
1327 bool DebugLogging) {
1328 auto &Name = E.Name;
1329 auto &InnerPipeline = E.InnerPipeline;
1330
1331 // First handle complex passes like the pass managers which carry pipelines.
1332 if (!InnerPipeline.empty()) {
1333 if (Name == "module") {
1334 ModulePassManager NestedMPM(DebugLogging);
1335 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1336 DebugLogging))
1337 return false;
1338 MPM.addPass(std::move(NestedMPM));
1339 return true;
1340 }
1341 if (Name == "cgscc") {
1342 CGSCCPassManager CGPM(DebugLogging);
1343 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1344 DebugLogging))
1345 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001346 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001347 return true;
1348 }
1349 if (Name == "function") {
1350 FunctionPassManager FPM(DebugLogging);
1351 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1352 DebugLogging))
1353 return false;
1354 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1355 return true;
1356 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001357 if (auto Count = parseRepeatPassName(Name)) {
1358 ModulePassManager NestedMPM(DebugLogging);
1359 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1360 DebugLogging))
1361 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001362 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001363 return true;
1364 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001365
1366 for (auto &C : ModulePipelineParsingCallbacks)
1367 if (C(Name, MPM, InnerPipeline))
1368 return true;
1369
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001370 // Normal passes can't have pipelines.
1371 return false;
1372 }
1373
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001374 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001375 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001376 SmallVector<StringRef, 3> Matches;
1377 if (!DefaultAliasRegex.match(Name, &Matches))
1378 return false;
1379 assert(Matches.size() == 3 && "Must capture two matched strings!");
1380
Jordan Rosef85a95f2016-07-25 18:34:51 +00001381 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001382 .Case("O0", O0)
1383 .Case("O1", O1)
1384 .Case("O2", O2)
1385 .Case("O3", O3)
1386 .Case("Os", Os)
1387 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001388 if (L == O0)
1389 // At O0 we do nothing at all!
1390 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001391
1392 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001393 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001394 } else if (Matches[1] == "thinlto-pre-link") {
1395 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1396 } else if (Matches[1] == "thinlto") {
1397 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001398 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001399 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001400 } else {
1401 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001402 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001403 }
1404 return true;
1405 }
1406
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001407 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001408#define MODULE_PASS(NAME, CREATE_PASS) \
1409 if (Name == NAME) { \
1410 MPM.addPass(CREATE_PASS); \
1411 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001412 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001413#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1414 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001415 MPM.addPass( \
1416 RequireAnalysisPass< \
1417 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001418 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001419 } \
1420 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001421 MPM.addPass(InvalidateAnalysisPass< \
1422 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001423 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001424 }
1425#include "PassRegistry.def"
1426
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001427 for (auto &C : ModulePipelineParsingCallbacks)
1428 if (C(Name, MPM, InnerPipeline))
1429 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001430 return false;
1431}
1432
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001433bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1434 const PipelineElement &E, bool VerifyEachPass,
1435 bool DebugLogging) {
1436 auto &Name = E.Name;
1437 auto &InnerPipeline = E.InnerPipeline;
1438
1439 // First handle complex passes like the pass managers which carry pipelines.
1440 if (!InnerPipeline.empty()) {
1441 if (Name == "cgscc") {
1442 CGSCCPassManager NestedCGPM(DebugLogging);
1443 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1444 DebugLogging))
1445 return false;
1446 // Add the nested pass manager with the appropriate adaptor.
1447 CGPM.addPass(std::move(NestedCGPM));
1448 return true;
1449 }
1450 if (Name == "function") {
1451 FunctionPassManager FPM(DebugLogging);
1452 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1453 DebugLogging))
1454 return false;
1455 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001456 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001457 return true;
1458 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001459 if (auto Count = parseRepeatPassName(Name)) {
1460 CGSCCPassManager NestedCGPM(DebugLogging);
1461 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1462 DebugLogging))
1463 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001464 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001465 return true;
1466 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001467 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1468 CGSCCPassManager NestedCGPM(DebugLogging);
1469 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1470 DebugLogging))
1471 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001472 CGPM.addPass(
1473 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001474 return true;
1475 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001476
1477 for (auto &C : CGSCCPipelineParsingCallbacks)
1478 if (C(Name, CGPM, InnerPipeline))
1479 return true;
1480
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001481 // Normal passes can't have pipelines.
1482 return false;
1483 }
1484
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001485// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001486#define CGSCC_PASS(NAME, CREATE_PASS) \
1487 if (Name == NAME) { \
1488 CGPM.addPass(CREATE_PASS); \
1489 return true; \
1490 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001491#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1492 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001493 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001494 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001495 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1496 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001497 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001498 } \
1499 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001500 CGPM.addPass(InvalidateAnalysisPass< \
1501 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001502 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001503 }
1504#include "PassRegistry.def"
1505
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001506 for (auto &C : CGSCCPipelineParsingCallbacks)
1507 if (C(Name, CGPM, InnerPipeline))
1508 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001509 return false;
1510}
1511
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001512bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1513 const PipelineElement &E,
1514 bool VerifyEachPass, bool DebugLogging) {
1515 auto &Name = E.Name;
1516 auto &InnerPipeline = E.InnerPipeline;
1517
1518 // First handle complex passes like the pass managers which carry pipelines.
1519 if (!InnerPipeline.empty()) {
1520 if (Name == "function") {
1521 FunctionPassManager NestedFPM(DebugLogging);
1522 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1523 DebugLogging))
1524 return false;
1525 // Add the nested pass manager with the appropriate adaptor.
1526 FPM.addPass(std::move(NestedFPM));
1527 return true;
1528 }
1529 if (Name == "loop") {
1530 LoopPassManager LPM(DebugLogging);
1531 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1532 DebugLogging))
1533 return false;
1534 // Add the nested pass manager with the appropriate adaptor.
1535 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1536 return true;
1537 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001538 if (auto Count = parseRepeatPassName(Name)) {
1539 FunctionPassManager NestedFPM(DebugLogging);
1540 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1541 DebugLogging))
1542 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001543 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001544 return true;
1545 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001546
1547 for (auto &C : FunctionPipelineParsingCallbacks)
1548 if (C(Name, FPM, InnerPipeline))
1549 return true;
1550
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001551 // Normal passes can't have pipelines.
1552 return false;
1553 }
1554
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001555// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001556#define FUNCTION_PASS(NAME, CREATE_PASS) \
1557 if (Name == NAME) { \
1558 FPM.addPass(CREATE_PASS); \
1559 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001560 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001561#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1562 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001563 FPM.addPass( \
1564 RequireAnalysisPass< \
1565 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001566 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001567 } \
1568 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001569 FPM.addPass(InvalidateAnalysisPass< \
1570 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001571 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001572 }
1573#include "PassRegistry.def"
1574
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001575 for (auto &C : FunctionPipelineParsingCallbacks)
1576 if (C(Name, FPM, InnerPipeline))
1577 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001578 return false;
1579}
1580
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001581bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001582 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001583 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001584 auto &InnerPipeline = E.InnerPipeline;
1585
1586 // First handle complex passes like the pass managers which carry pipelines.
1587 if (!InnerPipeline.empty()) {
1588 if (Name == "loop") {
1589 LoopPassManager NestedLPM(DebugLogging);
1590 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1591 DebugLogging))
1592 return false;
1593 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001594 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001595 return true;
1596 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001597 if (auto Count = parseRepeatPassName(Name)) {
1598 LoopPassManager NestedLPM(DebugLogging);
1599 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1600 DebugLogging))
1601 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001602 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001603 return true;
1604 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001605
1606 for (auto &C : LoopPipelineParsingCallbacks)
1607 if (C(Name, LPM, InnerPipeline))
1608 return true;
1609
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001610 // Normal passes can't have pipelines.
1611 return false;
1612 }
1613
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001614// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001615#define LOOP_PASS(NAME, CREATE_PASS) \
1616 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001617 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001618 return true; \
1619 }
1620#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1621 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001622 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001623 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1624 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1625 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001626 return true; \
1627 } \
1628 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001629 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001630 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001631 return true; \
1632 }
1633#include "PassRegistry.def"
1634
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001635 for (auto &C : LoopPipelineParsingCallbacks)
1636 if (C(Name, LPM, InnerPipeline))
1637 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001638 return false;
1639}
1640
Chandler Carruthedf59962016-02-18 09:45:17 +00001641bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001642#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1643 if (Name == NAME) { \
1644 AA.registerModuleAnalysis< \
1645 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1646 return true; \
1647 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001648#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1649 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001650 AA.registerFunctionAnalysis< \
1651 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001652 return true; \
1653 }
1654#include "PassRegistry.def"
1655
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001656 for (auto &C : AAParsingCallbacks)
1657 if (C(Name, AA))
1658 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001659 return false;
1660}
1661
Justin Bognereecc3c82016-02-25 07:23:08 +00001662bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001663 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001664 bool VerifyEachPass,
1665 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001666 for (const auto &Element : Pipeline) {
1667 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1668 return false;
1669 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001670 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001671 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001672}
1673
Chandler Carruth1ff77242015-03-07 09:02:36 +00001674bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001675 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001676 bool VerifyEachPass,
1677 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001678 for (const auto &Element : Pipeline) {
1679 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1680 return false;
1681 if (VerifyEachPass)
1682 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001683 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001684 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001685}
1686
Chandler Carruth1ff77242015-03-07 09:02:36 +00001687bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001688 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001689 bool VerifyEachPass,
1690 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001691 for (const auto &Element : Pipeline) {
1692 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1693 return false;
1694 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001695 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001696 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001697}
1698
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001699void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1700 FunctionAnalysisManager &FAM,
1701 CGSCCAnalysisManager &CGAM,
1702 ModuleAnalysisManager &MAM) {
1703 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1704 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001705 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1706 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1707 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1708 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1709 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1710}
1711
Chandler Carruth1ff77242015-03-07 09:02:36 +00001712bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001713 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001714 bool VerifyEachPass,
1715 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001716 for (const auto &Element : Pipeline) {
1717 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1718 return false;
1719 if (VerifyEachPass)
1720 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001721 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001722 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001723}
1724
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001725// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001726// FIXME: Should this routine accept a TargetMachine or require the caller to
1727// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001728bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1729 StringRef PipelineText, bool VerifyEachPass,
1730 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001731 auto Pipeline = parsePipelineText(PipelineText);
1732 if (!Pipeline || Pipeline->empty())
1733 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001734
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001735 // If the first name isn't at the module layer, wrap the pipeline up
1736 // automatically.
1737 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001738
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001739 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1740 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001741 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001742 } else if (isFunctionPassName(FirstName,
1743 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001744 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001745 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001746 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001747 } else {
1748 for (auto &C : TopLevelPipelineParsingCallbacks)
1749 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1750 return true;
1751
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001752 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001753 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001754 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001755 }
1756
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001757 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001758}
Chandler Carruthedf59962016-02-18 09:45:17 +00001759
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001760// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1761bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1762 StringRef PipelineText, bool VerifyEachPass,
1763 bool DebugLogging) {
1764 auto Pipeline = parsePipelineText(PipelineText);
1765 if (!Pipeline || Pipeline->empty())
1766 return false;
1767
1768 StringRef FirstName = Pipeline->front().Name;
1769 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1770 return false;
1771
1772 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1773}
1774
1775// Primary pass pipeline description parsing routine for a \c
1776// FunctionPassManager
1777bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1778 StringRef PipelineText, bool VerifyEachPass,
1779 bool DebugLogging) {
1780 auto Pipeline = parsePipelineText(PipelineText);
1781 if (!Pipeline || Pipeline->empty())
1782 return false;
1783
1784 StringRef FirstName = Pipeline->front().Name;
1785 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1786 return false;
1787
1788 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1789 DebugLogging);
1790}
1791
1792// Primary pass pipeline description parsing routine for a \c LoopPassManager
1793bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1794 StringRef PipelineText, bool VerifyEachPass,
1795 bool DebugLogging) {
1796 auto Pipeline = parsePipelineText(PipelineText);
1797 if (!Pipeline || Pipeline->empty())
1798 return false;
1799
1800 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1801}
1802
Chandler Carruthedf59962016-02-18 09:45:17 +00001803bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001804 // If the pipeline just consists of the word 'default' just replace the AA
1805 // manager with our default one.
1806 if (PipelineText == "default") {
1807 AA = buildDefaultAAPipeline();
1808 return true;
1809 }
1810
Chandler Carruthedf59962016-02-18 09:45:17 +00001811 while (!PipelineText.empty()) {
1812 StringRef Name;
1813 std::tie(Name, PipelineText) = PipelineText.split(',');
1814 if (!parseAAPassName(AA, Name))
1815 return false;
1816 }
1817
1818 return true;
1819}