blob: 2088ea0cea269db8016035832e159de47e7f2c84 [file] [log] [blame]
Chandler Carruth1ff77242015-03-07 09:02:36 +00001//===- Parsing, selection, and construction of pass pipelines -------------===//
Chandler Carruth66445382014-01-11 08:16:35 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9/// \file
10///
Chandler Carruth1ff77242015-03-07 09:02:36 +000011/// This file provides the implementation of the PassBuilder based on our
12/// static pass registry as well as related functionality. It also provides
13/// helpers to aid in analyzing, debugging, and testing passes and pass
14/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000015///
16//===----------------------------------------------------------------------===//
17
Chandler Carruth1ff77242015-03-07 09:02:36 +000018#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000019#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000021#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000022#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000023#include "llvm/Analysis/BasicAliasAnalysis.h"
Xinliang David Li28a93272016-05-05 21:13:27 +000024#include "llvm/Analysis/BlockFrequencyInfo.h"
25#include "llvm/Analysis/BlockFrequencyInfoImpl.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000026#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000027#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000028#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
29#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000030#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000031#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000032#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000033#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000034#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000035#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000036#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000037#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000038#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000039#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000040#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000041#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000042#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000043#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000044#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000045#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000046#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000047#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000048#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000049#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000050#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000061#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000062#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000063#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000064#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000065#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000066#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000067#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000068#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000069#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000070#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000071#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000072#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000073#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000074#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000075#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000076#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000077#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000078#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000079#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000080#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000081#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000082#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000083#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Davide Italianod737dd22016-06-14 21:44:19 +000084#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000085#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000086#include "llvm/Transforms/InstrProfiling.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000087#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000088#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000089#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000090#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000091#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000092#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000093#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000094#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000095#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000096#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000097#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000098#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +000099#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000100#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000101#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000102#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000103#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000104#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000105#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000106#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000107#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000108#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000109#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000110#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000111#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000112#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000113#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000114#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000115#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000116#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000117#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000118#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000119#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000120#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000121#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000122#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000123#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000124#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000125#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000126#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000127#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000128#include "llvm/Transforms/Scalar/Reassociate.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000129#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000130#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000131#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000132#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000133#include "llvm/Transforms/Scalar/Sink.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000134#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000135#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000136#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000137#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000138#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000139#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000140#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000141#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000142#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000143#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Daniel Berlin439042b2017-02-07 21:10:46 +0000144#include "llvm/Transforms/Utils/PredicateInfo.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000145#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000146#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000147#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000148#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000149
Chandler Carruth58dde8c2016-02-26 12:17:54 +0000150#include <type_traits>
Chandler Carruth66445382014-01-11 08:16:35 +0000151
152using namespace llvm;
153
Chandler Carruth719ffe12017-02-12 05:38:04 +0000154static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
155 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000156static cl::opt<bool>
157 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
158 cl::Hidden, cl::ZeroOrMore,
159 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000160
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000161static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000162 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000163 cl::Hidden, cl::ZeroOrMore,
164 cl::desc("Run NewGVN instead of GVN"));
165
Geoff Berry3cca1da2017-06-10 15:20:03 +0000166static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000167 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
168 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000169
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000170static cl::opt<bool> EnableGVNHoist(
171 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
172 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
173
Davide Italianobe1b6a92017-06-03 23:18:29 +0000174static cl::opt<bool> EnableGVNSink(
175 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
176 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
177
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000178static Regex DefaultAliasRegex(
179 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000180
Chandler Carruthe3f50642016-12-22 06:59:15 +0000181static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
182 switch (Level) {
183 case PassBuilder::O0:
184 case PassBuilder::O1:
185 case PassBuilder::O2:
186 case PassBuilder::O3:
187 return false;
188
189 case PassBuilder::Os:
190 case PassBuilder::Oz:
191 return true;
192 }
193 llvm_unreachable("Invalid optimization level!");
194}
195
Chandler Carruth66445382014-01-11 08:16:35 +0000196namespace {
197
Chandler Carruthd8330982014-01-12 09:34:22 +0000198/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000199struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000200 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000201 return PreservedAnalyses::all();
202 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000203 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000204};
205
Chandler Carruth0b576b32015-01-06 02:50:06 +0000206/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000207class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
208 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000209 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000210
211public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000213 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000214 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000215};
216
Chandler Carruth572e3402014-04-21 11:12:00 +0000217/// \brief No-op CGSCC pass which does nothing.
218struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000219 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
220 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000221 return PreservedAnalyses::all();
222 }
223 static StringRef name() { return "NoOpCGSCCPass"; }
224};
225
Chandler Carruth0b576b32015-01-06 02:50:06 +0000226/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000227class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
228 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000229 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000230
231public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000232 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000233 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000234 return Result();
235 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000236 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000237};
238
Chandler Carruthd8330982014-01-12 09:34:22 +0000239/// \brief No-op function pass which does nothing.
240struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000241 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000242 return PreservedAnalyses::all();
243 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000244 static StringRef name() { return "NoOpFunctionPass"; }
245};
246
Chandler Carruth0b576b32015-01-06 02:50:06 +0000247/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000248class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
249 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000250 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000251
252public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000254 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000255 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000256};
257
Justin Bognereecc3c82016-02-25 07:23:08 +0000258/// \brief No-op loop pass which does nothing.
259struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000260 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
261 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000262 return PreservedAnalyses::all();
263 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000264 static StringRef name() { return "NoOpLoopPass"; }
265};
266
267/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000268class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
269 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000270 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000271
272public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000273 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000274 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
275 return Result();
276 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000277 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000278};
279
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000280AnalysisKey NoOpModuleAnalysis::Key;
281AnalysisKey NoOpCGSCCAnalysis::Key;
282AnalysisKey NoOpFunctionAnalysis::Key;
283AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000284
Chandler Carruth66445382014-01-11 08:16:35 +0000285} // End anonymous namespace.
286
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000287void PassBuilder::invokePeepholeEPCallbacks(
288 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
289 for (auto &C : PeepholeEPCallbacks)
290 C(FPM, Level);
291}
292
Chandler Carruth1ff77242015-03-07 09:02:36 +0000293void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000294#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000295 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000296#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000297
298 for (auto &C : ModuleAnalysisRegistrationCallbacks)
299 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000300}
301
Chandler Carruth1ff77242015-03-07 09:02:36 +0000302void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000303#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000304 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000305#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000306
307 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
308 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000309}
310
Chandler Carruth1ff77242015-03-07 09:02:36 +0000311void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000312#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000313 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000314#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000315
316 for (auto &C : FunctionAnalysisRegistrationCallbacks)
317 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000318}
319
Justin Bognereecc3c82016-02-25 07:23:08 +0000320void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000321#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000322 LAM.registerPass([&] { return CREATE_PASS; });
323#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000324
325 for (auto &C : LoopAnalysisRegistrationCallbacks)
326 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000327}
328
Chandler Carruthe3f50642016-12-22 06:59:15 +0000329FunctionPassManager
330PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000331 ThinLTOPhase Phase,
332 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000333 assert(Level != O0 && "Must request optimizations!");
334 FunctionPassManager FPM(DebugLogging);
335
336 // Form SSA out of local memory accesses after breaking apart aggregates into
337 // scalars.
338 FPM.addPass(SROA());
339
340 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000341 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000342
Davide Italianoc3688312017-06-01 23:08:14 +0000343 // Hoisting of scalars and load expressions.
344 if (EnableGVNHoist)
345 FPM.addPass(GVNHoistPass());
346
Davide Italianobe1b6a92017-06-03 23:18:29 +0000347 // Global value numbering based sinking.
348 if (EnableGVNSink) {
349 FPM.addPass(GVNSinkPass());
350 FPM.addPass(SimplifyCFGPass());
351 }
352
Chandler Carruthe3f50642016-12-22 06:59:15 +0000353 // Speculative execution if the target has divergent branches; otherwise nop.
354 FPM.addPass(SpeculativeExecutionPass());
355
356 // Optimize based on known information about branches, and cleanup afterward.
357 FPM.addPass(JumpThreadingPass());
358 FPM.addPass(CorrelatedValuePropagationPass());
359 FPM.addPass(SimplifyCFGPass());
360 FPM.addPass(InstCombinePass());
361
362 if (!isOptimizingForSize(Level))
363 FPM.addPass(LibCallsShrinkWrapPass());
364
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000365 invokePeepholeEPCallbacks(FPM, Level);
366
Rong Xue1f42452017-10-23 22:21:29 +0000367 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
368 // using the size value profile. Don't perform this when optimizing for size.
369 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
370 !isOptimizingForSize(Level))
371 FPM.addPass(PGOMemOPSizeOpt());
372
Chandler Carruthe3f50642016-12-22 06:59:15 +0000373 FPM.addPass(TailCallElimPass());
374 FPM.addPass(SimplifyCFGPass());
375
376 // Form canonically associated expression trees, and simplify the trees using
377 // basic mathematical properties. For example, this will form (nearly)
378 // minimal multiplication trees.
379 FPM.addPass(ReassociatePass());
380
381 // Add the primary loop simplification pipeline.
382 // FIXME: Currently this is split into two loop pass pipelines because we run
383 // some function passes in between them. These can and should be replaced by
384 // loop pass equivalenst but those aren't ready yet. Specifically,
385 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
386 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
387 // the other we have is `LoopInstSimplify`.
388 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
389
390 // Rotate Loop - disable header duplication at -Oz
391 LPM1.addPass(LoopRotatePass(Level != Oz));
392 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000393 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000394 LPM2.addPass(IndVarSimplifyPass());
395 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000396
397 for (auto &C : LateLoopOptimizationsEPCallbacks)
398 C(LPM2, Level);
399
Chandler Carruth79b733b2017-01-26 23:21:17 +0000400 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000401 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000402 // because it changes IR to makes profile annotation in back compile
403 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000404 if (Phase != ThinLTOPhase::PreLink ||
405 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000406 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000407
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000408 for (auto &C : LoopOptimizerEndEPCallbacks)
409 C(LPM2, Level);
410
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000411 // We provide the opt remark emitter pass for LICM to use. We only need to do
412 // this once as it is immutable.
413 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000414 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1)));
415 FPM.addPass(SimplifyCFGPass());
416 FPM.addPass(InstCombinePass());
417 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2)));
418
419 // Eliminate redundancies.
420 if (Level != O1) {
421 // These passes add substantial compile time so skip them at O1.
422 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000423 if (RunNewGVN)
424 FPM.addPass(NewGVNPass());
425 else
426 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000427 }
428
429 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
430 FPM.addPass(MemCpyOptPass());
431
432 // Sparse conditional constant propagation.
433 // FIXME: It isn't clear why we do this *after* loop passes rather than
434 // before...
435 FPM.addPass(SCCPPass());
436
437 // Delete dead bit computations (instcombine runs after to fold away the dead
438 // computations, and then ADCE will run later to exploit any new DCE
439 // opportunities that creates).
440 FPM.addPass(BDCEPass());
441
442 // Run instcombine after redundancy and dead bit elimination to exploit
443 // opportunities opened up by them.
444 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000445 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000446
447 // Re-consider control flow based optimizations after redundancy elimination,
448 // redo DCE, etc.
449 FPM.addPass(JumpThreadingPass());
450 FPM.addPass(CorrelatedValuePropagationPass());
451 FPM.addPass(DSEPass());
452 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
453
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000454 for (auto &C : ScalarOptimizerLateEPCallbacks)
455 C(FPM, Level);
456
Chandler Carruthe3f50642016-12-22 06:59:15 +0000457 // Finally, do an expensive DCE pass to catch all the dead code exposed by
458 // the simplifications and basic cleanup after all the simplifications.
459 FPM.addPass(ADCEPass());
460 FPM.addPass(SimplifyCFGPass());
461 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000462 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000463
464 return FPM;
465}
466
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000467void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
468 PassBuilder::OptimizationLevel Level,
469 bool RunProfileGen,
470 std::string ProfileGenFile,
471 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000472 // Generally running simplification passes and the inliner with an high
473 // threshold results in smaller executables, but there may be cases where
474 // the size grows, so let's be conservative here and skip this simplification
475 // at -Os/Oz.
476 if (!isOptimizingForSize(Level)) {
477 InlineParams IP;
478
479 // In the old pass manager, this is a cl::opt. Should still this be one?
480 IP.DefaultThreshold = 75;
481
482 // FIXME: The hint threshold has the same value used by the regular inliner.
483 // This should probably be lowered after performance testing.
484 // FIXME: this comment is cargo culted from the old pass manager, revisit).
485 IP.HintThreshold = 325;
486
487 CGSCCPassManager CGPipeline(DebugLogging);
488
489 CGPipeline.addPass(InlinerPass(IP));
490
491 FunctionPassManager FPM;
492 FPM.addPass(SROA());
493 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
494 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
495 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000496 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000497
Davide Italiano513dfaa2017-02-13 15:26:22 +0000498 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
499
500 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
501 }
502
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000503 // Delete anything that is now dead to make sure that we don't instrument
504 // dead code. Instrumentation can end up keeping dead code around and
505 // dramatically increase code size.
506 MPM.addPass(GlobalDCEPass());
507
Davide Italiano513dfaa2017-02-13 15:26:22 +0000508 if (RunProfileGen) {
509 MPM.addPass(PGOInstrumentationGen());
510
Xinliang David Lib67530e2017-06-25 00:26:43 +0000511 FunctionPassManager FPM;
512 FPM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
513 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
514
Davide Italiano513dfaa2017-02-13 15:26:22 +0000515 // Add the profile lowering pass.
516 InstrProfOptions Options;
517 if (!ProfileGenFile.empty())
518 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000519 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000520 MPM.addPass(InstrProfiling(Options));
521 }
522
523 if (!ProfileUseFile.empty())
524 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
525}
526
Easwaran Raman8249fac2017-06-28 13:33:49 +0000527static InlineParams
528getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
529 auto O3 = PassBuilder::O3;
530 unsigned OptLevel = Level > O3 ? 2 : Level;
531 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
532 return getInlineParams(OptLevel, SizeLevel);
533}
534
Chandler Carruthe3f50642016-12-22 06:59:15 +0000535ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000536PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000537 ThinLTOPhase Phase,
538 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000539 ModulePassManager MPM(DebugLogging);
540
Chandler Carruthe3f50642016-12-22 06:59:15 +0000541 // Do basic inference of function attributes from known properties of system
542 // libraries and other oracles.
543 MPM.addPass(InferFunctionAttrsPass());
544
545 // Create an early function pass manager to cleanup the output of the
546 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000547 FunctionPassManager EarlyFPM(DebugLogging);
548 EarlyFPM.addPass(SimplifyCFGPass());
549 EarlyFPM.addPass(SROA());
550 EarlyFPM.addPass(EarlyCSEPass());
551 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000552 if (Level == O3)
553 EarlyFPM.addPass(CallSiteSplittingPass());
554
Dehao Chen08f88312017-08-07 20:23:20 +0000555 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
556 // to convert bitcast to direct calls so that they can be inlined during the
557 // profile annotation prepration step.
558 // More details about SamplePGO design can be found in:
559 // https://research.google.com/pubs/pub45290.html
560 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
561 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
562 Phase == ThinLTOPhase::PostLink)
563 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000564 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000565
Dehao Chen08f88312017-08-07 20:23:20 +0000566 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
567 // Annotate sample profile right after early FPM to ensure freshness of
568 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000569 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
570 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000571 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
572 // for the profile annotation to be accurate in the ThinLTO backend.
573 if (Phase != ThinLTOPhase::PreLink)
574 // We perform early indirect call promotion here, before globalopt.
575 // This is important for the ThinLTO backend phase because otherwise
576 // imported available_externally functions look unreferenced and are
577 // removed.
578 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
579 true));
580 }
581
Chandler Carruthe3f50642016-12-22 06:59:15 +0000582 // Interprocedural constant propagation now that basic cleanup has occured
583 // and prior to optimizing globals.
584 // FIXME: This position in the pipeline hasn't been carefully considered in
585 // years, it should be re-analyzed.
586 MPM.addPass(IPSCCPPass());
587
Matthew Simpsoncb585582017-10-25 13:40:08 +0000588 // Attach metadata to indirect call sites indicating the set of functions
589 // they may target at run-time. This should follow IPSCCP.
590 MPM.addPass(CalledValuePropagationPass());
591
Chandler Carruthe3f50642016-12-22 06:59:15 +0000592 // Optimize globals to try and fold them into constants.
593 MPM.addPass(GlobalOptPass());
594
595 // Promote any localized globals to SSA registers.
596 // FIXME: Should this instead by a run of SROA?
597 // FIXME: We should probably run instcombine and simplify-cfg afterward to
598 // delete control flows that are dead once globals have been folded to
599 // constants.
600 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
601
602 // Remove any dead arguments exposed by cleanups and constand folding
603 // globals.
604 MPM.addPass(DeadArgumentEliminationPass());
605
606 // Create a small function pass pipeline to cleanup after all the global
607 // optimizations.
608 FunctionPassManager GlobalCleanupPM(DebugLogging);
609 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000610 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
611
Chandler Carruthe3f50642016-12-22 06:59:15 +0000612 GlobalCleanupPM.addPass(SimplifyCFGPass());
613 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
614
Dehao Chen89d32262017-08-02 01:28:31 +0000615 // Add all the requested passes for instrumentation PGO, if requested.
616 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
617 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
618 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
619 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
620 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000621 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000622
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000623 // Require the GlobalsAA analysis for the module so we can query it within
624 // the CGSCC pipeline.
625 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000626
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000627 // Require the ProfileSummaryAnalysis for the module so we can query it within
628 // the inliner pass.
629 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
630
Chandler Carruthe3f50642016-12-22 06:59:15 +0000631 // Now begin the main postorder CGSCC pipeline.
632 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
633 // manager and trying to emulate its precise behavior. Much of this doesn't
634 // make a lot of sense and we should revisit the core CGSCC structure.
635 CGSCCPassManager MainCGPipeline(DebugLogging);
636
637 // Note: historically, the PruneEH pass was run first to deduce nounwind and
638 // generally clean up exception handling overhead. It isn't clear this is
639 // valuable as the inliner doesn't currently care whether it is inlining an
640 // invoke or a call.
641
642 // Run the inliner first. The theory is that we are walking bottom-up and so
643 // the callees have already been fully optimized, and we want to inline them
644 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000645 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000646 // because it makes profile annotation in the backend inaccurate.
647 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000648 if (Phase == ThinLTOPhase::PreLink &&
649 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000650 IP.HotCallSiteThreshold = 0;
651 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000652
653 // Now deduce any function attributes based in the current code.
654 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
655
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000656 // When at O3 add argument promotion to the pass pipeline.
657 // FIXME: It isn't at all clear why this should be limited to O3.
658 if (Level == O3)
659 MainCGPipeline.addPass(ArgumentPromotionPass());
660
Chandler Carruthe3f50642016-12-22 06:59:15 +0000661 // Lastly, add the core function simplification pipeline nested inside the
662 // CGSCC walk.
663 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000664 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000665
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000666 for (auto &C : CGSCCOptimizerLateEPCallbacks)
667 C(MainCGPipeline, Level);
668
Chandler Carruth719ffe12017-02-12 05:38:04 +0000669 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
670 // to detect when we devirtualize indirect calls and iterate the SCC passes
671 // in that case to try and catch knock-on inlining or function attrs
672 // opportunities. Then we add it to the module pipeline by walking the SCCs
673 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000674 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000675 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000676 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000677
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000678 return MPM;
679}
680
681ModulePassManager
682PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
683 bool DebugLogging) {
684 ModulePassManager MPM(DebugLogging);
685
686 // Optimize globals now that the module is fully simplified.
687 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000688 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000689
Xinliang David Li126157c2017-05-22 16:41:57 +0000690 // Run partial inlining pass to partially inline functions that have
691 // large bodies.
692 if (RunPartialInlining)
693 MPM.addPass(PartialInlinerPass());
694
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000695 // Remove avail extern fns and globals definitions since we aren't compiling
696 // an object file for later LTO. For LTO we want to preserve these so they
697 // are eligible for inlining at link-time. Note if they are unreferenced they
698 // will be removed by GlobalDCE later, so this only impacts referenced
699 // available externally globals. Eventually they will be suppressed during
700 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
701 // may make globals referenced by available external functions dead and saves
702 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000703 MPM.addPass(EliminateAvailableExternallyPass());
704
705 // Do RPO function attribute inference across the module to forward-propagate
706 // attributes where applicable.
707 // FIXME: Is this really an optimization rather than a canonicalization?
708 MPM.addPass(ReversePostOrderFunctionAttrsPass());
709
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000710 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000711 // useful as the above will have inlined, DCE'ed, and function-attr
712 // propagated everything. We should at this point have a reasonably minimal
713 // and richly annotated call graph. By computing aliasing and mod/ref
714 // information for all local globals here, the late loop passes and notably
715 // the vectorizer will be able to use them to help recognize vectorizable
716 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000717 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000718
719 FunctionPassManager OptimizePM(DebugLogging);
720 OptimizePM.addPass(Float2IntPass());
721 // FIXME: We need to run some loop optimizations to re-rotate loops after
722 // simplify-cfg and others undo their rotation.
723
724 // Optimize the loop execution. These passes operate on entire loop nests
725 // rather than on each loop in an inside-out manner, and so they are actually
726 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000727
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000728 for (auto &C : VectorizerStartEPCallbacks)
729 C(OptimizePM, Level);
730
Chandler Carrutha95ff382017-01-27 00:50:21 +0000731 // First rotate loops that may have been un-rotated by prior passes.
732 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass()));
733
734 // Distribute loops to allow partial vectorization. I.e. isolate dependences
735 // into separate loop that would otherwise inhibit vectorization. This is
736 // currently only performed for loops marked with the metadata
737 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000738 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000739
740 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000741 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000742
Chandler Carruthbaabda92017-01-27 01:32:26 +0000743 // Eliminate loads by forwarding stores from the previous iteration to loads
744 // of the current iteration.
745 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000746
747 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000748 OptimizePM.addPass(InstCombinePass());
749
Chandler Carrutha95ff382017-01-27 00:50:21 +0000750
751 // Now that we've formed fast to execute loop structures, we do further
752 // optimizations. These are run afterward as they might block doing complex
753 // analyses and transforms such as what are needed for loop vectorization.
754
Chandler Carruthe3f50642016-12-22 06:59:15 +0000755 // Optimize parallel scalar instruction chains into SIMD instructions.
756 OptimizePM.addPass(SLPVectorizerPass());
757
Sanjay Pateladf38912017-10-29 20:49:31 +0000758 // Cleanup after all of the vectorizers. Simplification passes like CVP and
759 // GVN, loop transforms, and others have already run, so it's now better to
760 // convert to more optimized IR using more aggressive simplify CFG options.
761 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
762 forwardSwitchCondToPhi(true).
763 convertSwitchToLookupTable(true).
764 needCanonicalLoops(false)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000765 OptimizePM.addPass(InstCombinePass());
766
767 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000768 // execution resources of an out-of-order processor. We also then need to
769 // clean up redundancies and loop invariant code.
770 // FIXME: It would be really good to use a loop-integrated instruction
771 // combiner for cleanup here so that the unrolling and LICM can be pipelined
772 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000773 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000774 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000775 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000776 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass()));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000777
778 // Now that we've vectorized and unrolled loops, we may have more refined
779 // alignment information, try to re-derive it here.
780 OptimizePM.addPass(AlignmentFromAssumptionsPass());
781
Chandler Carrutha95ff382017-01-27 00:50:21 +0000782 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
783 // canonicalization pass that enables other optimizations. As a result,
784 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
785 // result too early.
786 OptimizePM.addPass(LoopSinkPass());
787
788 // And finally clean up LCSSA form before generating code.
789 OptimizePM.addPass(InstSimplifierPass());
790
Sanjay Patel6fd43912017-09-09 13:38:18 +0000791 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
792 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
793 // flattening of blocks.
794 OptimizePM.addPass(DivRemPairsPass());
795
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000796 // LoopSink (and other loop passes since the last simplifyCFG) might have
797 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
798 OptimizePM.addPass(SimplifyCFGPass());
799
Chandler Carrutha95ff382017-01-27 00:50:21 +0000800 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000801 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
802
803 // Now we need to do some global optimization transforms.
804 // FIXME: It would seem like these should come first in the optimization
805 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
806 // ordering here.
807 MPM.addPass(GlobalDCEPass());
808 MPM.addPass(ConstantMergePass());
809
810 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000811}
812
Chandler Carruthe3f50642016-12-22 06:59:15 +0000813ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000814PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
815 bool DebugLogging) {
816 assert(Level != O0 && "Must request optimizations for the default pipeline!");
817
818 ModulePassManager MPM(DebugLogging);
819
820 // Force any function attributes we want the rest of the pipeline to observe.
821 MPM.addPass(ForceFunctionAttrsPass());
822
Dehao Chen08f88312017-08-07 20:23:20 +0000823 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000824 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
825
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000826 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000827 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
828 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000829
830 // Now add the optimization pipeline.
831 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
832
833 return MPM;
834}
835
836ModulePassManager
837PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
838 bool DebugLogging) {
839 assert(Level != O0 && "Must request optimizations for the default pipeline!");
840
841 ModulePassManager MPM(DebugLogging);
842
843 // Force any function attributes we want the rest of the pipeline to observe.
844 MPM.addPass(ForceFunctionAttrsPass());
845
Dehao Chen08f88312017-08-07 20:23:20 +0000846 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000847 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
848
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000849 // If we are planning to perform ThinLTO later, we don't bloat the code with
850 // unrolling/vectorization/... now. Just simplify the module as much as we
851 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000852 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
853 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000854
855 // Run partial inlining pass to partially inline functions that have
856 // large bodies.
857 // FIXME: It isn't clear whether this is really the right place to run this
858 // in ThinLTO. Because there is another canonicalization and simplification
859 // phase that will run after the thin link, running this here ends up with
860 // less information than will be available later and it may grow functions in
861 // ways that aren't beneficial.
862 if (RunPartialInlining)
863 MPM.addPass(PartialInlinerPass());
864
865 // Reduce the size of the IR as much as possible.
866 MPM.addPass(GlobalOptPass());
867
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000868 return MPM;
869}
870
871ModulePassManager
872PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
873 bool DebugLogging) {
874 // FIXME: The summary index is not hooked in the new pass manager yet.
875 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
876 // here.
877
878 ModulePassManager MPM(DebugLogging);
879
880 // Force any function attributes we want the rest of the pipeline to observe.
881 MPM.addPass(ForceFunctionAttrsPass());
882
883 // During the ThinLTO backend phase we perform early indirect call promotion
884 // here, before globalopt. Otherwise imported available_externally functions
885 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000886 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
887 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000888 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000889 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
890 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000891
892 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000893 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
894 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000895
896 // Now add the optimization pipeline.
897 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
898
899 return MPM;
900}
901
902ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000903PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
904 bool DebugLogging) {
905 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000906 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000907 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000908}
909
Chandler Carruthe3f50642016-12-22 06:59:15 +0000910ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
911 bool DebugLogging) {
912 assert(Level != O0 && "Must request optimizations for the default pipeline!");
913 ModulePassManager MPM(DebugLogging);
914
Davide Italiano089a9122017-01-24 00:57:39 +0000915 // Remove unused virtual tables to improve the quality of code generated by
916 // whole-program devirtualization and bitset lowering.
917 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000918
Davide Italiano089a9122017-01-24 00:57:39 +0000919 // Force any function attributes we want the rest of the pipeline to observe.
920 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000921
Davide Italiano089a9122017-01-24 00:57:39 +0000922 // Do basic inference of function attributes from known properties of system
923 // libraries and other oracles.
924 MPM.addPass(InferFunctionAttrsPass());
925
926 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000927 FunctionPassManager EarlyFPM(DebugLogging);
928 EarlyFPM.addPass(CallSiteSplittingPass());
929 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
930
Davide Italiano089a9122017-01-24 00:57:39 +0000931 // Indirect call promotion. This should promote all the targets that are
932 // left by the earlier promotion pass that promotes intra-module targets.
933 // This two-step promotion is to save the compile time. For LTO, it should
934 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000935 MPM.addPass(PGOIndirectCallPromotion(
936 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000937 // Propagate constants at call sites into the functions they call. This
938 // opens opportunities for globalopt (and inlining) by substituting function
939 // pointers passed as arguments to direct uses of functions.
940 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000941
942 // Attach metadata to indirect call sites indicating the set of functions
943 // they may target at run-time. This should follow IPSCCP.
944 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000945 }
946
947 // Now deduce any function attributes based in the current code.
948 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
949 PostOrderFunctionAttrsPass()));
950
951 // Do RPO function attribute inference across the module to forward-propagate
952 // attributes where applicable.
953 // FIXME: Is this really an optimization rather than a canonicalization?
954 MPM.addPass(ReversePostOrderFunctionAttrsPass());
955
956 // Use inragne annotations on GEP indices to split globals where beneficial.
957 MPM.addPass(GlobalSplitPass());
958
959 // Run whole program optimization of virtual call when the list of callees
960 // is fixed.
961 MPM.addPass(WholeProgramDevirtPass());
962
963 // Stop here at -O1.
964 if (Level == 1)
965 return MPM;
966
967 // Optimize globals to try and fold them into constants.
968 MPM.addPass(GlobalOptPass());
969
970 // Promote any localized globals to SSA registers.
971 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
972
973 // Linking modules together can lead to duplicate global constant, only
974 // keep one copy of each constant.
975 MPM.addPass(ConstantMergePass());
976
977 // Remove unused arguments from functions.
978 MPM.addPass(DeadArgumentEliminationPass());
979
980 // Reduce the code after globalopt and ipsccp. Both can open up significant
981 // simplification opportunities, and both can propagate functions through
982 // function pointers. When this happens, we often have to resolve varargs
983 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000984 FunctionPassManager PeepholeFPM(DebugLogging);
985 PeepholeFPM.addPass(InstCombinePass());
986 invokePeepholeEPCallbacks(PeepholeFPM, Level);
987
988 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +0000989
990 // Note: historically, the PruneEH pass was run first to deduce nounwind and
991 // generally clean up exception handling overhead. It isn't clear this is
992 // valuable as the inliner doesn't currently care whether it is inlining an
993 // invoke or a call.
994 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +0000995 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
996 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +0000997
998 // Optimize globals again after we ran the inliner.
999 MPM.addPass(GlobalOptPass());
1000
1001 // Garbage collect dead functions.
1002 // FIXME: Add ArgumentPromotion pass after once it's ported.
1003 MPM.addPass(GlobalDCEPass());
1004
1005 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001006 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001007 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001008 invokePeepholeEPCallbacks(FPM, Level);
1009
Davide Italiano089a9122017-01-24 00:57:39 +00001010 FPM.addPass(JumpThreadingPass());
1011
1012 // Break up allocas
1013 FPM.addPass(SROA());
1014
1015 // Run a few AA driver optimizations here and now to cleanup the code.
1016 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1017
1018 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1019 PostOrderFunctionAttrsPass()));
1020 // FIXME: here we run IP alias analysis in the legacy PM.
1021
1022 FunctionPassManager MainFPM;
1023
1024 // FIXME: once we fix LoopPass Manager, add LICM here.
1025 // FIXME: once we provide support for enabling MLSM, add it here.
1026 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001027 if (RunNewGVN)
1028 MainFPM.addPass(NewGVNPass());
1029 else
1030 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001031
1032 // Remove dead memcpy()'s.
1033 MainFPM.addPass(MemCpyOptPass());
1034
1035 // Nuke dead stores.
1036 MainFPM.addPass(DSEPass());
1037
1038 // FIXME: at this point, we run a bunch of loop passes:
1039 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1040 // loopVectorize. Enable them once the remaining issue with LPM
1041 // are sorted out.
1042
1043 MainFPM.addPass(InstCombinePass());
1044 MainFPM.addPass(SimplifyCFGPass());
1045 MainFPM.addPass(SCCPPass());
1046 MainFPM.addPass(InstCombinePass());
1047 MainFPM.addPass(BDCEPass());
1048
1049 // FIXME: We may want to run SLPVectorizer here.
1050 // After vectorization, assume intrinsics may tell us more
1051 // about pointer alignments.
1052#if 0
1053 MainFPM.add(AlignmentFromAssumptionsPass());
1054#endif
1055
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001056 // FIXME: Conditionally run LoadCombine here, after it's ported
1057 // (in case we still have this pass, given its questionable usefulness).
1058
Davide Italiano089a9122017-01-24 00:57:39 +00001059 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001060 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001061 MainFPM.addPass(JumpThreadingPass());
1062 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1063
1064 // Create a function that performs CFI checks for cross-DSO calls with
1065 // targets in the current module.
1066 MPM.addPass(CrossDSOCFIPass());
1067
1068 // Lower type metadata and the type.test intrinsic. This pass supports
1069 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1070 // to be run at link time if CFI is enabled. This pass does nothing if
1071 // CFI is disabled.
1072 // Enable once we add support for the summary in the new PM.
1073#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001074 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1075 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001076 Summary));
1077#endif
1078
1079 // Add late LTO optimization passes.
1080 // Delete basic blocks, which optimization passes may have killed.
1081 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1082
1083 // Drop bodies of available eternally objects to improve GlobalDCE.
1084 MPM.addPass(EliminateAvailableExternallyPass());
1085
1086 // Now that we have optimized the program, discard unreachable functions.
1087 MPM.addPass(GlobalDCEPass());
1088
1089 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001090 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001091}
1092
Chandler Carruth060ad612016-12-23 20:38:19 +00001093AAManager PassBuilder::buildDefaultAAPipeline() {
1094 AAManager AA;
1095
1096 // The order in which these are registered determines their priority when
1097 // being queried.
1098
1099 // First we register the basic alias analysis that provides the majority of
1100 // per-function local AA logic. This is a stateless, on-demand local set of
1101 // AA techniques.
1102 AA.registerFunctionAnalysis<BasicAA>();
1103
1104 // Next we query fast, specialized alias analyses that wrap IR-embedded
1105 // information about aliasing.
1106 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1107 AA.registerFunctionAnalysis<TypeBasedAA>();
1108
1109 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001110 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1111 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001112 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001113 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001114
1115 return AA;
1116}
1117
Chandler Carruth241bf242016-08-03 07:44:48 +00001118static Optional<int> parseRepeatPassName(StringRef Name) {
1119 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1120 return None;
1121 int Count;
1122 if (Name.getAsInteger(0, Count) || Count <= 0)
1123 return None;
1124 return Count;
1125}
1126
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001127static Optional<int> parseDevirtPassName(StringRef Name) {
1128 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1129 return None;
1130 int Count;
1131 if (Name.getAsInteger(0, Count) || Count <= 0)
1132 return None;
1133 return Count;
1134}
1135
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001136/// Tests whether a pass name starts with a valid prefix for a default pipeline
1137/// alias.
1138static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1139 return Name.startswith("default") || Name.startswith("thinlto") ||
1140 Name.startswith("lto");
1141}
1142
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001143/// Tests whether registered callbacks will accept a given pass name.
1144///
1145/// When parsing a pipeline text, the type of the outermost pipeline may be
1146/// omitted, in which case the type is automatically determined from the first
1147/// pass name in the text. This may be a name that is handled through one of the
1148/// callbacks. We check this through the oridinary parsing callbacks by setting
1149/// up a dummy PassManager in order to not force the client to also handle this
1150/// type of query.
1151template <typename PassManagerT, typename CallbacksT>
1152static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1153 if (!Callbacks.empty()) {
1154 PassManagerT DummyPM;
1155 for (auto &CB : Callbacks)
1156 if (CB(Name, DummyPM, {}))
1157 return true;
1158 }
1159 return false;
1160}
1161
1162template <typename CallbacksT>
1163static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001164 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001165 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001166 return DefaultAliasRegex.match(Name);
1167
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001168 // Explicitly handle pass manager names.
1169 if (Name == "module")
1170 return true;
1171 if (Name == "cgscc")
1172 return true;
1173 if (Name == "function")
1174 return true;
1175
Chandler Carruth241bf242016-08-03 07:44:48 +00001176 // Explicitly handle custom-parsed pass names.
1177 if (parseRepeatPassName(Name))
1178 return true;
1179
Chandler Carruth74a8a222016-06-17 07:15:29 +00001180#define MODULE_PASS(NAME, CREATE_PASS) \
1181 if (Name == NAME) \
1182 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001183#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001184 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001185 return true;
1186#include "PassRegistry.def"
1187
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001188 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001189}
1190
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001191template <typename CallbacksT>
1192static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001193 // Explicitly handle pass manager names.
1194 if (Name == "cgscc")
1195 return true;
1196 if (Name == "function")
1197 return true;
1198
Chandler Carruth241bf242016-08-03 07:44:48 +00001199 // Explicitly handle custom-parsed pass names.
1200 if (parseRepeatPassName(Name))
1201 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001202 if (parseDevirtPassName(Name))
1203 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001204
Chandler Carruth74a8a222016-06-17 07:15:29 +00001205#define CGSCC_PASS(NAME, CREATE_PASS) \
1206 if (Name == NAME) \
1207 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001208#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001209 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001210 return true;
1211#include "PassRegistry.def"
1212
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001213 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001214}
1215
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001216template <typename CallbacksT>
1217static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001218 // Explicitly handle pass manager names.
1219 if (Name == "function")
1220 return true;
1221 if (Name == "loop")
1222 return true;
1223
Chandler Carruth241bf242016-08-03 07:44:48 +00001224 // Explicitly handle custom-parsed pass names.
1225 if (parseRepeatPassName(Name))
1226 return true;
1227
Chandler Carruth74a8a222016-06-17 07:15:29 +00001228#define FUNCTION_PASS(NAME, CREATE_PASS) \
1229 if (Name == NAME) \
1230 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001231#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001232 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001233 return true;
1234#include "PassRegistry.def"
1235
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001236 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001237}
1238
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001239template <typename CallbacksT>
1240static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001241 // Explicitly handle pass manager names.
1242 if (Name == "loop")
1243 return true;
1244
Chandler Carruth241bf242016-08-03 07:44:48 +00001245 // Explicitly handle custom-parsed pass names.
1246 if (parseRepeatPassName(Name))
1247 return true;
1248
Chandler Carruth74a8a222016-06-17 07:15:29 +00001249#define LOOP_PASS(NAME, CREATE_PASS) \
1250 if (Name == NAME) \
1251 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001252#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1253 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1254 return true;
1255#include "PassRegistry.def"
1256
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001257 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001258}
1259
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001260Optional<std::vector<PassBuilder::PipelineElement>>
1261PassBuilder::parsePipelineText(StringRef Text) {
1262 std::vector<PipelineElement> ResultPipeline;
1263
1264 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1265 &ResultPipeline};
1266 for (;;) {
1267 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1268 size_t Pos = Text.find_first_of(",()");
1269 Pipeline.push_back({Text.substr(0, Pos), {}});
1270
1271 // If we have a single terminating name, we're done.
1272 if (Pos == Text.npos)
1273 break;
1274
1275 char Sep = Text[Pos];
1276 Text = Text.substr(Pos + 1);
1277 if (Sep == ',')
1278 // Just a name ending in a comma, continue.
1279 continue;
1280
1281 if (Sep == '(') {
1282 // Push the inner pipeline onto the stack to continue processing.
1283 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1284 continue;
1285 }
1286
1287 assert(Sep == ')' && "Bogus separator!");
1288 // When handling the close parenthesis, we greedily consume them to avoid
1289 // empty strings in the pipeline.
1290 do {
1291 // If we try to pop the outer pipeline we have unbalanced parentheses.
1292 if (PipelineStack.size() == 1)
1293 return None;
1294
1295 PipelineStack.pop_back();
1296 } while (Text.consume_front(")"));
1297
1298 // Check if we've finished parsing.
1299 if (Text.empty())
1300 break;
1301
1302 // Otherwise, the end of an inner pipeline always has to be followed by
1303 // a comma, and then we can continue.
1304 if (!Text.consume_front(","))
1305 return None;
1306 }
1307
1308 if (PipelineStack.size() > 1)
1309 // Unbalanced paretheses.
1310 return None;
1311
1312 assert(PipelineStack.back() == &ResultPipeline &&
1313 "Wrong pipeline at the bottom of the stack!");
1314 return {std::move(ResultPipeline)};
1315}
1316
1317bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1318 const PipelineElement &E, bool VerifyEachPass,
1319 bool DebugLogging) {
1320 auto &Name = E.Name;
1321 auto &InnerPipeline = E.InnerPipeline;
1322
1323 // First handle complex passes like the pass managers which carry pipelines.
1324 if (!InnerPipeline.empty()) {
1325 if (Name == "module") {
1326 ModulePassManager NestedMPM(DebugLogging);
1327 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1328 DebugLogging))
1329 return false;
1330 MPM.addPass(std::move(NestedMPM));
1331 return true;
1332 }
1333 if (Name == "cgscc") {
1334 CGSCCPassManager CGPM(DebugLogging);
1335 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1336 DebugLogging))
1337 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001338 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001339 return true;
1340 }
1341 if (Name == "function") {
1342 FunctionPassManager FPM(DebugLogging);
1343 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1344 DebugLogging))
1345 return false;
1346 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1347 return true;
1348 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001349 if (auto Count = parseRepeatPassName(Name)) {
1350 ModulePassManager NestedMPM(DebugLogging);
1351 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1352 DebugLogging))
1353 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001354 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001355 return true;
1356 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001357
1358 for (auto &C : ModulePipelineParsingCallbacks)
1359 if (C(Name, MPM, InnerPipeline))
1360 return true;
1361
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001362 // Normal passes can't have pipelines.
1363 return false;
1364 }
1365
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001366 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001367 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001368 SmallVector<StringRef, 3> Matches;
1369 if (!DefaultAliasRegex.match(Name, &Matches))
1370 return false;
1371 assert(Matches.size() == 3 && "Must capture two matched strings!");
1372
Jordan Rosef85a95f2016-07-25 18:34:51 +00001373 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001374 .Case("O0", O0)
1375 .Case("O1", O1)
1376 .Case("O2", O2)
1377 .Case("O3", O3)
1378 .Case("Os", Os)
1379 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001380 if (L == O0)
1381 // At O0 we do nothing at all!
1382 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001383
1384 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001385 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001386 } else if (Matches[1] == "thinlto-pre-link") {
1387 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1388 } else if (Matches[1] == "thinlto") {
1389 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001390 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001391 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001392 } else {
1393 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001394 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001395 }
1396 return true;
1397 }
1398
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001399 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001400#define MODULE_PASS(NAME, CREATE_PASS) \
1401 if (Name == NAME) { \
1402 MPM.addPass(CREATE_PASS); \
1403 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001404 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001405#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1406 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001407 MPM.addPass( \
1408 RequireAnalysisPass< \
1409 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001410 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001411 } \
1412 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001413 MPM.addPass(InvalidateAnalysisPass< \
1414 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001415 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001416 }
1417#include "PassRegistry.def"
1418
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001419 for (auto &C : ModulePipelineParsingCallbacks)
1420 if (C(Name, MPM, InnerPipeline))
1421 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001422 return false;
1423}
1424
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001425bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1426 const PipelineElement &E, bool VerifyEachPass,
1427 bool DebugLogging) {
1428 auto &Name = E.Name;
1429 auto &InnerPipeline = E.InnerPipeline;
1430
1431 // First handle complex passes like the pass managers which carry pipelines.
1432 if (!InnerPipeline.empty()) {
1433 if (Name == "cgscc") {
1434 CGSCCPassManager NestedCGPM(DebugLogging);
1435 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1436 DebugLogging))
1437 return false;
1438 // Add the nested pass manager with the appropriate adaptor.
1439 CGPM.addPass(std::move(NestedCGPM));
1440 return true;
1441 }
1442 if (Name == "function") {
1443 FunctionPassManager FPM(DebugLogging);
1444 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1445 DebugLogging))
1446 return false;
1447 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001448 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001449 return true;
1450 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001451 if (auto Count = parseRepeatPassName(Name)) {
1452 CGSCCPassManager NestedCGPM(DebugLogging);
1453 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1454 DebugLogging))
1455 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001456 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001457 return true;
1458 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001459 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1460 CGSCCPassManager NestedCGPM(DebugLogging);
1461 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1462 DebugLogging))
1463 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001464 CGPM.addPass(
1465 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001466 return true;
1467 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001468
1469 for (auto &C : CGSCCPipelineParsingCallbacks)
1470 if (C(Name, CGPM, InnerPipeline))
1471 return true;
1472
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001473 // Normal passes can't have pipelines.
1474 return false;
1475 }
1476
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001477// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001478#define CGSCC_PASS(NAME, CREATE_PASS) \
1479 if (Name == NAME) { \
1480 CGPM.addPass(CREATE_PASS); \
1481 return true; \
1482 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001483#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1484 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001485 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001486 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001487 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1488 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001489 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001490 } \
1491 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001492 CGPM.addPass(InvalidateAnalysisPass< \
1493 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001494 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001495 }
1496#include "PassRegistry.def"
1497
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001498 for (auto &C : CGSCCPipelineParsingCallbacks)
1499 if (C(Name, CGPM, InnerPipeline))
1500 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001501 return false;
1502}
1503
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001504bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1505 const PipelineElement &E,
1506 bool VerifyEachPass, bool DebugLogging) {
1507 auto &Name = E.Name;
1508 auto &InnerPipeline = E.InnerPipeline;
1509
1510 // First handle complex passes like the pass managers which carry pipelines.
1511 if (!InnerPipeline.empty()) {
1512 if (Name == "function") {
1513 FunctionPassManager NestedFPM(DebugLogging);
1514 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1515 DebugLogging))
1516 return false;
1517 // Add the nested pass manager with the appropriate adaptor.
1518 FPM.addPass(std::move(NestedFPM));
1519 return true;
1520 }
1521 if (Name == "loop") {
1522 LoopPassManager LPM(DebugLogging);
1523 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1524 DebugLogging))
1525 return false;
1526 // Add the nested pass manager with the appropriate adaptor.
1527 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM)));
1528 return true;
1529 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001530 if (auto Count = parseRepeatPassName(Name)) {
1531 FunctionPassManager NestedFPM(DebugLogging);
1532 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1533 DebugLogging))
1534 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001535 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001536 return true;
1537 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001538
1539 for (auto &C : FunctionPipelineParsingCallbacks)
1540 if (C(Name, FPM, InnerPipeline))
1541 return true;
1542
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001543 // Normal passes can't have pipelines.
1544 return false;
1545 }
1546
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001547// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001548#define FUNCTION_PASS(NAME, CREATE_PASS) \
1549 if (Name == NAME) { \
1550 FPM.addPass(CREATE_PASS); \
1551 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001552 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001553#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1554 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001555 FPM.addPass( \
1556 RequireAnalysisPass< \
1557 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001558 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001559 } \
1560 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001561 FPM.addPass(InvalidateAnalysisPass< \
1562 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001563 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001564 }
1565#include "PassRegistry.def"
1566
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001567 for (auto &C : FunctionPipelineParsingCallbacks)
1568 if (C(Name, FPM, InnerPipeline))
1569 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001570 return false;
1571}
1572
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001573bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001574 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001575 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001576 auto &InnerPipeline = E.InnerPipeline;
1577
1578 // First handle complex passes like the pass managers which carry pipelines.
1579 if (!InnerPipeline.empty()) {
1580 if (Name == "loop") {
1581 LoopPassManager NestedLPM(DebugLogging);
1582 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1583 DebugLogging))
1584 return false;
1585 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001586 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001587 return true;
1588 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001589 if (auto Count = parseRepeatPassName(Name)) {
1590 LoopPassManager NestedLPM(DebugLogging);
1591 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1592 DebugLogging))
1593 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001594 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001595 return true;
1596 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001597
1598 for (auto &C : LoopPipelineParsingCallbacks)
1599 if (C(Name, LPM, InnerPipeline))
1600 return true;
1601
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001602 // Normal passes can't have pipelines.
1603 return false;
1604 }
1605
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001606// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001607#define LOOP_PASS(NAME, CREATE_PASS) \
1608 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001609 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001610 return true; \
1611 }
1612#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1613 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001614 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001615 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1616 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1617 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001618 return true; \
1619 } \
1620 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001621 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001622 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001623 return true; \
1624 }
1625#include "PassRegistry.def"
1626
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001627 for (auto &C : LoopPipelineParsingCallbacks)
1628 if (C(Name, LPM, InnerPipeline))
1629 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001630 return false;
1631}
1632
Chandler Carruthedf59962016-02-18 09:45:17 +00001633bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001634#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1635 if (Name == NAME) { \
1636 AA.registerModuleAnalysis< \
1637 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1638 return true; \
1639 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001640#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1641 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001642 AA.registerFunctionAnalysis< \
1643 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001644 return true; \
1645 }
1646#include "PassRegistry.def"
1647
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001648 for (auto &C : AAParsingCallbacks)
1649 if (C(Name, AA))
1650 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001651 return false;
1652}
1653
Justin Bognereecc3c82016-02-25 07:23:08 +00001654bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001655 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001656 bool VerifyEachPass,
1657 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001658 for (const auto &Element : Pipeline) {
1659 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1660 return false;
1661 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001662 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001663 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001664}
1665
Chandler Carruth1ff77242015-03-07 09:02:36 +00001666bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001667 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001668 bool VerifyEachPass,
1669 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001670 for (const auto &Element : Pipeline) {
1671 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1672 return false;
1673 if (VerifyEachPass)
1674 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001675 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001676 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001677}
1678
Chandler Carruth1ff77242015-03-07 09:02:36 +00001679bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001680 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001681 bool VerifyEachPass,
1682 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001683 for (const auto &Element : Pipeline) {
1684 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1685 return false;
1686 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001687 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001688 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001689}
1690
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001691void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1692 FunctionAnalysisManager &FAM,
1693 CGSCCAnalysisManager &CGAM,
1694 ModuleAnalysisManager &MAM) {
1695 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1696 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001697 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1698 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1699 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1700 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1701 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1702}
1703
Chandler Carruth1ff77242015-03-07 09:02:36 +00001704bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001706 bool VerifyEachPass,
1707 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001708 for (const auto &Element : Pipeline) {
1709 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1710 return false;
1711 if (VerifyEachPass)
1712 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001713 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001714 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001715}
1716
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001717// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001718// FIXME: Should this routine accept a TargetMachine or require the caller to
1719// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001720bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1721 StringRef PipelineText, bool VerifyEachPass,
1722 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001723 auto Pipeline = parsePipelineText(PipelineText);
1724 if (!Pipeline || Pipeline->empty())
1725 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001726
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001727 // If the first name isn't at the module layer, wrap the pipeline up
1728 // automatically.
1729 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001730
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001731 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1732 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001733 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001734 } else if (isFunctionPassName(FirstName,
1735 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001736 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001737 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001738 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001739 } else {
1740 for (auto &C : TopLevelPipelineParsingCallbacks)
1741 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1742 return true;
1743
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001744 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001745 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001746 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001747 }
1748
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001749 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001750}
Chandler Carruthedf59962016-02-18 09:45:17 +00001751
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001752// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1753bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1754 StringRef PipelineText, bool VerifyEachPass,
1755 bool DebugLogging) {
1756 auto Pipeline = parsePipelineText(PipelineText);
1757 if (!Pipeline || Pipeline->empty())
1758 return false;
1759
1760 StringRef FirstName = Pipeline->front().Name;
1761 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1762 return false;
1763
1764 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1765}
1766
1767// Primary pass pipeline description parsing routine for a \c
1768// FunctionPassManager
1769bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1770 StringRef PipelineText, bool VerifyEachPass,
1771 bool DebugLogging) {
1772 auto Pipeline = parsePipelineText(PipelineText);
1773 if (!Pipeline || Pipeline->empty())
1774 return false;
1775
1776 StringRef FirstName = Pipeline->front().Name;
1777 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1778 return false;
1779
1780 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1781 DebugLogging);
1782}
1783
1784// Primary pass pipeline description parsing routine for a \c LoopPassManager
1785bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1786 StringRef PipelineText, bool VerifyEachPass,
1787 bool DebugLogging) {
1788 auto Pipeline = parsePipelineText(PipelineText);
1789 if (!Pipeline || Pipeline->empty())
1790 return false;
1791
1792 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1793}
1794
Chandler Carruthedf59962016-02-18 09:45:17 +00001795bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001796 // If the pipeline just consists of the word 'default' just replace the AA
1797 // manager with our default one.
1798 if (PipelineText == "default") {
1799 AA = buildDefaultAAPipeline();
1800 return true;
1801 }
1802
Chandler Carruthedf59962016-02-18 09:45:17 +00001803 while (!PipelineText.empty()) {
1804 StringRef Name;
1805 std::tie(Name, PipelineText) = PipelineText.split(',');
1806 if (!parseAAPassName(AA, Name))
1807 return false;
1808 }
1809
1810 return true;
1811}