blob: c9b4acfe3da69a1b0fae6cfa4994d7b18e1f6b53 [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
David L. Jones82b22e02017-11-06 00:32:01 +0000760 // Cleanup after all of the vectorizers.
761 OptimizePM.addPass(SimplifyCFGPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000762 OptimizePM.addPass(InstCombinePass());
763
764 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000765 // execution resources of an out-of-order processor. We also then need to
766 // clean up redundancies and loop invariant code.
767 // FIXME: It would be really good to use a loop-integrated instruction
768 // combiner for cleanup here so that the unrolling and LICM can be pipelined
769 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000770 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000771 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000772 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000773 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000774
775 // Now that we've vectorized and unrolled loops, we may have more refined
776 // alignment information, try to re-derive it here.
777 OptimizePM.addPass(AlignmentFromAssumptionsPass());
778
Chandler Carrutha95ff382017-01-27 00:50:21 +0000779 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
780 // canonicalization pass that enables other optimizations. As a result,
781 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
782 // result too early.
783 OptimizePM.addPass(LoopSinkPass());
784
785 // And finally clean up LCSSA form before generating code.
786 OptimizePM.addPass(InstSimplifierPass());
787
Sanjay Patel6fd43912017-09-09 13:38:18 +0000788 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
789 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
790 // flattening of blocks.
791 OptimizePM.addPass(DivRemPairsPass());
792
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000793 // LoopSink (and other loop passes since the last simplifyCFG) might have
794 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
795 OptimizePM.addPass(SimplifyCFGPass());
796
Chandler Carrutha95ff382017-01-27 00:50:21 +0000797 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000798 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
799
800 // Now we need to do some global optimization transforms.
801 // FIXME: It would seem like these should come first in the optimization
802 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
803 // ordering here.
804 MPM.addPass(GlobalDCEPass());
805 MPM.addPass(ConstantMergePass());
806
807 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000808}
809
Chandler Carruthe3f50642016-12-22 06:59:15 +0000810ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000811PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
812 bool DebugLogging) {
813 assert(Level != O0 && "Must request optimizations for the default pipeline!");
814
815 ModulePassManager MPM(DebugLogging);
816
817 // Force any function attributes we want the rest of the pipeline to observe.
818 MPM.addPass(ForceFunctionAttrsPass());
819
Dehao Chen08f88312017-08-07 20:23:20 +0000820 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000821 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
822
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000823 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000824 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
825 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000826
827 // Now add the optimization pipeline.
828 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
829
830 return MPM;
831}
832
833ModulePassManager
834PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
835 bool DebugLogging) {
836 assert(Level != O0 && "Must request optimizations for the default pipeline!");
837
838 ModulePassManager MPM(DebugLogging);
839
840 // Force any function attributes we want the rest of the pipeline to observe.
841 MPM.addPass(ForceFunctionAttrsPass());
842
Dehao Chen08f88312017-08-07 20:23:20 +0000843 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000844 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
845
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000846 // If we are planning to perform ThinLTO later, we don't bloat the code with
847 // unrolling/vectorization/... now. Just simplify the module as much as we
848 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000849 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
850 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000851
852 // Run partial inlining pass to partially inline functions that have
853 // large bodies.
854 // FIXME: It isn't clear whether this is really the right place to run this
855 // in ThinLTO. Because there is another canonicalization and simplification
856 // phase that will run after the thin link, running this here ends up with
857 // less information than will be available later and it may grow functions in
858 // ways that aren't beneficial.
859 if (RunPartialInlining)
860 MPM.addPass(PartialInlinerPass());
861
862 // Reduce the size of the IR as much as possible.
863 MPM.addPass(GlobalOptPass());
864
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000865 return MPM;
866}
867
868ModulePassManager
869PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
870 bool DebugLogging) {
871 // FIXME: The summary index is not hooked in the new pass manager yet.
872 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
873 // here.
874
875 ModulePassManager MPM(DebugLogging);
876
877 // Force any function attributes we want the rest of the pipeline to observe.
878 MPM.addPass(ForceFunctionAttrsPass());
879
880 // During the ThinLTO backend phase we perform early indirect call promotion
881 // here, before globalopt. Otherwise imported available_externally functions
882 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000883 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
884 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000885 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000886 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
887 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000888
889 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000890 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
891 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000892
893 // Now add the optimization pipeline.
894 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
895
896 return MPM;
897}
898
899ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000900PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
901 bool DebugLogging) {
902 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000903 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000904 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000905}
906
Chandler Carruthe3f50642016-12-22 06:59:15 +0000907ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
908 bool DebugLogging) {
909 assert(Level != O0 && "Must request optimizations for the default pipeline!");
910 ModulePassManager MPM(DebugLogging);
911
Davide Italiano089a9122017-01-24 00:57:39 +0000912 // Remove unused virtual tables to improve the quality of code generated by
913 // whole-program devirtualization and bitset lowering.
914 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000915
Davide Italiano089a9122017-01-24 00:57:39 +0000916 // Force any function attributes we want the rest of the pipeline to observe.
917 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000918
Davide Italiano089a9122017-01-24 00:57:39 +0000919 // Do basic inference of function attributes from known properties of system
920 // libraries and other oracles.
921 MPM.addPass(InferFunctionAttrsPass());
922
923 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000924 FunctionPassManager EarlyFPM(DebugLogging);
925 EarlyFPM.addPass(CallSiteSplittingPass());
926 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
927
Davide Italiano089a9122017-01-24 00:57:39 +0000928 // Indirect call promotion. This should promote all the targets that are
929 // left by the earlier promotion pass that promotes intra-module targets.
930 // This two-step promotion is to save the compile time. For LTO, it should
931 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000932 MPM.addPass(PGOIndirectCallPromotion(
933 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000934 // Propagate constants at call sites into the functions they call. This
935 // opens opportunities for globalopt (and inlining) by substituting function
936 // pointers passed as arguments to direct uses of functions.
937 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000938
939 // Attach metadata to indirect call sites indicating the set of functions
940 // they may target at run-time. This should follow IPSCCP.
941 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000942 }
943
944 // Now deduce any function attributes based in the current code.
945 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
946 PostOrderFunctionAttrsPass()));
947
948 // Do RPO function attribute inference across the module to forward-propagate
949 // attributes where applicable.
950 // FIXME: Is this really an optimization rather than a canonicalization?
951 MPM.addPass(ReversePostOrderFunctionAttrsPass());
952
953 // Use inragne annotations on GEP indices to split globals where beneficial.
954 MPM.addPass(GlobalSplitPass());
955
956 // Run whole program optimization of virtual call when the list of callees
957 // is fixed.
958 MPM.addPass(WholeProgramDevirtPass());
959
960 // Stop here at -O1.
961 if (Level == 1)
962 return MPM;
963
964 // Optimize globals to try and fold them into constants.
965 MPM.addPass(GlobalOptPass());
966
967 // Promote any localized globals to SSA registers.
968 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
969
970 // Linking modules together can lead to duplicate global constant, only
971 // keep one copy of each constant.
972 MPM.addPass(ConstantMergePass());
973
974 // Remove unused arguments from functions.
975 MPM.addPass(DeadArgumentEliminationPass());
976
977 // Reduce the code after globalopt and ipsccp. Both can open up significant
978 // simplification opportunities, and both can propagate functions through
979 // function pointers. When this happens, we often have to resolve varargs
980 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000981 FunctionPassManager PeepholeFPM(DebugLogging);
982 PeepholeFPM.addPass(InstCombinePass());
983 invokePeepholeEPCallbacks(PeepholeFPM, Level);
984
985 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000986
987 // Note: historically, the PruneEH pass was run first to deduce nounwind and
988 // generally clean up exception handling overhead. It isn't clear this is
989 // valuable as the inliner doesn't currently care whether it is inlining an
990 // invoke or a call.
991 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000992 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
993 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000994
995 // Optimize globals again after we ran the inliner.
996 MPM.addPass(GlobalOptPass());
997
998 // Garbage collect dead functions.
999 // FIXME: Add ArgumentPromotion pass after once it's ported.
1000 MPM.addPass(GlobalDCEPass());
1001
1002 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001003 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001004 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001005 invokePeepholeEPCallbacks(FPM, Level);
1006
Davide Italiano089a9122017-01-24 00:57:39 +00001007 FPM.addPass(JumpThreadingPass());
1008
1009 // Break up allocas
1010 FPM.addPass(SROA());
1011
1012 // Run a few AA driver optimizations here and now to cleanup the code.
1013 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1014
1015 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1016 PostOrderFunctionAttrsPass()));
1017 // FIXME: here we run IP alias analysis in the legacy PM.
1018
1019 FunctionPassManager MainFPM;
1020
1021 // FIXME: once we fix LoopPass Manager, add LICM here.
1022 // FIXME: once we provide support for enabling MLSM, add it here.
1023 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001024 if (RunNewGVN)
1025 MainFPM.addPass(NewGVNPass());
1026 else
1027 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001028
1029 // Remove dead memcpy()'s.
1030 MainFPM.addPass(MemCpyOptPass());
1031
1032 // Nuke dead stores.
1033 MainFPM.addPass(DSEPass());
1034
1035 // FIXME: at this point, we run a bunch of loop passes:
1036 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1037 // loopVectorize. Enable them once the remaining issue with LPM
1038 // are sorted out.
1039
1040 MainFPM.addPass(InstCombinePass());
1041 MainFPM.addPass(SimplifyCFGPass());
1042 MainFPM.addPass(SCCPPass());
1043 MainFPM.addPass(InstCombinePass());
1044 MainFPM.addPass(BDCEPass());
1045
1046 // FIXME: We may want to run SLPVectorizer here.
1047 // After vectorization, assume intrinsics may tell us more
1048 // about pointer alignments.
1049#if 0
1050 MainFPM.add(AlignmentFromAssumptionsPass());
1051#endif
1052
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001053 // FIXME: Conditionally run LoadCombine here, after it's ported
1054 // (in case we still have this pass, given its questionable usefulness).
1055
Davide Italiano089a9122017-01-24 00:57:39 +00001056 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001057 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001058 MainFPM.addPass(JumpThreadingPass());
1059 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1060
1061 // Create a function that performs CFI checks for cross-DSO calls with
1062 // targets in the current module.
1063 MPM.addPass(CrossDSOCFIPass());
1064
1065 // Lower type metadata and the type.test intrinsic. This pass supports
1066 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1067 // to be run at link time if CFI is enabled. This pass does nothing if
1068 // CFI is disabled.
1069 // Enable once we add support for the summary in the new PM.
1070#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001071 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1072 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001073 Summary));
1074#endif
1075
1076 // Add late LTO optimization passes.
1077 // Delete basic blocks, which optimization passes may have killed.
1078 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1079
1080 // Drop bodies of available eternally objects to improve GlobalDCE.
1081 MPM.addPass(EliminateAvailableExternallyPass());
1082
1083 // Now that we have optimized the program, discard unreachable functions.
1084 MPM.addPass(GlobalDCEPass());
1085
1086 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001087 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001088}
1089
Chandler Carruth060ad612016-12-23 20:38:19 +00001090AAManager PassBuilder::buildDefaultAAPipeline() {
1091 AAManager AA;
1092
1093 // The order in which these are registered determines their priority when
1094 // being queried.
1095
1096 // First we register the basic alias analysis that provides the majority of
1097 // per-function local AA logic. This is a stateless, on-demand local set of
1098 // AA techniques.
1099 AA.registerFunctionAnalysis<BasicAA>();
1100
1101 // Next we query fast, specialized alias analyses that wrap IR-embedded
1102 // information about aliasing.
1103 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1104 AA.registerFunctionAnalysis<TypeBasedAA>();
1105
1106 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001107 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1108 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001109 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001110 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001111
1112 return AA;
1113}
1114
Chandler Carruth241bf242016-08-03 07:44:48 +00001115static Optional<int> parseRepeatPassName(StringRef Name) {
1116 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1117 return None;
1118 int Count;
1119 if (Name.getAsInteger(0, Count) || Count <= 0)
1120 return None;
1121 return Count;
1122}
1123
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001124static Optional<int> parseDevirtPassName(StringRef Name) {
1125 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1126 return None;
1127 int Count;
1128 if (Name.getAsInteger(0, Count) || Count <= 0)
1129 return None;
1130 return Count;
1131}
1132
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001133/// Tests whether a pass name starts with a valid prefix for a default pipeline
1134/// alias.
1135static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1136 return Name.startswith("default") || Name.startswith("thinlto") ||
1137 Name.startswith("lto");
1138}
1139
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001140/// Tests whether registered callbacks will accept a given pass name.
1141///
1142/// When parsing a pipeline text, the type of the outermost pipeline may be
1143/// omitted, in which case the type is automatically determined from the first
1144/// pass name in the text. This may be a name that is handled through one of the
1145/// callbacks. We check this through the oridinary parsing callbacks by setting
1146/// up a dummy PassManager in order to not force the client to also handle this
1147/// type of query.
1148template <typename PassManagerT, typename CallbacksT>
1149static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1150 if (!Callbacks.empty()) {
1151 PassManagerT DummyPM;
1152 for (auto &CB : Callbacks)
1153 if (CB(Name, DummyPM, {}))
1154 return true;
1155 }
1156 return false;
1157}
1158
1159template <typename CallbacksT>
1160static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001161 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001162 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001163 return DefaultAliasRegex.match(Name);
1164
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001165 // Explicitly handle pass manager names.
1166 if (Name == "module")
1167 return true;
1168 if (Name == "cgscc")
1169 return true;
1170 if (Name == "function")
1171 return true;
1172
Chandler Carruth241bf242016-08-03 07:44:48 +00001173 // Explicitly handle custom-parsed pass names.
1174 if (parseRepeatPassName(Name))
1175 return true;
1176
Chandler Carruth74a8a222016-06-17 07:15:29 +00001177#define MODULE_PASS(NAME, CREATE_PASS) \
1178 if (Name == NAME) \
1179 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001180#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001181 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001182 return true;
1183#include "PassRegistry.def"
1184
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001185 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001186}
1187
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001188template <typename CallbacksT>
1189static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001190 // Explicitly handle pass manager names.
1191 if (Name == "cgscc")
1192 return true;
1193 if (Name == "function")
1194 return true;
1195
Chandler Carruth241bf242016-08-03 07:44:48 +00001196 // Explicitly handle custom-parsed pass names.
1197 if (parseRepeatPassName(Name))
1198 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001199 if (parseDevirtPassName(Name))
1200 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001201
Chandler Carruth74a8a222016-06-17 07:15:29 +00001202#define CGSCC_PASS(NAME, CREATE_PASS) \
1203 if (Name == NAME) \
1204 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001205#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001206 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001207 return true;
1208#include "PassRegistry.def"
1209
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001210 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001211}
1212
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001213template <typename CallbacksT>
1214static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001215 // Explicitly handle pass manager names.
1216 if (Name == "function")
1217 return true;
1218 if (Name == "loop")
1219 return true;
1220
Chandler Carruth241bf242016-08-03 07:44:48 +00001221 // Explicitly handle custom-parsed pass names.
1222 if (parseRepeatPassName(Name))
1223 return true;
1224
Chandler Carruth74a8a222016-06-17 07:15:29 +00001225#define FUNCTION_PASS(NAME, CREATE_PASS) \
1226 if (Name == NAME) \
1227 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001228#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001229 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001230 return true;
1231#include "PassRegistry.def"
1232
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001233 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001234}
1235
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001236template <typename CallbacksT>
1237static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001238 // Explicitly handle pass manager names.
1239 if (Name == "loop")
1240 return true;
1241
Chandler Carruth241bf242016-08-03 07:44:48 +00001242 // Explicitly handle custom-parsed pass names.
1243 if (parseRepeatPassName(Name))
1244 return true;
1245
Chandler Carruth74a8a222016-06-17 07:15:29 +00001246#define LOOP_PASS(NAME, CREATE_PASS) \
1247 if (Name == NAME) \
1248 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001249#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1250 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1251 return true;
1252#include "PassRegistry.def"
1253
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001254 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001255}
1256
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001257Optional<std::vector<PassBuilder::PipelineElement>>
1258PassBuilder::parsePipelineText(StringRef Text) {
1259 std::vector<PipelineElement> ResultPipeline;
1260
1261 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1262 &ResultPipeline};
1263 for (;;) {
1264 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1265 size_t Pos = Text.find_first_of(",()");
1266 Pipeline.push_back({Text.substr(0, Pos), {}});
1267
1268 // If we have a single terminating name, we're done.
1269 if (Pos == Text.npos)
1270 break;
1271
1272 char Sep = Text[Pos];
1273 Text = Text.substr(Pos + 1);
1274 if (Sep == ',')
1275 // Just a name ending in a comma, continue.
1276 continue;
1277
1278 if (Sep == '(') {
1279 // Push the inner pipeline onto the stack to continue processing.
1280 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1281 continue;
1282 }
1283
1284 assert(Sep == ')' && "Bogus separator!");
1285 // When handling the close parenthesis, we greedily consume them to avoid
1286 // empty strings in the pipeline.
1287 do {
1288 // If we try to pop the outer pipeline we have unbalanced parentheses.
1289 if (PipelineStack.size() == 1)
1290 return None;
1291
1292 PipelineStack.pop_back();
1293 } while (Text.consume_front(")"));
1294
1295 // Check if we've finished parsing.
1296 if (Text.empty())
1297 break;
1298
1299 // Otherwise, the end of an inner pipeline always has to be followed by
1300 // a comma, and then we can continue.
1301 if (!Text.consume_front(","))
1302 return None;
1303 }
1304
1305 if (PipelineStack.size() > 1)
1306 // Unbalanced paretheses.
1307 return None;
1308
1309 assert(PipelineStack.back() == &ResultPipeline &&
1310 "Wrong pipeline at the bottom of the stack!");
1311 return {std::move(ResultPipeline)};
1312}
1313
1314bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1315 const PipelineElement &E, bool VerifyEachPass,
1316 bool DebugLogging) {
1317 auto &Name = E.Name;
1318 auto &InnerPipeline = E.InnerPipeline;
1319
1320 // First handle complex passes like the pass managers which carry pipelines.
1321 if (!InnerPipeline.empty()) {
1322 if (Name == "module") {
1323 ModulePassManager NestedMPM(DebugLogging);
1324 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1325 DebugLogging))
1326 return false;
1327 MPM.addPass(std::move(NestedMPM));
1328 return true;
1329 }
1330 if (Name == "cgscc") {
1331 CGSCCPassManager CGPM(DebugLogging);
1332 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1333 DebugLogging))
1334 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001335 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001336 return true;
1337 }
1338 if (Name == "function") {
1339 FunctionPassManager FPM(DebugLogging);
1340 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1341 DebugLogging))
1342 return false;
1343 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1344 return true;
1345 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001346 if (auto Count = parseRepeatPassName(Name)) {
1347 ModulePassManager NestedMPM(DebugLogging);
1348 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1349 DebugLogging))
1350 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001351 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001352 return true;
1353 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001354
1355 for (auto &C : ModulePipelineParsingCallbacks)
1356 if (C(Name, MPM, InnerPipeline))
1357 return true;
1358
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001359 // Normal passes can't have pipelines.
1360 return false;
1361 }
1362
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001363 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001364 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001365 SmallVector<StringRef, 3> Matches;
1366 if (!DefaultAliasRegex.match(Name, &Matches))
1367 return false;
1368 assert(Matches.size() == 3 && "Must capture two matched strings!");
1369
Jordan Rosef85a95f2016-07-25 18:34:51 +00001370 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001371 .Case("O0", O0)
1372 .Case("O1", O1)
1373 .Case("O2", O2)
1374 .Case("O3", O3)
1375 .Case("Os", Os)
1376 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001377 if (L == O0)
1378 // At O0 we do nothing at all!
1379 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001380
1381 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001382 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001383 } else if (Matches[1] == "thinlto-pre-link") {
1384 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1385 } else if (Matches[1] == "thinlto") {
1386 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001387 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001388 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001389 } else {
1390 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001391 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001392 }
1393 return true;
1394 }
1395
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001396 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001397#define MODULE_PASS(NAME, CREATE_PASS) \
1398 if (Name == NAME) { \
1399 MPM.addPass(CREATE_PASS); \
1400 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001401 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001402#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1403 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001404 MPM.addPass( \
1405 RequireAnalysisPass< \
1406 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001407 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001408 } \
1409 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001410 MPM.addPass(InvalidateAnalysisPass< \
1411 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001412 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001413 }
1414#include "PassRegistry.def"
1415
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001416 for (auto &C : ModulePipelineParsingCallbacks)
1417 if (C(Name, MPM, InnerPipeline))
1418 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001419 return false;
1420}
1421
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001422bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1423 const PipelineElement &E, bool VerifyEachPass,
1424 bool DebugLogging) {
1425 auto &Name = E.Name;
1426 auto &InnerPipeline = E.InnerPipeline;
1427
1428 // First handle complex passes like the pass managers which carry pipelines.
1429 if (!InnerPipeline.empty()) {
1430 if (Name == "cgscc") {
1431 CGSCCPassManager NestedCGPM(DebugLogging);
1432 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1433 DebugLogging))
1434 return false;
1435 // Add the nested pass manager with the appropriate adaptor.
1436 CGPM.addPass(std::move(NestedCGPM));
1437 return true;
1438 }
1439 if (Name == "function") {
1440 FunctionPassManager FPM(DebugLogging);
1441 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1442 DebugLogging))
1443 return false;
1444 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001445 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001446 return true;
1447 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001448 if (auto Count = parseRepeatPassName(Name)) {
1449 CGSCCPassManager NestedCGPM(DebugLogging);
1450 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1451 DebugLogging))
1452 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001453 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001454 return true;
1455 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001456 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1457 CGSCCPassManager NestedCGPM(DebugLogging);
1458 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1459 DebugLogging))
1460 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001461 CGPM.addPass(
1462 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001463 return true;
1464 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001465
1466 for (auto &C : CGSCCPipelineParsingCallbacks)
1467 if (C(Name, CGPM, InnerPipeline))
1468 return true;
1469
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001470 // Normal passes can't have pipelines.
1471 return false;
1472 }
1473
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001474// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001475#define CGSCC_PASS(NAME, CREATE_PASS) \
1476 if (Name == NAME) { \
1477 CGPM.addPass(CREATE_PASS); \
1478 return true; \
1479 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001480#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1481 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001482 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001483 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001484 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1485 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001486 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001487 } \
1488 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001489 CGPM.addPass(InvalidateAnalysisPass< \
1490 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001491 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001492 }
1493#include "PassRegistry.def"
1494
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001495 for (auto &C : CGSCCPipelineParsingCallbacks)
1496 if (C(Name, CGPM, InnerPipeline))
1497 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001498 return false;
1499}
1500
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001501bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1502 const PipelineElement &E,
1503 bool VerifyEachPass, bool DebugLogging) {
1504 auto &Name = E.Name;
1505 auto &InnerPipeline = E.InnerPipeline;
1506
1507 // First handle complex passes like the pass managers which carry pipelines.
1508 if (!InnerPipeline.empty()) {
1509 if (Name == "function") {
1510 FunctionPassManager NestedFPM(DebugLogging);
1511 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1512 DebugLogging))
1513 return false;
1514 // Add the nested pass manager with the appropriate adaptor.
1515 FPM.addPass(std::move(NestedFPM));
1516 return true;
1517 }
1518 if (Name == "loop") {
1519 LoopPassManager LPM(DebugLogging);
1520 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1521 DebugLogging))
1522 return false;
1523 // Add the nested pass manager with the appropriate adaptor.
1524 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1525 return true;
1526 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001527 if (auto Count = parseRepeatPassName(Name)) {
1528 FunctionPassManager NestedFPM(DebugLogging);
1529 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1530 DebugLogging))
1531 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001532 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001533 return true;
1534 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001535
1536 for (auto &C : FunctionPipelineParsingCallbacks)
1537 if (C(Name, FPM, InnerPipeline))
1538 return true;
1539
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001540 // Normal passes can't have pipelines.
1541 return false;
1542 }
1543
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001544// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001545#define FUNCTION_PASS(NAME, CREATE_PASS) \
1546 if (Name == NAME) { \
1547 FPM.addPass(CREATE_PASS); \
1548 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001549 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001550#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1551 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001552 FPM.addPass( \
1553 RequireAnalysisPass< \
1554 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001555 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001556 } \
1557 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001558 FPM.addPass(InvalidateAnalysisPass< \
1559 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001560 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001561 }
1562#include "PassRegistry.def"
1563
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001564 for (auto &C : FunctionPipelineParsingCallbacks)
1565 if (C(Name, FPM, InnerPipeline))
1566 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001567 return false;
1568}
1569
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001570bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001571 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001572 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001573 auto &InnerPipeline = E.InnerPipeline;
1574
1575 // First handle complex passes like the pass managers which carry pipelines.
1576 if (!InnerPipeline.empty()) {
1577 if (Name == "loop") {
1578 LoopPassManager NestedLPM(DebugLogging);
1579 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1580 DebugLogging))
1581 return false;
1582 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001583 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001584 return true;
1585 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001586 if (auto Count = parseRepeatPassName(Name)) {
1587 LoopPassManager NestedLPM(DebugLogging);
1588 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1589 DebugLogging))
1590 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001591 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001592 return true;
1593 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001594
1595 for (auto &C : LoopPipelineParsingCallbacks)
1596 if (C(Name, LPM, InnerPipeline))
1597 return true;
1598
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001599 // Normal passes can't have pipelines.
1600 return false;
1601 }
1602
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001603// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001604#define LOOP_PASS(NAME, CREATE_PASS) \
1605 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001606 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001607 return true; \
1608 }
1609#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1610 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001611 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001612 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1613 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1614 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001615 return true; \
1616 } \
1617 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001618 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001619 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001620 return true; \
1621 }
1622#include "PassRegistry.def"
1623
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001624 for (auto &C : LoopPipelineParsingCallbacks)
1625 if (C(Name, LPM, InnerPipeline))
1626 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001627 return false;
1628}
1629
Chandler Carruthedf59962016-02-18 09:45:17 +00001630bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001631#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1632 if (Name == NAME) { \
1633 AA.registerModuleAnalysis< \
1634 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1635 return true; \
1636 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001637#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1638 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001639 AA.registerFunctionAnalysis< \
1640 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001641 return true; \
1642 }
1643#include "PassRegistry.def"
1644
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001645 for (auto &C : AAParsingCallbacks)
1646 if (C(Name, AA))
1647 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001648 return false;
1649}
1650
Justin Bognereecc3c82016-02-25 07:23:08 +00001651bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001652 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001653 bool VerifyEachPass,
1654 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001655 for (const auto &Element : Pipeline) {
1656 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1657 return false;
1658 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001659 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001660 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001661}
1662
Chandler Carruth1ff77242015-03-07 09:02:36 +00001663bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001664 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001665 bool VerifyEachPass,
1666 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001667 for (const auto &Element : Pipeline) {
1668 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1669 return false;
1670 if (VerifyEachPass)
1671 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001672 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001673 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001674}
1675
Chandler Carruth1ff77242015-03-07 09:02:36 +00001676bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001677 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001678 bool VerifyEachPass,
1679 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001680 for (const auto &Element : Pipeline) {
1681 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1682 return false;
1683 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001684 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001685 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001686}
1687
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001688void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1689 FunctionAnalysisManager &FAM,
1690 CGSCCAnalysisManager &CGAM,
1691 ModuleAnalysisManager &MAM) {
1692 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1693 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001694 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1695 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1696 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1697 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1698 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1699}
1700
Chandler Carruth1ff77242015-03-07 09:02:36 +00001701bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001702 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001703 bool VerifyEachPass,
1704 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 for (const auto &Element : Pipeline) {
1706 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1707 return false;
1708 if (VerifyEachPass)
1709 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001710 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001711 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001712}
1713
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001714// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001715// FIXME: Should this routine accept a TargetMachine or require the caller to
1716// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001717bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1718 StringRef PipelineText, bool VerifyEachPass,
1719 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001720 auto Pipeline = parsePipelineText(PipelineText);
1721 if (!Pipeline || Pipeline->empty())
1722 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001723
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001724 // If the first name isn't at the module layer, wrap the pipeline up
1725 // automatically.
1726 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001727
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001728 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1729 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001730 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001731 } else if (isFunctionPassName(FirstName,
1732 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001733 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001734 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001735 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001736 } else {
1737 for (auto &C : TopLevelPipelineParsingCallbacks)
1738 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1739 return true;
1740
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001741 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001742 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001743 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001744 }
1745
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001746 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001747}
Chandler Carruthedf59962016-02-18 09:45:17 +00001748
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001749// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1750bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1751 StringRef PipelineText, bool VerifyEachPass,
1752 bool DebugLogging) {
1753 auto Pipeline = parsePipelineText(PipelineText);
1754 if (!Pipeline || Pipeline->empty())
1755 return false;
1756
1757 StringRef FirstName = Pipeline->front().Name;
1758 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1759 return false;
1760
1761 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1762}
1763
1764// Primary pass pipeline description parsing routine for a \c
1765// FunctionPassManager
1766bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1767 StringRef PipelineText, bool VerifyEachPass,
1768 bool DebugLogging) {
1769 auto Pipeline = parsePipelineText(PipelineText);
1770 if (!Pipeline || Pipeline->empty())
1771 return false;
1772
1773 StringRef FirstName = Pipeline->front().Name;
1774 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1775 return false;
1776
1777 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1778 DebugLogging);
1779}
1780
1781// Primary pass pipeline description parsing routine for a \c LoopPassManager
1782bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1783 StringRef PipelineText, bool VerifyEachPass,
1784 bool DebugLogging) {
1785 auto Pipeline = parsePipelineText(PipelineText);
1786 if (!Pipeline || Pipeline->empty())
1787 return false;
1788
1789 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1790}
1791
Chandler Carruthedf59962016-02-18 09:45:17 +00001792bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001793 // If the pipeline just consists of the word 'default' just replace the AA
1794 // manager with our default one.
1795 if (PipelineText == "default") {
1796 AA = buildDefaultAAPipeline();
1797 return true;
1798 }
1799
Chandler Carruthedf59962016-02-18 09:45:17 +00001800 while (!PipelineText.empty()) {
1801 StringRef Name;
1802 std::tie(Name, PipelineText) = PipelineText.split(',');
1803 if (!parseAAPassName(AA, Name))
1804 return false;
1805 }
1806
1807 return true;
1808}