blob: 7235a885184473efd183673205a663ed7bdc4085 [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"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000135#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000136#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000137#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000138#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000139#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000140#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000141#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000142#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000143#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000144#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000145#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000146#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000147#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000148#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000149#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000150#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000151
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000152#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000153
154using namespace llvm;
155
Chandler Carruth719ffe12017-02-12 05:38:04 +0000156static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
157 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000158static cl::opt<bool>
159 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
160 cl::Hidden, cl::ZeroOrMore,
161 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000162
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000163static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000164 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000165 cl::Hidden, cl::ZeroOrMore,
166 cl::desc("Run NewGVN instead of GVN"));
167
Geoff Berry3cca1da2017-06-10 15:20:03 +0000168static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000169 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
170 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000171
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000172static cl::opt<bool> EnableGVNHoist(
173 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
174 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
175
Davide Italianobe1b6a92017-06-03 23:18:29 +0000176static cl::opt<bool> EnableGVNSink(
177 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
178 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
179
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000180static Regex DefaultAliasRegex(
181 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000182
Chandler Carruthe3f50642016-12-22 06:59:15 +0000183static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
184 switch (Level) {
185 case PassBuilder::O0:
186 case PassBuilder::O1:
187 case PassBuilder::O2:
188 case PassBuilder::O3:
189 return false;
190
191 case PassBuilder::Os:
192 case PassBuilder::Oz:
193 return true;
194 }
195 llvm_unreachable("Invalid optimization level!");
196}
197
Chandler Carruth66445382014-01-11 08:16:35 +0000198namespace {
199
Chandler Carruthd8330982014-01-12 09:34:22 +0000200/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000201struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000202 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000203 return PreservedAnalyses::all();
204 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000205 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000206};
207
Chandler Carruth0b576b32015-01-06 02:50:06 +0000208/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000209class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
210 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000211 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000212
213public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000214 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000215 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000216 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000217};
218
Chandler Carruth572e3402014-04-21 11:12:00 +0000219/// \brief No-op CGSCC pass which does nothing.
220struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000221 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
222 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000223 return PreservedAnalyses::all();
224 }
225 static StringRef name() { return "NoOpCGSCCPass"; }
226};
227
Chandler Carruth0b576b32015-01-06 02:50:06 +0000228/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000229class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
230 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000231 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000232
233public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000234 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000235 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000236 return Result();
237 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000238 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000239};
240
Chandler Carruthd8330982014-01-12 09:34:22 +0000241/// \brief No-op function pass which does nothing.
242struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000243 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000244 return PreservedAnalyses::all();
245 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000246 static StringRef name() { return "NoOpFunctionPass"; }
247};
248
Chandler Carruth0b576b32015-01-06 02:50:06 +0000249/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000250class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
251 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000252 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000253
254public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000255 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000256 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000257 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000258};
259
Justin Bognereecc3c82016-02-25 07:23:08 +0000260/// \brief No-op loop pass which does nothing.
261struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000262 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
263 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000264 return PreservedAnalyses::all();
265 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000266 static StringRef name() { return "NoOpLoopPass"; }
267};
268
269/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000270class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
271 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000272 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000273
274public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000275 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000276 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
277 return Result();
278 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000279 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000280};
281
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000282AnalysisKey NoOpModuleAnalysis::Key;
283AnalysisKey NoOpCGSCCAnalysis::Key;
284AnalysisKey NoOpFunctionAnalysis::Key;
285AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000286
Chandler Carruth66445382014-01-11 08:16:35 +0000287} // End anonymous namespace.
288
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000289void PassBuilder::invokePeepholeEPCallbacks(
290 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
291 for (auto &C : PeepholeEPCallbacks)
292 C(FPM, Level);
293}
294
Chandler Carruth1ff77242015-03-07 09:02:36 +0000295void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000296#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000297 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000298#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000299
300 for (auto &C : ModuleAnalysisRegistrationCallbacks)
301 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000302}
303
Chandler Carruth1ff77242015-03-07 09:02:36 +0000304void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000305#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000306 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000307#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000308
309 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
310 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000311}
312
Chandler Carruth1ff77242015-03-07 09:02:36 +0000313void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000314#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000315 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000316#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000317
318 for (auto &C : FunctionAnalysisRegistrationCallbacks)
319 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000320}
321
Justin Bognereecc3c82016-02-25 07:23:08 +0000322void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000323#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000324 LAM.registerPass([&] { return CREATE_PASS; });
325#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000326
327 for (auto &C : LoopAnalysisRegistrationCallbacks)
328 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000329}
330
Chandler Carruthe3f50642016-12-22 06:59:15 +0000331FunctionPassManager
332PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000333 ThinLTOPhase Phase,
334 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000335 assert(Level != O0 && "Must request optimizations!");
336 FunctionPassManager FPM(DebugLogging);
337
338 // Form SSA out of local memory accesses after breaking apart aggregates into
339 // scalars.
340 FPM.addPass(SROA());
341
342 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000343 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000344
Davide Italianoc3688312017-06-01 23:08:14 +0000345 // Hoisting of scalars and load expressions.
346 if (EnableGVNHoist)
347 FPM.addPass(GVNHoistPass());
348
Davide Italianobe1b6a92017-06-03 23:18:29 +0000349 // Global value numbering based sinking.
350 if (EnableGVNSink) {
351 FPM.addPass(GVNSinkPass());
352 FPM.addPass(SimplifyCFGPass());
353 }
354
Chandler Carruthe3f50642016-12-22 06:59:15 +0000355 // Speculative execution if the target has divergent branches; otherwise nop.
356 FPM.addPass(SpeculativeExecutionPass());
357
358 // Optimize based on known information about branches, and cleanup afterward.
359 FPM.addPass(JumpThreadingPass());
360 FPM.addPass(CorrelatedValuePropagationPass());
361 FPM.addPass(SimplifyCFGPass());
362 FPM.addPass(InstCombinePass());
363
364 if (!isOptimizingForSize(Level))
365 FPM.addPass(LibCallsShrinkWrapPass());
366
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000367 invokePeepholeEPCallbacks(FPM, Level);
368
Rong Xue1f42452017-10-23 22:21:29 +0000369 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
370 // using the size value profile. Don't perform this when optimizing for size.
371 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
372 !isOptimizingForSize(Level))
373 FPM.addPass(PGOMemOPSizeOpt());
374
Chandler Carruthe3f50642016-12-22 06:59:15 +0000375 FPM.addPass(TailCallElimPass());
376 FPM.addPass(SimplifyCFGPass());
377
378 // Form canonically associated expression trees, and simplify the trees using
379 // basic mathematical properties. For example, this will form (nearly)
380 // minimal multiplication trees.
381 FPM.addPass(ReassociatePass());
382
383 // Add the primary loop simplification pipeline.
384 // FIXME: Currently this is split into two loop pass pipelines because we run
385 // some function passes in between them. These can and should be replaced by
386 // loop pass equivalenst but those aren't ready yet. Specifically,
387 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
388 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
389 // the other we have is `LoopInstSimplify`.
390 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
391
392 // Rotate Loop - disable header duplication at -Oz
393 LPM1.addPass(LoopRotatePass(Level != Oz));
394 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000395 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000396 LPM2.addPass(IndVarSimplifyPass());
397 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000398
399 for (auto &C : LateLoopOptimizationsEPCallbacks)
400 C(LPM2, Level);
401
Chandler Carruth79b733b2017-01-26 23:21:17 +0000402 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000403 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000404 // because it changes IR to makes profile annotation in back compile
405 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000406 if (Phase != ThinLTOPhase::PreLink ||
407 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000408 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000409
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000410 for (auto &C : LoopOptimizerEndEPCallbacks)
411 C(LPM2, Level);
412
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000413 // We provide the opt remark emitter pass for LICM to use. We only need to do
414 // this once as it is immutable.
415 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000416 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
417 FPM.addPass(SimplifyCFGPass());
418 FPM.addPass(InstCombinePass());
419 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
420
421 // Eliminate redundancies.
422 if (Level != O1) {
423 // These passes add substantial compile time so skip them at O1.
424 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000425 if (RunNewGVN)
426 FPM.addPass(NewGVNPass());
427 else
428 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000429 }
430
431 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
432 FPM.addPass(MemCpyOptPass());
433
434 // Sparse conditional constant propagation.
435 // FIXME: It isn't clear why we do this *after* loop passes rather than
436 // before...
437 FPM.addPass(SCCPPass());
438
439 // Delete dead bit computations (instcombine runs after to fold away the dead
440 // computations, and then ADCE will run later to exploit any new DCE
441 // opportunities that creates).
442 FPM.addPass(BDCEPass());
443
444 // Run instcombine after redundancy and dead bit elimination to exploit
445 // opportunities opened up by them.
446 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000447 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000448
449 // Re-consider control flow based optimizations after redundancy elimination,
450 // redo DCE, etc.
451 FPM.addPass(JumpThreadingPass());
452 FPM.addPass(CorrelatedValuePropagationPass());
453 FPM.addPass(DSEPass());
454 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
455
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000456 for (auto &C : ScalarOptimizerLateEPCallbacks)
457 C(FPM, Level);
458
Chandler Carruthe3f50642016-12-22 06:59:15 +0000459 // Finally, do an expensive DCE pass to catch all the dead code exposed by
460 // the simplifications and basic cleanup after all the simplifications.
461 FPM.addPass(ADCEPass());
462 FPM.addPass(SimplifyCFGPass());
463 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000464 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000465
466 return FPM;
467}
468
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000469void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
470 PassBuilder::OptimizationLevel Level,
471 bool RunProfileGen,
472 std::string ProfileGenFile,
473 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000474 // Generally running simplification passes and the inliner with an high
475 // threshold results in smaller executables, but there may be cases where
476 // the size grows, so let's be conservative here and skip this simplification
477 // at -Os/Oz.
478 if (!isOptimizingForSize(Level)) {
479 InlineParams IP;
480
481 // In the old pass manager, this is a cl::opt. Should still this be one?
482 IP.DefaultThreshold = 75;
483
484 // FIXME: The hint threshold has the same value used by the regular inliner.
485 // This should probably be lowered after performance testing.
486 // FIXME: this comment is cargo culted from the old pass manager, revisit).
487 IP.HintThreshold = 325;
488
489 CGSCCPassManager CGPipeline(DebugLogging);
490
491 CGPipeline.addPass(InlinerPass(IP));
492
493 FunctionPassManager FPM;
494 FPM.addPass(SROA());
495 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
496 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
497 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000498 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000499
Davide Italiano513dfaa2017-02-13 15:26:22 +0000500 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
501
502 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
503 }
504
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000505 // Delete anything that is now dead to make sure that we don't instrument
506 // dead code. Instrumentation can end up keeping dead code around and
507 // dramatically increase code size.
508 MPM.addPass(GlobalDCEPass());
509
Davide Italiano513dfaa2017-02-13 15:26:22 +0000510 if (RunProfileGen) {
511 MPM.addPass(PGOInstrumentationGen());
512
Xinliang David Lib67530e2017-06-25 00:26:43 +0000513 FunctionPassManager FPM;
514 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
515 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
516
Davide Italiano513dfaa2017-02-13 15:26:22 +0000517 // Add the profile lowering pass.
518 InstrProfOptions Options;
519 if (!ProfileGenFile.empty())
520 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000521 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000522 MPM.addPass(InstrProfiling(Options));
523 }
524
525 if (!ProfileUseFile.empty())
526 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
527}
528
Easwaran Raman8249fac2017-06-28 13:33:49 +0000529static InlineParams
530getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
531 auto O3 = PassBuilder::O3;
532 unsigned OptLevel = Level > O3 ? 2 : Level;
533 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
534 return getInlineParams(OptLevel, SizeLevel);
535}
536
Chandler Carruthe3f50642016-12-22 06:59:15 +0000537ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000538PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000539 ThinLTOPhase Phase,
540 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000541 ModulePassManager MPM(DebugLogging);
542
Chandler Carruthe3f50642016-12-22 06:59:15 +0000543 // Do basic inference of function attributes from known properties of system
544 // libraries and other oracles.
545 MPM.addPass(InferFunctionAttrsPass());
546
547 // Create an early function pass manager to cleanup the output of the
548 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000549 FunctionPassManager EarlyFPM(DebugLogging);
550 EarlyFPM.addPass(SimplifyCFGPass());
551 EarlyFPM.addPass(SROA());
552 EarlyFPM.addPass(EarlyCSEPass());
553 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000554 if (Level == O3)
555 EarlyFPM.addPass(CallSiteSplittingPass());
556
Dehao Chen08f88312017-08-07 20:23:20 +0000557 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
558 // to convert bitcast to direct calls so that they can be inlined during the
559 // profile annotation prepration step.
560 // More details about SamplePGO design can be found in:
561 // https://research.google.com/pubs/pub45290.html
562 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
563 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
564 Phase == ThinLTOPhase::PostLink)
565 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000566 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000567
Dehao Chen08f88312017-08-07 20:23:20 +0000568 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
569 // Annotate sample profile right after early FPM to ensure freshness of
570 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000571 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
572 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000573 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
574 // for the profile annotation to be accurate in the ThinLTO backend.
575 if (Phase != ThinLTOPhase::PreLink)
576 // We perform early indirect call promotion here, before globalopt.
577 // This is important for the ThinLTO backend phase because otherwise
578 // imported available_externally functions look unreferenced and are
579 // removed.
580 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
581 true));
582 }
583
Chandler Carruthe3f50642016-12-22 06:59:15 +0000584 // Interprocedural constant propagation now that basic cleanup has occured
585 // and prior to optimizing globals.
586 // FIXME: This position in the pipeline hasn't been carefully considered in
587 // years, it should be re-analyzed.
588 MPM.addPass(IPSCCPPass());
589
Matthew Simpsoncb585582017-10-25 13:40:08 +0000590 // Attach metadata to indirect call sites indicating the set of functions
591 // they may target at run-time. This should follow IPSCCP.
592 MPM.addPass(CalledValuePropagationPass());
593
Chandler Carruthe3f50642016-12-22 06:59:15 +0000594 // Optimize globals to try and fold them into constants.
595 MPM.addPass(GlobalOptPass());
596
597 // Promote any localized globals to SSA registers.
598 // FIXME: Should this instead by a run of SROA?
599 // FIXME: We should probably run instcombine and simplify-cfg afterward to
600 // delete control flows that are dead once globals have been folded to
601 // constants.
602 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
603
604 // Remove any dead arguments exposed by cleanups and constand folding
605 // globals.
606 MPM.addPass(DeadArgumentEliminationPass());
607
608 // Create a small function pass pipeline to cleanup after all the global
609 // optimizations.
610 FunctionPassManager GlobalCleanupPM(DebugLogging);
611 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000612 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
613
Chandler Carruthe3f50642016-12-22 06:59:15 +0000614 GlobalCleanupPM.addPass(SimplifyCFGPass());
615 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
616
Dehao Chen89d32262017-08-02 01:28:31 +0000617 // Add all the requested passes for instrumentation PGO, if requested.
618 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
619 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
620 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
621 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
622 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000623 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000624
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000625 // Require the GlobalsAA analysis for the module so we can query it within
626 // the CGSCC pipeline.
627 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000628
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000629 // Require the ProfileSummaryAnalysis for the module so we can query it within
630 // the inliner pass.
631 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
632
Chandler Carruthe3f50642016-12-22 06:59:15 +0000633 // Now begin the main postorder CGSCC pipeline.
634 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
635 // manager and trying to emulate its precise behavior. Much of this doesn't
636 // make a lot of sense and we should revisit the core CGSCC structure.
637 CGSCCPassManager MainCGPipeline(DebugLogging);
638
639 // Note: historically, the PruneEH pass was run first to deduce nounwind and
640 // generally clean up exception handling overhead. It isn't clear this is
641 // valuable as the inliner doesn't currently care whether it is inlining an
642 // invoke or a call.
643
644 // Run the inliner first. The theory is that we are walking bottom-up and so
645 // the callees have already been fully optimized, and we want to inline them
646 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000647 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000648 // because it makes profile annotation in the backend inaccurate.
649 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000650 if (Phase == ThinLTOPhase::PreLink &&
651 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000652 IP.HotCallSiteThreshold = 0;
653 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000654
655 // Now deduce any function attributes based in the current code.
656 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
657
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000658 // When at O3 add argument promotion to the pass pipeline.
659 // FIXME: It isn't at all clear why this should be limited to O3.
660 if (Level == O3)
661 MainCGPipeline.addPass(ArgumentPromotionPass());
662
Chandler Carruthe3f50642016-12-22 06:59:15 +0000663 // Lastly, add the core function simplification pipeline nested inside the
664 // CGSCC walk.
665 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000666 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000667
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000668 for (auto &C : CGSCCOptimizerLateEPCallbacks)
669 C(MainCGPipeline, Level);
670
Chandler Carruth719ffe12017-02-12 05:38:04 +0000671 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
672 // to detect when we devirtualize indirect calls and iterate the SCC passes
673 // in that case to try and catch knock-on inlining or function attrs
674 // opportunities. Then we add it to the module pipeline by walking the SCCs
675 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000677 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000678 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000679
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000680 return MPM;
681}
682
683ModulePassManager
684PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
685 bool DebugLogging) {
686 ModulePassManager MPM(DebugLogging);
687
688 // Optimize globals now that the module is fully simplified.
689 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000690 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000691
Xinliang David Li126157c2017-05-22 16:41:57 +0000692 // Run partial inlining pass to partially inline functions that have
693 // large bodies.
694 if (RunPartialInlining)
695 MPM.addPass(PartialInlinerPass());
696
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000697 // Remove avail extern fns and globals definitions since we aren't compiling
698 // an object file for later LTO. For LTO we want to preserve these so they
699 // are eligible for inlining at link-time. Note if they are unreferenced they
700 // will be removed by GlobalDCE later, so this only impacts referenced
701 // available externally globals. Eventually they will be suppressed during
702 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
703 // may make globals referenced by available external functions dead and saves
704 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000705 MPM.addPass(EliminateAvailableExternallyPass());
706
707 // Do RPO function attribute inference across the module to forward-propagate
708 // attributes where applicable.
709 // FIXME: Is this really an optimization rather than a canonicalization?
710 MPM.addPass(ReversePostOrderFunctionAttrsPass());
711
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000712 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000713 // useful as the above will have inlined, DCE'ed, and function-attr
714 // propagated everything. We should at this point have a reasonably minimal
715 // and richly annotated call graph. By computing aliasing and mod/ref
716 // information for all local globals here, the late loop passes and notably
717 // the vectorizer will be able to use them to help recognize vectorizable
718 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000719 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000720
721 FunctionPassManager OptimizePM(DebugLogging);
722 OptimizePM.addPass(Float2IntPass());
723 // FIXME: We need to run some loop optimizations to re-rotate loops after
724 // simplify-cfg and others undo their rotation.
725
726 // Optimize the loop execution. These passes operate on entire loop nests
727 // rather than on each loop in an inside-out manner, and so they are actually
728 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000729
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000730 for (auto &C : VectorizerStartEPCallbacks)
731 C(OptimizePM, Level);
732
Chandler Carrutha95ff382017-01-27 00:50:21 +0000733 // First rotate loops that may have been un-rotated by prior passes.
734 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
735
736 // Distribute loops to allow partial vectorization. I.e. isolate dependences
737 // into separate loop that would otherwise inhibit vectorization. This is
738 // currently only performed for loops marked with the metadata
739 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000740 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000741
742 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000743 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000744
Chandler Carruthbaabda92017-01-27 01:32:26 +0000745 // Eliminate loads by forwarding stores from the previous iteration to loads
746 // of the current iteration.
747 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000748
749 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000750 OptimizePM.addPass(InstCombinePass());
751
Chandler Carrutha95ff382017-01-27 00:50:21 +0000752
753 // Now that we've formed fast to execute loop structures, we do further
754 // optimizations. These are run afterward as they might block doing complex
755 // analyses and transforms such as what are needed for loop vectorization.
756
Chandler Carruthe3f50642016-12-22 06:59:15 +0000757 // Optimize parallel scalar instruction chains into SIMD instructions.
758 OptimizePM.addPass(SLPVectorizerPass());
759
Sanjay Patel3e298902017-11-15 16:33:11 +0000760 // Cleanup after all of the vectorizers. Simplification passes like CVP and
761 // GVN, loop transforms, and others have already run, so it's now better to
762 // convert to more optimized IR using more aggressive simplify CFG options.
763 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
764 forwardSwitchCondToPhi(true).
765 convertSwitchToLookupTable(true).
766 needCanonicalLoops(false)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000767 OptimizePM.addPass(InstCombinePass());
768
769 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000770 // execution resources of an out-of-order processor. We also then need to
771 // clean up redundancies and loop invariant code.
772 // FIXME: It would be really good to use a loop-integrated instruction
773 // combiner for cleanup here so that the unrolling and LICM can be pipelined
774 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000775 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000776 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000777 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000778 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000779
780 // Now that we've vectorized and unrolled loops, we may have more refined
781 // alignment information, try to re-derive it here.
782 OptimizePM.addPass(AlignmentFromAssumptionsPass());
783
Chandler Carrutha95ff382017-01-27 00:50:21 +0000784 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
785 // canonicalization pass that enables other optimizations. As a result,
786 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
787 // result too early.
788 OptimizePM.addPass(LoopSinkPass());
789
790 // And finally clean up LCSSA form before generating code.
791 OptimizePM.addPass(InstSimplifierPass());
792
Sanjay Patel6fd43912017-09-09 13:38:18 +0000793 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
794 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
795 // flattening of blocks.
796 OptimizePM.addPass(DivRemPairsPass());
797
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000798 // LoopSink (and other loop passes since the last simplifyCFG) might have
799 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
800 OptimizePM.addPass(SimplifyCFGPass());
801
Chandler Carrutha95ff382017-01-27 00:50:21 +0000802 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000803 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
804
805 // Now we need to do some global optimization transforms.
806 // FIXME: It would seem like these should come first in the optimization
807 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
808 // ordering here.
809 MPM.addPass(GlobalDCEPass());
810 MPM.addPass(ConstantMergePass());
811
812 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000813}
814
Chandler Carruthe3f50642016-12-22 06:59:15 +0000815ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000816PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
817 bool DebugLogging) {
818 assert(Level != O0 && "Must request optimizations for the default pipeline!");
819
820 ModulePassManager MPM(DebugLogging);
821
822 // Force any function attributes we want the rest of the pipeline to observe.
823 MPM.addPass(ForceFunctionAttrsPass());
824
Dehao Chen08f88312017-08-07 20:23:20 +0000825 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000826 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
827
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000828 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000829 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
830 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000831
832 // Now add the optimization pipeline.
833 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
834
835 return MPM;
836}
837
838ModulePassManager
839PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
840 bool DebugLogging) {
841 assert(Level != O0 && "Must request optimizations for the default pipeline!");
842
843 ModulePassManager MPM(DebugLogging);
844
845 // Force any function attributes we want the rest of the pipeline to observe.
846 MPM.addPass(ForceFunctionAttrsPass());
847
Dehao Chen08f88312017-08-07 20:23:20 +0000848 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000849 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
850
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000851 // If we are planning to perform ThinLTO later, we don't bloat the code with
852 // unrolling/vectorization/... now. Just simplify the module as much as we
853 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000854 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
855 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000856
857 // Run partial inlining pass to partially inline functions that have
858 // large bodies.
859 // FIXME: It isn't clear whether this is really the right place to run this
860 // in ThinLTO. Because there is another canonicalization and simplification
861 // phase that will run after the thin link, running this here ends up with
862 // less information than will be available later and it may grow functions in
863 // ways that aren't beneficial.
864 if (RunPartialInlining)
865 MPM.addPass(PartialInlinerPass());
866
867 // Reduce the size of the IR as much as possible.
868 MPM.addPass(GlobalOptPass());
869
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000870 return MPM;
871}
872
873ModulePassManager
874PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
875 bool DebugLogging) {
876 // FIXME: The summary index is not hooked in the new pass manager yet.
877 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
878 // here.
879
880 ModulePassManager MPM(DebugLogging);
881
882 // Force any function attributes we want the rest of the pipeline to observe.
883 MPM.addPass(ForceFunctionAttrsPass());
884
885 // During the ThinLTO backend phase we perform early indirect call promotion
886 // here, before globalopt. Otherwise imported available_externally functions
887 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000888 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
889 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000890 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000891 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
892 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000893
894 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000895 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
896 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000897
898 // Now add the optimization pipeline.
899 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
900
901 return MPM;
902}
903
904ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000905PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
906 bool DebugLogging) {
907 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000908 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000909 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000910}
911
Chandler Carruthe3f50642016-12-22 06:59:15 +0000912ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
913 bool DebugLogging) {
914 assert(Level != O0 && "Must request optimizations for the default pipeline!");
915 ModulePassManager MPM(DebugLogging);
916
Davide Italiano089a9122017-01-24 00:57:39 +0000917 // Remove unused virtual tables to improve the quality of code generated by
918 // whole-program devirtualization and bitset lowering.
919 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000920
Davide Italiano089a9122017-01-24 00:57:39 +0000921 // Force any function attributes we want the rest of the pipeline to observe.
922 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000923
Davide Italiano089a9122017-01-24 00:57:39 +0000924 // Do basic inference of function attributes from known properties of system
925 // libraries and other oracles.
926 MPM.addPass(InferFunctionAttrsPass());
927
928 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000929 FunctionPassManager EarlyFPM(DebugLogging);
930 EarlyFPM.addPass(CallSiteSplittingPass());
931 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
932
Davide Italiano089a9122017-01-24 00:57:39 +0000933 // Indirect call promotion. This should promote all the targets that are
934 // left by the earlier promotion pass that promotes intra-module targets.
935 // This two-step promotion is to save the compile time. For LTO, it should
936 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000937 MPM.addPass(PGOIndirectCallPromotion(
938 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000939 // Propagate constants at call sites into the functions they call. This
940 // opens opportunities for globalopt (and inlining) by substituting function
941 // pointers passed as arguments to direct uses of functions.
942 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000943
944 // Attach metadata to indirect call sites indicating the set of functions
945 // they may target at run-time. This should follow IPSCCP.
946 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000947 }
948
949 // Now deduce any function attributes based in the current code.
950 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
951 PostOrderFunctionAttrsPass()));
952
953 // Do RPO function attribute inference across the module to forward-propagate
954 // attributes where applicable.
955 // FIXME: Is this really an optimization rather than a canonicalization?
956 MPM.addPass(ReversePostOrderFunctionAttrsPass());
957
958 // Use inragne annotations on GEP indices to split globals where beneficial.
959 MPM.addPass(GlobalSplitPass());
960
961 // Run whole program optimization of virtual call when the list of callees
962 // is fixed.
963 MPM.addPass(WholeProgramDevirtPass());
964
965 // Stop here at -O1.
966 if (Level == 1)
967 return MPM;
968
969 // Optimize globals to try and fold them into constants.
970 MPM.addPass(GlobalOptPass());
971
972 // Promote any localized globals to SSA registers.
973 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
974
975 // Linking modules together can lead to duplicate global constant, only
976 // keep one copy of each constant.
977 MPM.addPass(ConstantMergePass());
978
979 // Remove unused arguments from functions.
980 MPM.addPass(DeadArgumentEliminationPass());
981
982 // Reduce the code after globalopt and ipsccp. Both can open up significant
983 // simplification opportunities, and both can propagate functions through
984 // function pointers. When this happens, we often have to resolve varargs
985 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000986 FunctionPassManager PeepholeFPM(DebugLogging);
987 PeepholeFPM.addPass(InstCombinePass());
988 invokePeepholeEPCallbacks(PeepholeFPM, Level);
989
990 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000991
992 // Note: historically, the PruneEH pass was run first to deduce nounwind and
993 // generally clean up exception handling overhead. It isn't clear this is
994 // valuable as the inliner doesn't currently care whether it is inlining an
995 // invoke or a call.
996 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000997 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
998 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000999
1000 // Optimize globals again after we ran the inliner.
1001 MPM.addPass(GlobalOptPass());
1002
1003 // Garbage collect dead functions.
1004 // FIXME: Add ArgumentPromotion pass after once it's ported.
1005 MPM.addPass(GlobalDCEPass());
1006
1007 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001008 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001009 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001010 invokePeepholeEPCallbacks(FPM, Level);
1011
Davide Italiano089a9122017-01-24 00:57:39 +00001012 FPM.addPass(JumpThreadingPass());
1013
1014 // Break up allocas
1015 FPM.addPass(SROA());
1016
1017 // Run a few AA driver optimizations here and now to cleanup the code.
1018 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1019
1020 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1021 PostOrderFunctionAttrsPass()));
1022 // FIXME: here we run IP alias analysis in the legacy PM.
1023
1024 FunctionPassManager MainFPM;
1025
1026 // FIXME: once we fix LoopPass Manager, add LICM here.
1027 // FIXME: once we provide support for enabling MLSM, add it here.
1028 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001029 if (RunNewGVN)
1030 MainFPM.addPass(NewGVNPass());
1031 else
1032 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001033
1034 // Remove dead memcpy()'s.
1035 MainFPM.addPass(MemCpyOptPass());
1036
1037 // Nuke dead stores.
1038 MainFPM.addPass(DSEPass());
1039
1040 // FIXME: at this point, we run a bunch of loop passes:
1041 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1042 // loopVectorize. Enable them once the remaining issue with LPM
1043 // are sorted out.
1044
1045 MainFPM.addPass(InstCombinePass());
1046 MainFPM.addPass(SimplifyCFGPass());
1047 MainFPM.addPass(SCCPPass());
1048 MainFPM.addPass(InstCombinePass());
1049 MainFPM.addPass(BDCEPass());
1050
1051 // FIXME: We may want to run SLPVectorizer here.
1052 // After vectorization, assume intrinsics may tell us more
1053 // about pointer alignments.
1054#if 0
1055 MainFPM.add(AlignmentFromAssumptionsPass());
1056#endif
1057
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001058 // FIXME: Conditionally run LoadCombine here, after it's ported
1059 // (in case we still have this pass, given its questionable usefulness).
1060
Davide Italiano089a9122017-01-24 00:57:39 +00001061 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001062 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001063 MainFPM.addPass(JumpThreadingPass());
1064 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1065
1066 // Create a function that performs CFI checks for cross-DSO calls with
1067 // targets in the current module.
1068 MPM.addPass(CrossDSOCFIPass());
1069
1070 // Lower type metadata and the type.test intrinsic. This pass supports
1071 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1072 // to be run at link time if CFI is enabled. This pass does nothing if
1073 // CFI is disabled.
1074 // Enable once we add support for the summary in the new PM.
1075#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001076 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1077 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001078 Summary));
1079#endif
1080
1081 // Add late LTO optimization passes.
1082 // Delete basic blocks, which optimization passes may have killed.
1083 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1084
1085 // Drop bodies of available eternally objects to improve GlobalDCE.
1086 MPM.addPass(EliminateAvailableExternallyPass());
1087
1088 // Now that we have optimized the program, discard unreachable functions.
1089 MPM.addPass(GlobalDCEPass());
1090
1091 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001092 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001093}
1094
Chandler Carruth060ad612016-12-23 20:38:19 +00001095AAManager PassBuilder::buildDefaultAAPipeline() {
1096 AAManager AA;
1097
1098 // The order in which these are registered determines their priority when
1099 // being queried.
1100
1101 // First we register the basic alias analysis that provides the majority of
1102 // per-function local AA logic. This is a stateless, on-demand local set of
1103 // AA techniques.
1104 AA.registerFunctionAnalysis<BasicAA>();
1105
1106 // Next we query fast, specialized alias analyses that wrap IR-embedded
1107 // information about aliasing.
1108 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1109 AA.registerFunctionAnalysis<TypeBasedAA>();
1110
1111 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001112 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1113 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001114 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001115 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001116
1117 return AA;
1118}
1119
Chandler Carruth241bf242016-08-03 07:44:48 +00001120static Optional<int> parseRepeatPassName(StringRef Name) {
1121 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1122 return None;
1123 int Count;
1124 if (Name.getAsInteger(0, Count) || Count <= 0)
1125 return None;
1126 return Count;
1127}
1128
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001129static Optional<int> parseDevirtPassName(StringRef Name) {
1130 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1131 return None;
1132 int Count;
1133 if (Name.getAsInteger(0, Count) || Count <= 0)
1134 return None;
1135 return Count;
1136}
1137
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001138/// Tests whether a pass name starts with a valid prefix for a default pipeline
1139/// alias.
1140static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1141 return Name.startswith("default") || Name.startswith("thinlto") ||
1142 Name.startswith("lto");
1143}
1144
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001145/// Tests whether registered callbacks will accept a given pass name.
1146///
1147/// When parsing a pipeline text, the type of the outermost pipeline may be
1148/// omitted, in which case the type is automatically determined from the first
1149/// pass name in the text. This may be a name that is handled through one of the
1150/// callbacks. We check this through the oridinary parsing callbacks by setting
1151/// up a dummy PassManager in order to not force the client to also handle this
1152/// type of query.
1153template <typename PassManagerT, typename CallbacksT>
1154static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1155 if (!Callbacks.empty()) {
1156 PassManagerT DummyPM;
1157 for (auto &CB : Callbacks)
1158 if (CB(Name, DummyPM, {}))
1159 return true;
1160 }
1161 return false;
1162}
1163
1164template <typename CallbacksT>
1165static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001166 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001167 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001168 return DefaultAliasRegex.match(Name);
1169
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001170 // Explicitly handle pass manager names.
1171 if (Name == "module")
1172 return true;
1173 if (Name == "cgscc")
1174 return true;
1175 if (Name == "function")
1176 return true;
1177
Chandler Carruth241bf242016-08-03 07:44:48 +00001178 // Explicitly handle custom-parsed pass names.
1179 if (parseRepeatPassName(Name))
1180 return true;
1181
Chandler Carruth74a8a222016-06-17 07:15:29 +00001182#define MODULE_PASS(NAME, CREATE_PASS) \
1183 if (Name == NAME) \
1184 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001185#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001186 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001187 return true;
1188#include "PassRegistry.def"
1189
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001190 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001191}
1192
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001193template <typename CallbacksT>
1194static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001195 // Explicitly handle pass manager names.
1196 if (Name == "cgscc")
1197 return true;
1198 if (Name == "function")
1199 return true;
1200
Chandler Carruth241bf242016-08-03 07:44:48 +00001201 // Explicitly handle custom-parsed pass names.
1202 if (parseRepeatPassName(Name))
1203 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001204 if (parseDevirtPassName(Name))
1205 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001206
Chandler Carruth74a8a222016-06-17 07:15:29 +00001207#define CGSCC_PASS(NAME, CREATE_PASS) \
1208 if (Name == NAME) \
1209 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001210#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001211 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001212 return true;
1213#include "PassRegistry.def"
1214
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001215 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001216}
1217
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001218template <typename CallbacksT>
1219static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001220 // Explicitly handle pass manager names.
1221 if (Name == "function")
1222 return true;
1223 if (Name == "loop")
1224 return true;
1225
Chandler Carruth241bf242016-08-03 07:44:48 +00001226 // Explicitly handle custom-parsed pass names.
1227 if (parseRepeatPassName(Name))
1228 return true;
1229
Chandler Carruth74a8a222016-06-17 07:15:29 +00001230#define FUNCTION_PASS(NAME, CREATE_PASS) \
1231 if (Name == NAME) \
1232 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001233#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001234 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001235 return true;
1236#include "PassRegistry.def"
1237
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001238 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001239}
1240
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001241template <typename CallbacksT>
1242static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001243 // Explicitly handle pass manager names.
1244 if (Name == "loop")
1245 return true;
1246
Chandler Carruth241bf242016-08-03 07:44:48 +00001247 // Explicitly handle custom-parsed pass names.
1248 if (parseRepeatPassName(Name))
1249 return true;
1250
Chandler Carruth74a8a222016-06-17 07:15:29 +00001251#define LOOP_PASS(NAME, CREATE_PASS) \
1252 if (Name == NAME) \
1253 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001254#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1255 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1256 return true;
1257#include "PassRegistry.def"
1258
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001259 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001260}
1261
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001262Optional<std::vector<PassBuilder::PipelineElement>>
1263PassBuilder::parsePipelineText(StringRef Text) {
1264 std::vector<PipelineElement> ResultPipeline;
1265
1266 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1267 &ResultPipeline};
1268 for (;;) {
1269 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1270 size_t Pos = Text.find_first_of(",()");
1271 Pipeline.push_back({Text.substr(0, Pos), {}});
1272
1273 // If we have a single terminating name, we're done.
1274 if (Pos == Text.npos)
1275 break;
1276
1277 char Sep = Text[Pos];
1278 Text = Text.substr(Pos + 1);
1279 if (Sep == ',')
1280 // Just a name ending in a comma, continue.
1281 continue;
1282
1283 if (Sep == '(') {
1284 // Push the inner pipeline onto the stack to continue processing.
1285 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1286 continue;
1287 }
1288
1289 assert(Sep == ')' && "Bogus separator!");
1290 // When handling the close parenthesis, we greedily consume them to avoid
1291 // empty strings in the pipeline.
1292 do {
1293 // If we try to pop the outer pipeline we have unbalanced parentheses.
1294 if (PipelineStack.size() == 1)
1295 return None;
1296
1297 PipelineStack.pop_back();
1298 } while (Text.consume_front(")"));
1299
1300 // Check if we've finished parsing.
1301 if (Text.empty())
1302 break;
1303
1304 // Otherwise, the end of an inner pipeline always has to be followed by
1305 // a comma, and then we can continue.
1306 if (!Text.consume_front(","))
1307 return None;
1308 }
1309
1310 if (PipelineStack.size() > 1)
1311 // Unbalanced paretheses.
1312 return None;
1313
1314 assert(PipelineStack.back() == &ResultPipeline &&
1315 "Wrong pipeline at the bottom of the stack!");
1316 return {std::move(ResultPipeline)};
1317}
1318
1319bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1320 const PipelineElement &E, bool VerifyEachPass,
1321 bool DebugLogging) {
1322 auto &Name = E.Name;
1323 auto &InnerPipeline = E.InnerPipeline;
1324
1325 // First handle complex passes like the pass managers which carry pipelines.
1326 if (!InnerPipeline.empty()) {
1327 if (Name == "module") {
1328 ModulePassManager NestedMPM(DebugLogging);
1329 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1330 DebugLogging))
1331 return false;
1332 MPM.addPass(std::move(NestedMPM));
1333 return true;
1334 }
1335 if (Name == "cgscc") {
1336 CGSCCPassManager CGPM(DebugLogging);
1337 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1338 DebugLogging))
1339 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001340 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001341 return true;
1342 }
1343 if (Name == "function") {
1344 FunctionPassManager FPM(DebugLogging);
1345 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1346 DebugLogging))
1347 return false;
1348 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1349 return true;
1350 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001351 if (auto Count = parseRepeatPassName(Name)) {
1352 ModulePassManager NestedMPM(DebugLogging);
1353 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1354 DebugLogging))
1355 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001356 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001357 return true;
1358 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001359
1360 for (auto &C : ModulePipelineParsingCallbacks)
1361 if (C(Name, MPM, InnerPipeline))
1362 return true;
1363
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001364 // Normal passes can't have pipelines.
1365 return false;
1366 }
1367
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001368 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001369 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001370 SmallVector<StringRef, 3> Matches;
1371 if (!DefaultAliasRegex.match(Name, &Matches))
1372 return false;
1373 assert(Matches.size() == 3 && "Must capture two matched strings!");
1374
Jordan Rosef85a95f2016-07-25 18:34:51 +00001375 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001376 .Case("O0", O0)
1377 .Case("O1", O1)
1378 .Case("O2", O2)
1379 .Case("O3", O3)
1380 .Case("Os", Os)
1381 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001382 if (L == O0)
1383 // At O0 we do nothing at all!
1384 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001385
1386 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001387 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001388 } else if (Matches[1] == "thinlto-pre-link") {
1389 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1390 } else if (Matches[1] == "thinlto") {
1391 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001392 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001393 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001394 } else {
1395 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001396 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001397 }
1398 return true;
1399 }
1400
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001401 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001402#define MODULE_PASS(NAME, CREATE_PASS) \
1403 if (Name == NAME) { \
1404 MPM.addPass(CREATE_PASS); \
1405 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001406 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001407#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1408 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001409 MPM.addPass( \
1410 RequireAnalysisPass< \
1411 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001412 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001413 } \
1414 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001415 MPM.addPass(InvalidateAnalysisPass< \
1416 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001417 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001418 }
1419#include "PassRegistry.def"
1420
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001421 for (auto &C : ModulePipelineParsingCallbacks)
1422 if (C(Name, MPM, InnerPipeline))
1423 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001424 return false;
1425}
1426
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001427bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1428 const PipelineElement &E, bool VerifyEachPass,
1429 bool DebugLogging) {
1430 auto &Name = E.Name;
1431 auto &InnerPipeline = E.InnerPipeline;
1432
1433 // First handle complex passes like the pass managers which carry pipelines.
1434 if (!InnerPipeline.empty()) {
1435 if (Name == "cgscc") {
1436 CGSCCPassManager NestedCGPM(DebugLogging);
1437 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1438 DebugLogging))
1439 return false;
1440 // Add the nested pass manager with the appropriate adaptor.
1441 CGPM.addPass(std::move(NestedCGPM));
1442 return true;
1443 }
1444 if (Name == "function") {
1445 FunctionPassManager FPM(DebugLogging);
1446 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1447 DebugLogging))
1448 return false;
1449 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001450 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001451 return true;
1452 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001453 if (auto Count = parseRepeatPassName(Name)) {
1454 CGSCCPassManager NestedCGPM(DebugLogging);
1455 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1456 DebugLogging))
1457 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001458 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001459 return true;
1460 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001461 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1462 CGSCCPassManager NestedCGPM(DebugLogging);
1463 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1464 DebugLogging))
1465 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001466 CGPM.addPass(
1467 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001468 return true;
1469 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001470
1471 for (auto &C : CGSCCPipelineParsingCallbacks)
1472 if (C(Name, CGPM, InnerPipeline))
1473 return true;
1474
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001475 // Normal passes can't have pipelines.
1476 return false;
1477 }
1478
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001479// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001480#define CGSCC_PASS(NAME, CREATE_PASS) \
1481 if (Name == NAME) { \
1482 CGPM.addPass(CREATE_PASS); \
1483 return true; \
1484 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001485#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1486 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001487 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001488 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001489 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1490 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001491 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001492 } \
1493 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001494 CGPM.addPass(InvalidateAnalysisPass< \
1495 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001496 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001497 }
1498#include "PassRegistry.def"
1499
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001500 for (auto &C : CGSCCPipelineParsingCallbacks)
1501 if (C(Name, CGPM, InnerPipeline))
1502 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001503 return false;
1504}
1505
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001506bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1507 const PipelineElement &E,
1508 bool VerifyEachPass, bool DebugLogging) {
1509 auto &Name = E.Name;
1510 auto &InnerPipeline = E.InnerPipeline;
1511
1512 // First handle complex passes like the pass managers which carry pipelines.
1513 if (!InnerPipeline.empty()) {
1514 if (Name == "function") {
1515 FunctionPassManager NestedFPM(DebugLogging);
1516 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1517 DebugLogging))
1518 return false;
1519 // Add the nested pass manager with the appropriate adaptor.
1520 FPM.addPass(std::move(NestedFPM));
1521 return true;
1522 }
1523 if (Name == "loop") {
1524 LoopPassManager LPM(DebugLogging);
1525 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1526 DebugLogging))
1527 return false;
1528 // Add the nested pass manager with the appropriate adaptor.
1529 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1530 return true;
1531 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001532 if (auto Count = parseRepeatPassName(Name)) {
1533 FunctionPassManager NestedFPM(DebugLogging);
1534 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1535 DebugLogging))
1536 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001537 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001538 return true;
1539 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001540
1541 for (auto &C : FunctionPipelineParsingCallbacks)
1542 if (C(Name, FPM, InnerPipeline))
1543 return true;
1544
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001545 // Normal passes can't have pipelines.
1546 return false;
1547 }
1548
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001549// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001550#define FUNCTION_PASS(NAME, CREATE_PASS) \
1551 if (Name == NAME) { \
1552 FPM.addPass(CREATE_PASS); \
1553 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001554 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001555#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1556 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001557 FPM.addPass( \
1558 RequireAnalysisPass< \
1559 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001560 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001561 } \
1562 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001563 FPM.addPass(InvalidateAnalysisPass< \
1564 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001565 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001566 }
1567#include "PassRegistry.def"
1568
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001569 for (auto &C : FunctionPipelineParsingCallbacks)
1570 if (C(Name, FPM, InnerPipeline))
1571 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001572 return false;
1573}
1574
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001575bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001576 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001577 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001578 auto &InnerPipeline = E.InnerPipeline;
1579
1580 // First handle complex passes like the pass managers which carry pipelines.
1581 if (!InnerPipeline.empty()) {
1582 if (Name == "loop") {
1583 LoopPassManager NestedLPM(DebugLogging);
1584 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1585 DebugLogging))
1586 return false;
1587 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001588 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001589 return true;
1590 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001591 if (auto Count = parseRepeatPassName(Name)) {
1592 LoopPassManager NestedLPM(DebugLogging);
1593 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1594 DebugLogging))
1595 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001596 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001597 return true;
1598 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001599
1600 for (auto &C : LoopPipelineParsingCallbacks)
1601 if (C(Name, LPM, InnerPipeline))
1602 return true;
1603
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001604 // Normal passes can't have pipelines.
1605 return false;
1606 }
1607
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001608// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001609#define LOOP_PASS(NAME, CREATE_PASS) \
1610 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001611 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001612 return true; \
1613 }
1614#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1615 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001616 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001617 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1618 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1619 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001620 return true; \
1621 } \
1622 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001623 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001624 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001625 return true; \
1626 }
1627#include "PassRegistry.def"
1628
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001629 for (auto &C : LoopPipelineParsingCallbacks)
1630 if (C(Name, LPM, InnerPipeline))
1631 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001632 return false;
1633}
1634
Chandler Carruthedf59962016-02-18 09:45:17 +00001635bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001636#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1637 if (Name == NAME) { \
1638 AA.registerModuleAnalysis< \
1639 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1640 return true; \
1641 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001642#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1643 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001644 AA.registerFunctionAnalysis< \
1645 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001646 return true; \
1647 }
1648#include "PassRegistry.def"
1649
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001650 for (auto &C : AAParsingCallbacks)
1651 if (C(Name, AA))
1652 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001653 return false;
1654}
1655
Justin Bognereecc3c82016-02-25 07:23:08 +00001656bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001657 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001658 bool VerifyEachPass,
1659 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001660 for (const auto &Element : Pipeline) {
1661 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1662 return false;
1663 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001664 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001665 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001666}
1667
Chandler Carruth1ff77242015-03-07 09:02:36 +00001668bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001669 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001670 bool VerifyEachPass,
1671 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001672 for (const auto &Element : Pipeline) {
1673 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1674 return false;
1675 if (VerifyEachPass)
1676 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001677 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001678 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001679}
1680
Chandler Carruth1ff77242015-03-07 09:02:36 +00001681bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001682 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001683 bool VerifyEachPass,
1684 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001685 for (const auto &Element : Pipeline) {
1686 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1687 return false;
1688 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001689 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001690 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001691}
1692
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001693void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1694 FunctionAnalysisManager &FAM,
1695 CGSCCAnalysisManager &CGAM,
1696 ModuleAnalysisManager &MAM) {
1697 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1698 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001699 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1700 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1701 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1702 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1703 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1704}
1705
Chandler Carruth1ff77242015-03-07 09:02:36 +00001706bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001707 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001708 bool VerifyEachPass,
1709 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001710 for (const auto &Element : Pipeline) {
1711 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1712 return false;
1713 if (VerifyEachPass)
1714 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001715 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001716 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001717}
1718
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001719// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001720// FIXME: Should this routine accept a TargetMachine or require the caller to
1721// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001722bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1723 StringRef PipelineText, bool VerifyEachPass,
1724 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001725 auto Pipeline = parsePipelineText(PipelineText);
1726 if (!Pipeline || Pipeline->empty())
1727 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001728
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001729 // If the first name isn't at the module layer, wrap the pipeline up
1730 // automatically.
1731 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001732
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001733 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1734 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001735 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001736 } else if (isFunctionPassName(FirstName,
1737 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001738 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001739 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001740 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001741 } else {
1742 for (auto &C : TopLevelPipelineParsingCallbacks)
1743 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1744 return true;
1745
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001746 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001747 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001748 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001749 }
1750
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001751 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001752}
Chandler Carruthedf59962016-02-18 09:45:17 +00001753
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001754// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1755bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1756 StringRef PipelineText, bool VerifyEachPass,
1757 bool DebugLogging) {
1758 auto Pipeline = parsePipelineText(PipelineText);
1759 if (!Pipeline || Pipeline->empty())
1760 return false;
1761
1762 StringRef FirstName = Pipeline->front().Name;
1763 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1764 return false;
1765
1766 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1767}
1768
1769// Primary pass pipeline description parsing routine for a \c
1770// FunctionPassManager
1771bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1772 StringRef PipelineText, bool VerifyEachPass,
1773 bool DebugLogging) {
1774 auto Pipeline = parsePipelineText(PipelineText);
1775 if (!Pipeline || Pipeline->empty())
1776 return false;
1777
1778 StringRef FirstName = Pipeline->front().Name;
1779 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1780 return false;
1781
1782 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1783 DebugLogging);
1784}
1785
1786// Primary pass pipeline description parsing routine for a \c LoopPassManager
1787bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1788 StringRef PipelineText, bool VerifyEachPass,
1789 bool DebugLogging) {
1790 auto Pipeline = parsePipelineText(PipelineText);
1791 if (!Pipeline || Pipeline->empty())
1792 return false;
1793
1794 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1795}
1796
Chandler Carruthedf59962016-02-18 09:45:17 +00001797bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001798 // If the pipeline just consists of the word 'default' just replace the AA
1799 // manager with our default one.
1800 if (PipelineText == "default") {
1801 AA = buildDefaultAAPipeline();
1802 return true;
1803 }
1804
Chandler Carruthedf59962016-02-18 09:45:17 +00001805 while (!PipelineText.empty()) {
1806 StringRef Name;
1807 std::tie(Name, PipelineText) = PipelineText.split(',');
1808 if (!parseAAPassName(AA, Name))
1809 return false;
1810 }
1811
1812 return true;
1813}