blob: 07c6f55afd666e2014deaa3e777fc2149561b5ab [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"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000025#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000026#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000027#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
28#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000029#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000030#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000031#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000032#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000033#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000034#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000035#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000036#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000037#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000038#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000039#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000040#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000041#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000042#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000043#include "llvm/Analysis/OptimizationRemarkEmitter.h"
Hongbin Zheng3f978402016-02-25 17:54:07 +000044#include "llvm/Analysis/PostDominators.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +000045#include "llvm/Analysis/ProfileSummaryInfo.h"
Hongbin Zhengbc539772016-02-25 17:54:25 +000046#include "llvm/Analysis/RegionInfo.h"
Chandler Carruth2f1fd162015-08-17 02:08:17 +000047#include "llvm/Analysis/ScalarEvolution.h"
Chandler Carruth2b3d0442016-02-20 04:01:45 +000048#include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
Chandler Carruthd6091a02016-02-20 04:03:06 +000049#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000050#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000051#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000052#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000053#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000054#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000055#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000056#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000057#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000058#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000059#include "llvm/Support/Debug.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000060#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000061#include "llvm/Target/TargetMachine.h"
Xinliang David Li64dbb292016-06-05 05:12:23 +000062#include "llvm/Transforms/GCOVProfiler.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000063#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000064#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000065#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000066#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000067#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000068#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000069#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000070#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000071#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000072#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000073#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000074#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000075#include "llvm/Transforms/IPO/GlobalSplit.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000076#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000077#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000078#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000079#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000080#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000081#include "llvm/Transforms/IPO/SCCP.h"
Justin Bogner21e15372015-10-30 23:28:12 +000082#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000083#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000084#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000085#include "llvm/Transforms/InstCombine/InstCombine.h"
Xinliang David Lie6b89292016-04-18 17:47:38 +000086#include "llvm/Transforms/InstrProfiling.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000087#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Xinliang David Li8aebf442016-05-06 05:49:19 +000088#include "llvm/Transforms/PGOInstrumentation.h"
Xinliang David Lid38392e2016-05-27 23:20:16 +000089#include "llvm/Transforms/SampleProfile.h"
Justin Bogner19b67992015-10-30 23:13:18 +000090#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +000091#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +000092#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +000093#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +000094#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +000095#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +000096#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +000097#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +000098#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +000099#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000100#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000101#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000102#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000103#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000104#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Sean Silva46590d52016-06-14 00:51:09 +0000105#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000106#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000107#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000108#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000109#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000110#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000111#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Dehao Chendcafd5e2016-07-15 16:42:11 +0000112#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000113#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000114#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000115#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000116#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000117#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000118#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000119#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000120#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000121#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000122#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000123#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Sean Silva6347df02016-06-14 02:44:55 +0000124#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000125#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000126#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000127#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000128#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000129#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000130#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000131#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000132#include "llvm/Transforms/Scalar/SROA.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000133#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000134#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000135#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000136#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000137#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000138#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000139#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000140#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000141#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000142#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000143#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000144#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000145#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000146#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000147#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Davide Italiano16284df2016-07-07 21:14:36 +0000148#include "llvm/Transforms/Utils/SimplifyInstructions.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000149#include "llvm/Transforms/Utils/SymbolRewriter.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000150#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000151#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000152
Chandler Carruth66445382014-01-11 08:16:35 +0000153using namespace llvm;
154
Chandler Carruth719ffe12017-02-12 05:38:04 +0000155static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
156 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000157static cl::opt<bool>
158 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
159 cl::Hidden, cl::ZeroOrMore,
160 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000161
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000162static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000163 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000164 cl::Hidden, cl::ZeroOrMore,
165 cl::desc("Run NewGVN instead of GVN"));
166
Geoff Berry3cca1da2017-06-10 15:20:03 +0000167static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000168 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
169 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000170
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000171static cl::opt<bool> EnableGVNHoist(
172 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
173 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
174
Davide Italianobe1b6a92017-06-03 23:18:29 +0000175static cl::opt<bool> EnableGVNSink(
176 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
177 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
178
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000179static cl::opt<bool> EnableSyntheticCounts(
180 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
181 cl::desc("Run synthetic function entry count generation "
182 "pass"));
183
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000184static Regex DefaultAliasRegex(
185 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000186
Chandler Carruthe3f50642016-12-22 06:59:15 +0000187static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
188 switch (Level) {
189 case PassBuilder::O0:
190 case PassBuilder::O1:
191 case PassBuilder::O2:
192 case PassBuilder::O3:
193 return false;
194
195 case PassBuilder::Os:
196 case PassBuilder::Oz:
197 return true;
198 }
199 llvm_unreachable("Invalid optimization level!");
200}
201
Chandler Carruth66445382014-01-11 08:16:35 +0000202namespace {
203
Chandler Carruthd8330982014-01-12 09:34:22 +0000204/// \brief No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000205struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000206 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000207 return PreservedAnalyses::all();
208 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000209 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000210};
211
Chandler Carruth0b576b32015-01-06 02:50:06 +0000212/// \brief No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000213class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
214 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000215 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000216
217public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000218 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000219 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000220 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000221};
222
Chandler Carruth572e3402014-04-21 11:12:00 +0000223/// \brief No-op CGSCC pass which does nothing.
224struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000225 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
226 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000227 return PreservedAnalyses::all();
228 }
229 static StringRef name() { return "NoOpCGSCCPass"; }
230};
231
Chandler Carruth0b576b32015-01-06 02:50:06 +0000232/// \brief No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000233class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
234 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000235 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000236
237public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000238 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000239 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000240 return Result();
241 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000242 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000243};
244
Chandler Carruthd8330982014-01-12 09:34:22 +0000245/// \brief No-op function pass which does nothing.
246struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000247 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000248 return PreservedAnalyses::all();
249 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000250 static StringRef name() { return "NoOpFunctionPass"; }
251};
252
Chandler Carruth0b576b32015-01-06 02:50:06 +0000253/// \brief No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000254class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
255 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000256 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000257
258public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000259 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000260 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000261 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000262};
263
Justin Bognereecc3c82016-02-25 07:23:08 +0000264/// \brief No-op loop pass which does nothing.
265struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000266 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
267 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000268 return PreservedAnalyses::all();
269 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000270 static StringRef name() { return "NoOpLoopPass"; }
271};
272
273/// \brief No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000274class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
275 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000276 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000277
278public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000279 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000280 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
281 return Result();
282 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000283 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000284};
285
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000286AnalysisKey NoOpModuleAnalysis::Key;
287AnalysisKey NoOpCGSCCAnalysis::Key;
288AnalysisKey NoOpFunctionAnalysis::Key;
289AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000290
Chandler Carruth66445382014-01-11 08:16:35 +0000291} // End anonymous namespace.
292
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000293void PassBuilder::invokePeepholeEPCallbacks(
294 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
295 for (auto &C : PeepholeEPCallbacks)
296 C(FPM, Level);
297}
298
Chandler Carruth1ff77242015-03-07 09:02:36 +0000299void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000300#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000301 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000302#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000303
304 for (auto &C : ModuleAnalysisRegistrationCallbacks)
305 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000306}
307
Chandler Carruth1ff77242015-03-07 09:02:36 +0000308void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000309#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000310 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000311#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000312
313 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
314 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000315}
316
Chandler Carruth1ff77242015-03-07 09:02:36 +0000317void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000318#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000319 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000320#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000321
322 for (auto &C : FunctionAnalysisRegistrationCallbacks)
323 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000324}
325
Justin Bognereecc3c82016-02-25 07:23:08 +0000326void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000327#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000328 LAM.registerPass([&] { return CREATE_PASS; });
329#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000330
331 for (auto &C : LoopAnalysisRegistrationCallbacks)
332 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000333}
334
Chandler Carruthe3f50642016-12-22 06:59:15 +0000335FunctionPassManager
336PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000337 ThinLTOPhase Phase,
338 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000339 assert(Level != O0 && "Must request optimizations!");
340 FunctionPassManager FPM(DebugLogging);
341
342 // Form SSA out of local memory accesses after breaking apart aggregates into
343 // scalars.
344 FPM.addPass(SROA());
345
346 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000347 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000348
Davide Italianoc3688312017-06-01 23:08:14 +0000349 // Hoisting of scalars and load expressions.
350 if (EnableGVNHoist)
351 FPM.addPass(GVNHoistPass());
352
Davide Italianobe1b6a92017-06-03 23:18:29 +0000353 // Global value numbering based sinking.
354 if (EnableGVNSink) {
355 FPM.addPass(GVNSinkPass());
356 FPM.addPass(SimplifyCFGPass());
357 }
358
Chandler Carruthe3f50642016-12-22 06:59:15 +0000359 // Speculative execution if the target has divergent branches; otherwise nop.
360 FPM.addPass(SpeculativeExecutionPass());
361
362 // Optimize based on known information about branches, and cleanup afterward.
363 FPM.addPass(JumpThreadingPass());
364 FPM.addPass(CorrelatedValuePropagationPass());
365 FPM.addPass(SimplifyCFGPass());
366 FPM.addPass(InstCombinePass());
367
368 if (!isOptimizingForSize(Level))
369 FPM.addPass(LibCallsShrinkWrapPass());
370
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000371 invokePeepholeEPCallbacks(FPM, Level);
372
Rong Xue1f42452017-10-23 22:21:29 +0000373 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
374 // using the size value profile. Don't perform this when optimizing for size.
375 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
376 !isOptimizingForSize(Level))
377 FPM.addPass(PGOMemOPSizeOpt());
378
Chandler Carruthe3f50642016-12-22 06:59:15 +0000379 FPM.addPass(TailCallElimPass());
380 FPM.addPass(SimplifyCFGPass());
381
382 // Form canonically associated expression trees, and simplify the trees using
383 // basic mathematical properties. For example, this will form (nearly)
384 // minimal multiplication trees.
385 FPM.addPass(ReassociatePass());
386
387 // Add the primary loop simplification pipeline.
388 // FIXME: Currently this is split into two loop pass pipelines because we run
389 // some function passes in between them. These can and should be replaced by
390 // loop pass equivalenst but those aren't ready yet. Specifically,
391 // `SimplifyCFGPass` and `InstCombinePass` are used. We have
392 // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to
393 // the other we have is `LoopInstSimplify`.
394 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
395
396 // Rotate Loop - disable header duplication at -Oz
397 LPM1.addPass(LoopRotatePass(Level != Oz));
398 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000399 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000400 LPM2.addPass(IndVarSimplifyPass());
401 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000402
403 for (auto &C : LateLoopOptimizationsEPCallbacks)
404 C(LPM2, Level);
405
Chandler Carruth79b733b2017-01-26 23:21:17 +0000406 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000407 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000408 // because it changes IR to makes profile annotation in back compile
409 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000410 if (Phase != ThinLTOPhase::PreLink ||
411 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000412 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000413
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000414 for (auto &C : LoopOptimizerEndEPCallbacks)
415 C(LPM2, Level);
416
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000417 // We provide the opt remark emitter pass for LICM to use. We only need to do
418 // this once as it is immutable.
419 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000420 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000421 FPM.addPass(SimplifyCFGPass());
422 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000423 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000424
425 // Eliminate redundancies.
426 if (Level != O1) {
427 // These passes add substantial compile time so skip them at O1.
428 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000429 if (RunNewGVN)
430 FPM.addPass(NewGVNPass());
431 else
432 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000433 }
434
435 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
436 FPM.addPass(MemCpyOptPass());
437
438 // Sparse conditional constant propagation.
439 // FIXME: It isn't clear why we do this *after* loop passes rather than
440 // before...
441 FPM.addPass(SCCPPass());
442
443 // Delete dead bit computations (instcombine runs after to fold away the dead
444 // computations, and then ADCE will run later to exploit any new DCE
445 // opportunities that creates).
446 FPM.addPass(BDCEPass());
447
448 // Run instcombine after redundancy and dead bit elimination to exploit
449 // opportunities opened up by them.
450 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000451 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000452
453 // Re-consider control flow based optimizations after redundancy elimination,
454 // redo DCE, etc.
455 FPM.addPass(JumpThreadingPass());
456 FPM.addPass(CorrelatedValuePropagationPass());
457 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000458 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000459
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000460 for (auto &C : ScalarOptimizerLateEPCallbacks)
461 C(FPM, Level);
462
Chandler Carruthe3f50642016-12-22 06:59:15 +0000463 // Finally, do an expensive DCE pass to catch all the dead code exposed by
464 // the simplifications and basic cleanup after all the simplifications.
465 FPM.addPass(ADCEPass());
466 FPM.addPass(SimplifyCFGPass());
467 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000468 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000469
470 return FPM;
471}
472
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000473void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
474 PassBuilder::OptimizationLevel Level,
475 bool RunProfileGen,
476 std::string ProfileGenFile,
477 std::string ProfileUseFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000478 // Generally running simplification passes and the inliner with an high
479 // threshold results in smaller executables, but there may be cases where
480 // the size grows, so let's be conservative here and skip this simplification
481 // at -Os/Oz.
482 if (!isOptimizingForSize(Level)) {
483 InlineParams IP;
484
485 // In the old pass manager, this is a cl::opt. Should still this be one?
486 IP.DefaultThreshold = 75;
487
488 // FIXME: The hint threshold has the same value used by the regular inliner.
489 // This should probably be lowered after performance testing.
490 // FIXME: this comment is cargo culted from the old pass manager, revisit).
491 IP.HintThreshold = 325;
492
493 CGSCCPassManager CGPipeline(DebugLogging);
494
495 CGPipeline.addPass(InlinerPass(IP));
496
497 FunctionPassManager FPM;
498 FPM.addPass(SROA());
499 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
500 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
501 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000502 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000503
Davide Italiano513dfaa2017-02-13 15:26:22 +0000504 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
505
506 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
507 }
508
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000509 // Delete anything that is now dead to make sure that we don't instrument
510 // dead code. Instrumentation can end up keeping dead code around and
511 // dramatically increase code size.
512 MPM.addPass(GlobalDCEPass());
513
Davide Italiano513dfaa2017-02-13 15:26:22 +0000514 if (RunProfileGen) {
515 MPM.addPass(PGOInstrumentationGen());
516
Xinliang David Lib67530e2017-06-25 00:26:43 +0000517 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000518 FPM.addPass(
519 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000520 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
521
Davide Italiano513dfaa2017-02-13 15:26:22 +0000522 // Add the profile lowering pass.
523 InstrProfOptions Options;
524 if (!ProfileGenFile.empty())
525 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000526 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000527 MPM.addPass(InstrProfiling(Options));
528 }
529
530 if (!ProfileUseFile.empty())
531 MPM.addPass(PGOInstrumentationUse(ProfileUseFile));
532}
533
Easwaran Raman8249fac2017-06-28 13:33:49 +0000534static InlineParams
535getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
536 auto O3 = PassBuilder::O3;
537 unsigned OptLevel = Level > O3 ? 2 : Level;
538 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
539 return getInlineParams(OptLevel, SizeLevel);
540}
541
Chandler Carruthe3f50642016-12-22 06:59:15 +0000542ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000543PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000544 ThinLTOPhase Phase,
545 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000546 ModulePassManager MPM(DebugLogging);
547
Chandler Carruthe3f50642016-12-22 06:59:15 +0000548 // Do basic inference of function attributes from known properties of system
549 // libraries and other oracles.
550 MPM.addPass(InferFunctionAttrsPass());
551
552 // Create an early function pass manager to cleanup the output of the
553 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000554 FunctionPassManager EarlyFPM(DebugLogging);
555 EarlyFPM.addPass(SimplifyCFGPass());
556 EarlyFPM.addPass(SROA());
557 EarlyFPM.addPass(EarlyCSEPass());
558 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000559 if (Level == O3)
560 EarlyFPM.addPass(CallSiteSplittingPass());
561
Dehao Chen08f88312017-08-07 20:23:20 +0000562 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
563 // to convert bitcast to direct calls so that they can be inlined during the
564 // profile annotation prepration step.
565 // More details about SamplePGO design can be found in:
566 // https://research.google.com/pubs/pub45290.html
567 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
568 if (PGOOpt && !PGOOpt->SampleProfileFile.empty() &&
569 Phase == ThinLTOPhase::PostLink)
570 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000571 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000572
Dehao Chen08f88312017-08-07 20:23:20 +0000573 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
574 // Annotate sample profile right after early FPM to ensure freshness of
575 // the debug info.
Dehao Chend26dae02017-10-01 05:24:51 +0000576 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
577 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000578 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
579 // for the profile annotation to be accurate in the ThinLTO backend.
580 if (Phase != ThinLTOPhase::PreLink)
581 // We perform early indirect call promotion here, before globalopt.
582 // This is important for the ThinLTO backend phase because otherwise
583 // imported available_externally functions look unreferenced and are
584 // removed.
585 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
586 true));
587 }
588
Chandler Carruthe3f50642016-12-22 06:59:15 +0000589 // Interprocedural constant propagation now that basic cleanup has occured
590 // and prior to optimizing globals.
591 // FIXME: This position in the pipeline hasn't been carefully considered in
592 // years, it should be re-analyzed.
593 MPM.addPass(IPSCCPPass());
594
Matthew Simpsoncb585582017-10-25 13:40:08 +0000595 // Attach metadata to indirect call sites indicating the set of functions
596 // they may target at run-time. This should follow IPSCCP.
597 MPM.addPass(CalledValuePropagationPass());
598
Chandler Carruthe3f50642016-12-22 06:59:15 +0000599 // Optimize globals to try and fold them into constants.
600 MPM.addPass(GlobalOptPass());
601
602 // Promote any localized globals to SSA registers.
603 // FIXME: Should this instead by a run of SROA?
604 // FIXME: We should probably run instcombine and simplify-cfg afterward to
605 // delete control flows that are dead once globals have been folded to
606 // constants.
607 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
608
609 // Remove any dead arguments exposed by cleanups and constand folding
610 // globals.
611 MPM.addPass(DeadArgumentEliminationPass());
612
613 // Create a small function pass pipeline to cleanup after all the global
614 // optimizations.
615 FunctionPassManager GlobalCleanupPM(DebugLogging);
616 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000617 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
618
Chandler Carruthe3f50642016-12-22 06:59:15 +0000619 GlobalCleanupPM.addPass(SimplifyCFGPass());
620 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
621
Dehao Chen89d32262017-08-02 01:28:31 +0000622 // Add all the requested passes for instrumentation PGO, if requested.
623 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
624 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
625 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
626 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile);
627 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000628 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000629
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000630 // Synthesize function entry counts for non-PGO compilation.
631 if (EnableSyntheticCounts && !PGOOpt)
632 MPM.addPass(SyntheticCountsPropagation());
633
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000634 // Require the GlobalsAA analysis for the module so we can query it within
635 // the CGSCC pipeline.
636 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000637
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000638 // Require the ProfileSummaryAnalysis for the module so we can query it within
639 // the inliner pass.
640 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
641
Chandler Carruthe3f50642016-12-22 06:59:15 +0000642 // Now begin the main postorder CGSCC pipeline.
643 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
644 // manager and trying to emulate its precise behavior. Much of this doesn't
645 // make a lot of sense and we should revisit the core CGSCC structure.
646 CGSCCPassManager MainCGPipeline(DebugLogging);
647
648 // Note: historically, the PruneEH pass was run first to deduce nounwind and
649 // generally clean up exception handling overhead. It isn't clear this is
650 // valuable as the inliner doesn't currently care whether it is inlining an
651 // invoke or a call.
652
653 // Run the inliner first. The theory is that we are walking bottom-up and so
654 // the callees have already been fully optimized, and we want to inline them
655 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000656 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000657 // because it makes profile annotation in the backend inaccurate.
658 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000659 if (Phase == ThinLTOPhase::PreLink &&
660 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000661 IP.HotCallSiteThreshold = 0;
662 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000663
664 // Now deduce any function attributes based in the current code.
665 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
666
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000667 // When at O3 add argument promotion to the pass pipeline.
668 // FIXME: It isn't at all clear why this should be limited to O3.
669 if (Level == O3)
670 MainCGPipeline.addPass(ArgumentPromotionPass());
671
Chandler Carruthe3f50642016-12-22 06:59:15 +0000672 // Lastly, add the core function simplification pipeline nested inside the
673 // CGSCC walk.
674 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000675 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000676
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000677 for (auto &C : CGSCCOptimizerLateEPCallbacks)
678 C(MainCGPipeline, Level);
679
Chandler Carruth719ffe12017-02-12 05:38:04 +0000680 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
681 // to detect when we devirtualize indirect calls and iterate the SCC passes
682 // in that case to try and catch knock-on inlining or function attrs
683 // opportunities. Then we add it to the module pipeline by walking the SCCs
684 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000685 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000686 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000687 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000688
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000689 return MPM;
690}
691
692ModulePassManager
693PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
694 bool DebugLogging) {
695 ModulePassManager MPM(DebugLogging);
696
697 // Optimize globals now that the module is fully simplified.
698 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000699 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000700
Xinliang David Li126157c2017-05-22 16:41:57 +0000701 // Run partial inlining pass to partially inline functions that have
702 // large bodies.
703 if (RunPartialInlining)
704 MPM.addPass(PartialInlinerPass());
705
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000706 // Remove avail extern fns and globals definitions since we aren't compiling
707 // an object file for later LTO. For LTO we want to preserve these so they
708 // are eligible for inlining at link-time. Note if they are unreferenced they
709 // will be removed by GlobalDCE later, so this only impacts referenced
710 // available externally globals. Eventually they will be suppressed during
711 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
712 // may make globals referenced by available external functions dead and saves
713 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000714 MPM.addPass(EliminateAvailableExternallyPass());
715
716 // Do RPO function attribute inference across the module to forward-propagate
717 // attributes where applicable.
718 // FIXME: Is this really an optimization rather than a canonicalization?
719 MPM.addPass(ReversePostOrderFunctionAttrsPass());
720
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000721 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000722 // useful as the above will have inlined, DCE'ed, and function-attr
723 // propagated everything. We should at this point have a reasonably minimal
724 // and richly annotated call graph. By computing aliasing and mod/ref
725 // information for all local globals here, the late loop passes and notably
726 // the vectorizer will be able to use them to help recognize vectorizable
727 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000728 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000729
730 FunctionPassManager OptimizePM(DebugLogging);
731 OptimizePM.addPass(Float2IntPass());
732 // FIXME: We need to run some loop optimizations to re-rotate loops after
733 // simplify-cfg and others undo their rotation.
734
735 // Optimize the loop execution. These passes operate on entire loop nests
736 // rather than on each loop in an inside-out manner, and so they are actually
737 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000738
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000739 for (auto &C : VectorizerStartEPCallbacks)
740 C(OptimizePM, Level);
741
Chandler Carrutha95ff382017-01-27 00:50:21 +0000742 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000743 OptimizePM.addPass(
744 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000745
746 // Distribute loops to allow partial vectorization. I.e. isolate dependences
747 // into separate loop that would otherwise inhibit vectorization. This is
748 // currently only performed for loops marked with the metadata
749 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000750 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000751
752 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000753 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000754
Chandler Carruthbaabda92017-01-27 01:32:26 +0000755 // Eliminate loads by forwarding stores from the previous iteration to loads
756 // of the current iteration.
757 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000758
759 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000760 OptimizePM.addPass(InstCombinePass());
761
Chandler Carrutha95ff382017-01-27 00:50:21 +0000762 // Now that we've formed fast to execute loop structures, we do further
763 // optimizations. These are run afterward as they might block doing complex
764 // analyses and transforms such as what are needed for loop vectorization.
765
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000766 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
767 // GVN, loop transforms, and others have already run, so it's now better to
768 // convert to more optimized IR using more aggressive simplify CFG options.
769 // The extra sinking transform can create larger basic blocks, so do this
770 // before SLP vectorization.
771 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
772 forwardSwitchCondToPhi(true).
773 convertSwitchToLookupTable(true).
774 needCanonicalLoops(false).
775 sinkCommonInsts(true)));
776
Chandler Carruthe3f50642016-12-22 06:59:15 +0000777 // Optimize parallel scalar instruction chains into SIMD instructions.
778 OptimizePM.addPass(SLPVectorizerPass());
779
Chandler Carruthe3f50642016-12-22 06:59:15 +0000780 OptimizePM.addPass(InstCombinePass());
781
782 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000783 // execution resources of an out-of-order processor. We also then need to
784 // clean up redundancies and loop invariant code.
785 // FIXME: It would be really good to use a loop-integrated instruction
786 // combiner for cleanup here so that the unrolling and LICM can be pipelined
787 // across the loop nests.
Teresa Johnsonecd90132017-08-02 20:35:29 +0000788 OptimizePM.addPass(LoopUnrollPass(Level));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000789 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000790 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000791 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000792
793 // Now that we've vectorized and unrolled loops, we may have more refined
794 // alignment information, try to re-derive it here.
795 OptimizePM.addPass(AlignmentFromAssumptionsPass());
796
Chandler Carrutha95ff382017-01-27 00:50:21 +0000797 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
798 // canonicalization pass that enables other optimizations. As a result,
799 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
800 // result too early.
801 OptimizePM.addPass(LoopSinkPass());
802
803 // And finally clean up LCSSA form before generating code.
804 OptimizePM.addPass(InstSimplifierPass());
805
Sanjay Patel6fd43912017-09-09 13:38:18 +0000806 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
807 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
808 // flattening of blocks.
809 OptimizePM.addPass(DivRemPairsPass());
810
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000811 // LoopSink (and other loop passes since the last simplifyCFG) might have
812 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
813 OptimizePM.addPass(SimplifyCFGPass());
814
Chandler Carruthc34f7892017-11-28 11:32:31 +0000815 // Optimize PHIs by speculating around them when profitable. Note that this
816 // pass needs to be run after any PRE or similar pass as it is essentially
817 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
818 OptimizePM.addPass(SpeculateAroundPHIsPass());
819
Chandler Carrutha95ff382017-01-27 00:50:21 +0000820 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000821 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
822
823 // Now we need to do some global optimization transforms.
824 // FIXME: It would seem like these should come first in the optimization
825 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
826 // ordering here.
827 MPM.addPass(GlobalDCEPass());
828 MPM.addPass(ConstantMergePass());
829
830 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000831}
832
Chandler Carruthe3f50642016-12-22 06:59:15 +0000833ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000834PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
835 bool DebugLogging) {
836 assert(Level != O0 && "Must request optimizations for the default pipeline!");
837
838 ModulePassManager MPM(DebugLogging);
839
840 // Force any function attributes we want the rest of the pipeline to observe.
841 MPM.addPass(ForceFunctionAttrsPass());
842
David Blaikie0c64f5a2018-01-23 01:25:20 +0000843 // Apply module pipeline start EP callback.
844 for (auto &C : PipelineStartEPCallbacks)
845 C(MPM);
846
Dehao Chen08f88312017-08-07 20:23:20 +0000847 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000848 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
849
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000850 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000851 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
852 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000853
854 // Now add the optimization pipeline.
855 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
856
857 return MPM;
858}
859
860ModulePassManager
861PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
862 bool DebugLogging) {
863 assert(Level != O0 && "Must request optimizations for the default pipeline!");
864
865 ModulePassManager MPM(DebugLogging);
866
867 // Force any function attributes we want the rest of the pipeline to observe.
868 MPM.addPass(ForceFunctionAttrsPass());
869
Dehao Chen08f88312017-08-07 20:23:20 +0000870 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000871 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
872
David Blaikie0c64f5a2018-01-23 01:25:20 +0000873 // Apply module pipeline start EP callback.
874 for (auto &C : PipelineStartEPCallbacks)
875 C(MPM);
876
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000877 // If we are planning to perform ThinLTO later, we don't bloat the code with
878 // unrolling/vectorization/... now. Just simplify the module as much as we
879 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000880 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
881 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000882
883 // Run partial inlining pass to partially inline functions that have
884 // large bodies.
885 // FIXME: It isn't clear whether this is really the right place to run this
886 // in ThinLTO. Because there is another canonicalization and simplification
887 // phase that will run after the thin link, running this here ends up with
888 // less information than will be available later and it may grow functions in
889 // ways that aren't beneficial.
890 if (RunPartialInlining)
891 MPM.addPass(PartialInlinerPass());
892
893 // Reduce the size of the IR as much as possible.
894 MPM.addPass(GlobalOptPass());
895
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000896 return MPM;
897}
898
899ModulePassManager
900PassBuilder::buildThinLTODefaultPipeline(OptimizationLevel Level,
901 bool DebugLogging) {
902 // FIXME: The summary index is not hooked in the new pass manager yet.
903 // When it's going to be hooked, enable WholeProgramDevirt and LowerTypeTest
904 // here.
905
906 ModulePassManager MPM(DebugLogging);
907
908 // Force any function attributes we want the rest of the pipeline to observe.
909 MPM.addPass(ForceFunctionAttrsPass());
910
911 // During the ThinLTO backend phase we perform early indirect call promotion
912 // here, before globalopt. Otherwise imported available_externally functions
913 // look unreferenced and are removed.
Dehao Chen08f88312017-08-07 20:23:20 +0000914 // FIXME: move this into buildModuleSimplificationPipeline to merge the logic
915 // with SamplePGO.
Dehao Chen2f4e2e22017-08-10 05:10:32 +0000916 if (!PGOOpt || PGOOpt->SampleProfileFile.empty())
Dehao Chen08f88312017-08-07 20:23:20 +0000917 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */,
918 false /* SamplePGO */));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000919
920 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000921 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
922 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000923
924 // Now add the optimization pipeline.
925 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
926
927 return MPM;
928}
929
930ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +0000931PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
932 bool DebugLogging) {
933 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000934 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +0000935 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000936}
937
Chandler Carruthe3f50642016-12-22 06:59:15 +0000938ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level,
939 bool DebugLogging) {
940 assert(Level != O0 && "Must request optimizations for the default pipeline!");
941 ModulePassManager MPM(DebugLogging);
942
Davide Italiano089a9122017-01-24 00:57:39 +0000943 // Remove unused virtual tables to improve the quality of code generated by
944 // whole-program devirtualization and bitset lowering.
945 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000946
Davide Italiano089a9122017-01-24 00:57:39 +0000947 // Force any function attributes we want the rest of the pipeline to observe.
948 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000949
Davide Italiano089a9122017-01-24 00:57:39 +0000950 // Do basic inference of function attributes from known properties of system
951 // libraries and other oracles.
952 MPM.addPass(InferFunctionAttrsPass());
953
954 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +0000955 FunctionPassManager EarlyFPM(DebugLogging);
956 EarlyFPM.addPass(CallSiteSplittingPass());
957 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
958
Davide Italiano089a9122017-01-24 00:57:39 +0000959 // Indirect call promotion. This should promote all the targets that are
960 // left by the earlier promotion pass that promotes intra-module targets.
961 // This two-step promotion is to save the compile time. For LTO, it should
962 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +0000963 MPM.addPass(PGOIndirectCallPromotion(
964 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +0000965 // Propagate constants at call sites into the functions they call. This
966 // opens opportunities for globalopt (and inlining) by substituting function
967 // pointers passed as arguments to direct uses of functions.
968 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +0000969
970 // Attach metadata to indirect call sites indicating the set of functions
971 // they may target at run-time. This should follow IPSCCP.
972 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +0000973 }
974
975 // Now deduce any function attributes based in the current code.
976 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
977 PostOrderFunctionAttrsPass()));
978
979 // Do RPO function attribute inference across the module to forward-propagate
980 // attributes where applicable.
981 // FIXME: Is this really an optimization rather than a canonicalization?
982 MPM.addPass(ReversePostOrderFunctionAttrsPass());
983
984 // Use inragne annotations on GEP indices to split globals where beneficial.
985 MPM.addPass(GlobalSplitPass());
986
987 // Run whole program optimization of virtual call when the list of callees
988 // is fixed.
989 MPM.addPass(WholeProgramDevirtPass());
990
991 // Stop here at -O1.
992 if (Level == 1)
993 return MPM;
994
995 // Optimize globals to try and fold them into constants.
996 MPM.addPass(GlobalOptPass());
997
998 // Promote any localized globals to SSA registers.
999 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1000
1001 // Linking modules together can lead to duplicate global constant, only
1002 // keep one copy of each constant.
1003 MPM.addPass(ConstantMergePass());
1004
1005 // Remove unused arguments from functions.
1006 MPM.addPass(DeadArgumentEliminationPass());
1007
1008 // Reduce the code after globalopt and ipsccp. Both can open up significant
1009 // simplification opportunities, and both can propagate functions through
1010 // function pointers. When this happens, we often have to resolve varargs
1011 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001012 FunctionPassManager PeepholeFPM(DebugLogging);
1013 PeepholeFPM.addPass(InstCombinePass());
1014 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1015
1016 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001017
1018 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1019 // generally clean up exception handling overhead. It isn't clear this is
1020 // valuable as the inliner doesn't currently care whether it is inlining an
1021 // invoke or a call.
1022 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001023 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1024 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001025
1026 // Optimize globals again after we ran the inliner.
1027 MPM.addPass(GlobalOptPass());
1028
1029 // Garbage collect dead functions.
1030 // FIXME: Add ArgumentPromotion pass after once it's ported.
1031 MPM.addPass(GlobalDCEPass());
1032
1033 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001034 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001035 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001036 invokePeepholeEPCallbacks(FPM, Level);
1037
Davide Italiano089a9122017-01-24 00:57:39 +00001038 FPM.addPass(JumpThreadingPass());
1039
1040 // Break up allocas
1041 FPM.addPass(SROA());
1042
1043 // Run a few AA driver optimizations here and now to cleanup the code.
1044 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1045
1046 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1047 PostOrderFunctionAttrsPass()));
1048 // FIXME: here we run IP alias analysis in the legacy PM.
1049
1050 FunctionPassManager MainFPM;
1051
1052 // FIXME: once we fix LoopPass Manager, add LICM here.
1053 // FIXME: once we provide support for enabling MLSM, add it here.
1054 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001055 if (RunNewGVN)
1056 MainFPM.addPass(NewGVNPass());
1057 else
1058 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001059
1060 // Remove dead memcpy()'s.
1061 MainFPM.addPass(MemCpyOptPass());
1062
1063 // Nuke dead stores.
1064 MainFPM.addPass(DSEPass());
1065
1066 // FIXME: at this point, we run a bunch of loop passes:
1067 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1068 // loopVectorize. Enable them once the remaining issue with LPM
1069 // are sorted out.
1070
1071 MainFPM.addPass(InstCombinePass());
1072 MainFPM.addPass(SimplifyCFGPass());
1073 MainFPM.addPass(SCCPPass());
1074 MainFPM.addPass(InstCombinePass());
1075 MainFPM.addPass(BDCEPass());
1076
1077 // FIXME: We may want to run SLPVectorizer here.
1078 // After vectorization, assume intrinsics may tell us more
1079 // about pointer alignments.
1080#if 0
1081 MainFPM.add(AlignmentFromAssumptionsPass());
1082#endif
1083
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001084 // FIXME: Conditionally run LoadCombine here, after it's ported
1085 // (in case we still have this pass, given its questionable usefulness).
1086
Davide Italiano089a9122017-01-24 00:57:39 +00001087 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001088 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001089 MainFPM.addPass(JumpThreadingPass());
1090 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1091
1092 // Create a function that performs CFI checks for cross-DSO calls with
1093 // targets in the current module.
1094 MPM.addPass(CrossDSOCFIPass());
1095
1096 // Lower type metadata and the type.test intrinsic. This pass supports
1097 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1098 // to be run at link time if CFI is enabled. This pass does nothing if
1099 // CFI is disabled.
1100 // Enable once we add support for the summary in the new PM.
1101#if 0
Peter Collingbourne857aba42017-02-09 21:45:01 +00001102 MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export :
1103 PassSummaryAction::None,
Davide Italiano089a9122017-01-24 00:57:39 +00001104 Summary));
1105#endif
1106
1107 // Add late LTO optimization passes.
1108 // Delete basic blocks, which optimization passes may have killed.
1109 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1110
1111 // Drop bodies of available eternally objects to improve GlobalDCE.
1112 MPM.addPass(EliminateAvailableExternallyPass());
1113
1114 // Now that we have optimized the program, discard unreachable functions.
1115 MPM.addPass(GlobalDCEPass());
1116
1117 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001118 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001119}
1120
Chandler Carruth060ad612016-12-23 20:38:19 +00001121AAManager PassBuilder::buildDefaultAAPipeline() {
1122 AAManager AA;
1123
1124 // The order in which these are registered determines their priority when
1125 // being queried.
1126
1127 // First we register the basic alias analysis that provides the majority of
1128 // per-function local AA logic. This is a stateless, on-demand local set of
1129 // AA techniques.
1130 AA.registerFunctionAnalysis<BasicAA>();
1131
1132 // Next we query fast, specialized alias analyses that wrap IR-embedded
1133 // information about aliasing.
1134 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1135 AA.registerFunctionAnalysis<TypeBasedAA>();
1136
1137 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001138 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1139 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001140 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001141 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001142
1143 return AA;
1144}
1145
Chandler Carruth241bf242016-08-03 07:44:48 +00001146static Optional<int> parseRepeatPassName(StringRef Name) {
1147 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1148 return None;
1149 int Count;
1150 if (Name.getAsInteger(0, Count) || Count <= 0)
1151 return None;
1152 return Count;
1153}
1154
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001155static Optional<int> parseDevirtPassName(StringRef Name) {
1156 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1157 return None;
1158 int Count;
1159 if (Name.getAsInteger(0, Count) || Count <= 0)
1160 return None;
1161 return Count;
1162}
1163
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001164/// Tests whether a pass name starts with a valid prefix for a default pipeline
1165/// alias.
1166static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1167 return Name.startswith("default") || Name.startswith("thinlto") ||
1168 Name.startswith("lto");
1169}
1170
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001171/// Tests whether registered callbacks will accept a given pass name.
1172///
1173/// When parsing a pipeline text, the type of the outermost pipeline may be
1174/// omitted, in which case the type is automatically determined from the first
1175/// pass name in the text. This may be a name that is handled through one of the
1176/// callbacks. We check this through the oridinary parsing callbacks by setting
1177/// up a dummy PassManager in order to not force the client to also handle this
1178/// type of query.
1179template <typename PassManagerT, typename CallbacksT>
1180static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1181 if (!Callbacks.empty()) {
1182 PassManagerT DummyPM;
1183 for (auto &CB : Callbacks)
1184 if (CB(Name, DummyPM, {}))
1185 return true;
1186 }
1187 return false;
1188}
1189
1190template <typename CallbacksT>
1191static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001192 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001193 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001194 return DefaultAliasRegex.match(Name);
1195
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001196 // Explicitly handle pass manager names.
1197 if (Name == "module")
1198 return true;
1199 if (Name == "cgscc")
1200 return true;
1201 if (Name == "function")
1202 return true;
1203
Chandler Carruth241bf242016-08-03 07:44:48 +00001204 // Explicitly handle custom-parsed pass names.
1205 if (parseRepeatPassName(Name))
1206 return true;
1207
Chandler Carruth74a8a222016-06-17 07:15:29 +00001208#define MODULE_PASS(NAME, CREATE_PASS) \
1209 if (Name == NAME) \
1210 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001211#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001212 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001213 return true;
1214#include "PassRegistry.def"
1215
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001216 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001217}
1218
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001219template <typename CallbacksT>
1220static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001221 // Explicitly handle pass manager names.
1222 if (Name == "cgscc")
1223 return true;
1224 if (Name == "function")
1225 return true;
1226
Chandler Carruth241bf242016-08-03 07:44:48 +00001227 // Explicitly handle custom-parsed pass names.
1228 if (parseRepeatPassName(Name))
1229 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001230 if (parseDevirtPassName(Name))
1231 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001232
Chandler Carruth74a8a222016-06-17 07:15:29 +00001233#define CGSCC_PASS(NAME, CREATE_PASS) \
1234 if (Name == NAME) \
1235 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001236#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001237 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001238 return true;
1239#include "PassRegistry.def"
1240
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001241 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001242}
1243
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001244template <typename CallbacksT>
1245static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001246 // Explicitly handle pass manager names.
1247 if (Name == "function")
1248 return true;
1249 if (Name == "loop")
1250 return true;
1251
Chandler Carruth241bf242016-08-03 07:44:48 +00001252 // Explicitly handle custom-parsed pass names.
1253 if (parseRepeatPassName(Name))
1254 return true;
1255
Chandler Carruth74a8a222016-06-17 07:15:29 +00001256#define FUNCTION_PASS(NAME, CREATE_PASS) \
1257 if (Name == NAME) \
1258 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001259#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001260 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001261 return true;
1262#include "PassRegistry.def"
1263
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001264 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001265}
1266
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001267template <typename CallbacksT>
1268static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001269 // Explicitly handle pass manager names.
1270 if (Name == "loop")
1271 return true;
1272
Chandler Carruth241bf242016-08-03 07:44:48 +00001273 // Explicitly handle custom-parsed pass names.
1274 if (parseRepeatPassName(Name))
1275 return true;
1276
Chandler Carruth74a8a222016-06-17 07:15:29 +00001277#define LOOP_PASS(NAME, CREATE_PASS) \
1278 if (Name == NAME) \
1279 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001280#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1281 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1282 return true;
1283#include "PassRegistry.def"
1284
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001285 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001286}
1287
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001288Optional<std::vector<PassBuilder::PipelineElement>>
1289PassBuilder::parsePipelineText(StringRef Text) {
1290 std::vector<PipelineElement> ResultPipeline;
1291
1292 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1293 &ResultPipeline};
1294 for (;;) {
1295 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1296 size_t Pos = Text.find_first_of(",()");
1297 Pipeline.push_back({Text.substr(0, Pos), {}});
1298
1299 // If we have a single terminating name, we're done.
1300 if (Pos == Text.npos)
1301 break;
1302
1303 char Sep = Text[Pos];
1304 Text = Text.substr(Pos + 1);
1305 if (Sep == ',')
1306 // Just a name ending in a comma, continue.
1307 continue;
1308
1309 if (Sep == '(') {
1310 // Push the inner pipeline onto the stack to continue processing.
1311 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1312 continue;
1313 }
1314
1315 assert(Sep == ')' && "Bogus separator!");
1316 // When handling the close parenthesis, we greedily consume them to avoid
1317 // empty strings in the pipeline.
1318 do {
1319 // If we try to pop the outer pipeline we have unbalanced parentheses.
1320 if (PipelineStack.size() == 1)
1321 return None;
1322
1323 PipelineStack.pop_back();
1324 } while (Text.consume_front(")"));
1325
1326 // Check if we've finished parsing.
1327 if (Text.empty())
1328 break;
1329
1330 // Otherwise, the end of an inner pipeline always has to be followed by
1331 // a comma, and then we can continue.
1332 if (!Text.consume_front(","))
1333 return None;
1334 }
1335
1336 if (PipelineStack.size() > 1)
1337 // Unbalanced paretheses.
1338 return None;
1339
1340 assert(PipelineStack.back() == &ResultPipeline &&
1341 "Wrong pipeline at the bottom of the stack!");
1342 return {std::move(ResultPipeline)};
1343}
1344
1345bool PassBuilder::parseModulePass(ModulePassManager &MPM,
1346 const PipelineElement &E, bool VerifyEachPass,
1347 bool DebugLogging) {
1348 auto &Name = E.Name;
1349 auto &InnerPipeline = E.InnerPipeline;
1350
1351 // First handle complex passes like the pass managers which carry pipelines.
1352 if (!InnerPipeline.empty()) {
1353 if (Name == "module") {
1354 ModulePassManager NestedMPM(DebugLogging);
1355 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1356 DebugLogging))
1357 return false;
1358 MPM.addPass(std::move(NestedMPM));
1359 return true;
1360 }
1361 if (Name == "cgscc") {
1362 CGSCCPassManager CGPM(DebugLogging);
1363 if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1364 DebugLogging))
1365 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001366 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001367 return true;
1368 }
1369 if (Name == "function") {
1370 FunctionPassManager FPM(DebugLogging);
1371 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1372 DebugLogging))
1373 return false;
1374 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1375 return true;
1376 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001377 if (auto Count = parseRepeatPassName(Name)) {
1378 ModulePassManager NestedMPM(DebugLogging);
1379 if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass,
1380 DebugLogging))
1381 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001382 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001383 return true;
1384 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001385
1386 for (auto &C : ModulePipelineParsingCallbacks)
1387 if (C(Name, MPM, InnerPipeline))
1388 return true;
1389
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001390 // Normal passes can't have pipelines.
1391 return false;
1392 }
1393
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001394 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001395 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001396 SmallVector<StringRef, 3> Matches;
1397 if (!DefaultAliasRegex.match(Name, &Matches))
1398 return false;
1399 assert(Matches.size() == 3 && "Must capture two matched strings!");
1400
Jordan Rosef85a95f2016-07-25 18:34:51 +00001401 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001402 .Case("O0", O0)
1403 .Case("O1", O1)
1404 .Case("O2", O2)
1405 .Case("O3", O3)
1406 .Case("Os", Os)
1407 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001408 if (L == O0)
1409 // At O0 we do nothing at all!
1410 return true;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001411
1412 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001413 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001414 } else if (Matches[1] == "thinlto-pre-link") {
1415 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1416 } else if (Matches[1] == "thinlto") {
1417 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001418 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001419 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001420 } else {
1421 assert(Matches[1] == "lto" && "Not one of the matched options!");
Chandler Carruthe3f50642016-12-22 06:59:15 +00001422 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001423 }
1424 return true;
1425 }
1426
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001427 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001428#define MODULE_PASS(NAME, CREATE_PASS) \
1429 if (Name == NAME) { \
1430 MPM.addPass(CREATE_PASS); \
1431 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001432 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001433#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1434 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001435 MPM.addPass( \
1436 RequireAnalysisPass< \
1437 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001438 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001439 } \
1440 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001441 MPM.addPass(InvalidateAnalysisPass< \
1442 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001443 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001444 }
1445#include "PassRegistry.def"
1446
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001447 for (auto &C : ModulePipelineParsingCallbacks)
1448 if (C(Name, MPM, InnerPipeline))
1449 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001450 return false;
1451}
1452
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001453bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1454 const PipelineElement &E, bool VerifyEachPass,
1455 bool DebugLogging) {
1456 auto &Name = E.Name;
1457 auto &InnerPipeline = E.InnerPipeline;
1458
1459 // First handle complex passes like the pass managers which carry pipelines.
1460 if (!InnerPipeline.empty()) {
1461 if (Name == "cgscc") {
1462 CGSCCPassManager NestedCGPM(DebugLogging);
1463 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1464 DebugLogging))
1465 return false;
1466 // Add the nested pass manager with the appropriate adaptor.
1467 CGPM.addPass(std::move(NestedCGPM));
1468 return true;
1469 }
1470 if (Name == "function") {
1471 FunctionPassManager FPM(DebugLogging);
1472 if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass,
1473 DebugLogging))
1474 return false;
1475 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001476 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001477 return true;
1478 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001479 if (auto Count = parseRepeatPassName(Name)) {
1480 CGSCCPassManager NestedCGPM(DebugLogging);
1481 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1482 DebugLogging))
1483 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001484 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001485 return true;
1486 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001487 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1488 CGSCCPassManager NestedCGPM(DebugLogging);
1489 if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass,
1490 DebugLogging))
1491 return false;
Chandler Carruth19913b22017-08-11 05:47:13 +00001492 CGPM.addPass(
1493 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001494 return true;
1495 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001496
1497 for (auto &C : CGSCCPipelineParsingCallbacks)
1498 if (C(Name, CGPM, InnerPipeline))
1499 return true;
1500
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001501 // Normal passes can't have pipelines.
1502 return false;
1503 }
1504
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001505// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001506#define CGSCC_PASS(NAME, CREATE_PASS) \
1507 if (Name == NAME) { \
1508 CGPM.addPass(CREATE_PASS); \
1509 return true; \
1510 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001511#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1512 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001513 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001514 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001515 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1516 CGSCCUpdateResult &>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001517 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001518 } \
1519 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001520 CGPM.addPass(InvalidateAnalysisPass< \
1521 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001522 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001523 }
1524#include "PassRegistry.def"
1525
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001526 for (auto &C : CGSCCPipelineParsingCallbacks)
1527 if (C(Name, CGPM, InnerPipeline))
1528 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001529 return false;
1530}
1531
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001532bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1533 const PipelineElement &E,
1534 bool VerifyEachPass, bool DebugLogging) {
1535 auto &Name = E.Name;
1536 auto &InnerPipeline = E.InnerPipeline;
1537
1538 // First handle complex passes like the pass managers which carry pipelines.
1539 if (!InnerPipeline.empty()) {
1540 if (Name == "function") {
1541 FunctionPassManager NestedFPM(DebugLogging);
1542 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1543 DebugLogging))
1544 return false;
1545 // Add the nested pass manager with the appropriate adaptor.
1546 FPM.addPass(std::move(NestedFPM));
1547 return true;
1548 }
1549 if (Name == "loop") {
1550 LoopPassManager LPM(DebugLogging);
1551 if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1552 DebugLogging))
1553 return false;
1554 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001555 FPM.addPass(
1556 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001557 return true;
1558 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001559 if (auto Count = parseRepeatPassName(Name)) {
1560 FunctionPassManager NestedFPM(DebugLogging);
1561 if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass,
1562 DebugLogging))
1563 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001564 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001565 return true;
1566 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001567
1568 for (auto &C : FunctionPipelineParsingCallbacks)
1569 if (C(Name, FPM, InnerPipeline))
1570 return true;
1571
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001572 // Normal passes can't have pipelines.
1573 return false;
1574 }
1575
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001576// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001577#define FUNCTION_PASS(NAME, CREATE_PASS) \
1578 if (Name == NAME) { \
1579 FPM.addPass(CREATE_PASS); \
1580 return true; \
Chandler Carruth52eef882014-01-12 12:15:39 +00001581 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001582#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1583 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001584 FPM.addPass( \
1585 RequireAnalysisPass< \
1586 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001587 return true; \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001588 } \
1589 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001590 FPM.addPass(InvalidateAnalysisPass< \
1591 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001592 return true; \
Chandler Carruth628503e2015-01-06 02:10:51 +00001593 }
1594#include "PassRegistry.def"
1595
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001596 for (auto &C : FunctionPipelineParsingCallbacks)
1597 if (C(Name, FPM, InnerPipeline))
1598 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001599 return false;
1600}
1601
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001602bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001603 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001604 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001605 auto &InnerPipeline = E.InnerPipeline;
1606
1607 // First handle complex passes like the pass managers which carry pipelines.
1608 if (!InnerPipeline.empty()) {
1609 if (Name == "loop") {
1610 LoopPassManager NestedLPM(DebugLogging);
1611 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1612 DebugLogging))
1613 return false;
1614 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001615 LPM.addPass(std::move(NestedLPM));
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001616 return true;
1617 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001618 if (auto Count = parseRepeatPassName(Name)) {
1619 LoopPassManager NestedLPM(DebugLogging);
1620 if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass,
1621 DebugLogging))
1622 return false;
Chandler Carrutha053a882016-08-04 03:52:53 +00001623 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Chandler Carruth241bf242016-08-03 07:44:48 +00001624 return true;
1625 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001626
1627 for (auto &C : LoopPipelineParsingCallbacks)
1628 if (C(Name, LPM, InnerPipeline))
1629 return true;
1630
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001631 // Normal passes can't have pipelines.
1632 return false;
1633 }
1634
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001635// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001636#define LOOP_PASS(NAME, CREATE_PASS) \
1637 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001638 LPM.addPass(CREATE_PASS); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001639 return true; \
1640 }
1641#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1642 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001643 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001644 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1645 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1646 LPMUpdater &>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001647 return true; \
1648 } \
1649 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001650 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001651 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001652 return true; \
1653 }
1654#include "PassRegistry.def"
1655
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001656 for (auto &C : LoopPipelineParsingCallbacks)
1657 if (C(Name, LPM, InnerPipeline))
1658 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001659 return false;
1660}
1661
Chandler Carruthedf59962016-02-18 09:45:17 +00001662bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001663#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1664 if (Name == NAME) { \
1665 AA.registerModuleAnalysis< \
1666 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1667 return true; \
1668 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001669#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1670 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001671 AA.registerFunctionAnalysis< \
1672 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001673 return true; \
1674 }
1675#include "PassRegistry.def"
1676
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001677 for (auto &C : AAParsingCallbacks)
1678 if (C(Name, AA))
1679 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001680 return false;
1681}
1682
Justin Bognereecc3c82016-02-25 07:23:08 +00001683bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001684 ArrayRef<PipelineElement> Pipeline,
Justin Bognereecc3c82016-02-25 07:23:08 +00001685 bool VerifyEachPass,
1686 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001687 for (const auto &Element : Pipeline) {
1688 if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1689 return false;
1690 // FIXME: No verifier support for Loop passes!
Justin Bognereecc3c82016-02-25 07:23:08 +00001691 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001692 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001693}
1694
Chandler Carruth1ff77242015-03-07 09:02:36 +00001695bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001696 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001697 bool VerifyEachPass,
1698 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001699 for (const auto &Element : Pipeline) {
1700 if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1701 return false;
1702 if (VerifyEachPass)
1703 FPM.addPass(VerifierPass());
Chandler Carruthd8330982014-01-12 09:34:22 +00001704 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 return true;
Chandler Carruthd8330982014-01-12 09:34:22 +00001706}
1707
Chandler Carruth1ff77242015-03-07 09:02:36 +00001708bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001709 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001710 bool VerifyEachPass,
1711 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001712 for (const auto &Element : Pipeline) {
1713 if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1714 return false;
1715 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001716 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001717 return true;
Chandler Carruth572e3402014-04-21 11:12:00 +00001718}
1719
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001720void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1721 FunctionAnalysisManager &FAM,
1722 CGSCCAnalysisManager &CGAM,
1723 ModuleAnalysisManager &MAM) {
1724 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1725 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001726 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1727 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1728 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1729 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1730 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1731}
1732
Chandler Carruth1ff77242015-03-07 09:02:36 +00001733bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001734 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001735 bool VerifyEachPass,
1736 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001737 for (const auto &Element : Pipeline) {
1738 if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1739 return false;
1740 if (VerifyEachPass)
1741 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001742 }
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001743 return true;
Chandler Carruth66445382014-01-11 08:16:35 +00001744}
1745
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001746// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001747// FIXME: Should this routine accept a TargetMachine or require the caller to
1748// pre-populate the analysis managers with target-specific stuff?
Chandler Carruth1ff77242015-03-07 09:02:36 +00001749bool PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1750 StringRef PipelineText, bool VerifyEachPass,
1751 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001752 auto Pipeline = parsePipelineText(PipelineText);
1753 if (!Pipeline || Pipeline->empty())
1754 return false;
Chandler Carruth66445382014-01-11 08:16:35 +00001755
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001756 // If the first name isn't at the module layer, wrap the pipeline up
1757 // automatically.
1758 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00001759
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001760 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
1761 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001762 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001763 } else if (isFunctionPassName(FirstName,
1764 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001765 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001766 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001767 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001768 } else {
1769 for (auto &C : TopLevelPipelineParsingCallbacks)
1770 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
1771 return true;
1772
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001773 // Unknown pass name!
Chandler Carruth572e3402014-04-21 11:12:00 +00001774 return false;
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001775 }
Chandler Carruth572e3402014-04-21 11:12:00 +00001776 }
1777
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001778 return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging);
Chandler Carruth66445382014-01-11 08:16:35 +00001779}
Chandler Carruthedf59962016-02-18 09:45:17 +00001780
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001781// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
1782bool PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
1783 StringRef PipelineText, bool VerifyEachPass,
1784 bool DebugLogging) {
1785 auto Pipeline = parsePipelineText(PipelineText);
1786 if (!Pipeline || Pipeline->empty())
1787 return false;
1788
1789 StringRef FirstName = Pipeline->front().Name;
1790 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
1791 return false;
1792
1793 return parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1794}
1795
1796// Primary pass pipeline description parsing routine for a \c
1797// FunctionPassManager
1798bool PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
1799 StringRef PipelineText, bool VerifyEachPass,
1800 bool DebugLogging) {
1801 auto Pipeline = parsePipelineText(PipelineText);
1802 if (!Pipeline || Pipeline->empty())
1803 return false;
1804
1805 StringRef FirstName = Pipeline->front().Name;
1806 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
1807 return false;
1808
1809 return parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
1810 DebugLogging);
1811}
1812
1813// Primary pass pipeline description parsing routine for a \c LoopPassManager
1814bool PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
1815 StringRef PipelineText, bool VerifyEachPass,
1816 bool DebugLogging) {
1817 auto Pipeline = parsePipelineText(PipelineText);
1818 if (!Pipeline || Pipeline->empty())
1819 return false;
1820
1821 return parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging);
1822}
1823
Chandler Carruthedf59962016-02-18 09:45:17 +00001824bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00001825 // If the pipeline just consists of the word 'default' just replace the AA
1826 // manager with our default one.
1827 if (PipelineText == "default") {
1828 AA = buildDefaultAAPipeline();
1829 return true;
1830 }
1831
Chandler Carruthedf59962016-02-18 09:45:17 +00001832 while (!PipelineText.empty()) {
1833 StringRef Name;
1834 std::tie(Name, PipelineText) = PipelineText.split(',');
1835 if (!parseAAPassName(AA, Name))
1836 return false;
1837 }
1838
1839 return true;
1840}