blob: 8e62c1d0b690a4d6855e59e9533ba2986a81b352 [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Chandler Carruth66445382014-01-11 08:16:35 +00006//
7//===----------------------------------------------------------------------===//
8/// \file
9///
Chandler Carruth1ff77242015-03-07 09:02:36 +000010/// This file provides the implementation of the PassBuilder based on our
11/// static pass registry as well as related functionality. It also provides
12/// helpers to aid in analyzing, debugging, and testing passes and pass
13/// pipelines.
Chandler Carruth66445382014-01-11 08:16:35 +000014///
15//===----------------------------------------------------------------------===//
16
Chandler Carruth1ff77242015-03-07 09:02:36 +000017#include "llvm/Passes/PassBuilder.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000018#include "llvm/ADT/StringSwitch.h"
Chandler Carruth6f5770b102016-02-13 23:32:00 +000019#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth4f846a52016-02-20 03:46:03 +000020#include "llvm/Analysis/AliasAnalysisEvaluator.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000021#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruthbece8d52016-02-13 23:46:24 +000022#include "llvm/Analysis/BasicAliasAnalysis.h"
Xinliang David Li28a93272016-05-05 21:13:27 +000023#include "llvm/Analysis/BlockFrequencyInfo.h"
Xinliang David Li6e5dd412016-05-05 02:59:57 +000024#include "llvm/Analysis/BranchProbabilityInfo.h"
Mehdi Amini27d23792016-09-16 16:56:30 +000025#include "llvm/Analysis/CFGPrinter.h"
George Burgess IVbfa401e2016-07-06 00:26:41 +000026#include "llvm/Analysis/CFLAndersAliasAnalysis.h"
27#include "llvm/Analysis/CFLSteensAliasAnalysis.h"
Chandler Carruth572e3402014-04-21 11:12:00 +000028#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruth4c660f72016-03-10 11:24:11 +000029#include "llvm/Analysis/CallGraph.h"
Michael Kupersteinde16b442016-04-18 23:55:01 +000030#include "llvm/Analysis/DemandedBits.h"
Chandler Carruth49c22192016-05-12 22:19:39 +000031#include "llvm/Analysis/DependenceAnalysis.h"
Hongbin Zheng751337f2016-02-25 17:54:15 +000032#include "llvm/Analysis/DominanceFrontier.h"
Chandler Carruth45a9c202016-03-11 09:15:11 +000033#include "llvm/Analysis/GlobalsModRef.h"
Dehao Chen1a444522016-07-16 22:51:33 +000034#include "llvm/Analysis/IVUsers.h"
Chandler Carruthbf71a342014-02-06 04:37:03 +000035#include "llvm/Analysis/LazyCallGraph.h"
Sean Silva687019f2016-06-13 22:01:25 +000036#include "llvm/Analysis/LazyValueInfo.h"
Xinliang David Li8a021312016-07-02 21:18:40 +000037#include "llvm/Analysis/LoopAccessAnalysis.h"
Chandler Carruthaaf0b4c2015-01-20 10:58:50 +000038#include "llvm/Analysis/LoopInfo.h"
Chandler Carruth61440d22016-03-10 00:55:30 +000039#include "llvm/Analysis/MemoryDependenceAnalysis.h"
Daniel Berlin554dcd82017-04-11 20:06:36 +000040#include "llvm/Analysis/MemorySSA.h"
Teresa Johnsonf93b2462016-08-12 13:53:02 +000041#include "llvm/Analysis/ModuleSummaryAnalysis.h"
Adam Nemet0965da22017-10-09 23:19:02 +000042#include "llvm/Analysis/OptimizationRemarkEmitter.h"
John Brawnbdbbd832018-06-28 14:13:06 +000043#include "llvm/Analysis/PhiValues.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"
Vitaly Buka4493fe12018-11-26 21:57:47 +000050#include "llvm/Analysis/StackSafetyAnalysis.h"
Chandler Carruth8ca43222015-01-15 11:39:46 +000051#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000052#include "llvm/Analysis/TargetTransformInfo.h"
Chandler Carruthc1dc3842016-02-20 04:04:52 +000053#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Michael Kuperstein82d5da52016-06-24 20:13:42 +000054#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
Wei Mi90d195a2016-07-08 03:32:49 +000055#include "llvm/CodeGen/UnreachableBlockElim.h"
Chandler Carruth64764b42015-01-14 10:19:28 +000056#include "llvm/IR/Dominators.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000057#include "llvm/IR/IRPrintingPasses.h"
Chandler Carruth66445382014-01-11 08:16:35 +000058#include "llvm/IR/PassManager.h"
Chandler Carruth4d356312014-01-20 11:34:08 +000059#include "llvm/IR/Verifier.h"
Chandler Carruth52eef882014-01-12 12:15:39 +000060#include "llvm/Support/Debug.h"
Fedor Sergeevbd6b2132018-10-17 10:36:23 +000061#include "llvm/Support/FormatVariadic.h"
Chandler Carruth8b5a74192016-02-28 22:16:03 +000062#include "llvm/Support/Regex.h"
Chandler Carruthe0385522015-02-01 10:11:22 +000063#include "llvm/Target/TargetMachine.h"
Amjad Aboudf1f57a32018-01-25 12:06:32 +000064#include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
Chandler Carruth67fc52f2016-08-17 02:56:20 +000065#include "llvm/Transforms/IPO/AlwaysInliner.h"
Chandler Carruthaddcda42017-02-09 23:46:27 +000066#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Matthew Simpsoncb585582017-10-25 13:40:08 +000067#include "llvm/Transforms/IPO/CalledValuePropagation.h"
Davide Italiano164b9bc2016-05-05 00:51:09 +000068#include "llvm/Transforms/IPO/ConstantMerge.h"
Davide Italiano92b933a2016-07-09 03:25:35 +000069#include "llvm/Transforms/IPO/CrossDSOCFI.h"
Sean Silvae3bb4572016-06-12 09:16:39 +000070#include "llvm/Transforms/IPO/DeadArgumentElimination.h"
Davide Italiano344e8382016-05-05 02:37:32 +000071#include "llvm/Transforms/IPO/ElimAvailExtern.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000072#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth9c4ed172016-02-18 11:03:11 +000073#include "llvm/Transforms/IPO/FunctionAttrs.h"
Teresa Johnson21241572016-07-18 21:22:24 +000074#include "llvm/Transforms/IPO/FunctionImport.h"
Davide Italiano66228c42016-05-03 19:39:15 +000075#include "llvm/Transforms/IPO/GlobalDCE.h"
Justin Bogner1a075012016-04-26 00:28:01 +000076#include "llvm/Transforms/IPO/GlobalOpt.h"
Davide Italiano2ae76dd2016-11-21 00:28:23 +000077#include "llvm/Transforms/IPO/GlobalSplit.h"
Aditya Kumar9e20ade2018-10-03 05:55:20 +000078#include "llvm/Transforms/IPO/HotColdSplitting.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000079#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Chandler Carruth1d963112016-12-20 03:15:32 +000080#include "llvm/Transforms/IPO/Inliner.h"
Justin Bogner4563a062016-04-26 20:15:52 +000081#include "llvm/Transforms/IPO/Internalize.h"
Davide Italianoe8ae0b52016-07-11 18:10:06 +000082#include "llvm/Transforms/IPO/LowerTypeTests.h"
Easwaran Raman1832bf62016-06-27 16:50:18 +000083#include "llvm/Transforms/IPO/PartialInlining.h"
Davide Italianof54f2f02016-05-05 21:05:36 +000084#include "llvm/Transforms/IPO/SCCP.h"
David Blaikie301627f2018-03-22 22:42:44 +000085#include "llvm/Transforms/IPO/SampleProfile.h"
Justin Bogner21e15372015-10-30 23:28:12 +000086#include "llvm/Transforms/IPO/StripDeadPrototypes.h"
Easwaran Ramanbdf20262018-01-09 19:39:35 +000087#include "llvm/Transforms/IPO/SyntheticCountsPropagation.h"
Davide Italianod737dd22016-06-14 21:44:19 +000088#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000089#include "llvm/Transforms/InstCombine/InstCombine.h"
Philip Pfaffeb39a97c2019-01-03 13:42:44 +000090#include "llvm/Transforms/Instrumentation.h"
Chandler Carruth00a301d2017-11-14 01:30:04 +000091#include "llvm/Transforms/Instrumentation/BoundsChecking.h"
Vitaly Buka4493fe12018-11-26 21:57:47 +000092#include "llvm/Transforms/Instrumentation/CGProfile.h"
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +000093#include "llvm/Transforms/Instrumentation/ControlHeightReduction.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +000094#include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
95#include "llvm/Transforms/Instrumentation/InstrProfiling.h"
Philip Pfaffeb39a97c2019-01-03 13:42:44 +000096#include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
Philip Pfaffe685c76d2019-01-16 09:28:01 +000097#include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
David Blaikie4fe1fe12018-03-23 22:11:06 +000098#include "llvm/Transforms/Instrumentation/PGOInstrumentation.h"
Justin Bogner19b67992015-10-30 23:13:18 +000099#include "llvm/Transforms/Scalar/ADCE.h"
Sean Silvaa4c2d152016-06-15 06:18:01 +0000100#include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h"
Davide Italiano655a1452016-05-25 01:57:04 +0000101#include "llvm/Transforms/Scalar/BDCE.h"
Jun Bum Lim0c990072017-11-03 20:41:16 +0000102#include "llvm/Transforms/Scalar/CallSiteSplitting.h"
Adam Nemet3beef412016-07-18 16:29:17 +0000103#include "llvm/Transforms/Scalar/ConstantHoisting.h"
Sean Silvab025d372016-07-06 23:26:29 +0000104#include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h"
Justin Bogner395c2122016-04-22 19:40:41 +0000105#include "llvm/Transforms/Scalar/DCE.h"
Justin Bogner594e07b2016-05-17 21:38:13 +0000106#include "llvm/Transforms/Scalar/DeadStoreElimination.h"
Sanjay Patel6fd43912017-09-09 13:38:18 +0000107#include "llvm/Transforms/Scalar/DivRemPairs.h"
Chandler Carruthe8c686a2015-02-01 10:51:23 +0000108#include "llvm/Transforms/Scalar/EarlyCSE.h"
Michael Kuperstein83b753d2016-06-24 23:32:02 +0000109#include "llvm/Transforms/Scalar/Float2Int.h"
Easwaran Raman019e0bf2016-06-03 22:54:26 +0000110#include "llvm/Transforms/Scalar/GVN.h"
Davide Italiano89ab89d2016-06-14 00:49:23 +0000111#include "llvm/Transforms/Scalar/GuardWidening.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000112#include "llvm/Transforms/Scalar/IVUsersPrinter.h"
Sanjoy Das4d4339d2016-06-05 18:01:19 +0000113#include "llvm/Transforms/Scalar/IndVarSimplify.h"
Fedor Sergeev194a4072018-03-15 11:01:19 +0000114#include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h"
Chandler Carruth7c557f82018-06-29 23:36:03 +0000115#include "llvm/Transforms/Scalar/InstSimplifyPass.h"
Sean Silva46590d52016-06-14 00:51:09 +0000116#include "llvm/Transforms/Scalar/JumpThreading.h"
Dehao Chenf400a092016-07-12 22:42:24 +0000117#include "llvm/Transforms/Scalar/LICM.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000118#include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h"
Teresa Johnson1eca6bc2016-08-13 04:11:27 +0000119#include "llvm/Transforms/Scalar/LoopDataPrefetch.h"
Jun Bum Limc837af32016-07-14 18:28:29 +0000120#include "llvm/Transforms/Scalar/LoopDeletion.h"
Adam Nemetb2593f72016-07-18 16:29:27 +0000121#include "llvm/Transforms/Scalar/LoopDistribute.h"
Dehao Chenb9f8e292016-07-12 18:45:51 +0000122#include "llvm/Transforms/Scalar/LoopIdiomRecognize.h"
Chandler Carruthe6c30fd2018-05-25 01:32:36 +0000123#include "llvm/Transforms/Scalar/LoopInstSimplify.h"
Chandler Carruthbaabda92017-01-27 01:32:26 +0000124#include "llvm/Transforms/Scalar/LoopLoadElimination.h"
Chandler Carruth3bab7e12017-01-11 09:43:56 +0000125#include "llvm/Transforms/Scalar/LoopPassManager.h"
Artur Pilipenko8fb3d572017-01-25 16:00:44 +0000126#include "llvm/Transforms/Scalar/LoopPredication.h"
Justin Bognerd0d23412016-05-03 22:02:31 +0000127#include "llvm/Transforms/Scalar/LoopRotation.h"
Justin Bognerab6a5132016-05-03 21:47:32 +0000128#include "llvm/Transforms/Scalar/LoopSimplifyCFG.h"
Chandler Carruthe9b18e32017-01-20 08:42:19 +0000129#include "llvm/Transforms/Scalar/LoopSink.h"
Dehao Chen6132ee82016-07-18 21:41:50 +0000130#include "llvm/Transforms/Scalar/LoopStrengthReduce.h"
David Green963401d2018-07-01 12:47:30 +0000131#include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h"
Sean Silvae3c18a52016-07-19 23:54:23 +0000132#include "llvm/Transforms/Scalar/LoopUnrollPass.h"
Davide Italiano99223442016-05-13 22:52:35 +0000133#include "llvm/Transforms/Scalar/LowerAtomic.h"
Chandler Carruth43e590e2015-01-24 11:13:02 +0000134#include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h"
Michael Kupersteine45d4d92016-07-28 22:08:41 +0000135#include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h"
Max Kazantsevf392bc82019-01-31 09:10:17 +0000136#include "llvm/Transforms/Scalar/LowerWidenableCondition.h"
Max Kazantsevb9e65cb2018-12-07 14:39:46 +0000137#include "llvm/Transforms/Scalar/MakeGuardsExplicit.h"
Sean Silva6347df02016-06-14 02:44:55 +0000138#include "llvm/Transforms/Scalar/MemCpyOptimizer.h"
Davide Italianob49aa5c2016-06-17 19:10:09 +0000139#include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h"
Wei Mi1cf58f82016-07-21 22:28:52 +0000140#include "llvm/Transforms/Scalar/NaryReassociate.h"
Davide Italianoe05e3302016-12-22 16:35:02 +0000141#include "llvm/Transforms/Scalar/NewGVN.h"
Davide Italiano1021c682016-05-25 23:38:53 +0000142#include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h"
Justin Bognerc2bf63d2016-04-26 23:39:29 +0000143#include "llvm/Transforms/Scalar/Reassociate.h"
Fedor Sergeev4b86d792017-12-15 09:32:11 +0000144#include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h"
Davide Italiano98f7e0e2016-05-18 15:18:25 +0000145#include "llvm/Transforms/Scalar/SCCP.h"
Chandler Carruth29a18a42015-09-12 09:09:14 +0000146#include "llvm/Transforms/Scalar/SROA.h"
Mikael Holmenb6f76002018-11-21 14:00:17 +0000147#include "llvm/Transforms/Scalar/Scalarizer.h"
Chandler Carruth1353f9a2017-04-27 18:45:20 +0000148#include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000149#include "llvm/Transforms/Scalar/SimplifyCFG.h"
Justin Bognerb9394902016-04-22 19:54:10 +0000150#include "llvm/Transforms/Scalar/Sink.h"
Chandler Carruthc34f7892017-11-28 11:32:31 +0000151#include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h"
Michael Kupersteinc4061862016-08-01 21:48:33 +0000152#include "llvm/Transforms/Scalar/SpeculativeExecution.h"
Sean Silva59fe82f2016-07-06 23:48:41 +0000153#include "llvm/Transforms/Scalar/TailRecursionElimination.h"
Michael Kruse72448522018-12-12 17:32:52 +0000154#include "llvm/Transforms/Scalar/WarnMissedTransforms.h"
Xinliang David Li1eaecef2016-06-15 21:51:30 +0000155#include "llvm/Transforms/Utils/AddDiscriminators.h"
Wei Mie04d0ef2016-07-22 18:04:25 +0000156#include "llvm/Transforms/Utils/BreakCriticalEdges.h"
Teresa Johnson853b9622019-01-04 19:04:54 +0000157#include "llvm/Transforms/Utils/CanonicalizeAliases.h"
Hans Wennborge1ecd612017-11-14 21:09:45 +0000158#include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
Easwaran Ramane12c4872016-06-09 19:44:46 +0000159#include "llvm/Transforms/Utils/LCSSA.h"
Rong Xu1c0e9b92016-10-18 21:36:27 +0000160#include "llvm/Transforms/Utils/LibCallsShrinkWrap.h"
Davide Italianocd96cfd2016-07-09 03:03:01 +0000161#include "llvm/Transforms/Utils/LoopSimplify.h"
Michael Kuperstein31b83992016-08-12 17:28:27 +0000162#include "llvm/Transforms/Utils/LowerInvoke.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000163#include "llvm/Transforms/Utils/Mem2Reg.h"
Mehdi Amini27d23792016-09-16 16:56:30 +0000164#include "llvm/Transforms/Utils/NameAnonGlobals.h"
Michael Kuperstein39feb622016-07-25 20:52:00 +0000165#include "llvm/Transforms/Utils/SymbolRewriter.h"
Markus Lavin4dc4ebd2018-12-07 08:23:37 +0000166#include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h"
Sean Silvadb90d4d2016-07-09 22:56:50 +0000167#include "llvm/Transforms/Vectorize/LoopVectorize.h"
Sean Silva0dacbd82016-07-09 03:11:29 +0000168#include "llvm/Transforms/Vectorize/SLPVectorizer.h"
Davide Italianocccf4f02016-06-14 03:22:22 +0000169
Chandler Carruth66445382014-01-11 08:16:35 +0000170using namespace llvm;
171
Chandler Carruth719ffe12017-02-12 05:38:04 +0000172static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations",
173 cl::ReallyHidden, cl::init(4));
Xinliang David Li126157c2017-05-22 16:41:57 +0000174static cl::opt<bool>
175 RunPartialInlining("enable-npm-partial-inlining", cl::init(false),
176 cl::Hidden, cl::ZeroOrMore,
177 cl::desc("Run Partial inlinining pass"));
Chandler Carruth719ffe12017-02-12 05:38:04 +0000178
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000179static cl::opt<bool>
Davide Italiano8e7d11a2017-05-22 23:47:11 +0000180 RunNewGVN("enable-npm-newgvn", cl::init(false),
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000181 cl::Hidden, cl::ZeroOrMore,
182 cl::desc("Run NewGVN instead of GVN"));
183
Geoff Berry3cca1da2017-06-10 15:20:03 +0000184static cl::opt<bool> EnableEarlyCSEMemSSA(
Geoff Berry2573a192017-06-27 22:25:02 +0000185 "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden,
186 cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)"));
Geoff Berry3cca1da2017-06-10 15:20:03 +0000187
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000188static cl::opt<bool> EnableGVNHoist(
Eric Christopherdcf1d972018-10-01 18:57:08 +0000189 "enable-npm-gvn-hoist", cl::init(false), cl::Hidden,
190 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
Chandler Carruthc246a4c2017-04-27 00:28:03 +0000191
Davide Italianobe1b6a92017-06-03 23:18:29 +0000192static cl::opt<bool> EnableGVNSink(
193 "enable-npm-gvn-sink", cl::init(false), cl::Hidden,
194 cl::desc("Enable the GVN hoisting pass for the new PM (default = off)"));
195
David Green963401d2018-07-01 12:47:30 +0000196static cl::opt<bool> EnableUnrollAndJam(
197 "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden,
198 cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)"));
199
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000200static cl::opt<bool> EnableSyntheticCounts(
201 "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore,
202 cl::desc("Run synthetic function entry count generation "
203 "pass"));
204
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000205static Regex DefaultAliasRegex(
206 "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$");
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000207
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000208static cl::opt<bool>
209 EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden,
210 cl::desc("Enable control height reduction optimization (CHR)"));
211
Aditya Kumar9e20ade2018-10-03 05:55:20 +0000212extern cl::opt<bool> EnableHotColdSplit;
213
Wei Mi3bcccdf2019-01-17 20:48:34 +0000214extern cl::opt<bool> FlattenedProfileUsed;
215
Chandler Carruthe3f50642016-12-22 06:59:15 +0000216static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) {
217 switch (Level) {
218 case PassBuilder::O0:
219 case PassBuilder::O1:
220 case PassBuilder::O2:
221 case PassBuilder::O3:
222 return false;
223
224 case PassBuilder::Os:
225 case PassBuilder::Oz:
226 return true;
227 }
228 llvm_unreachable("Invalid optimization level!");
229}
230
Chandler Carruth66445382014-01-11 08:16:35 +0000231namespace {
232
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000233/// No-op module pass which does nothing.
Chandler Carruth66445382014-01-11 08:16:35 +0000234struct NoOpModulePass {
Sean Silvafd03ac62016-08-09 00:28:38 +0000235 PreservedAnalyses run(Module &M, ModuleAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000236 return PreservedAnalyses::all();
237 }
Chandler Carrutha13f27c2014-01-11 11:52:05 +0000238 static StringRef name() { return "NoOpModulePass"; }
Chandler Carruth66445382014-01-11 08:16:35 +0000239};
240
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000241/// No-op module analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000242class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> {
243 friend AnalysisInfoMixin<NoOpModuleAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000244 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000245
246public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000247 struct Result {};
Sean Silvafd03ac62016-08-09 00:28:38 +0000248 Result run(Module &, ModuleAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000249 static StringRef name() { return "NoOpModuleAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000250};
251
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000252/// No-op CGSCC pass which does nothing.
Chandler Carruth572e3402014-04-21 11:12:00 +0000253struct NoOpCGSCCPass {
Chandler Carruth88823462016-08-24 09:37:14 +0000254 PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &,
255 LazyCallGraph &, CGSCCUpdateResult &UR) {
Chandler Carruth572e3402014-04-21 11:12:00 +0000256 return PreservedAnalyses::all();
257 }
258 static StringRef name() { return "NoOpCGSCCPass"; }
259};
260
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000261/// No-op CGSCC analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000262class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> {
263 friend AnalysisInfoMixin<NoOpCGSCCAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000264 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000265
266public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000267 struct Result {};
Chandler Carruth88823462016-08-24 09:37:14 +0000268 Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000269 return Result();
270 }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000271 static StringRef name() { return "NoOpCGSCCAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000272};
273
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000274/// No-op function pass which does nothing.
Chandler Carruthd8330982014-01-12 09:34:22 +0000275struct NoOpFunctionPass {
Sean Silva36e0d012016-08-09 00:28:15 +0000276 PreservedAnalyses run(Function &F, FunctionAnalysisManager &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000277 return PreservedAnalyses::all();
278 }
Chandler Carruthd8330982014-01-12 09:34:22 +0000279 static StringRef name() { return "NoOpFunctionPass"; }
280};
281
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000282/// No-op function analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000283class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> {
284 friend AnalysisInfoMixin<NoOpFunctionAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000285 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000286
287public:
Chandler Carruth0b576b32015-01-06 02:50:06 +0000288 struct Result {};
Sean Silva36e0d012016-08-09 00:28:15 +0000289 Result run(Function &, FunctionAnalysisManager &) { return Result(); }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000290 static StringRef name() { return "NoOpFunctionAnalysis"; }
Chandler Carruth0b576b32015-01-06 02:50:06 +0000291};
292
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000293/// No-op loop pass which does nothing.
Justin Bognereecc3c82016-02-25 07:23:08 +0000294struct NoOpLoopPass {
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000295 PreservedAnalyses run(Loop &L, LoopAnalysisManager &,
296 LoopStandardAnalysisResults &, LPMUpdater &) {
Chandler Carruth164a2aa62016-06-17 00:11:01 +0000297 return PreservedAnalyses::all();
298 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000299 static StringRef name() { return "NoOpLoopPass"; }
300};
301
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000302/// No-op loop analysis.
Chandler Carruth30a07302016-03-11 10:33:22 +0000303class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> {
304 friend AnalysisInfoMixin<NoOpLoopAnalysis>;
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000305 static AnalysisKey Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000306
307public:
Justin Bognereecc3c82016-02-25 07:23:08 +0000308 struct Result {};
Chandler Carruth410eaeb2017-01-11 06:23:21 +0000309 Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) {
310 return Result();
311 }
Justin Bognereecc3c82016-02-25 07:23:08 +0000312 static StringRef name() { return "NoOpLoopAnalysis"; }
Justin Bognereecc3c82016-02-25 07:23:08 +0000313};
314
Chandler Carruthdab4eae2016-11-23 17:53:26 +0000315AnalysisKey NoOpModuleAnalysis::Key;
316AnalysisKey NoOpCGSCCAnalysis::Key;
317AnalysisKey NoOpFunctionAnalysis::Key;
318AnalysisKey NoOpLoopAnalysis::Key;
Chandler Carruthb4faf132016-03-11 10:22:49 +0000319
Chandler Carruth66445382014-01-11 08:16:35 +0000320} // End anonymous namespace.
321
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000322void PassBuilder::invokePeepholeEPCallbacks(
323 FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
324 for (auto &C : PeepholeEPCallbacks)
325 C(FPM, Level);
326}
327
Chandler Carruth1ff77242015-03-07 09:02:36 +0000328void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000329#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000330 MAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000331#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000332
333 for (auto &C : ModuleAnalysisRegistrationCallbacks)
334 C(MAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000335}
336
Chandler Carruth1ff77242015-03-07 09:02:36 +0000337void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000338#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000339 CGAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000340#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000341
342 for (auto &C : CGSCCAnalysisRegistrationCallbacks)
343 C(CGAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000344}
345
Chandler Carruth1ff77242015-03-07 09:02:36 +0000346void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000347#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruthedf59962016-02-18 09:45:17 +0000348 FAM.registerPass([&] { return CREATE_PASS; });
Chandler Carruthb70f6732015-01-06 02:21:37 +0000349#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000350
351 for (auto &C : FunctionAnalysisRegistrationCallbacks)
352 C(FAM);
Chandler Carruthb70f6732015-01-06 02:21:37 +0000353}
354
Justin Bognereecc3c82016-02-25 07:23:08 +0000355void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) {
Chandler Carruth74a8a222016-06-17 07:15:29 +0000356#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
Justin Bognereecc3c82016-02-25 07:23:08 +0000357 LAM.registerPass([&] { return CREATE_PASS; });
358#include "PassRegistry.def"
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000359
360 for (auto &C : LoopAnalysisRegistrationCallbacks)
361 C(LAM);
Justin Bognereecc3c82016-02-25 07:23:08 +0000362}
363
Chandler Carruthe3f50642016-12-22 06:59:15 +0000364FunctionPassManager
365PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000366 ThinLTOPhase Phase,
367 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000368 assert(Level != O0 && "Must request optimizations!");
369 FunctionPassManager FPM(DebugLogging);
370
371 // Form SSA out of local memory accesses after breaking apart aggregates into
372 // scalars.
373 FPM.addPass(SROA());
374
375 // Catch trivial redundancies
Geoff Berry3cca1da2017-06-10 15:20:03 +0000376 FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000377
Davide Italianoc3688312017-06-01 23:08:14 +0000378 // Hoisting of scalars and load expressions.
379 if (EnableGVNHoist)
380 FPM.addPass(GVNHoistPass());
381
Davide Italianobe1b6a92017-06-03 23:18:29 +0000382 // Global value numbering based sinking.
383 if (EnableGVNSink) {
384 FPM.addPass(GVNSinkPass());
385 FPM.addPass(SimplifyCFGPass());
386 }
387
Chandler Carruthe3f50642016-12-22 06:59:15 +0000388 // Speculative execution if the target has divergent branches; otherwise nop.
389 FPM.addPass(SpeculativeExecutionPass());
390
391 // Optimize based on known information about branches, and cleanup afterward.
392 FPM.addPass(JumpThreadingPass());
393 FPM.addPass(CorrelatedValuePropagationPass());
394 FPM.addPass(SimplifyCFGPass());
Amjad Aboudf1f57a32018-01-25 12:06:32 +0000395 if (Level == O3)
396 FPM.addPass(AggressiveInstCombinePass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000397 FPM.addPass(InstCombinePass());
398
399 if (!isOptimizingForSize(Level))
400 FPM.addPass(LibCallsShrinkWrapPass());
401
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000402 invokePeepholeEPCallbacks(FPM, Level);
403
Rong Xue1f42452017-10-23 22:21:29 +0000404 // For PGO use pipeline, try to optimize memory intrinsics such as memcpy
405 // using the size value profile. Don't perform this when optimizing for size.
406 if (PGOOpt && !PGOOpt->ProfileUseFile.empty() &&
407 !isOptimizingForSize(Level))
408 FPM.addPass(PGOMemOPSizeOpt());
409
Chandler Carruthe3f50642016-12-22 06:59:15 +0000410 FPM.addPass(TailCallElimPass());
411 FPM.addPass(SimplifyCFGPass());
412
413 // Form canonically associated expression trees, and simplify the trees using
414 // basic mathematical properties. For example, this will form (nearly)
415 // minimal multiplication trees.
416 FPM.addPass(ReassociatePass());
417
418 // Add the primary loop simplification pipeline.
419 // FIXME: Currently this is split into two loop pass pipelines because we run
Chandler Carruth71fd2702018-05-30 02:46:45 +0000420 // some function passes in between them. These can and should be removed
421 // and/or replaced by scheduling the loop pass equivalents in the correct
422 // positions. But those equivalent passes aren't powerful enough yet.
423 // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still
424 // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to
425 // fully replace `SimplifyCFGPass`, and the closest to the other we have is
426 // `LoopInstSimplify`.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000427 LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging);
428
Chandler Carruth71fd2702018-05-30 02:46:45 +0000429 // Simplify the loop body. We do this initially to clean up after other loop
430 // passes run, either when iterating on a loop or on inner loops with
431 // implications on the outer loop.
432 LPM1.addPass(LoopInstSimplifyPass());
433 LPM1.addPass(LoopSimplifyCFGPass());
434
Chandler Carruthe3f50642016-12-22 06:59:15 +0000435 // Rotate Loop - disable header duplication at -Oz
436 LPM1.addPass(LoopRotatePass(Level != Oz));
437 LPM1.addPass(LICMPass());
Chandler Carruth86248d52017-05-26 01:24:11 +0000438 LPM1.addPass(SimpleLoopUnswitchPass());
Chandler Carruth79b733b2017-01-26 23:21:17 +0000439 LPM2.addPass(IndVarSimplifyPass());
440 LPM2.addPass(LoopIdiomRecognizePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000441
442 for (auto &C : LateLoopOptimizationsEPCallbacks)
443 C(LPM2, Level);
444
Chandler Carruth79b733b2017-01-26 23:21:17 +0000445 LPM2.addPass(LoopDeletionPass());
Dehao Chen95f00302017-07-30 04:55:39 +0000446 // Do not enable unrolling in PreLinkThinLTO phase during sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000447 // because it changes IR to makes profile annotation in back compile
448 // inaccurate.
Dehao Chen95f00302017-07-30 04:55:39 +0000449 if (Phase != ThinLTOPhase::PreLink ||
450 !PGOOpt || PGOOpt->SampleProfileFile.empty())
Teresa Johnsonecd90132017-08-02 20:35:29 +0000451 LPM2.addPass(LoopFullUnrollPass(Level));
Chandler Carruth79b733b2017-01-26 23:21:17 +0000452
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000453 for (auto &C : LoopOptimizerEndEPCallbacks)
454 C(LPM2, Level);
455
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000456 // We provide the opt remark emitter pass for LICM to use. We only need to do
457 // this once as it is immutable.
458 FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000459 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000460 FPM.addPass(SimplifyCFGPass());
461 FPM.addPass(InstCombinePass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000462 FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000463
464 // Eliminate redundancies.
465 if (Level != O1) {
466 // These passes add substantial compile time so skip them at O1.
467 FPM.addPass(MergedLoadStoreMotionPass());
Davide Italiano8a09b8e2017-05-22 23:41:40 +0000468 if (RunNewGVN)
469 FPM.addPass(NewGVNPass());
470 else
471 FPM.addPass(GVN());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000472 }
473
474 // Specially optimize memory movement as it doesn't look like dataflow in SSA.
475 FPM.addPass(MemCpyOptPass());
476
477 // Sparse conditional constant propagation.
478 // FIXME: It isn't clear why we do this *after* loop passes rather than
479 // before...
480 FPM.addPass(SCCPPass());
481
482 // Delete dead bit computations (instcombine runs after to fold away the dead
483 // computations, and then ADCE will run later to exploit any new DCE
484 // opportunities that creates).
485 FPM.addPass(BDCEPass());
486
487 // Run instcombine after redundancy and dead bit elimination to exploit
488 // opportunities opened up by them.
489 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000490 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000491
492 // Re-consider control flow based optimizations after redundancy elimination,
493 // redo DCE, etc.
494 FPM.addPass(JumpThreadingPass());
495 FPM.addPass(CorrelatedValuePropagationPass());
496 FPM.addPass(DSEPass());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000497 FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000498
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000499 for (auto &C : ScalarOptimizerLateEPCallbacks)
500 C(FPM, Level);
501
Chandler Carruthe3f50642016-12-22 06:59:15 +0000502 // Finally, do an expensive DCE pass to catch all the dead code exposed by
503 // the simplifications and basic cleanup after all the simplifications.
504 FPM.addPass(ADCEPass());
505 FPM.addPass(SimplifyCFGPass());
506 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000507 invokePeepholeEPCallbacks(FPM, Level);
Chandler Carruthe3f50642016-12-22 06:59:15 +0000508
Hiroshi Yamauchi9775a622018-09-04 17:19:13 +0000509 if (EnableCHR && Level == O3 && PGOOpt &&
510 (!PGOOpt->ProfileUseFile.empty() || !PGOOpt->SampleProfileFile.empty()))
511 FPM.addPass(ControlHeightReductionPass());
512
Chandler Carruthe3f50642016-12-22 06:59:15 +0000513 return FPM;
514}
515
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000516void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging,
517 PassBuilder::OptimizationLevel Level,
518 bool RunProfileGen,
519 std::string ProfileGenFile,
Richard Smith6c676622018-10-10 23:13:47 +0000520 std::string ProfileUseFile,
521 std::string ProfileRemappingFile) {
Davide Italiano513dfaa2017-02-13 15:26:22 +0000522 // Generally running simplification passes and the inliner with an high
523 // threshold results in smaller executables, but there may be cases where
524 // the size grows, so let's be conservative here and skip this simplification
525 // at -Os/Oz.
526 if (!isOptimizingForSize(Level)) {
527 InlineParams IP;
528
529 // In the old pass manager, this is a cl::opt. Should still this be one?
530 IP.DefaultThreshold = 75;
531
532 // FIXME: The hint threshold has the same value used by the regular inliner.
533 // This should probably be lowered after performance testing.
534 // FIXME: this comment is cargo culted from the old pass manager, revisit).
535 IP.HintThreshold = 325;
536
537 CGSCCPassManager CGPipeline(DebugLogging);
538
539 CGPipeline.addPass(InlinerPass(IP));
540
541 FunctionPassManager FPM;
542 FPM.addPass(SROA());
543 FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies.
544 FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks.
545 FPM.addPass(InstCombinePass()); // Combine silly sequences.
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000546 invokePeepholeEPCallbacks(FPM, Level);
Davide Italiano513dfaa2017-02-13 15:26:22 +0000547
Davide Italiano513dfaa2017-02-13 15:26:22 +0000548 CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
549
550 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline)));
551 }
552
Chandler Carruthf4d62c42017-05-25 07:15:09 +0000553 // Delete anything that is now dead to make sure that we don't instrument
554 // dead code. Instrumentation can end up keeping dead code around and
555 // dramatically increase code size.
556 MPM.addPass(GlobalDCEPass());
557
Davide Italiano513dfaa2017-02-13 15:26:22 +0000558 if (RunProfileGen) {
559 MPM.addPass(PGOInstrumentationGen());
560
Xinliang David Lib67530e2017-06-25 00:26:43 +0000561 FunctionPassManager FPM;
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000562 FPM.addPass(
563 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Xinliang David Lib67530e2017-06-25 00:26:43 +0000564 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
565
Davide Italiano513dfaa2017-02-13 15:26:22 +0000566 // Add the profile lowering pass.
567 InstrProfOptions Options;
568 if (!ProfileGenFile.empty())
569 Options.InstrProfileOutput = ProfileGenFile;
Xinliang David Lib67530e2017-06-25 00:26:43 +0000570 Options.DoCounterPromotion = true;
Davide Italiano513dfaa2017-02-13 15:26:22 +0000571 MPM.addPass(InstrProfiling(Options));
572 }
573
574 if (!ProfileUseFile.empty())
Richard Smith6c676622018-10-10 23:13:47 +0000575 MPM.addPass(PGOInstrumentationUse(ProfileUseFile, ProfileRemappingFile));
Davide Italiano513dfaa2017-02-13 15:26:22 +0000576}
577
Easwaran Raman8249fac2017-06-28 13:33:49 +0000578static InlineParams
579getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) {
580 auto O3 = PassBuilder::O3;
581 unsigned OptLevel = Level > O3 ? 2 : Level;
582 unsigned SizeLevel = Level > O3 ? Level - O3 : 0;
583 return getInlineParams(OptLevel, SizeLevel);
584}
585
Chandler Carruthe3f50642016-12-22 06:59:15 +0000586ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000587PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level,
Dehao Chen95f00302017-07-30 04:55:39 +0000588 ThinLTOPhase Phase,
589 bool DebugLogging) {
Chandler Carruthe3f50642016-12-22 06:59:15 +0000590 ModulePassManager MPM(DebugLogging);
591
Teresa Johnsonb0bf5302019-02-05 04:09:19 +0000592 bool HasSampleProfile = PGOOpt && !PGOOpt->SampleProfileFile.empty();
593
594 // In ThinLTO mode, when flattened profile is used, all the available
595 // profile information will be annotated in PreLink phase so there is
596 // no need to load the profile again in PostLink.
597 bool LoadSampleProfile =
598 HasSampleProfile &&
599 !(FlattenedProfileUsed && Phase == ThinLTOPhase::PostLink);
600
601 // During the ThinLTO backend phase we perform early indirect call promotion
602 // here, before globalopt. Otherwise imported available_externally functions
603 // look unreferenced and are removed. If we are going to load the sample
604 // profile then defer until later.
605 // TODO: See if we can move later and consolidate with the location where
606 // we perform ICP when we are loading a sample profile.
607 // TODO: We pass HasSampleProfile (whether there was a sample profile file
608 // passed to the compile) to the SamplePGO flag of ICP. This is used to
609 // determine whether the new direct calls are annotated with prof metadata.
610 // Ideally this should be determined from whether the IR is annotated with
611 // sample profile, and not whether the a sample profile was provided on the
612 // command line. E.g. for flattened profiles where we will not be reloading
613 // the sample profile in the ThinLTO backend, we ideally shouldn't have to
614 // provide the sample profile file.
615 if (Phase == ThinLTOPhase::PostLink && !LoadSampleProfile)
616 MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile));
617
Chandler Carruthe3f50642016-12-22 06:59:15 +0000618 // Do basic inference of function attributes from known properties of system
619 // libraries and other oracles.
620 MPM.addPass(InferFunctionAttrsPass());
621
622 // Create an early function pass manager to cleanup the output of the
623 // frontend.
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000624 FunctionPassManager EarlyFPM(DebugLogging);
625 EarlyFPM.addPass(SimplifyCFGPass());
626 EarlyFPM.addPass(SROA());
627 EarlyFPM.addPass(EarlyCSEPass());
628 EarlyFPM.addPass(LowerExpectIntrinsicPass());
Jun Bum Lim0c990072017-11-03 20:41:16 +0000629 if (Level == O3)
630 EarlyFPM.addPass(CallSiteSplittingPass());
631
Dehao Chen08f88312017-08-07 20:23:20 +0000632 // In SamplePGO ThinLTO backend, we need instcombine before profile annotation
633 // to convert bitcast to direct calls so that they can be inlined during the
634 // profile annotation prepration step.
635 // More details about SamplePGO design can be found in:
636 // https://research.google.com/pubs/pub45290.html
637 // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured.
Teresa Johnsonb0bf5302019-02-05 04:09:19 +0000638 if (LoadSampleProfile)
Dehao Chen08f88312017-08-07 20:23:20 +0000639 EarlyFPM.addPass(InstCombinePass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000640 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000641
Teresa Johnsonb0bf5302019-02-05 04:09:19 +0000642 if (LoadSampleProfile) {
Dehao Chen08f88312017-08-07 20:23:20 +0000643 // Annotate sample profile right after early FPM to ensure freshness of
644 // the debug info.
Teresa Johnsonb0bf5302019-02-05 04:09:19 +0000645 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
646 PGOOpt->ProfileRemappingFile,
647 Phase == ThinLTOPhase::PreLink));
Dehao Chen08f88312017-08-07 20:23:20 +0000648 // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard
649 // for the profile annotation to be accurate in the ThinLTO backend.
650 if (Phase != ThinLTOPhase::PreLink)
651 // We perform early indirect call promotion here, before globalopt.
652 // This is important for the ThinLTO backend phase because otherwise
653 // imported available_externally functions look unreferenced and are
654 // removed.
655 MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink,
Teresa Johnsonb0bf5302019-02-05 04:09:19 +0000656 true /* SamplePGO */));
Dehao Chen08f88312017-08-07 20:23:20 +0000657 }
658
Malcolm Parsons21e545d2018-01-24 10:33:39 +0000659 // Interprocedural constant propagation now that basic cleanup has occurred
Chandler Carruthe3f50642016-12-22 06:59:15 +0000660 // and prior to optimizing globals.
661 // FIXME: This position in the pipeline hasn't been carefully considered in
662 // years, it should be re-analyzed.
663 MPM.addPass(IPSCCPPass());
664
Matthew Simpsoncb585582017-10-25 13:40:08 +0000665 // Attach metadata to indirect call sites indicating the set of functions
666 // they may target at run-time. This should follow IPSCCP.
667 MPM.addPass(CalledValuePropagationPass());
668
Chandler Carruthe3f50642016-12-22 06:59:15 +0000669 // Optimize globals to try and fold them into constants.
670 MPM.addPass(GlobalOptPass());
671
672 // Promote any localized globals to SSA registers.
673 // FIXME: Should this instead by a run of SROA?
674 // FIXME: We should probably run instcombine and simplify-cfg afterward to
675 // delete control flows that are dead once globals have been folded to
676 // constants.
677 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
678
679 // Remove any dead arguments exposed by cleanups and constand folding
680 // globals.
681 MPM.addPass(DeadArgumentEliminationPass());
682
683 // Create a small function pass pipeline to cleanup after all the global
684 // optimizations.
685 FunctionPassManager GlobalCleanupPM(DebugLogging);
686 GlobalCleanupPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000687 invokePeepholeEPCallbacks(GlobalCleanupPM, Level);
688
Chandler Carruthe3f50642016-12-22 06:59:15 +0000689 GlobalCleanupPM.addPass(SimplifyCFGPass());
690 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM)));
691
Dehao Chen89d32262017-08-02 01:28:31 +0000692 // Add all the requested passes for instrumentation PGO, if requested.
693 if (PGOOpt && Phase != ThinLTOPhase::PostLink &&
694 (!PGOOpt->ProfileGenFile.empty() || !PGOOpt->ProfileUseFile.empty())) {
695 addPGOInstrPasses(MPM, DebugLogging, Level, PGOOpt->RunProfileGen,
Richard Smith6c676622018-10-10 23:13:47 +0000696 PGOOpt->ProfileGenFile, PGOOpt->ProfileUseFile,
697 PGOOpt->ProfileRemappingFile);
Dehao Chen89d32262017-08-02 01:28:31 +0000698 MPM.addPass(PGOIndirectCallPromotion(false, false));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000699 }
Davide Italiano513dfaa2017-02-13 15:26:22 +0000700
Easwaran Ramanbdf20262018-01-09 19:39:35 +0000701 // Synthesize function entry counts for non-PGO compilation.
702 if (EnableSyntheticCounts && !PGOOpt)
703 MPM.addPass(SyntheticCountsPropagation());
704
Teresa Johnson716abbe2019-02-06 04:29:39 +0000705 // Split out cold code. Splitting is done before inlining because 1) the most
706 // common kinds of cold regions can (a) be found before inlining and (b) do
707 // not grow after inlining, and 2) inhibiting inlining of cold code improves
708 // code size & compile time. Split after Mem2Reg to make code model estimates
709 // more accurate, but before InstCombine to allow it to clean things up.
710 if (EnableHotColdSplit && Phase != ThinLTOPhase::PostLink)
711 MPM.addPass(HotColdSplittingPass());
712
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000713 // Require the GlobalsAA analysis for the module so we can query it within
714 // the CGSCC pipeline.
715 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000716
Easwaran Raman5e6f9bd2017-05-04 16:58:45 +0000717 // Require the ProfileSummaryAnalysis for the module so we can query it within
718 // the inliner pass.
719 MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>());
720
Chandler Carruthe3f50642016-12-22 06:59:15 +0000721 // Now begin the main postorder CGSCC pipeline.
722 // FIXME: The current CGSCC pipeline has its origins in the legacy pass
723 // manager and trying to emulate its precise behavior. Much of this doesn't
724 // make a lot of sense and we should revisit the core CGSCC structure.
725 CGSCCPassManager MainCGPipeline(DebugLogging);
726
727 // Note: historically, the PruneEH pass was run first to deduce nounwind and
728 // generally clean up exception handling overhead. It isn't clear this is
729 // valuable as the inliner doesn't currently care whether it is inlining an
730 // invoke or a call.
731
732 // Run the inliner first. The theory is that we are walking bottom-up and so
733 // the callees have already been fully optimized, and we want to inline them
734 // into the callers so that our optimizations can reflect that.
Dehao Chen95f00302017-07-30 04:55:39 +0000735 // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO
Dehao Chen3a9861422017-07-07 20:53:10 +0000736 // because it makes profile annotation in the backend inaccurate.
737 InlineParams IP = getInlineParamsFromOptLevel(Level);
Dehao Chen95f00302017-07-30 04:55:39 +0000738 if (Phase == ThinLTOPhase::PreLink &&
739 PGOOpt && !PGOOpt->SampleProfileFile.empty())
Dehao Chen3a9861422017-07-07 20:53:10 +0000740 IP.HotCallSiteThreshold = 0;
741 MainCGPipeline.addPass(InlinerPass(IP));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000742
743 // Now deduce any function attributes based in the current code.
744 MainCGPipeline.addPass(PostOrderFunctionAttrsPass());
745
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000746 // When at O3 add argument promotion to the pass pipeline.
747 // FIXME: It isn't at all clear why this should be limited to O3.
748 if (Level == O3)
749 MainCGPipeline.addPass(ArgumentPromotionPass());
750
Chandler Carruthe3f50642016-12-22 06:59:15 +0000751 // Lastly, add the core function simplification pipeline nested inside the
752 // CGSCC walk.
753 MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor(
Dehao Chen95f00302017-07-30 04:55:39 +0000754 buildFunctionSimplificationPipeline(Level, Phase, DebugLogging)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000755
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000756 for (auto &C : CGSCCOptimizerLateEPCallbacks)
757 C(MainCGPipeline, Level);
758
Chandler Carruth719ffe12017-02-12 05:38:04 +0000759 // We wrap the CGSCC pipeline in a devirtualization repeater. This will try
760 // to detect when we devirtualize indirect calls and iterate the SCC passes
761 // in that case to try and catch knock-on inlining or function attrs
762 // opportunities. Then we add it to the module pipeline by walking the SCCs
763 // in postorder (or bottom-up).
Chandler Carruthe3f50642016-12-22 06:59:15 +0000764 MPM.addPass(
Chandler Carruth719ffe12017-02-12 05:38:04 +0000765 createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass(
Chandler Carruth19913b22017-08-11 05:47:13 +0000766 std::move(MainCGPipeline), MaxDevirtIterations)));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000767
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000768 return MPM;
769}
770
771ModulePassManager
772PassBuilder::buildModuleOptimizationPipeline(OptimizationLevel Level,
773 bool DebugLogging) {
774 ModulePassManager MPM(DebugLogging);
775
776 // Optimize globals now that the module is fully simplified.
777 MPM.addPass(GlobalOptPass());
Davide Italianoe0707212017-10-05 18:36:01 +0000778 MPM.addPass(GlobalDCEPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000779
Xinliang David Li126157c2017-05-22 16:41:57 +0000780 // Run partial inlining pass to partially inline functions that have
781 // large bodies.
782 if (RunPartialInlining)
783 MPM.addPass(PartialInlinerPass());
784
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000785 // Remove avail extern fns and globals definitions since we aren't compiling
786 // an object file for later LTO. For LTO we want to preserve these so they
787 // are eligible for inlining at link-time. Note if they are unreferenced they
788 // will be removed by GlobalDCE later, so this only impacts referenced
789 // available externally globals. Eventually they will be suppressed during
790 // codegen, but eliminating here enables more opportunity for GlobalDCE as it
791 // may make globals referenced by available external functions dead and saves
792 // running remaining passes on the eliminated functions.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000793 MPM.addPass(EliminateAvailableExternallyPass());
794
795 // Do RPO function attribute inference across the module to forward-propagate
796 // attributes where applicable.
797 // FIXME: Is this really an optimization rather than a canonicalization?
798 MPM.addPass(ReversePostOrderFunctionAttrsPass());
799
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000800 // Re-require GloblasAA here prior to function passes. This is particularly
Chandler Carruthe3f50642016-12-22 06:59:15 +0000801 // useful as the above will have inlined, DCE'ed, and function-attr
802 // propagated everything. We should at this point have a reasonably minimal
803 // and richly annotated call graph. By computing aliasing and mod/ref
804 // information for all local globals here, the late loop passes and notably
805 // the vectorizer will be able to use them to help recognize vectorizable
806 // memory operations.
Chandler Carruthe87fc8c2017-02-12 05:34:04 +0000807 MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>());
Chandler Carruthe3f50642016-12-22 06:59:15 +0000808
809 FunctionPassManager OptimizePM(DebugLogging);
810 OptimizePM.addPass(Float2IntPass());
811 // FIXME: We need to run some loop optimizations to re-rotate loops after
812 // simplify-cfg and others undo their rotation.
813
814 // Optimize the loop execution. These passes operate on entire loop nests
815 // rather than on each loop in an inside-out manner, and so they are actually
816 // function passes.
Chandler Carrutha95ff382017-01-27 00:50:21 +0000817
Philip Pfaffe730f2f92017-07-10 10:57:55 +0000818 for (auto &C : VectorizerStartEPCallbacks)
819 C(OptimizePM, Level);
820
Chandler Carrutha95ff382017-01-27 00:50:21 +0000821 // First rotate loops that may have been un-rotated by prior passes.
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000822 OptimizePM.addPass(
823 createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging));
Chandler Carrutha95ff382017-01-27 00:50:21 +0000824
825 // Distribute loops to allow partial vectorization. I.e. isolate dependences
826 // into separate loop that would otherwise inhibit vectorization. This is
827 // currently only performed for loops marked with the metadata
828 // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000829 OptimizePM.addPass(LoopDistributePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000830
831 // Now run the core loop vectorizer.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000832 OptimizePM.addPass(LoopVectorizePass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000833
Chandler Carruthbaabda92017-01-27 01:32:26 +0000834 // Eliminate loads by forwarding stores from the previous iteration to loads
835 // of the current iteration.
836 OptimizePM.addPass(LoopLoadEliminationPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000837
838 // Cleanup after the loop optimization passes.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000839 OptimizePM.addPass(InstCombinePass());
840
Chandler Carrutha95ff382017-01-27 00:50:21 +0000841 // Now that we've formed fast to execute loop structures, we do further
842 // optimizations. These are run afterward as they might block doing complex
843 // analyses and transforms such as what are needed for loop vectorization.
844
Sanjay Patel0ab0c1a2017-12-14 22:05:20 +0000845 // Cleanup after loop vectorization, etc. Simplification passes like CVP and
846 // GVN, loop transforms, and others have already run, so it's now better to
847 // convert to more optimized IR using more aggressive simplify CFG options.
848 // The extra sinking transform can create larger basic blocks, so do this
849 // before SLP vectorization.
850 OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions().
851 forwardSwitchCondToPhi(true).
852 convertSwitchToLookupTable(true).
853 needCanonicalLoops(false).
854 sinkCommonInsts(true)));
855
Chandler Carruthe3f50642016-12-22 06:59:15 +0000856 // Optimize parallel scalar instruction chains into SIMD instructions.
857 OptimizePM.addPass(SLPVectorizerPass());
858
Chandler Carruthe3f50642016-12-22 06:59:15 +0000859 OptimizePM.addPass(InstCombinePass());
860
861 // Unroll small loops to hide loop backedge latency and saturate any parallel
Chandler Carrutha95ff382017-01-27 00:50:21 +0000862 // execution resources of an out-of-order processor. We also then need to
863 // clean up redundancies and loop invariant code.
864 // FIXME: It would be really good to use a loop-integrated instruction
865 // combiner for cleanup here so that the unrolling and LICM can be pipelined
866 // across the loop nests.
David Green963401d2018-07-01 12:47:30 +0000867 // We do UnrollAndJam in a separate LPM to ensure it happens before unroll
868 if (EnableUnrollAndJam) {
869 OptimizePM.addPass(
870 createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level)));
871 }
Fedor Sergeev412ed342018-10-31 14:33:14 +0000872 OptimizePM.addPass(LoopUnrollPass(LoopUnrollOptions(Level)));
Michael Kruse72448522018-12-12 17:32:52 +0000873 OptimizePM.addPass(WarnMissedTransformationsPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000874 OptimizePM.addPass(InstCombinePass());
Chandler Carruth0ede22e2017-02-09 23:54:57 +0000875 OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>());
Fedor Sergeev02e7f022017-12-29 08:16:06 +0000876 OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging));
Chandler Carruthe3f50642016-12-22 06:59:15 +0000877
878 // Now that we've vectorized and unrolled loops, we may have more refined
879 // alignment information, try to re-derive it here.
880 OptimizePM.addPass(AlignmentFromAssumptionsPass());
881
Chandler Carrutha95ff382017-01-27 00:50:21 +0000882 // LoopSink pass sinks instructions hoisted by LICM, which serves as a
883 // canonicalization pass that enables other optimizations. As a result,
884 // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
885 // result too early.
886 OptimizePM.addPass(LoopSinkPass());
887
888 // And finally clean up LCSSA form before generating code.
Chandler Carruth7c557f82018-06-29 23:36:03 +0000889 OptimizePM.addPass(InstSimplifyPass());
Chandler Carrutha95ff382017-01-27 00:50:21 +0000890
Sanjay Patel6fd43912017-09-09 13:38:18 +0000891 // This hoists/decomposes div/rem ops. It should run after other sink/hoist
892 // passes to avoid re-sinking, but before SimplifyCFG because it can allow
893 // flattening of blocks.
894 OptimizePM.addPass(DivRemPairsPass());
895
Filipe Cabecinhas92dc3482017-04-26 12:02:41 +0000896 // LoopSink (and other loop passes since the last simplifyCFG) might have
897 // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
898 OptimizePM.addPass(SimplifyCFGPass());
899
Chandler Carruthc34f7892017-11-28 11:32:31 +0000900 // Optimize PHIs by speculating around them when profitable. Note that this
901 // pass needs to be run after any PRE or similar pass as it is essentially
902 // inserting redudnancies into the progrem. This even includes SimplifyCFG.
903 OptimizePM.addPass(SpeculateAroundPHIsPass());
904
Philip Pfaffe2d4effb2018-11-12 11:17:07 +0000905 for (auto &C : OptimizerLastEPCallbacks)
906 C(OptimizePM, Level);
907
Chandler Carrutha95ff382017-01-27 00:50:21 +0000908 // Add the core optimizing pipeline.
Chandler Carruthe3f50642016-12-22 06:59:15 +0000909 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM)));
910
Michael J. Spencer7bb27672018-07-16 00:28:24 +0000911 MPM.addPass(CGProfilePass());
912
Chandler Carruthe3f50642016-12-22 06:59:15 +0000913 // Now we need to do some global optimization transforms.
914 // FIXME: It would seem like these should come first in the optimization
915 // pipeline and maybe be the bottom of the canonicalization pipeline? Weird
916 // ordering here.
917 MPM.addPass(GlobalDCEPass());
918 MPM.addPass(ConstantMergePass());
919
920 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +0000921}
922
Chandler Carruthe3f50642016-12-22 06:59:15 +0000923ModulePassManager
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000924PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level,
925 bool DebugLogging) {
926 assert(Level != O0 && "Must request optimizations for the default pipeline!");
927
928 ModulePassManager MPM(DebugLogging);
929
930 // Force any function attributes we want the rest of the pipeline to observe.
931 MPM.addPass(ForceFunctionAttrsPass());
932
David Blaikie0c64f5a2018-01-23 01:25:20 +0000933 // Apply module pipeline start EP callback.
934 for (auto &C : PipelineStartEPCallbacks)
935 C(MPM);
936
Dehao Chen08f88312017-08-07 20:23:20 +0000937 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000938 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
939
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000940 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +0000941 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None,
942 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000943
944 // Now add the optimization pipeline.
945 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
946
947 return MPM;
948}
949
950ModulePassManager
951PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level,
952 bool DebugLogging) {
953 assert(Level != O0 && "Must request optimizations for the default pipeline!");
954
955 ModulePassManager MPM(DebugLogging);
956
957 // Force any function attributes we want the rest of the pipeline to observe.
958 MPM.addPass(ForceFunctionAttrsPass());
959
Dehao Chen08f88312017-08-07 20:23:20 +0000960 if (PGOOpt && PGOOpt->SamplePGOSupport)
Dehao Chence0842c2017-07-29 04:10:24 +0000961 MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass()));
962
David Blaikie0c64f5a2018-01-23 01:25:20 +0000963 // Apply module pipeline start EP callback.
964 for (auto &C : PipelineStartEPCallbacks)
965 C(MPM);
966
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000967 // If we are planning to perform ThinLTO later, we don't bloat the code with
968 // unrolling/vectorization/... now. Just simplify the module as much as we
969 // can.
Dehao Chen95f00302017-07-30 04:55:39 +0000970 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink,
971 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000972
973 // Run partial inlining pass to partially inline functions that have
974 // large bodies.
975 // FIXME: It isn't clear whether this is really the right place to run this
976 // in ThinLTO. Because there is another canonicalization and simplification
977 // phase that will run after the thin link, running this here ends up with
978 // less information than will be available later and it may grow functions in
979 // ways that aren't beneficial.
980 if (RunPartialInlining)
981 MPM.addPass(PartialInlinerPass());
982
983 // Reduce the size of the IR as much as possible.
984 MPM.addPass(GlobalOptPass());
985
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000986 return MPM;
987}
988
Teresa Johnson28023db2018-07-19 14:51:32 +0000989ModulePassManager PassBuilder::buildThinLTODefaultPipeline(
990 OptimizationLevel Level, bool DebugLogging,
991 const ModuleSummaryIndex *ImportSummary) {
Chandler Carruth8b3be4e2017-06-01 11:39:39 +0000992 ModulePassManager MPM(DebugLogging);
993
Teresa Johnson28023db2018-07-19 14:51:32 +0000994 if (ImportSummary) {
995 // These passes import type identifier resolutions for whole-program
996 // devirtualization and CFI. They must run early because other passes may
997 // disturb the specific instruction patterns that these passes look for,
998 // creating dependencies on resolutions that may not appear in the summary.
999 //
1000 // For example, GVN may transform the pattern assume(type.test) appearing in
1001 // two basic blocks into assume(phi(type.test, type.test)), which would
1002 // transform a dependency on a WPD resolution into a dependency on a type
1003 // identifier resolution for CFI.
1004 //
1005 // Also, WPD has access to more precise information than ICP and can
1006 // devirtualize more effectively, so it should operate on the IR first.
1007 MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary));
1008 MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary));
1009 }
1010
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001011 // Force any function attributes we want the rest of the pipeline to observe.
1012 MPM.addPass(ForceFunctionAttrsPass());
1013
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001014 // Add the core simplification pipeline.
Dehao Chen95f00302017-07-30 04:55:39 +00001015 MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink,
1016 DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001017
1018 // Now add the optimization pipeline.
1019 MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging));
1020
1021 return MPM;
1022}
1023
1024ModulePassManager
Chandler Carruthe3f50642016-12-22 06:59:15 +00001025PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level,
1026 bool DebugLogging) {
1027 assert(Level != O0 && "Must request optimizations for the default pipeline!");
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001028 // FIXME: We should use a customized pre-link pipeline!
Chandler Carruthe3f50642016-12-22 06:59:15 +00001029 return buildPerModuleDefaultPipeline(Level, DebugLogging);
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001030}
1031
Teresa Johnson28023db2018-07-19 14:51:32 +00001032ModulePassManager
1033PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging,
1034 ModuleSummaryIndex *ExportSummary) {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001035 assert(Level != O0 && "Must request optimizations for the default pipeline!");
1036 ModulePassManager MPM(DebugLogging);
1037
Xin Tong642c8d32018-11-15 18:06:42 +00001038 if (PGOOpt && !PGOOpt->SampleProfileFile.empty()) {
1039 // Load sample profile before running the LTO optimization pipeline.
1040 MPM.addPass(SampleProfileLoaderPass(PGOOpt->SampleProfileFile,
1041 PGOOpt->ProfileRemappingFile,
1042 false /* ThinLTOPhase::PreLink */));
1043 }
1044
Davide Italiano089a9122017-01-24 00:57:39 +00001045 // Remove unused virtual tables to improve the quality of code generated by
1046 // whole-program devirtualization and bitset lowering.
1047 MPM.addPass(GlobalDCEPass());
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001048
Davide Italiano089a9122017-01-24 00:57:39 +00001049 // Force any function attributes we want the rest of the pipeline to observe.
1050 MPM.addPass(ForceFunctionAttrsPass());
Chandler Carruthe3f50642016-12-22 06:59:15 +00001051
Davide Italiano089a9122017-01-24 00:57:39 +00001052 // Do basic inference of function attributes from known properties of system
1053 // libraries and other oracles.
1054 MPM.addPass(InferFunctionAttrsPass());
1055
1056 if (Level > 1) {
Jun Bum Lim0c990072017-11-03 20:41:16 +00001057 FunctionPassManager EarlyFPM(DebugLogging);
1058 EarlyFPM.addPass(CallSiteSplittingPass());
1059 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM)));
1060
Davide Italiano089a9122017-01-24 00:57:39 +00001061 // Indirect call promotion. This should promote all the targets that are
1062 // left by the earlier promotion pass that promotes intra-module targets.
1063 // This two-step promotion is to save the compile time. For LTO, it should
1064 // produce the same result as if we only do promotion here.
Dehao Chen2f31d0d2017-06-29 23:33:05 +00001065 MPM.addPass(PGOIndirectCallPromotion(
1066 true /* InLTO */, PGOOpt && !PGOOpt->SampleProfileFile.empty()));
Davide Italiano089a9122017-01-24 00:57:39 +00001067 // Propagate constants at call sites into the functions they call. This
1068 // opens opportunities for globalopt (and inlining) by substituting function
1069 // pointers passed as arguments to direct uses of functions.
1070 MPM.addPass(IPSCCPPass());
Matthew Simpsoncb585582017-10-25 13:40:08 +00001071
1072 // Attach metadata to indirect call sites indicating the set of functions
1073 // they may target at run-time. This should follow IPSCCP.
1074 MPM.addPass(CalledValuePropagationPass());
Davide Italiano089a9122017-01-24 00:57:39 +00001075 }
1076
1077 // Now deduce any function attributes based in the current code.
1078 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1079 PostOrderFunctionAttrsPass()));
1080
1081 // Do RPO function attribute inference across the module to forward-propagate
1082 // attributes where applicable.
1083 // FIXME: Is this really an optimization rather than a canonicalization?
1084 MPM.addPass(ReversePostOrderFunctionAttrsPass());
1085
1086 // Use inragne annotations on GEP indices to split globals where beneficial.
1087 MPM.addPass(GlobalSplitPass());
1088
1089 // Run whole program optimization of virtual call when the list of callees
1090 // is fixed.
Teresa Johnson28023db2018-07-19 14:51:32 +00001091 MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001092
1093 // Stop here at -O1.
Teresa Johnson28023db2018-07-19 14:51:32 +00001094 if (Level == 1) {
1095 // The LowerTypeTestsPass needs to run to lower type metadata and the
1096 // type.test intrinsics. The pass does nothing if CFI is disabled.
1097 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001098 return MPM;
Teresa Johnson28023db2018-07-19 14:51:32 +00001099 }
Davide Italiano089a9122017-01-24 00:57:39 +00001100
1101 // Optimize globals to try and fold them into constants.
1102 MPM.addPass(GlobalOptPass());
1103
1104 // Promote any localized globals to SSA registers.
1105 MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass()));
1106
1107 // Linking modules together can lead to duplicate global constant, only
1108 // keep one copy of each constant.
1109 MPM.addPass(ConstantMergePass());
1110
1111 // Remove unused arguments from functions.
1112 MPM.addPass(DeadArgumentEliminationPass());
1113
1114 // Reduce the code after globalopt and ipsccp. Both can open up significant
1115 // simplification opportunities, and both can propagate functions through
1116 // function pointers. When this happens, we often have to resolve varargs
1117 // calls, etc, so let instcombine do this.
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001118 FunctionPassManager PeepholeFPM(DebugLogging);
Amjad Aboudf1f57a32018-01-25 12:06:32 +00001119 if (Level == O3)
1120 PeepholeFPM.addPass(AggressiveInstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001121 PeepholeFPM.addPass(InstCombinePass());
1122 invokePeepholeEPCallbacks(PeepholeFPM, Level);
1123
1124 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM)));
Davide Italiano089a9122017-01-24 00:57:39 +00001125
1126 // Note: historically, the PruneEH pass was run first to deduce nounwind and
1127 // generally clean up exception handling overhead. It isn't clear this is
1128 // valuable as the inliner doesn't currently care whether it is inlining an
1129 // invoke or a call.
1130 // Run the inliner now.
Easwaran Raman8249fac2017-06-28 13:33:49 +00001131 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1132 InlinerPass(getInlineParamsFromOptLevel(Level))));
Davide Italiano089a9122017-01-24 00:57:39 +00001133
1134 // Optimize globals again after we ran the inliner.
1135 MPM.addPass(GlobalOptPass());
1136
1137 // Garbage collect dead functions.
1138 // FIXME: Add ArgumentPromotion pass after once it's ported.
1139 MPM.addPass(GlobalDCEPass());
1140
1141 FunctionPassManager FPM(DebugLogging);
Davide Italiano089a9122017-01-24 00:57:39 +00001142 // The IPO Passes may leave cruft around. Clean up after them.
Davide Italiano089a9122017-01-24 00:57:39 +00001143 FPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001144 invokePeepholeEPCallbacks(FPM, Level);
1145
Davide Italiano089a9122017-01-24 00:57:39 +00001146 FPM.addPass(JumpThreadingPass());
1147
1148 // Break up allocas
1149 FPM.addPass(SROA());
1150
1151 // Run a few AA driver optimizations here and now to cleanup the code.
1152 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1153
1154 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(
1155 PostOrderFunctionAttrsPass()));
1156 // FIXME: here we run IP alias analysis in the legacy PM.
1157
1158 FunctionPassManager MainFPM;
1159
1160 // FIXME: once we fix LoopPass Manager, add LICM here.
1161 // FIXME: once we provide support for enabling MLSM, add it here.
1162 // FIXME: once we provide support for enabling NewGVN, add it here.
Davide Italiano8a09b8e2017-05-22 23:41:40 +00001163 if (RunNewGVN)
1164 MainFPM.addPass(NewGVNPass());
1165 else
1166 MainFPM.addPass(GVN());
Davide Italiano089a9122017-01-24 00:57:39 +00001167
1168 // Remove dead memcpy()'s.
1169 MainFPM.addPass(MemCpyOptPass());
1170
1171 // Nuke dead stores.
1172 MainFPM.addPass(DSEPass());
1173
1174 // FIXME: at this point, we run a bunch of loop passes:
1175 // indVarSimplify, loopDeletion, loopInterchange, loopUnrool,
1176 // loopVectorize. Enable them once the remaining issue with LPM
1177 // are sorted out.
1178
1179 MainFPM.addPass(InstCombinePass());
1180 MainFPM.addPass(SimplifyCFGPass());
1181 MainFPM.addPass(SCCPPass());
1182 MainFPM.addPass(InstCombinePass());
1183 MainFPM.addPass(BDCEPass());
1184
1185 // FIXME: We may want to run SLPVectorizer here.
1186 // After vectorization, assume intrinsics may tell us more
1187 // about pointer alignments.
1188#if 0
1189 MainFPM.add(AlignmentFromAssumptionsPass());
1190#endif
1191
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001192 // FIXME: Conditionally run LoadCombine here, after it's ported
1193 // (in case we still have this pass, given its questionable usefulness).
1194
Davide Italiano089a9122017-01-24 00:57:39 +00001195 MainFPM.addPass(InstCombinePass());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001196 invokePeepholeEPCallbacks(MainFPM, Level);
Davide Italiano089a9122017-01-24 00:57:39 +00001197 MainFPM.addPass(JumpThreadingPass());
1198 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM)));
1199
1200 // Create a function that performs CFI checks for cross-DSO calls with
1201 // targets in the current module.
1202 MPM.addPass(CrossDSOCFIPass());
1203
1204 // Lower type metadata and the type.test intrinsic. This pass supports
1205 // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs
1206 // to be run at link time if CFI is enabled. This pass does nothing if
1207 // CFI is disabled.
Teresa Johnson28023db2018-07-19 14:51:32 +00001208 MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr));
Davide Italiano089a9122017-01-24 00:57:39 +00001209
1210 // Add late LTO optimization passes.
1211 // Delete basic blocks, which optimization passes may have killed.
1212 MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass()));
1213
1214 // Drop bodies of available eternally objects to improve GlobalDCE.
1215 MPM.addPass(EliminateAvailableExternallyPass());
1216
1217 // Now that we have optimized the program, discard unreachable functions.
1218 MPM.addPass(GlobalDCEPass());
1219
1220 // FIXME: Enable MergeFuncs, conditionally, after ported, maybe.
Chandler Carruthe3f50642016-12-22 06:59:15 +00001221 return MPM;
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001222}
1223
Chandler Carruth060ad612016-12-23 20:38:19 +00001224AAManager PassBuilder::buildDefaultAAPipeline() {
1225 AAManager AA;
1226
1227 // The order in which these are registered determines their priority when
1228 // being queried.
1229
1230 // First we register the basic alias analysis that provides the majority of
1231 // per-function local AA logic. This is a stateless, on-demand local set of
1232 // AA techniques.
1233 AA.registerFunctionAnalysis<BasicAA>();
1234
1235 // Next we query fast, specialized alias analyses that wrap IR-embedded
1236 // information about aliasing.
1237 AA.registerFunctionAnalysis<ScopedNoAliasAA>();
1238 AA.registerFunctionAnalysis<TypeBasedAA>();
1239
1240 // Add support for querying global aliasing information when available.
Chandler Carruth534d6442016-12-24 05:11:17 +00001241 // Because the `AAManager` is a function analysis and `GlobalsAA` is a module
1242 // analysis, all that the `AAManager` can do is query for any *cached*
Chandler Carruthe87fc8c2017-02-12 05:34:04 +00001243 // results from `GlobalsAA` through a readonly proxy.
Chandler Carruth060ad612016-12-23 20:38:19 +00001244 AA.registerModuleAnalysis<GlobalsAA>();
Chandler Carruth060ad612016-12-23 20:38:19 +00001245
1246 return AA;
1247}
1248
Chandler Carruth241bf242016-08-03 07:44:48 +00001249static Optional<int> parseRepeatPassName(StringRef Name) {
1250 if (!Name.consume_front("repeat<") || !Name.consume_back(">"))
1251 return None;
1252 int Count;
1253 if (Name.getAsInteger(0, Count) || Count <= 0)
1254 return None;
1255 return Count;
1256}
1257
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001258static Optional<int> parseDevirtPassName(StringRef Name) {
1259 if (!Name.consume_front("devirt<") || !Name.consume_back(">"))
1260 return None;
1261 int Count;
1262 if (Name.getAsInteger(0, Count) || Count <= 0)
1263 return None;
1264 return Count;
1265}
1266
Fedor Sergeevb7871402019-01-10 10:01:53 +00001267static bool checkParametrizedPassName(StringRef Name, StringRef PassName) {
1268 if (!Name.consume_front(PassName))
1269 return false;
1270 // normal pass name w/o parameters == default parameters
1271 if (Name.empty())
1272 return true;
1273 return Name.startswith("<") && Name.endswith(">");
1274}
1275
1276namespace {
1277
1278/// This performs customized parsing of pass name with parameters.
1279///
1280/// We do not need parametrization of passes in textual pipeline very often,
1281/// yet on a rare occasion ability to specify parameters right there can be
1282/// useful.
1283///
1284/// \p Name - parameterized specification of a pass from a textual pipeline
1285/// is a string in a form of :
1286/// PassName '<' parameter-list '>'
1287///
1288/// Parameter list is being parsed by the parser callable argument, \p Parser,
1289/// It takes a string-ref of parameters and returns either StringError or a
1290/// parameter list in a form of a custom parameters type, all wrapped into
1291/// Expected<> template class.
1292///
1293template <typename ParametersParseCallableT>
1294auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name,
1295 StringRef PassName) -> decltype(Parser(StringRef{})) {
1296 using ParametersT = typename decltype(Parser(StringRef{}))::value_type;
1297
1298 StringRef Params = Name;
1299 if (!Params.consume_front(PassName)) {
1300 assert(false &&
1301 "unable to strip pass name from parametrized pass specification");
1302 }
1303 if (Params.empty())
1304 return ParametersT{};
1305 if (!Params.consume_front("<") || !Params.consume_back(">")) {
1306 assert(false && "invalid format for parametrized pass name");
1307 }
1308
1309 Expected<ParametersT> Result = Parser(Params);
1310 assert((Result || Result.template errorIsA<StringError>()) &&
1311 "Pass parameter parser can only return StringErrors.");
1312 return std::move(Result);
1313}
1314
1315/// Parser of parameters for LoopUnroll pass.
1316Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) {
1317 LoopUnrollOptions UnrollOpts;
1318 while (!Params.empty()) {
1319 StringRef ParamName;
1320 std::tie(ParamName, Params) = Params.split(';');
1321 int OptLevel = StringSwitch<int>(ParamName)
1322 .Case("O0", 0)
1323 .Case("O1", 1)
1324 .Case("O2", 2)
1325 .Case("O3", 3)
1326 .Default(-1);
1327 if (OptLevel >= 0) {
1328 UnrollOpts.setOptLevel(OptLevel);
1329 continue;
1330 }
1331
1332 bool Enable = !ParamName.consume_front("no-");
1333 if (ParamName == "partial") {
1334 UnrollOpts.setPartial(Enable);
1335 } else if (ParamName == "peeling") {
1336 UnrollOpts.setPeeling(Enable);
1337 } else if (ParamName == "runtime") {
1338 UnrollOpts.setRuntime(Enable);
1339 } else if (ParamName == "upperbound") {
1340 UnrollOpts.setUpperBound(Enable);
1341 } else {
1342 return make_error<StringError>(
1343 formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(),
1344 inconvertibleErrorCode());
1345 }
1346 }
1347 return UnrollOpts;
1348}
1349
Philip Pfaffe0ee6a932019-02-04 21:02:49 +00001350Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) {
1351 MemorySanitizerOptions Result;
1352 while (!Params.empty()) {
1353 StringRef ParamName;
1354 std::tie(ParamName, Params) = Params.split(';');
1355
1356 if (ParamName == "recover") {
1357 Result.Recover = true;
1358 } else if (ParamName == "kernel") {
1359 Result.Kernel = true;
1360 } else if (ParamName.consume_front("track-origins=")) {
1361 if (ParamName.getAsInteger(0, Result.TrackOrigins))
1362 return make_error<StringError>(
1363 formatv("invalid argument to MemorySanitizer pass track-origins "
1364 "parameter: '{0}' ",
1365 ParamName)
1366 .str(),
1367 inconvertibleErrorCode());
1368 } else {
1369 return make_error<StringError>(
1370 formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName)
1371 .str(),
1372 inconvertibleErrorCode());
1373 }
1374 }
1375 return Result;
1376}
1377
Fedor Sergeevb7871402019-01-10 10:01:53 +00001378} // namespace
1379
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001380/// Tests whether a pass name starts with a valid prefix for a default pipeline
1381/// alias.
1382static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) {
1383 return Name.startswith("default") || Name.startswith("thinlto") ||
1384 Name.startswith("lto");
1385}
1386
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001387/// Tests whether registered callbacks will accept a given pass name.
1388///
1389/// When parsing a pipeline text, the type of the outermost pipeline may be
1390/// omitted, in which case the type is automatically determined from the first
1391/// pass name in the text. This may be a name that is handled through one of the
1392/// callbacks. We check this through the oridinary parsing callbacks by setting
1393/// up a dummy PassManager in order to not force the client to also handle this
1394/// type of query.
1395template <typename PassManagerT, typename CallbacksT>
1396static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) {
1397 if (!Callbacks.empty()) {
1398 PassManagerT DummyPM;
1399 for (auto &CB : Callbacks)
1400 if (CB(Name, DummyPM, {}))
1401 return true;
1402 }
1403 return false;
1404}
1405
1406template <typename CallbacksT>
1407static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001408 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001409 if (startsWithDefaultPipelineAliasPrefix(Name))
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001410 return DefaultAliasRegex.match(Name);
1411
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001412 // Explicitly handle pass manager names.
1413 if (Name == "module")
1414 return true;
1415 if (Name == "cgscc")
1416 return true;
1417 if (Name == "function")
1418 return true;
1419
Chandler Carruth241bf242016-08-03 07:44:48 +00001420 // Explicitly handle custom-parsed pass names.
1421 if (parseRepeatPassName(Name))
1422 return true;
1423
Chandler Carruth74a8a222016-06-17 07:15:29 +00001424#define MODULE_PASS(NAME, CREATE_PASS) \
1425 if (Name == NAME) \
1426 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001427#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001428 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001429 return true;
1430#include "PassRegistry.def"
1431
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001432 return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks);
Chandler Carruth66445382014-01-11 08:16:35 +00001433}
1434
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001435template <typename CallbacksT>
1436static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001437 // Explicitly handle pass manager names.
1438 if (Name == "cgscc")
1439 return true;
1440 if (Name == "function")
1441 return true;
1442
Chandler Carruth241bf242016-08-03 07:44:48 +00001443 // Explicitly handle custom-parsed pass names.
1444 if (parseRepeatPassName(Name))
1445 return true;
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001446 if (parseDevirtPassName(Name))
1447 return true;
Chandler Carruth241bf242016-08-03 07:44:48 +00001448
Chandler Carruth74a8a222016-06-17 07:15:29 +00001449#define CGSCC_PASS(NAME, CREATE_PASS) \
1450 if (Name == NAME) \
1451 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001452#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001453 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001454 return true;
1455#include "PassRegistry.def"
1456
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001457 return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks);
Chandler Carruth572e3402014-04-21 11:12:00 +00001458}
1459
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001460template <typename CallbacksT>
1461static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001462 // Explicitly handle pass manager names.
1463 if (Name == "function")
1464 return true;
1465 if (Name == "loop")
1466 return true;
1467
Chandler Carruth241bf242016-08-03 07:44:48 +00001468 // Explicitly handle custom-parsed pass names.
1469 if (parseRepeatPassName(Name))
1470 return true;
1471
Chandler Carruth74a8a222016-06-17 07:15:29 +00001472#define FUNCTION_PASS(NAME, CREATE_PASS) \
1473 if (Name == NAME) \
1474 return true;
Fedor Sergeevb7871402019-01-10 10:01:53 +00001475#define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \
1476 if (checkParametrizedPassName(Name, NAME)) \
1477 return true;
Chandler Carruth628503e2015-01-06 02:10:51 +00001478#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001479 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
Chandler Carruth628503e2015-01-06 02:10:51 +00001480 return true;
1481#include "PassRegistry.def"
1482
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001483 return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks);
Chandler Carruthd8330982014-01-12 09:34:22 +00001484}
1485
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001486template <typename CallbacksT>
1487static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001488 // Explicitly handle pass manager names.
1489 if (Name == "loop")
1490 return true;
1491
Chandler Carruth241bf242016-08-03 07:44:48 +00001492 // Explicitly handle custom-parsed pass names.
1493 if (parseRepeatPassName(Name))
1494 return true;
1495
Chandler Carruth74a8a222016-06-17 07:15:29 +00001496#define LOOP_PASS(NAME, CREATE_PASS) \
1497 if (Name == NAME) \
1498 return true;
Justin Bognereecc3c82016-02-25 07:23:08 +00001499#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1500 if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \
1501 return true;
1502#include "PassRegistry.def"
1503
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001504 return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks);
Justin Bognereecc3c82016-02-25 07:23:08 +00001505}
1506
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001507Optional<std::vector<PassBuilder::PipelineElement>>
1508PassBuilder::parsePipelineText(StringRef Text) {
1509 std::vector<PipelineElement> ResultPipeline;
1510
1511 SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = {
1512 &ResultPipeline};
1513 for (;;) {
1514 std::vector<PipelineElement> &Pipeline = *PipelineStack.back();
1515 size_t Pos = Text.find_first_of(",()");
1516 Pipeline.push_back({Text.substr(0, Pos), {}});
1517
1518 // If we have a single terminating name, we're done.
1519 if (Pos == Text.npos)
1520 break;
1521
1522 char Sep = Text[Pos];
1523 Text = Text.substr(Pos + 1);
1524 if (Sep == ',')
1525 // Just a name ending in a comma, continue.
1526 continue;
1527
1528 if (Sep == '(') {
1529 // Push the inner pipeline onto the stack to continue processing.
1530 PipelineStack.push_back(&Pipeline.back().InnerPipeline);
1531 continue;
1532 }
1533
1534 assert(Sep == ')' && "Bogus separator!");
1535 // When handling the close parenthesis, we greedily consume them to avoid
1536 // empty strings in the pipeline.
1537 do {
1538 // If we try to pop the outer pipeline we have unbalanced parentheses.
1539 if (PipelineStack.size() == 1)
1540 return None;
1541
1542 PipelineStack.pop_back();
1543 } while (Text.consume_front(")"));
1544
1545 // Check if we've finished parsing.
1546 if (Text.empty())
1547 break;
1548
1549 // Otherwise, the end of an inner pipeline always has to be followed by
1550 // a comma, and then we can continue.
1551 if (!Text.consume_front(","))
1552 return None;
1553 }
1554
1555 if (PipelineStack.size() > 1)
1556 // Unbalanced paretheses.
1557 return None;
1558
1559 assert(PipelineStack.back() == &ResultPipeline &&
1560 "Wrong pipeline at the bottom of the stack!");
1561 return {std::move(ResultPipeline)};
1562}
1563
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001564Error PassBuilder::parseModulePass(ModulePassManager &MPM,
1565 const PipelineElement &E,
1566 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001567 auto &Name = E.Name;
1568 auto &InnerPipeline = E.InnerPipeline;
1569
1570 // First handle complex passes like the pass managers which carry pipelines.
1571 if (!InnerPipeline.empty()) {
1572 if (Name == "module") {
1573 ModulePassManager NestedMPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001574 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline,
1575 VerifyEachPass, DebugLogging))
1576 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001577 MPM.addPass(std::move(NestedMPM));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001578 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001579 }
1580 if (Name == "cgscc") {
1581 CGSCCPassManager CGPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001582 if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass,
1583 DebugLogging))
1584 return Err;
Chandler Carruth19913b22017-08-11 05:47:13 +00001585 MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001586 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001587 }
1588 if (Name == "function") {
1589 FunctionPassManager FPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001590 if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline,
1591 VerifyEachPass, DebugLogging))
1592 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001593 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001594 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001595 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001596 if (auto Count = parseRepeatPassName(Name)) {
1597 ModulePassManager NestedMPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001598 if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline,
1599 VerifyEachPass, DebugLogging))
1600 return Err;
Chandler Carrutha053a882016-08-04 03:52:53 +00001601 MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001602 return Error::success();
Chandler Carruth241bf242016-08-03 07:44:48 +00001603 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001604
1605 for (auto &C : ModulePipelineParsingCallbacks)
1606 if (C(Name, MPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001607 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001608
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001609 // Normal passes can't have pipelines.
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001610 return make_error<StringError>(
1611 formatv("invalid use of '{0}' pass as module pipeline", Name).str(),
1612 inconvertibleErrorCode());
1613 ;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001614 }
1615
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001616 // Manually handle aliases for pre-configured pipeline fragments.
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001617 if (startsWithDefaultPipelineAliasPrefix(Name)) {
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001618 SmallVector<StringRef, 3> Matches;
1619 if (!DefaultAliasRegex.match(Name, &Matches))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001620 return make_error<StringError>(
1621 formatv("unknown default pipeline alias '{0}'", Name).str(),
1622 inconvertibleErrorCode());
1623
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001624 assert(Matches.size() == 3 && "Must capture two matched strings!");
1625
Jordan Rosef85a95f2016-07-25 18:34:51 +00001626 OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2])
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001627 .Case("O0", O0)
1628 .Case("O1", O1)
1629 .Case("O2", O2)
1630 .Case("O3", O3)
1631 .Case("Os", Os)
1632 .Case("Oz", Oz);
Chandler Carruthe3f50642016-12-22 06:59:15 +00001633 if (L == O0)
1634 // At O0 we do nothing at all!
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001635 return Error::success();
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001636
1637 if (Matches[1] == "default") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001638 MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging));
Chandler Carruth8b3be4e2017-06-01 11:39:39 +00001639 } else if (Matches[1] == "thinlto-pre-link") {
1640 MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging));
1641 } else if (Matches[1] == "thinlto") {
Teresa Johnson28023db2018-07-19 14:51:32 +00001642 MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001643 } else if (Matches[1] == "lto-pre-link") {
Chandler Carruthe3f50642016-12-22 06:59:15 +00001644 MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001645 } else {
1646 assert(Matches[1] == "lto" && "Not one of the matched options!");
Teresa Johnson28023db2018-07-19 14:51:32 +00001647 MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr));
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001648 }
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001649 return Error::success();
Chandler Carruth8b5a74192016-02-28 22:16:03 +00001650 }
1651
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001652 // Finally expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001653#define MODULE_PASS(NAME, CREATE_PASS) \
1654 if (Name == NAME) { \
1655 MPM.addPass(CREATE_PASS); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001656 return Error::success(); \
Chandler Carruth52eef882014-01-12 12:15:39 +00001657 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001658#define MODULE_ANALYSIS(NAME, CREATE_PASS) \
1659 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001660 MPM.addPass( \
1661 RequireAnalysisPass< \
1662 std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001663 return Error::success(); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001664 } \
1665 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001666 MPM.addPass(InvalidateAnalysisPass< \
1667 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001668 return Error::success(); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001669 }
1670#include "PassRegistry.def"
1671
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001672 for (auto &C : ModulePipelineParsingCallbacks)
1673 if (C(Name, MPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001674 return Error::success();
1675 return make_error<StringError>(
1676 formatv("unknown module pass '{0}'", Name).str(),
1677 inconvertibleErrorCode());
Chandler Carruth66445382014-01-11 08:16:35 +00001678}
1679
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001680Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM,
1681 const PipelineElement &E, bool VerifyEachPass,
1682 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001683 auto &Name = E.Name;
1684 auto &InnerPipeline = E.InnerPipeline;
1685
1686 // First handle complex passes like the pass managers which carry pipelines.
1687 if (!InnerPipeline.empty()) {
1688 if (Name == "cgscc") {
1689 CGSCCPassManager NestedCGPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001690 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1691 VerifyEachPass, DebugLogging))
1692 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001693 // Add the nested pass manager with the appropriate adaptor.
1694 CGPM.addPass(std::move(NestedCGPM));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001695 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001696 }
1697 if (Name == "function") {
1698 FunctionPassManager FPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001699 if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline,
1700 VerifyEachPass, DebugLogging))
1701 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001702 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruth19913b22017-08-11 05:47:13 +00001703 CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001704 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001705 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001706 if (auto Count = parseRepeatPassName(Name)) {
1707 CGSCCPassManager NestedCGPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001708 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1709 VerifyEachPass, DebugLogging))
1710 return Err;
Chandler Carrutha053a882016-08-04 03:52:53 +00001711 CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001712 return Error::success();
Chandler Carruth241bf242016-08-03 07:44:48 +00001713 }
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001714 if (auto MaxRepetitions = parseDevirtPassName(Name)) {
1715 CGSCCPassManager NestedCGPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001716 if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline,
1717 VerifyEachPass, DebugLogging))
1718 return Err;
Chandler Carruth19913b22017-08-11 05:47:13 +00001719 CGPM.addPass(
1720 createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001721 return Error::success();
Chandler Carruth05ca5ac2016-12-28 11:07:33 +00001722 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001723
1724 for (auto &C : CGSCCPipelineParsingCallbacks)
1725 if (C(Name, CGPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001726 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001727
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001728 // Normal passes can't have pipelines.
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001729 return make_error<StringError>(
1730 formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(),
1731 inconvertibleErrorCode());
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001732 }
1733
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001734// Now expand the basic registered passes from the .inc file.
Chandler Carruth572e3402014-04-21 11:12:00 +00001735#define CGSCC_PASS(NAME, CREATE_PASS) \
1736 if (Name == NAME) { \
1737 CGPM.addPass(CREATE_PASS); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001738 return Error::success(); \
Chandler Carruth572e3402014-04-21 11:12:00 +00001739 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001740#define CGSCC_ANALYSIS(NAME, CREATE_PASS) \
1741 if (Name == "require<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001742 CGPM.addPass(RequireAnalysisPass< \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001743 std::remove_reference<decltype(CREATE_PASS)>::type, \
Chandler Carruth88823462016-08-24 09:37:14 +00001744 LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \
1745 CGSCCUpdateResult &>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001746 return Error::success(); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001747 } \
1748 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001749 CGPM.addPass(InvalidateAnalysisPass< \
1750 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001751 return Error::success(); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001752 }
1753#include "PassRegistry.def"
1754
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001755 for (auto &C : CGSCCPipelineParsingCallbacks)
1756 if (C(Name, CGPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001757 return Error::success();
1758 return make_error<StringError>(
1759 formatv("unknown cgscc pass '{0}'", Name).str(),
1760 inconvertibleErrorCode());
Chandler Carruth572e3402014-04-21 11:12:00 +00001761}
1762
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001763Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM,
1764 const PipelineElement &E,
1765 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001766 auto &Name = E.Name;
1767 auto &InnerPipeline = E.InnerPipeline;
1768
1769 // First handle complex passes like the pass managers which carry pipelines.
1770 if (!InnerPipeline.empty()) {
1771 if (Name == "function") {
1772 FunctionPassManager NestedFPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001773 if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline,
1774 VerifyEachPass, DebugLogging))
1775 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001776 // Add the nested pass manager with the appropriate adaptor.
1777 FPM.addPass(std::move(NestedFPM));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001778 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001779 }
1780 if (Name == "loop") {
1781 LoopPassManager LPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001782 if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass,
1783 DebugLogging))
1784 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001785 // Add the nested pass manager with the appropriate adaptor.
Fedor Sergeev02e7f022017-12-29 08:16:06 +00001786 FPM.addPass(
1787 createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001788 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001789 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001790 if (auto Count = parseRepeatPassName(Name)) {
1791 FunctionPassManager NestedFPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001792 if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline,
1793 VerifyEachPass, DebugLogging))
1794 return Err;
Chandler Carrutha053a882016-08-04 03:52:53 +00001795 FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001796 return Error::success();
Chandler Carruth241bf242016-08-03 07:44:48 +00001797 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001798
1799 for (auto &C : FunctionPipelineParsingCallbacks)
1800 if (C(Name, FPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001801 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001802
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001803 // Normal passes can't have pipelines.
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001804 return make_error<StringError>(
1805 formatv("invalid use of '{0}' pass as function pipeline", Name).str(),
1806 inconvertibleErrorCode());
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001807 }
1808
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001809// Now expand the basic registered passes from the .inc file.
Chandler Carruth58944182014-04-21 08:08:50 +00001810#define FUNCTION_PASS(NAME, CREATE_PASS) \
1811 if (Name == NAME) { \
1812 FPM.addPass(CREATE_PASS); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001813 return Error::success(); \
Chandler Carruth52eef882014-01-12 12:15:39 +00001814 }
Fedor Sergeevb7871402019-01-10 10:01:53 +00001815#define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \
1816 if (checkParametrizedPassName(Name, NAME)) { \
1817 auto Params = parsePassParameters(PARSER, Name, NAME); \
1818 if (!Params) \
1819 return Params.takeError(); \
1820 FPM.addPass(CREATE_PASS(Params.get())); \
1821 return Error::success(); \
1822 }
Chandler Carruth628503e2015-01-06 02:10:51 +00001823#define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \
1824 if (Name == "require<" NAME ">") { \
Chandler Carruth9b35e6d2016-08-19 18:36:06 +00001825 FPM.addPass( \
1826 RequireAnalysisPass< \
1827 std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001828 return Error::success(); \
Chandler Carruth3472ffb2015-01-06 04:49:44 +00001829 } \
1830 if (Name == "invalidate<" NAME ">") { \
Chandler Carruth470734b2016-02-26 12:30:18 +00001831 FPM.addPass(InvalidateAnalysisPass< \
1832 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001833 return Error::success(); \
Chandler Carruth628503e2015-01-06 02:10:51 +00001834 }
1835#include "PassRegistry.def"
1836
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001837 for (auto &C : FunctionPipelineParsingCallbacks)
1838 if (C(Name, FPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001839 return Error::success();
1840 return make_error<StringError>(
1841 formatv("unknown function pass '{0}'", Name).str(),
1842 inconvertibleErrorCode());
Chandler Carruthd8330982014-01-12 09:34:22 +00001843}
1844
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001845Error PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E,
1846 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth241bf242016-08-03 07:44:48 +00001847 StringRef Name = E.Name;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001848 auto &InnerPipeline = E.InnerPipeline;
1849
1850 // First handle complex passes like the pass managers which carry pipelines.
1851 if (!InnerPipeline.empty()) {
1852 if (Name == "loop") {
1853 LoopPassManager NestedLPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001854 if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline,
1855 VerifyEachPass, DebugLogging))
1856 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001857 // Add the nested pass manager with the appropriate adaptor.
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001858 LPM.addPass(std::move(NestedLPM));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001859 return Error::success();
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001860 }
Chandler Carruth241bf242016-08-03 07:44:48 +00001861 if (auto Count = parseRepeatPassName(Name)) {
1862 LoopPassManager NestedLPM(DebugLogging);
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001863 if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline,
1864 VerifyEachPass, DebugLogging))
1865 return Err;
Chandler Carrutha053a882016-08-04 03:52:53 +00001866 LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM)));
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001867 return Error::success();
Chandler Carruth241bf242016-08-03 07:44:48 +00001868 }
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001869
1870 for (auto &C : LoopPipelineParsingCallbacks)
1871 if (C(Name, LPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001872 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001873
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001874 // Normal passes can't have pipelines.
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001875 return make_error<StringError>(
1876 formatv("invalid use of '{0}' pass as loop pipeline", Name).str(),
1877 inconvertibleErrorCode());
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001878 }
1879
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001880// Now expand the basic registered passes from the .inc file.
Justin Bognereecc3c82016-02-25 07:23:08 +00001881#define LOOP_PASS(NAME, CREATE_PASS) \
1882 if (Name == NAME) { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001883 LPM.addPass(CREATE_PASS); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001884 return Error::success(); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001885 }
1886#define LOOP_ANALYSIS(NAME, CREATE_PASS) \
1887 if (Name == "require<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001888 LPM.addPass(RequireAnalysisPass< \
Chandler Carruth410eaeb2017-01-11 06:23:21 +00001889 std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \
1890 LoopAnalysisManager, LoopStandardAnalysisResults &, \
1891 LPMUpdater &>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001892 return Error::success(); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001893 } \
1894 if (Name == "invalidate<" NAME ">") { \
Chandler Carruthfdc6ba12016-08-03 09:14:03 +00001895 LPM.addPass(InvalidateAnalysisPass< \
Chandler Carruth470734b2016-02-26 12:30:18 +00001896 std::remove_reference<decltype(CREATE_PASS)>::type>()); \
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001897 return Error::success(); \
Justin Bognereecc3c82016-02-25 07:23:08 +00001898 }
1899#include "PassRegistry.def"
1900
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001901 for (auto &C : LoopPipelineParsingCallbacks)
1902 if (C(Name, LPM, InnerPipeline))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001903 return Error::success();
1904 return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(),
1905 inconvertibleErrorCode());
Justin Bognereecc3c82016-02-25 07:23:08 +00001906}
1907
Chandler Carruthedf59962016-02-18 09:45:17 +00001908bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) {
Chandler Carruth45a9c202016-03-11 09:15:11 +00001909#define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1910 if (Name == NAME) { \
1911 AA.registerModuleAnalysis< \
1912 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
1913 return true; \
1914 }
Chandler Carruthedf59962016-02-18 09:45:17 +00001915#define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \
1916 if (Name == NAME) { \
Chandler Carruth58dde8c2016-02-26 12:17:54 +00001917 AA.registerFunctionAnalysis< \
1918 std::remove_reference<decltype(CREATE_PASS)>::type>(); \
Chandler Carruthedf59962016-02-18 09:45:17 +00001919 return true; \
1920 }
1921#include "PassRegistry.def"
1922
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001923 for (auto &C : AAParsingCallbacks)
1924 if (C(Name, AA))
1925 return true;
Chandler Carruthedf59962016-02-18 09:45:17 +00001926 return false;
1927}
1928
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001929Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM,
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001930 ArrayRef<PipelineElement> Pipeline,
Chandler Carruth1ff77242015-03-07 09:02:36 +00001931 bool VerifyEachPass,
1932 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001933 for (const auto &Element : Pipeline) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001934 if (auto Err = parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging))
1935 return Err;
1936 // FIXME: No verifier support for Loop passes!
1937 }
1938 return Error::success();
1939}
1940
1941Error PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM,
1942 ArrayRef<PipelineElement> Pipeline,
1943 bool VerifyEachPass,
1944 bool DebugLogging) {
1945 for (const auto &Element : Pipeline) {
1946 if (auto Err =
1947 parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging))
1948 return Err;
1949 if (VerifyEachPass)
1950 FPM.addPass(VerifierPass());
1951 }
1952 return Error::success();
1953}
1954
1955Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM,
1956 ArrayRef<PipelineElement> Pipeline,
1957 bool VerifyEachPass,
1958 bool DebugLogging) {
1959 for (const auto &Element : Pipeline) {
1960 if (auto Err = parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging))
1961 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001962 // FIXME: No verifier support for CGSCC passes!
Chandler Carruth572e3402014-04-21 11:12:00 +00001963 }
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001964 return Error::success();
Chandler Carruth572e3402014-04-21 11:12:00 +00001965}
1966
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001967void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM,
1968 FunctionAnalysisManager &FAM,
1969 CGSCCAnalysisManager &CGAM,
1970 ModuleAnalysisManager &MAM) {
1971 MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); });
1972 MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); });
NAKAMURA Takumia42f5282016-05-16 10:13:37 +00001973 CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); });
1974 FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); });
1975 FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); });
1976 FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); });
1977 LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); });
1978}
1979
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001980Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM,
1981 ArrayRef<PipelineElement> Pipeline,
1982 bool VerifyEachPass,
1983 bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001984 for (const auto &Element : Pipeline) {
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001985 if (auto Err = parseModulePass(MPM, Element, VerifyEachPass, DebugLogging))
1986 return Err;
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001987 if (VerifyEachPass)
1988 MPM.addPass(VerifierPass());
Chandler Carruth66445382014-01-11 08:16:35 +00001989 }
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001990 return Error::success();
Chandler Carruth66445382014-01-11 08:16:35 +00001991}
1992
Philip Pfaffe730f2f92017-07-10 10:57:55 +00001993// Primary pass pipeline description parsing routine for a \c ModulePassManager
Chandler Carruth66445382014-01-11 08:16:35 +00001994// FIXME: Should this routine accept a TargetMachine or require the caller to
1995// pre-populate the analysis managers with target-specific stuff?
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00001996Error PassBuilder::parsePassPipeline(ModulePassManager &MPM,
1997 StringRef PipelineText,
1998 bool VerifyEachPass, bool DebugLogging) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00001999 auto Pipeline = parsePipelineText(PipelineText);
2000 if (!Pipeline || Pipeline->empty())
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002001 return make_error<StringError>(
2002 formatv("invalid pipeline '{0}'", PipelineText).str(),
2003 inconvertibleErrorCode());
Chandler Carruth66445382014-01-11 08:16:35 +00002004
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00002005 // If the first name isn't at the module layer, wrap the pipeline up
2006 // automatically.
2007 StringRef FirstName = Pipeline->front().Name;
Chandler Carruth66445382014-01-11 08:16:35 +00002008
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002009 if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) {
2010 if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00002011 Pipeline = {{"cgscc", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002012 } else if (isFunctionPassName(FirstName,
2013 FunctionPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00002014 Pipeline = {{"function", std::move(*Pipeline)}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002015 } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) {
Chandler Carruth6cb2ab22016-08-03 03:21:41 +00002016 Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}};
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002017 } else {
2018 for (auto &C : TopLevelPipelineParsingCallbacks)
2019 if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002020 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002021
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002022 // Unknown pass or pipeline name!
2023 auto &InnerPipeline = Pipeline->front().InnerPipeline;
2024 return make_error<StringError>(
2025 formatv("unknown {0} name '{1}'",
2026 (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName)
2027 .str(),
2028 inconvertibleErrorCode());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002029 }
Chandler Carruth572e3402014-04-21 11:12:00 +00002030 }
2031
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002032 if (auto Err =
2033 parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging))
2034 return Err;
2035 return Error::success();
Chandler Carruth66445382014-01-11 08:16:35 +00002036}
Chandler Carruthedf59962016-02-18 09:45:17 +00002037
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002038// Primary pass pipeline description parsing routine for a \c CGSCCPassManager
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002039Error PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM,
2040 StringRef PipelineText,
2041 bool VerifyEachPass, bool DebugLogging) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002042 auto Pipeline = parsePipelineText(PipelineText);
2043 if (!Pipeline || Pipeline->empty())
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002044 return make_error<StringError>(
2045 formatv("invalid pipeline '{0}'", PipelineText).str(),
2046 inconvertibleErrorCode());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002047
2048 StringRef FirstName = Pipeline->front().Name;
2049 if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002050 return make_error<StringError>(
2051 formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName,
2052 PipelineText)
2053 .str(),
2054 inconvertibleErrorCode());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002055
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002056 if (auto Err =
2057 parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging))
2058 return Err;
2059 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002060}
2061
2062// Primary pass pipeline description parsing routine for a \c
2063// FunctionPassManager
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002064Error PassBuilder::parsePassPipeline(FunctionPassManager &FPM,
2065 StringRef PipelineText,
2066 bool VerifyEachPass, bool DebugLogging) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002067 auto Pipeline = parsePipelineText(PipelineText);
2068 if (!Pipeline || Pipeline->empty())
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002069 return make_error<StringError>(
2070 formatv("invalid pipeline '{0}'", PipelineText).str(),
2071 inconvertibleErrorCode());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002072
2073 StringRef FirstName = Pipeline->front().Name;
2074 if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002075 return make_error<StringError>(
2076 formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName,
2077 PipelineText)
2078 .str(),
2079 inconvertibleErrorCode());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002080
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002081 if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass,
2082 DebugLogging))
2083 return Err;
2084 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002085}
2086
2087// Primary pass pipeline description parsing routine for a \c LoopPassManager
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002088Error PassBuilder::parsePassPipeline(LoopPassManager &CGPM,
2089 StringRef PipelineText,
2090 bool VerifyEachPass, bool DebugLogging) {
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002091 auto Pipeline = parsePipelineText(PipelineText);
2092 if (!Pipeline || Pipeline->empty())
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002093 return make_error<StringError>(
2094 formatv("invalid pipeline '{0}'", PipelineText).str(),
2095 inconvertibleErrorCode());
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002096
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002097 if (auto Err =
2098 parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging))
2099 return Err;
2100
2101 return Error::success();
Philip Pfaffe730f2f92017-07-10 10:57:55 +00002102}
2103
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002104Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) {
Chandler Carruth060ad612016-12-23 20:38:19 +00002105 // If the pipeline just consists of the word 'default' just replace the AA
2106 // manager with our default one.
2107 if (PipelineText == "default") {
2108 AA = buildDefaultAAPipeline();
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002109 return Error::success();
Chandler Carruth060ad612016-12-23 20:38:19 +00002110 }
2111
Chandler Carruthedf59962016-02-18 09:45:17 +00002112 while (!PipelineText.empty()) {
2113 StringRef Name;
2114 std::tie(Name, PipelineText) = PipelineText.split(',');
2115 if (!parseAAPassName(AA, Name))
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002116 return make_error<StringError>(
2117 formatv("unknown alias analysis name '{0}'", Name).str(),
2118 inconvertibleErrorCode());
Chandler Carruthedf59962016-02-18 09:45:17 +00002119 }
2120
Fedor Sergeevbd6b2132018-10-17 10:36:23 +00002121 return Error::success();
Chandler Carruthedf59962016-02-18 09:45:17 +00002122}