Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1 | //===- Parsing, selection, and construction of pass pipelines -------------===// |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // 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 Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// \file |
| 9 | /// |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 10 | /// 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 Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 14 | /// |
| 15 | //===----------------------------------------------------------------------===// |
| 16 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 17 | #include "llvm/Passes/PassBuilder.h" |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSwitch.h" |
Chandler Carruth | 6f5770b10 | 2016-02-13 23:32:00 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/AliasAnalysis.h" |
Chandler Carruth | 4f846a5 | 2016-02-20 03:46:03 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/AliasAnalysisEvaluator.h" |
Daniel Jasper | aec2fa3 | 2016-12-19 08:22:17 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AssumptionCache.h" |
Chandler Carruth | bece8d5 | 2016-02-13 23:46:24 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Xinliang David Li | 28a9327 | 2016-05-05 21:13:27 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/BlockFrequencyInfo.h" |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/CFGPrinter.h" |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/CFLAndersAliasAnalysis.h" |
| 27 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CGSCCPassManager.h" |
Chandler Carruth | 4c660f7 | 2016-03-10 11:24:11 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CallGraph.h" |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/DemandedBits.h" |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/DependenceAnalysis.h" |
Hongbin Zheng | 751337f | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/DominanceFrontier.h" |
Chandler Carruth | 45a9c20 | 2016-03-11 09:15:11 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/GlobalsModRef.h" |
Dehao Chen | 1a44452 | 2016-07-16 22:51:33 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/IVUsers.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/LazyCallGraph.h" |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/LazyValueInfo.h" |
Xinliang David Li | 8a02131 | 2016-07-02 21:18:40 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
Chandler Carruth | aaf0b4c | 2015-01-20 10:58:50 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Daniel Berlin | 554dcd8 | 2017-04-11 20:06:36 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/MemorySSA.h" |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Adam Nemet | 0965da2 | 2017-10-09 23:19:02 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/OptimizationRemarkEmitter.h" |
John Brawn | bdbbd83 | 2018-06-28 14:13:06 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/PhiValues.h" |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/PostDominators.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 2b3d044 | 2016-02-20 04:01:45 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Chandler Carruth | d6091a0 | 2016-02-20 04:03:06 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Vitaly Buka | 4493fe1 | 2018-11-26 21:57:47 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/StackSafetyAnalysis.h" |
Chandler Carruth | 8ca4322 | 2015-01-15 11:39:46 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | c1dc384 | 2016-02-20 04:04:52 +0000 | [diff] [blame] | 53 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Michael Kuperstein | 82d5da5 | 2016-06-24 20:13:42 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/PreISelIntrinsicLowering.h" |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 55 | #include "llvm/CodeGen/UnreachableBlockElim.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 56 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 57 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 58 | #include "llvm/IR/PassManager.h" |
Fedor Sergeev | a2ed448 | 2019-03-31 10:15:39 +0000 | [diff] [blame^] | 59 | #include "llvm/IR/SafepointIRVerifier.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 60 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 61 | #include "llvm/Support/Debug.h" |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 62 | #include "llvm/Support/FormatVariadic.h" |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 63 | #include "llvm/Support/Regex.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 64 | #include "llvm/Target/TargetMachine.h" |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 65 | #include "llvm/Transforms/AggressiveInstCombine/AggressiveInstCombine.h" |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 66 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
Chandler Carruth | addcda4 | 2017-02-09 23:46:27 +0000 | [diff] [blame] | 67 | #include "llvm/Transforms/IPO/ArgumentPromotion.h" |
Matthew Simpson | cb58558 | 2017-10-25 13:40:08 +0000 | [diff] [blame] | 68 | #include "llvm/Transforms/IPO/CalledValuePropagation.h" |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 69 | #include "llvm/Transforms/IPO/ConstantMerge.h" |
Davide Italiano | 92b933a | 2016-07-09 03:25:35 +0000 | [diff] [blame] | 70 | #include "llvm/Transforms/IPO/CrossDSOCFI.h" |
Sean Silva | e3bb457 | 2016-06-12 09:16:39 +0000 | [diff] [blame] | 71 | #include "llvm/Transforms/IPO/DeadArgumentElimination.h" |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 72 | #include "llvm/Transforms/IPO/ElimAvailExtern.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 73 | #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 74 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Teresa Johnson | 2124157 | 2016-07-18 21:22:24 +0000 | [diff] [blame] | 75 | #include "llvm/Transforms/IPO/FunctionImport.h" |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 76 | #include "llvm/Transforms/IPO/GlobalDCE.h" |
Justin Bogner | 1a07501 | 2016-04-26 00:28:01 +0000 | [diff] [blame] | 77 | #include "llvm/Transforms/IPO/GlobalOpt.h" |
Davide Italiano | 2ae76dd | 2016-11-21 00:28:23 +0000 | [diff] [blame] | 78 | #include "llvm/Transforms/IPO/GlobalSplit.h" |
Aditya Kumar | 9e20ade | 2018-10-03 05:55:20 +0000 | [diff] [blame] | 79 | #include "llvm/Transforms/IPO/HotColdSplitting.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 80 | #include "llvm/Transforms/IPO/InferFunctionAttrs.h" |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 81 | #include "llvm/Transforms/IPO/Inliner.h" |
Justin Bogner | 4563a06 | 2016-04-26 20:15:52 +0000 | [diff] [blame] | 82 | #include "llvm/Transforms/IPO/Internalize.h" |
Davide Italiano | e8ae0b5 | 2016-07-11 18:10:06 +0000 | [diff] [blame] | 83 | #include "llvm/Transforms/IPO/LowerTypeTests.h" |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 84 | #include "llvm/Transforms/IPO/PartialInlining.h" |
Davide Italiano | f54f2f0 | 2016-05-05 21:05:36 +0000 | [diff] [blame] | 85 | #include "llvm/Transforms/IPO/SCCP.h" |
David Blaikie | 301627f | 2018-03-22 22:42:44 +0000 | [diff] [blame] | 86 | #include "llvm/Transforms/IPO/SampleProfile.h" |
Justin Bogner | 21e1537 | 2015-10-30 23:28:12 +0000 | [diff] [blame] | 87 | #include "llvm/Transforms/IPO/StripDeadPrototypes.h" |
Easwaran Raman | bdf2026 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 88 | #include "llvm/Transforms/IPO/SyntheticCountsPropagation.h" |
Davide Italiano | d737dd2 | 2016-06-14 21:44:19 +0000 | [diff] [blame] | 89 | #include "llvm/Transforms/IPO/WholeProgramDevirt.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 90 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
Philip Pfaffe | b39a97c | 2019-01-03 13:42:44 +0000 | [diff] [blame] | 91 | #include "llvm/Transforms/Instrumentation.h" |
Leonard Chan | 436fb2b | 2019-02-13 22:22:48 +0000 | [diff] [blame] | 92 | #include "llvm/Transforms/Instrumentation/AddressSanitizer.h" |
Chandler Carruth | 00a301d | 2017-11-14 01:30:04 +0000 | [diff] [blame] | 93 | #include "llvm/Transforms/Instrumentation/BoundsChecking.h" |
Vitaly Buka | 4493fe1 | 2018-11-26 21:57:47 +0000 | [diff] [blame] | 94 | #include "llvm/Transforms/Instrumentation/CGProfile.h" |
Hiroshi Yamauchi | 9775a62 | 2018-09-04 17:19:13 +0000 | [diff] [blame] | 95 | #include "llvm/Transforms/Instrumentation/ControlHeightReduction.h" |
Chandler Carruth | 7c557f8 | 2018-06-29 23:36:03 +0000 | [diff] [blame] | 96 | #include "llvm/Transforms/Instrumentation/GCOVProfiler.h" |
Manman Ren | 1829512 | 2019-02-28 20:13:38 +0000 | [diff] [blame] | 97 | #include "llvm/Transforms/Instrumentation/InstrOrderFile.h" |
Chandler Carruth | 7c557f8 | 2018-06-29 23:36:03 +0000 | [diff] [blame] | 98 | #include "llvm/Transforms/Instrumentation/InstrProfiling.h" |
Philip Pfaffe | b39a97c | 2019-01-03 13:42:44 +0000 | [diff] [blame] | 99 | #include "llvm/Transforms/Instrumentation/MemorySanitizer.h" |
David Blaikie | 4fe1fe1 | 2018-03-23 22:11:06 +0000 | [diff] [blame] | 100 | #include "llvm/Transforms/Instrumentation/PGOInstrumentation.h" |
Leonard Chan | 436fb2b | 2019-02-13 22:22:48 +0000 | [diff] [blame] | 101 | #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h" |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 102 | #include "llvm/Transforms/Scalar/ADCE.h" |
Sean Silva | a4c2d15 | 2016-06-15 06:18:01 +0000 | [diff] [blame] | 103 | #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 104 | #include "llvm/Transforms/Scalar/BDCE.h" |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 105 | #include "llvm/Transforms/Scalar/CallSiteSplitting.h" |
Adam Nemet | 3beef41 | 2016-07-18 16:29:17 +0000 | [diff] [blame] | 106 | #include "llvm/Transforms/Scalar/ConstantHoisting.h" |
Sean Silva | b025d37 | 2016-07-06 23:26:29 +0000 | [diff] [blame] | 107 | #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" |
Justin Bogner | 395c212 | 2016-04-22 19:40:41 +0000 | [diff] [blame] | 108 | #include "llvm/Transforms/Scalar/DCE.h" |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 109 | #include "llvm/Transforms/Scalar/DeadStoreElimination.h" |
Sanjay Patel | 6fd4391 | 2017-09-09 13:38:18 +0000 | [diff] [blame] | 110 | #include "llvm/Transforms/Scalar/DivRemPairs.h" |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 111 | #include "llvm/Transforms/Scalar/EarlyCSE.h" |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 112 | #include "llvm/Transforms/Scalar/Float2Int.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 113 | #include "llvm/Transforms/Scalar/GVN.h" |
Davide Italiano | 89ab89d | 2016-06-14 00:49:23 +0000 | [diff] [blame] | 114 | #include "llvm/Transforms/Scalar/GuardWidening.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 115 | #include "llvm/Transforms/Scalar/IVUsersPrinter.h" |
Sanjoy Das | 4d4339d | 2016-06-05 18:01:19 +0000 | [diff] [blame] | 116 | #include "llvm/Transforms/Scalar/IndVarSimplify.h" |
Fedor Sergeev | 194a407 | 2018-03-15 11:01:19 +0000 | [diff] [blame] | 117 | #include "llvm/Transforms/Scalar/InductiveRangeCheckElimination.h" |
Chandler Carruth | 7c557f8 | 2018-06-29 23:36:03 +0000 | [diff] [blame] | 118 | #include "llvm/Transforms/Scalar/InstSimplifyPass.h" |
Sean Silva | 46590d5 | 2016-06-14 00:51:09 +0000 | [diff] [blame] | 119 | #include "llvm/Transforms/Scalar/JumpThreading.h" |
Dehao Chen | f400a09 | 2016-07-12 22:42:24 +0000 | [diff] [blame] | 120 | #include "llvm/Transforms/Scalar/LICM.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 121 | #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h" |
Teresa Johnson | 1eca6bc | 2016-08-13 04:11:27 +0000 | [diff] [blame] | 122 | #include "llvm/Transforms/Scalar/LoopDataPrefetch.h" |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 123 | #include "llvm/Transforms/Scalar/LoopDeletion.h" |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 124 | #include "llvm/Transforms/Scalar/LoopDistribute.h" |
Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 125 | #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" |
Chandler Carruth | e6c30fd | 2018-05-25 01:32:36 +0000 | [diff] [blame] | 126 | #include "llvm/Transforms/Scalar/LoopInstSimplify.h" |
Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 127 | #include "llvm/Transforms/Scalar/LoopLoadElimination.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 128 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 129 | #include "llvm/Transforms/Scalar/LoopPredication.h" |
Justin Bogner | d0d2341 | 2016-05-03 22:02:31 +0000 | [diff] [blame] | 130 | #include "llvm/Transforms/Scalar/LoopRotation.h" |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 131 | #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" |
Chandler Carruth | e9b18e3 | 2017-01-20 08:42:19 +0000 | [diff] [blame] | 132 | #include "llvm/Transforms/Scalar/LoopSink.h" |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 133 | #include "llvm/Transforms/Scalar/LoopStrengthReduce.h" |
David Green | 963401d | 2018-07-01 12:47:30 +0000 | [diff] [blame] | 134 | #include "llvm/Transforms/Scalar/LoopUnrollAndJamPass.h" |
Sean Silva | e3c18a5 | 2016-07-19 23:54:23 +0000 | [diff] [blame] | 135 | #include "llvm/Transforms/Scalar/LoopUnrollPass.h" |
Davide Italiano | 9922344 | 2016-05-13 22:52:35 +0000 | [diff] [blame] | 136 | #include "llvm/Transforms/Scalar/LowerAtomic.h" |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 137 | #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 138 | #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h" |
Max Kazantsev | f392bc8 | 2019-01-31 09:10:17 +0000 | [diff] [blame] | 139 | #include "llvm/Transforms/Scalar/LowerWidenableCondition.h" |
Max Kazantsev | b9e65cb | 2018-12-07 14:39:46 +0000 | [diff] [blame] | 140 | #include "llvm/Transforms/Scalar/MakeGuardsExplicit.h" |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 141 | #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 142 | #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" |
Wei Mi | 1cf58f8 | 2016-07-21 22:28:52 +0000 | [diff] [blame] | 143 | #include "llvm/Transforms/Scalar/NaryReassociate.h" |
Davide Italiano | e05e330 | 2016-12-22 16:35:02 +0000 | [diff] [blame] | 144 | #include "llvm/Transforms/Scalar/NewGVN.h" |
Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 145 | #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h" |
Justin Bogner | c2bf63d | 2016-04-26 23:39:29 +0000 | [diff] [blame] | 146 | #include "llvm/Transforms/Scalar/Reassociate.h" |
Fedor Sergeev | 4b86d79 | 2017-12-15 09:32:11 +0000 | [diff] [blame] | 147 | #include "llvm/Transforms/Scalar/RewriteStatepointsForGC.h" |
Davide Italiano | 98f7e0e | 2016-05-18 15:18:25 +0000 | [diff] [blame] | 148 | #include "llvm/Transforms/Scalar/SCCP.h" |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 149 | #include "llvm/Transforms/Scalar/SROA.h" |
Mikael Holmen | b6f7600 | 2018-11-21 14:00:17 +0000 | [diff] [blame] | 150 | #include "llvm/Transforms/Scalar/Scalarizer.h" |
Chandler Carruth | 1353f9a | 2017-04-27 18:45:20 +0000 | [diff] [blame] | 151 | #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 152 | #include "llvm/Transforms/Scalar/SimplifyCFG.h" |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 153 | #include "llvm/Transforms/Scalar/Sink.h" |
Chandler Carruth | c34f789 | 2017-11-28 11:32:31 +0000 | [diff] [blame] | 154 | #include "llvm/Transforms/Scalar/SpeculateAroundPHIs.h" |
Michael Kuperstein | c406186 | 2016-08-01 21:48:33 +0000 | [diff] [blame] | 155 | #include "llvm/Transforms/Scalar/SpeculativeExecution.h" |
Sean Silva | 59fe82f | 2016-07-06 23:48:41 +0000 | [diff] [blame] | 156 | #include "llvm/Transforms/Scalar/TailRecursionElimination.h" |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 157 | #include "llvm/Transforms/Scalar/WarnMissedTransforms.h" |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 158 | #include "llvm/Transforms/Utils/AddDiscriminators.h" |
Wei Mi | e04d0ef | 2016-07-22 18:04:25 +0000 | [diff] [blame] | 159 | #include "llvm/Transforms/Utils/BreakCriticalEdges.h" |
Teresa Johnson | 853b962 | 2019-01-04 19:04:54 +0000 | [diff] [blame] | 160 | #include "llvm/Transforms/Utils/CanonicalizeAliases.h" |
Hans Wennborg | e1ecd61 | 2017-11-14 21:09:45 +0000 | [diff] [blame] | 161 | #include "llvm/Transforms/Utils/EntryExitInstrumenter.h" |
Easwaran Raman | e12c487 | 2016-06-09 19:44:46 +0000 | [diff] [blame] | 162 | #include "llvm/Transforms/Utils/LCSSA.h" |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 163 | #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" |
Davide Italiano | cd96cfd | 2016-07-09 03:03:01 +0000 | [diff] [blame] | 164 | #include "llvm/Transforms/Utils/LoopSimplify.h" |
Michael Kuperstein | 31b8399 | 2016-08-12 17:28:27 +0000 | [diff] [blame] | 165 | #include "llvm/Transforms/Utils/LowerInvoke.h" |
Davide Italiano | cccf4f0 | 2016-06-14 03:22:22 +0000 | [diff] [blame] | 166 | #include "llvm/Transforms/Utils/Mem2Reg.h" |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 167 | #include "llvm/Transforms/Utils/NameAnonGlobals.h" |
Michael Kuperstein | 39feb62 | 2016-07-25 20:52:00 +0000 | [diff] [blame] | 168 | #include "llvm/Transforms/Utils/SymbolRewriter.h" |
Markus Lavin | 4dc4ebd | 2018-12-07 08:23:37 +0000 | [diff] [blame] | 169 | #include "llvm/Transforms/Vectorize/LoadStoreVectorizer.h" |
Sean Silva | db90d4d | 2016-07-09 22:56:50 +0000 | [diff] [blame] | 170 | #include "llvm/Transforms/Vectorize/LoopVectorize.h" |
Sean Silva | 0dacbd8 | 2016-07-09 03:11:29 +0000 | [diff] [blame] | 171 | #include "llvm/Transforms/Vectorize/SLPVectorizer.h" |
Davide Italiano | cccf4f0 | 2016-06-14 03:22:22 +0000 | [diff] [blame] | 172 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 173 | using namespace llvm; |
| 174 | |
Chandler Carruth | 719ffe1 | 2017-02-12 05:38:04 +0000 | [diff] [blame] | 175 | static cl::opt<unsigned> MaxDevirtIterations("pm-max-devirt-iterations", |
| 176 | cl::ReallyHidden, cl::init(4)); |
Xinliang David Li | 126157c | 2017-05-22 16:41:57 +0000 | [diff] [blame] | 177 | static cl::opt<bool> |
| 178 | RunPartialInlining("enable-npm-partial-inlining", cl::init(false), |
| 179 | cl::Hidden, cl::ZeroOrMore, |
| 180 | cl::desc("Run Partial inlinining pass")); |
Chandler Carruth | 719ffe1 | 2017-02-12 05:38:04 +0000 | [diff] [blame] | 181 | |
Davide Italiano | 8a09b8e | 2017-05-22 23:41:40 +0000 | [diff] [blame] | 182 | static cl::opt<bool> |
Davide Italiano | 8e7d11a | 2017-05-22 23:47:11 +0000 | [diff] [blame] | 183 | RunNewGVN("enable-npm-newgvn", cl::init(false), |
Davide Italiano | 8a09b8e | 2017-05-22 23:41:40 +0000 | [diff] [blame] | 184 | cl::Hidden, cl::ZeroOrMore, |
| 185 | cl::desc("Run NewGVN instead of GVN")); |
| 186 | |
Geoff Berry | 3cca1da | 2017-06-10 15:20:03 +0000 | [diff] [blame] | 187 | static cl::opt<bool> EnableEarlyCSEMemSSA( |
Geoff Berry | 2573a19 | 2017-06-27 22:25:02 +0000 | [diff] [blame] | 188 | "enable-npm-earlycse-memssa", cl::init(true), cl::Hidden, |
| 189 | cl::desc("Enable the EarlyCSE w/ MemorySSA pass for the new PM (default = on)")); |
Geoff Berry | 3cca1da | 2017-06-10 15:20:03 +0000 | [diff] [blame] | 190 | |
Chandler Carruth | c246a4c | 2017-04-27 00:28:03 +0000 | [diff] [blame] | 191 | static cl::opt<bool> EnableGVNHoist( |
Eric Christopher | dcf1d97 | 2018-10-01 18:57:08 +0000 | [diff] [blame] | 192 | "enable-npm-gvn-hoist", cl::init(false), cl::Hidden, |
| 193 | cl::desc("Enable the GVN hoisting pass for the new PM (default = off)")); |
Chandler Carruth | c246a4c | 2017-04-27 00:28:03 +0000 | [diff] [blame] | 194 | |
Davide Italiano | be1b6a9 | 2017-06-03 23:18:29 +0000 | [diff] [blame] | 195 | static cl::opt<bool> EnableGVNSink( |
| 196 | "enable-npm-gvn-sink", cl::init(false), cl::Hidden, |
| 197 | cl::desc("Enable the GVN hoisting pass for the new PM (default = off)")); |
| 198 | |
David Green | 963401d | 2018-07-01 12:47:30 +0000 | [diff] [blame] | 199 | static cl::opt<bool> EnableUnrollAndJam( |
| 200 | "enable-npm-unroll-and-jam", cl::init(false), cl::Hidden, |
| 201 | cl::desc("Enable the Unroll and Jam pass for the new PM (default = off)")); |
| 202 | |
Easwaran Raman | bdf2026 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 203 | static cl::opt<bool> EnableSyntheticCounts( |
| 204 | "enable-npm-synthetic-counts", cl::init(false), cl::Hidden, cl::ZeroOrMore, |
| 205 | cl::desc("Run synthetic function entry count generation " |
| 206 | "pass")); |
| 207 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 208 | static Regex DefaultAliasRegex( |
| 209 | "^(default|thinlto-pre-link|thinlto|lto-pre-link|lto)<(O[0123sz])>$"); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 210 | |
Hiroshi Yamauchi | 9775a62 | 2018-09-04 17:19:13 +0000 | [diff] [blame] | 211 | static cl::opt<bool> |
| 212 | EnableCHR("enable-chr-npm", cl::init(true), cl::Hidden, |
| 213 | cl::desc("Enable control height reduction optimization (CHR)")); |
| 214 | |
Aditya Kumar | 9e20ade | 2018-10-03 05:55:20 +0000 | [diff] [blame] | 215 | extern cl::opt<bool> EnableHotColdSplit; |
Manman Ren | 1829512 | 2019-02-28 20:13:38 +0000 | [diff] [blame] | 216 | extern cl::opt<bool> EnableOrderFileInstrumentation; |
Aditya Kumar | 9e20ade | 2018-10-03 05:55:20 +0000 | [diff] [blame] | 217 | |
Wei Mi | 3bcccdf | 2019-01-17 20:48:34 +0000 | [diff] [blame] | 218 | extern cl::opt<bool> FlattenedProfileUsed; |
| 219 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 220 | static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) { |
| 221 | switch (Level) { |
| 222 | case PassBuilder::O0: |
| 223 | case PassBuilder::O1: |
| 224 | case PassBuilder::O2: |
| 225 | case PassBuilder::O3: |
| 226 | return false; |
| 227 | |
| 228 | case PassBuilder::Os: |
| 229 | case PassBuilder::Oz: |
| 230 | return true; |
| 231 | } |
| 232 | llvm_unreachable("Invalid optimization level!"); |
| 233 | } |
| 234 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 235 | namespace { |
| 236 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 237 | /// No-op module pass which does nothing. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 238 | struct NoOpModulePass { |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 239 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 240 | return PreservedAnalyses::all(); |
| 241 | } |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 242 | static StringRef name() { return "NoOpModulePass"; } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 243 | }; |
| 244 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 245 | /// No-op module analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 246 | class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> { |
| 247 | friend AnalysisInfoMixin<NoOpModuleAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 248 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 249 | |
| 250 | public: |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 251 | struct Result {}; |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 252 | Result run(Module &, ModuleAnalysisManager &) { return Result(); } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 253 | static StringRef name() { return "NoOpModuleAnalysis"; } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 254 | }; |
| 255 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 256 | /// No-op CGSCC pass which does nothing. |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 257 | struct NoOpCGSCCPass { |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 258 | PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &, |
| 259 | LazyCallGraph &, CGSCCUpdateResult &UR) { |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 260 | return PreservedAnalyses::all(); |
| 261 | } |
| 262 | static StringRef name() { return "NoOpCGSCCPass"; } |
| 263 | }; |
| 264 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 265 | /// No-op CGSCC analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 266 | class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> { |
| 267 | friend AnalysisInfoMixin<NoOpCGSCCAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 268 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 269 | |
| 270 | public: |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 271 | struct Result {}; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 272 | Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 273 | return Result(); |
| 274 | } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 275 | static StringRef name() { return "NoOpCGSCCAnalysis"; } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 276 | }; |
| 277 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 278 | /// No-op function pass which does nothing. |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 279 | struct NoOpFunctionPass { |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 280 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 281 | return PreservedAnalyses::all(); |
| 282 | } |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 283 | static StringRef name() { return "NoOpFunctionPass"; } |
| 284 | }; |
| 285 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 286 | /// No-op function analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 287 | class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> { |
| 288 | friend AnalysisInfoMixin<NoOpFunctionAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 289 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 290 | |
| 291 | public: |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 292 | struct Result {}; |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 293 | Result run(Function &, FunctionAnalysisManager &) { return Result(); } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 294 | static StringRef name() { return "NoOpFunctionAnalysis"; } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 295 | }; |
| 296 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 297 | /// No-op loop pass which does nothing. |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 298 | struct NoOpLoopPass { |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 299 | PreservedAnalyses run(Loop &L, LoopAnalysisManager &, |
| 300 | LoopStandardAnalysisResults &, LPMUpdater &) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 301 | return PreservedAnalyses::all(); |
| 302 | } |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 303 | static StringRef name() { return "NoOpLoopPass"; } |
| 304 | }; |
| 305 | |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 306 | /// No-op loop analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 307 | class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> { |
| 308 | friend AnalysisInfoMixin<NoOpLoopAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 309 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 310 | |
| 311 | public: |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 312 | struct Result {}; |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 313 | Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) { |
| 314 | return Result(); |
| 315 | } |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 316 | static StringRef name() { return "NoOpLoopAnalysis"; } |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 317 | }; |
| 318 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 319 | AnalysisKey NoOpModuleAnalysis::Key; |
| 320 | AnalysisKey NoOpCGSCCAnalysis::Key; |
| 321 | AnalysisKey NoOpFunctionAnalysis::Key; |
| 322 | AnalysisKey NoOpLoopAnalysis::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 323 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 324 | } // End anonymous namespace. |
| 325 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 326 | void PassBuilder::invokePeepholeEPCallbacks( |
| 327 | FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) { |
| 328 | for (auto &C : PeepholeEPCallbacks) |
| 329 | C(FPM, Level); |
| 330 | } |
| 331 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 332 | void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 333 | #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 334 | MAM.registerPass([&] { return CREATE_PASS; }); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 335 | #include "PassRegistry.def" |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 336 | |
| 337 | for (auto &C : ModuleAnalysisRegistrationCallbacks) |
| 338 | C(MAM); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 341 | void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 342 | #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 343 | CGAM.registerPass([&] { return CREATE_PASS; }); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 344 | #include "PassRegistry.def" |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 345 | |
| 346 | for (auto &C : CGSCCAnalysisRegistrationCallbacks) |
| 347 | C(CGAM); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 348 | } |
| 349 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 350 | void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 351 | #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 352 | FAM.registerPass([&] { return CREATE_PASS; }); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 353 | #include "PassRegistry.def" |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 354 | |
| 355 | for (auto &C : FunctionAnalysisRegistrationCallbacks) |
| 356 | C(FAM); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 357 | } |
| 358 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 359 | void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 360 | #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 361 | LAM.registerPass([&] { return CREATE_PASS; }); |
| 362 | #include "PassRegistry.def" |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 363 | |
| 364 | for (auto &C : LoopAnalysisRegistrationCallbacks) |
| 365 | C(LAM); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 366 | } |
| 367 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 368 | FunctionPassManager |
| 369 | PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 370 | ThinLTOPhase Phase, |
| 371 | bool DebugLogging) { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 372 | assert(Level != O0 && "Must request optimizations!"); |
| 373 | FunctionPassManager FPM(DebugLogging); |
| 374 | |
| 375 | // Form SSA out of local memory accesses after breaking apart aggregates into |
| 376 | // scalars. |
| 377 | FPM.addPass(SROA()); |
| 378 | |
| 379 | // Catch trivial redundancies |
Geoff Berry | 3cca1da | 2017-06-10 15:20:03 +0000 | [diff] [blame] | 380 | FPM.addPass(EarlyCSEPass(EnableEarlyCSEMemSSA)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 381 | |
Davide Italiano | c368831 | 2017-06-01 23:08:14 +0000 | [diff] [blame] | 382 | // Hoisting of scalars and load expressions. |
| 383 | if (EnableGVNHoist) |
| 384 | FPM.addPass(GVNHoistPass()); |
| 385 | |
Davide Italiano | be1b6a9 | 2017-06-03 23:18:29 +0000 | [diff] [blame] | 386 | // Global value numbering based sinking. |
| 387 | if (EnableGVNSink) { |
| 388 | FPM.addPass(GVNSinkPass()); |
| 389 | FPM.addPass(SimplifyCFGPass()); |
| 390 | } |
| 391 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 392 | // Speculative execution if the target has divergent branches; otherwise nop. |
| 393 | FPM.addPass(SpeculativeExecutionPass()); |
| 394 | |
| 395 | // Optimize based on known information about branches, and cleanup afterward. |
| 396 | FPM.addPass(JumpThreadingPass()); |
| 397 | FPM.addPass(CorrelatedValuePropagationPass()); |
| 398 | FPM.addPass(SimplifyCFGPass()); |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 399 | if (Level == O3) |
| 400 | FPM.addPass(AggressiveInstCombinePass()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 401 | FPM.addPass(InstCombinePass()); |
| 402 | |
| 403 | if (!isOptimizingForSize(Level)) |
| 404 | FPM.addPass(LibCallsShrinkWrapPass()); |
| 405 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 406 | invokePeepholeEPCallbacks(FPM, Level); |
| 407 | |
Rong Xu | e1f4245 | 2017-10-23 22:21:29 +0000 | [diff] [blame] | 408 | // For PGO use pipeline, try to optimize memory intrinsics such as memcpy |
| 409 | // using the size value profile. Don't perform this when optimizing for size. |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 410 | if (PGOOpt && PGOOpt->Action == PGOOptions::IRUse && |
Rong Xu | e1f4245 | 2017-10-23 22:21:29 +0000 | [diff] [blame] | 411 | !isOptimizingForSize(Level)) |
| 412 | FPM.addPass(PGOMemOPSizeOpt()); |
| 413 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 414 | FPM.addPass(TailCallElimPass()); |
| 415 | FPM.addPass(SimplifyCFGPass()); |
| 416 | |
| 417 | // Form canonically associated expression trees, and simplify the trees using |
| 418 | // basic mathematical properties. For example, this will form (nearly) |
| 419 | // minimal multiplication trees. |
| 420 | FPM.addPass(ReassociatePass()); |
| 421 | |
| 422 | // Add the primary loop simplification pipeline. |
| 423 | // FIXME: Currently this is split into two loop pass pipelines because we run |
Chandler Carruth | 71fd270 | 2018-05-30 02:46:45 +0000 | [diff] [blame] | 424 | // some function passes in between them. These can and should be removed |
| 425 | // and/or replaced by scheduling the loop pass equivalents in the correct |
| 426 | // positions. But those equivalent passes aren't powerful enough yet. |
| 427 | // Specifically, `SimplifyCFGPass` and `InstCombinePass` are currently still |
| 428 | // used. We have `LoopSimplifyCFGPass` which isn't yet powerful enough yet to |
| 429 | // fully replace `SimplifyCFGPass`, and the closest to the other we have is |
| 430 | // `LoopInstSimplify`. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 431 | LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging); |
| 432 | |
Chandler Carruth | 71fd270 | 2018-05-30 02:46:45 +0000 | [diff] [blame] | 433 | // Simplify the loop body. We do this initially to clean up after other loop |
| 434 | // passes run, either when iterating on a loop or on inner loops with |
| 435 | // implications on the outer loop. |
| 436 | LPM1.addPass(LoopInstSimplifyPass()); |
| 437 | LPM1.addPass(LoopSimplifyCFGPass()); |
| 438 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 439 | // Rotate Loop - disable header duplication at -Oz |
| 440 | LPM1.addPass(LoopRotatePass(Level != Oz)); |
| 441 | LPM1.addPass(LICMPass()); |
Chandler Carruth | 86248d5 | 2017-05-26 01:24:11 +0000 | [diff] [blame] | 442 | LPM1.addPass(SimpleLoopUnswitchPass()); |
Chandler Carruth | 79b733b | 2017-01-26 23:21:17 +0000 | [diff] [blame] | 443 | LPM2.addPass(IndVarSimplifyPass()); |
| 444 | LPM2.addPass(LoopIdiomRecognizePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 445 | |
| 446 | for (auto &C : LateLoopOptimizationsEPCallbacks) |
| 447 | C(LPM2, Level); |
| 448 | |
Chandler Carruth | 79b733b | 2017-01-26 23:21:17 +0000 | [diff] [blame] | 449 | LPM2.addPass(LoopDeletionPass()); |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 450 | // Do not enable unrolling in PreLinkThinLTO phase during sample PGO |
Dehao Chen | 3a986142 | 2017-07-07 20:53:10 +0000 | [diff] [blame] | 451 | // because it changes IR to makes profile annotation in back compile |
| 452 | // inaccurate. |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 453 | if (Phase != ThinLTOPhase::PreLink || !PGOOpt || |
| 454 | PGOOpt->Action != PGOOptions::SampleUse) |
Teresa Johnson | ecd9013 | 2017-08-02 20:35:29 +0000 | [diff] [blame] | 455 | LPM2.addPass(LoopFullUnrollPass(Level)); |
Chandler Carruth | 79b733b | 2017-01-26 23:21:17 +0000 | [diff] [blame] | 456 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 457 | for (auto &C : LoopOptimizerEndEPCallbacks) |
| 458 | C(LPM2, Level); |
| 459 | |
Chandler Carruth | 0ede22e | 2017-02-09 23:54:57 +0000 | [diff] [blame] | 460 | // We provide the opt remark emitter pass for LICM to use. We only need to do |
| 461 | // this once as it is immutable. |
| 462 | FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 463 | FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1), DebugLogging)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 464 | FPM.addPass(SimplifyCFGPass()); |
| 465 | FPM.addPass(InstCombinePass()); |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 466 | FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2), DebugLogging)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 467 | |
| 468 | // Eliminate redundancies. |
| 469 | if (Level != O1) { |
| 470 | // These passes add substantial compile time so skip them at O1. |
| 471 | FPM.addPass(MergedLoadStoreMotionPass()); |
Davide Italiano | 8a09b8e | 2017-05-22 23:41:40 +0000 | [diff] [blame] | 472 | if (RunNewGVN) |
| 473 | FPM.addPass(NewGVNPass()); |
| 474 | else |
| 475 | FPM.addPass(GVN()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | // Specially optimize memory movement as it doesn't look like dataflow in SSA. |
| 479 | FPM.addPass(MemCpyOptPass()); |
| 480 | |
| 481 | // Sparse conditional constant propagation. |
| 482 | // FIXME: It isn't clear why we do this *after* loop passes rather than |
| 483 | // before... |
| 484 | FPM.addPass(SCCPPass()); |
| 485 | |
| 486 | // Delete dead bit computations (instcombine runs after to fold away the dead |
| 487 | // computations, and then ADCE will run later to exploit any new DCE |
| 488 | // opportunities that creates). |
| 489 | FPM.addPass(BDCEPass()); |
| 490 | |
| 491 | // Run instcombine after redundancy and dead bit elimination to exploit |
| 492 | // opportunities opened up by them. |
| 493 | FPM.addPass(InstCombinePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 494 | invokePeepholeEPCallbacks(FPM, Level); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 495 | |
| 496 | // Re-consider control flow based optimizations after redundancy elimination, |
| 497 | // redo DCE, etc. |
| 498 | FPM.addPass(JumpThreadingPass()); |
| 499 | FPM.addPass(CorrelatedValuePropagationPass()); |
| 500 | FPM.addPass(DSEPass()); |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 501 | FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 502 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 503 | for (auto &C : ScalarOptimizerLateEPCallbacks) |
| 504 | C(FPM, Level); |
| 505 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 506 | // Finally, do an expensive DCE pass to catch all the dead code exposed by |
| 507 | // the simplifications and basic cleanup after all the simplifications. |
| 508 | FPM.addPass(ADCEPass()); |
| 509 | FPM.addPass(SimplifyCFGPass()); |
| 510 | FPM.addPass(InstCombinePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 511 | invokePeepholeEPCallbacks(FPM, Level); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 512 | |
Hiroshi Yamauchi | 9775a62 | 2018-09-04 17:19:13 +0000 | [diff] [blame] | 513 | if (EnableCHR && Level == O3 && PGOOpt && |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 514 | (PGOOpt->Action == PGOOptions::IRUse || |
| 515 | PGOOpt->Action == PGOOptions::SampleUse)) |
Hiroshi Yamauchi | 9775a62 | 2018-09-04 17:19:13 +0000 | [diff] [blame] | 516 | FPM.addPass(ControlHeightReductionPass()); |
| 517 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 518 | return FPM; |
| 519 | } |
| 520 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 521 | void PassBuilder::addPGOInstrPasses(ModulePassManager &MPM, bool DebugLogging, |
| 522 | PassBuilder::OptimizationLevel Level, |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 523 | bool RunProfileGen, bool IsCS, |
| 524 | std::string ProfileFile, |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 525 | std::string ProfileRemappingFile) { |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 526 | // Generally running simplification passes and the inliner with an high |
| 527 | // threshold results in smaller executables, but there may be cases where |
| 528 | // the size grows, so let's be conservative here and skip this simplification |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 529 | // at -Os/Oz. We will not do this inline for context sensistive PGO (when |
| 530 | // IsCS is true). |
| 531 | if (!isOptimizingForSize(Level) && !IsCS) { |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 532 | InlineParams IP; |
| 533 | |
| 534 | // In the old pass manager, this is a cl::opt. Should still this be one? |
| 535 | IP.DefaultThreshold = 75; |
| 536 | |
| 537 | // FIXME: The hint threshold has the same value used by the regular inliner. |
| 538 | // This should probably be lowered after performance testing. |
| 539 | // FIXME: this comment is cargo culted from the old pass manager, revisit). |
| 540 | IP.HintThreshold = 325; |
| 541 | |
| 542 | CGSCCPassManager CGPipeline(DebugLogging); |
| 543 | |
| 544 | CGPipeline.addPass(InlinerPass(IP)); |
| 545 | |
| 546 | FunctionPassManager FPM; |
| 547 | FPM.addPass(SROA()); |
| 548 | FPM.addPass(EarlyCSEPass()); // Catch trivial redundancies. |
| 549 | FPM.addPass(SimplifyCFGPass()); // Merge & remove basic blocks. |
| 550 | FPM.addPass(InstCombinePass()); // Combine silly sequences. |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 551 | invokePeepholeEPCallbacks(FPM, Level); |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 552 | |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 553 | CGPipeline.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM))); |
| 554 | |
| 555 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPipeline))); |
| 556 | } |
| 557 | |
Chandler Carruth | f4d62c4 | 2017-05-25 07:15:09 +0000 | [diff] [blame] | 558 | // Delete anything that is now dead to make sure that we don't instrument |
| 559 | // dead code. Instrumentation can end up keeping dead code around and |
| 560 | // dramatically increase code size. |
| 561 | MPM.addPass(GlobalDCEPass()); |
| 562 | |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 563 | if (RunProfileGen) { |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 564 | MPM.addPass(PGOInstrumentationGen(IsCS)); |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 565 | |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 566 | FunctionPassManager FPM; |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 567 | FPM.addPass( |
| 568 | createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging)); |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 569 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 570 | |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 571 | // Add the profile lowering pass. |
| 572 | InstrProfOptions Options; |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 573 | if (!ProfileFile.empty()) |
| 574 | Options.InstrProfileOutput = ProfileFile; |
Xinliang David Li | b67530e | 2017-06-25 00:26:43 +0000 | [diff] [blame] | 575 | Options.DoCounterPromotion = true; |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 576 | Options.UseBFIInPromotion = IsCS; |
| 577 | MPM.addPass(InstrProfiling(Options, IsCS)); |
| 578 | } else if (!ProfileFile.empty()) |
| 579 | MPM.addPass(PGOInstrumentationUse(ProfileFile, ProfileRemappingFile, IsCS)); |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 580 | } |
| 581 | |
Easwaran Raman | 8249fac | 2017-06-28 13:33:49 +0000 | [diff] [blame] | 582 | static InlineParams |
| 583 | getInlineParamsFromOptLevel(PassBuilder::OptimizationLevel Level) { |
| 584 | auto O3 = PassBuilder::O3; |
| 585 | unsigned OptLevel = Level > O3 ? 2 : Level; |
| 586 | unsigned SizeLevel = Level > O3 ? Level - O3 : 0; |
| 587 | return getInlineParams(OptLevel, SizeLevel); |
| 588 | } |
| 589 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 590 | ModulePassManager |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 591 | PassBuilder::buildModuleSimplificationPipeline(OptimizationLevel Level, |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 592 | ThinLTOPhase Phase, |
| 593 | bool DebugLogging) { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 594 | ModulePassManager MPM(DebugLogging); |
| 595 | |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 596 | bool HasSampleProfile = PGOOpt && (PGOOpt->Action == PGOOptions::SampleUse); |
Teresa Johnson | b0bf530 | 2019-02-05 04:09:19 +0000 | [diff] [blame] | 597 | |
| 598 | // In ThinLTO mode, when flattened profile is used, all the available |
| 599 | // profile information will be annotated in PreLink phase so there is |
| 600 | // no need to load the profile again in PostLink. |
| 601 | bool LoadSampleProfile = |
| 602 | HasSampleProfile && |
| 603 | !(FlattenedProfileUsed && Phase == ThinLTOPhase::PostLink); |
| 604 | |
| 605 | // During the ThinLTO backend phase we perform early indirect call promotion |
| 606 | // here, before globalopt. Otherwise imported available_externally functions |
| 607 | // look unreferenced and are removed. If we are going to load the sample |
| 608 | // profile then defer until later. |
| 609 | // TODO: See if we can move later and consolidate with the location where |
| 610 | // we perform ICP when we are loading a sample profile. |
| 611 | // TODO: We pass HasSampleProfile (whether there was a sample profile file |
| 612 | // passed to the compile) to the SamplePGO flag of ICP. This is used to |
| 613 | // determine whether the new direct calls are annotated with prof metadata. |
| 614 | // Ideally this should be determined from whether the IR is annotated with |
| 615 | // sample profile, and not whether the a sample profile was provided on the |
| 616 | // command line. E.g. for flattened profiles where we will not be reloading |
| 617 | // the sample profile in the ThinLTO backend, we ideally shouldn't have to |
| 618 | // provide the sample profile file. |
| 619 | if (Phase == ThinLTOPhase::PostLink && !LoadSampleProfile) |
| 620 | MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */, HasSampleProfile)); |
| 621 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 622 | // Do basic inference of function attributes from known properties of system |
| 623 | // libraries and other oracles. |
| 624 | MPM.addPass(InferFunctionAttrsPass()); |
| 625 | |
| 626 | // Create an early function pass manager to cleanup the output of the |
| 627 | // frontend. |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 628 | FunctionPassManager EarlyFPM(DebugLogging); |
| 629 | EarlyFPM.addPass(SimplifyCFGPass()); |
| 630 | EarlyFPM.addPass(SROA()); |
| 631 | EarlyFPM.addPass(EarlyCSEPass()); |
| 632 | EarlyFPM.addPass(LowerExpectIntrinsicPass()); |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 633 | if (Level == O3) |
| 634 | EarlyFPM.addPass(CallSiteSplittingPass()); |
| 635 | |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 636 | // In SamplePGO ThinLTO backend, we need instcombine before profile annotation |
| 637 | // to convert bitcast to direct calls so that they can be inlined during the |
| 638 | // profile annotation prepration step. |
| 639 | // More details about SamplePGO design can be found in: |
| 640 | // https://research.google.com/pubs/pub45290.html |
| 641 | // FIXME: revisit how SampleProfileLoad/Inliner/ICP is structured. |
Teresa Johnson | b0bf530 | 2019-02-05 04:09:19 +0000 | [diff] [blame] | 642 | if (LoadSampleProfile) |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 643 | EarlyFPM.addPass(InstCombinePass()); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 644 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 645 | |
Teresa Johnson | b0bf530 | 2019-02-05 04:09:19 +0000 | [diff] [blame] | 646 | if (LoadSampleProfile) { |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 647 | // Annotate sample profile right after early FPM to ensure freshness of |
| 648 | // the debug info. |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 649 | MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, |
Teresa Johnson | b0bf530 | 2019-02-05 04:09:19 +0000 | [diff] [blame] | 650 | PGOOpt->ProfileRemappingFile, |
| 651 | Phase == ThinLTOPhase::PreLink)); |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 652 | // Do not invoke ICP in the ThinLTOPrelink phase as it makes it hard |
| 653 | // for the profile annotation to be accurate in the ThinLTO backend. |
| 654 | if (Phase != ThinLTOPhase::PreLink) |
| 655 | // We perform early indirect call promotion here, before globalopt. |
| 656 | // This is important for the ThinLTO backend phase because otherwise |
| 657 | // imported available_externally functions look unreferenced and are |
| 658 | // removed. |
| 659 | MPM.addPass(PGOIndirectCallPromotion(Phase == ThinLTOPhase::PostLink, |
Teresa Johnson | b0bf530 | 2019-02-05 04:09:19 +0000 | [diff] [blame] | 660 | true /* SamplePGO */)); |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 661 | } |
| 662 | |
Malcolm Parsons | 21e545d | 2018-01-24 10:33:39 +0000 | [diff] [blame] | 663 | // Interprocedural constant propagation now that basic cleanup has occurred |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 664 | // and prior to optimizing globals. |
| 665 | // FIXME: This position in the pipeline hasn't been carefully considered in |
| 666 | // years, it should be re-analyzed. |
| 667 | MPM.addPass(IPSCCPPass()); |
| 668 | |
Matthew Simpson | cb58558 | 2017-10-25 13:40:08 +0000 | [diff] [blame] | 669 | // Attach metadata to indirect call sites indicating the set of functions |
| 670 | // they may target at run-time. This should follow IPSCCP. |
| 671 | MPM.addPass(CalledValuePropagationPass()); |
| 672 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 673 | // Optimize globals to try and fold them into constants. |
| 674 | MPM.addPass(GlobalOptPass()); |
| 675 | |
| 676 | // Promote any localized globals to SSA registers. |
| 677 | // FIXME: Should this instead by a run of SROA? |
| 678 | // FIXME: We should probably run instcombine and simplify-cfg afterward to |
| 679 | // delete control flows that are dead once globals have been folded to |
| 680 | // constants. |
| 681 | MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); |
| 682 | |
| 683 | // Remove any dead arguments exposed by cleanups and constand folding |
| 684 | // globals. |
| 685 | MPM.addPass(DeadArgumentEliminationPass()); |
| 686 | |
| 687 | // Create a small function pass pipeline to cleanup after all the global |
| 688 | // optimizations. |
| 689 | FunctionPassManager GlobalCleanupPM(DebugLogging); |
| 690 | GlobalCleanupPM.addPass(InstCombinePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 691 | invokePeepholeEPCallbacks(GlobalCleanupPM, Level); |
| 692 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 693 | GlobalCleanupPM.addPass(SimplifyCFGPass()); |
| 694 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM))); |
| 695 | |
Dehao Chen | 89d3226 | 2017-08-02 01:28:31 +0000 | [diff] [blame] | 696 | // Add all the requested passes for instrumentation PGO, if requested. |
| 697 | if (PGOOpt && Phase != ThinLTOPhase::PostLink && |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 698 | (PGOOpt->Action == PGOOptions::IRInstr || |
| 699 | PGOOpt->Action == PGOOptions::IRUse)) { |
| 700 | addPGOInstrPasses(MPM, DebugLogging, Level, |
| 701 | /* RunProfileGen */ PGOOpt->Action == PGOOptions::IRInstr, |
| 702 | /* IsCS */ false, PGOOpt->ProfileFile, |
Richard Smith | 6c67662 | 2018-10-10 23:13:47 +0000 | [diff] [blame] | 703 | PGOOpt->ProfileRemappingFile); |
Dehao Chen | 89d3226 | 2017-08-02 01:28:31 +0000 | [diff] [blame] | 704 | MPM.addPass(PGOIndirectCallPromotion(false, false)); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 705 | } |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 706 | if (PGOOpt && Phase != ThinLTOPhase::PostLink && |
| 707 | PGOOpt->CSAction == PGOOptions::CSIRInstr) |
| 708 | MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->CSProfileGenFile)); |
Davide Italiano | 513dfaa | 2017-02-13 15:26:22 +0000 | [diff] [blame] | 709 | |
Easwaran Raman | bdf2026 | 2018-01-09 19:39:35 +0000 | [diff] [blame] | 710 | // Synthesize function entry counts for non-PGO compilation. |
| 711 | if (EnableSyntheticCounts && !PGOOpt) |
| 712 | MPM.addPass(SyntheticCountsPropagation()); |
| 713 | |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame] | 714 | // Require the GlobalsAA analysis for the module so we can query it within |
| 715 | // the CGSCC pipeline. |
| 716 | MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 717 | |
Easwaran Raman | 5e6f9bd | 2017-05-04 16:58:45 +0000 | [diff] [blame] | 718 | // Require the ProfileSummaryAnalysis for the module so we can query it within |
| 719 | // the inliner pass. |
| 720 | MPM.addPass(RequireAnalysisPass<ProfileSummaryAnalysis, Module>()); |
| 721 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 722 | // Now begin the main postorder CGSCC pipeline. |
| 723 | // FIXME: The current CGSCC pipeline has its origins in the legacy pass |
| 724 | // manager and trying to emulate its precise behavior. Much of this doesn't |
| 725 | // make a lot of sense and we should revisit the core CGSCC structure. |
| 726 | CGSCCPassManager MainCGPipeline(DebugLogging); |
| 727 | |
| 728 | // Note: historically, the PruneEH pass was run first to deduce nounwind and |
| 729 | // generally clean up exception handling overhead. It isn't clear this is |
| 730 | // valuable as the inliner doesn't currently care whether it is inlining an |
| 731 | // invoke or a call. |
| 732 | |
| 733 | // Run the inliner first. The theory is that we are walking bottom-up and so |
| 734 | // the callees have already been fully optimized, and we want to inline them |
| 735 | // into the callers so that our optimizations can reflect that. |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 736 | // For PreLinkThinLTO pass, we disable hot-caller heuristic for sample PGO |
Dehao Chen | 3a986142 | 2017-07-07 20:53:10 +0000 | [diff] [blame] | 737 | // because it makes profile annotation in the backend inaccurate. |
| 738 | InlineParams IP = getInlineParamsFromOptLevel(Level); |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 739 | if (Phase == ThinLTOPhase::PreLink && PGOOpt && |
| 740 | PGOOpt->Action == PGOOptions::SampleUse) |
Dehao Chen | 3a986142 | 2017-07-07 20:53:10 +0000 | [diff] [blame] | 741 | IP.HotCallSiteThreshold = 0; |
| 742 | MainCGPipeline.addPass(InlinerPass(IP)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 743 | |
| 744 | // Now deduce any function attributes based in the current code. |
| 745 | MainCGPipeline.addPass(PostOrderFunctionAttrsPass()); |
| 746 | |
Chandler Carruth | 0ede22e | 2017-02-09 23:54:57 +0000 | [diff] [blame] | 747 | // When at O3 add argument promotion to the pass pipeline. |
| 748 | // FIXME: It isn't at all clear why this should be limited to O3. |
| 749 | if (Level == O3) |
| 750 | MainCGPipeline.addPass(ArgumentPromotionPass()); |
| 751 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 752 | // Lastly, add the core function simplification pipeline nested inside the |
| 753 | // CGSCC walk. |
| 754 | MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 755 | buildFunctionSimplificationPipeline(Level, Phase, DebugLogging))); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 756 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 757 | for (auto &C : CGSCCOptimizerLateEPCallbacks) |
| 758 | C(MainCGPipeline, Level); |
| 759 | |
Chandler Carruth | 719ffe1 | 2017-02-12 05:38:04 +0000 | [diff] [blame] | 760 | // We wrap the CGSCC pipeline in a devirtualization repeater. This will try |
| 761 | // to detect when we devirtualize indirect calls and iterate the SCC passes |
| 762 | // in that case to try and catch knock-on inlining or function attrs |
| 763 | // opportunities. Then we add it to the module pipeline by walking the SCCs |
| 764 | // in postorder (or bottom-up). |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 765 | MPM.addPass( |
Chandler Carruth | 719ffe1 | 2017-02-12 05:38:04 +0000 | [diff] [blame] | 766 | createModuleToPostOrderCGSCCPassAdaptor(createDevirtSCCRepeatedPass( |
Chandler Carruth | 19913b2 | 2017-08-11 05:47:13 +0000 | [diff] [blame] | 767 | std::move(MainCGPipeline), MaxDevirtIterations))); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 768 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 769 | return MPM; |
| 770 | } |
| 771 | |
Vedant Kumar | 47a0c9b | 2019-02-15 18:46:44 +0000 | [diff] [blame] | 772 | ModulePassManager PassBuilder::buildModuleOptimizationPipeline( |
| 773 | OptimizationLevel Level, bool DebugLogging, bool LTOPreLink) { |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 774 | ModulePassManager MPM(DebugLogging); |
| 775 | |
| 776 | // Optimize globals now that the module is fully simplified. |
| 777 | MPM.addPass(GlobalOptPass()); |
Davide Italiano | e070721 | 2017-10-05 18:36:01 +0000 | [diff] [blame] | 778 | MPM.addPass(GlobalDCEPass()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 779 | |
Xinliang David Li | 126157c | 2017-05-22 16:41:57 +0000 | [diff] [blame] | 780 | // Run partial inlining pass to partially inline functions that have |
| 781 | // large bodies. |
| 782 | if (RunPartialInlining) |
| 783 | MPM.addPass(PartialInlinerPass()); |
| 784 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 785 | // 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 Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 793 | MPM.addPass(EliminateAvailableExternallyPass()); |
| 794 | |
Manman Ren | 1829512 | 2019-02-28 20:13:38 +0000 | [diff] [blame] | 795 | if (EnableOrderFileInstrumentation) |
| 796 | MPM.addPass(InstrOrderFilePass()); |
| 797 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 798 | // Do RPO function attribute inference across the module to forward-propagate |
| 799 | // attributes where applicable. |
| 800 | // FIXME: Is this really an optimization rather than a canonicalization? |
| 801 | MPM.addPass(ReversePostOrderFunctionAttrsPass()); |
| 802 | |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 803 | // Do a post inline PGO instrumentation and use pass. This is a context |
| 804 | // sensitive PGO pass. We don't want to do this in LTOPreLink phrase as |
| 805 | // cross-module inline has not been done yet. The context sensitive |
| 806 | // instrumentation is after all the inlines are done. |
| 807 | if (!LTOPreLink && PGOOpt) { |
| 808 | if (PGOOpt->CSAction == PGOOptions::CSIRInstr) |
| 809 | addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true, |
| 810 | /* IsCS */ true, PGOOpt->CSProfileGenFile, |
| 811 | PGOOpt->ProfileRemappingFile); |
| 812 | else if (PGOOpt->CSAction == PGOOptions::CSIRUse) |
| 813 | addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false, |
| 814 | /* IsCS */ true, PGOOpt->ProfileFile, |
| 815 | PGOOpt->ProfileRemappingFile); |
| 816 | } |
| 817 | |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame] | 818 | // Re-require GloblasAA here prior to function passes. This is particularly |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 819 | // useful as the above will have inlined, DCE'ed, and function-attr |
| 820 | // propagated everything. We should at this point have a reasonably minimal |
| 821 | // and richly annotated call graph. By computing aliasing and mod/ref |
| 822 | // information for all local globals here, the late loop passes and notably |
| 823 | // the vectorizer will be able to use them to help recognize vectorizable |
| 824 | // memory operations. |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame] | 825 | MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 826 | |
| 827 | FunctionPassManager OptimizePM(DebugLogging); |
| 828 | OptimizePM.addPass(Float2IntPass()); |
| 829 | // FIXME: We need to run some loop optimizations to re-rotate loops after |
| 830 | // simplify-cfg and others undo their rotation. |
| 831 | |
| 832 | // Optimize the loop execution. These passes operate on entire loop nests |
| 833 | // rather than on each loop in an inside-out manner, and so they are actually |
| 834 | // function passes. |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 835 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 836 | for (auto &C : VectorizerStartEPCallbacks) |
| 837 | C(OptimizePM, Level); |
| 838 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 839 | // First rotate loops that may have been un-rotated by prior passes. |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 840 | OptimizePM.addPass( |
| 841 | createFunctionToLoopPassAdaptor(LoopRotatePass(), DebugLogging)); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 842 | |
| 843 | // Distribute loops to allow partial vectorization. I.e. isolate dependences |
| 844 | // into separate loop that would otherwise inhibit vectorization. This is |
| 845 | // currently only performed for loops marked with the metadata |
| 846 | // llvm.loop.distribute=true or when -enable-loop-distribute is specified. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 847 | OptimizePM.addPass(LoopDistributePass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 848 | |
| 849 | // Now run the core loop vectorizer. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 850 | OptimizePM.addPass(LoopVectorizePass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 851 | |
Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 852 | // Eliminate loads by forwarding stores from the previous iteration to loads |
| 853 | // of the current iteration. |
| 854 | OptimizePM.addPass(LoopLoadEliminationPass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 855 | |
| 856 | // Cleanup after the loop optimization passes. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 857 | OptimizePM.addPass(InstCombinePass()); |
| 858 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 859 | // Now that we've formed fast to execute loop structures, we do further |
| 860 | // optimizations. These are run afterward as they might block doing complex |
| 861 | // analyses and transforms such as what are needed for loop vectorization. |
| 862 | |
Sanjay Patel | 0ab0c1a | 2017-12-14 22:05:20 +0000 | [diff] [blame] | 863 | // Cleanup after loop vectorization, etc. Simplification passes like CVP and |
| 864 | // GVN, loop transforms, and others have already run, so it's now better to |
| 865 | // convert to more optimized IR using more aggressive simplify CFG options. |
| 866 | // The extra sinking transform can create larger basic blocks, so do this |
| 867 | // before SLP vectorization. |
| 868 | OptimizePM.addPass(SimplifyCFGPass(SimplifyCFGOptions(). |
| 869 | forwardSwitchCondToPhi(true). |
| 870 | convertSwitchToLookupTable(true). |
| 871 | needCanonicalLoops(false). |
| 872 | sinkCommonInsts(true))); |
| 873 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 874 | // Optimize parallel scalar instruction chains into SIMD instructions. |
| 875 | OptimizePM.addPass(SLPVectorizerPass()); |
| 876 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 877 | OptimizePM.addPass(InstCombinePass()); |
| 878 | |
| 879 | // Unroll small loops to hide loop backedge latency and saturate any parallel |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 880 | // execution resources of an out-of-order processor. We also then need to |
| 881 | // clean up redundancies and loop invariant code. |
| 882 | // FIXME: It would be really good to use a loop-integrated instruction |
| 883 | // combiner for cleanup here so that the unrolling and LICM can be pipelined |
| 884 | // across the loop nests. |
David Green | 963401d | 2018-07-01 12:47:30 +0000 | [diff] [blame] | 885 | // We do UnrollAndJam in a separate LPM to ensure it happens before unroll |
| 886 | if (EnableUnrollAndJam) { |
| 887 | OptimizePM.addPass( |
| 888 | createFunctionToLoopPassAdaptor(LoopUnrollAndJamPass(Level))); |
| 889 | } |
Fedor Sergeev | 412ed34 | 2018-10-31 14:33:14 +0000 | [diff] [blame] | 890 | OptimizePM.addPass(LoopUnrollPass(LoopUnrollOptions(Level))); |
Michael Kruse | 7244852 | 2018-12-12 17:32:52 +0000 | [diff] [blame] | 891 | OptimizePM.addPass(WarnMissedTransformationsPass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 892 | OptimizePM.addPass(InstCombinePass()); |
Chandler Carruth | 0ede22e | 2017-02-09 23:54:57 +0000 | [diff] [blame] | 893 | OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 894 | OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass(), DebugLogging)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 895 | |
| 896 | // Now that we've vectorized and unrolled loops, we may have more refined |
| 897 | // alignment information, try to re-derive it here. |
| 898 | OptimizePM.addPass(AlignmentFromAssumptionsPass()); |
| 899 | |
Vedant Kumar | 47a0c9b | 2019-02-15 18:46:44 +0000 | [diff] [blame] | 900 | // Split out cold code. Splitting is done late to avoid hiding context from |
| 901 | // other optimizations and inadvertently regressing performance. The tradeoff |
| 902 | // is that this has a higher code size cost than splitting early. |
| 903 | if (EnableHotColdSplit && !LTOPreLink) |
| 904 | MPM.addPass(HotColdSplittingPass()); |
| 905 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 906 | // LoopSink pass sinks instructions hoisted by LICM, which serves as a |
| 907 | // canonicalization pass that enables other optimizations. As a result, |
| 908 | // LoopSink pass needs to be a very late IR pass to avoid undoing LICM |
| 909 | // result too early. |
| 910 | OptimizePM.addPass(LoopSinkPass()); |
| 911 | |
| 912 | // And finally clean up LCSSA form before generating code. |
Chandler Carruth | 7c557f8 | 2018-06-29 23:36:03 +0000 | [diff] [blame] | 913 | OptimizePM.addPass(InstSimplifyPass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 914 | |
Sanjay Patel | 6fd4391 | 2017-09-09 13:38:18 +0000 | [diff] [blame] | 915 | // This hoists/decomposes div/rem ops. It should run after other sink/hoist |
| 916 | // passes to avoid re-sinking, but before SimplifyCFG because it can allow |
| 917 | // flattening of blocks. |
| 918 | OptimizePM.addPass(DivRemPairsPass()); |
| 919 | |
Filipe Cabecinhas | 92dc348 | 2017-04-26 12:02:41 +0000 | [diff] [blame] | 920 | // LoopSink (and other loop passes since the last simplifyCFG) might have |
| 921 | // resulted in single-entry-single-exit or empty blocks. Clean up the CFG. |
| 922 | OptimizePM.addPass(SimplifyCFGPass()); |
| 923 | |
Chandler Carruth | c34f789 | 2017-11-28 11:32:31 +0000 | [diff] [blame] | 924 | // Optimize PHIs by speculating around them when profitable. Note that this |
| 925 | // pass needs to be run after any PRE or similar pass as it is essentially |
| 926 | // inserting redudnancies into the progrem. This even includes SimplifyCFG. |
| 927 | OptimizePM.addPass(SpeculateAroundPHIsPass()); |
| 928 | |
Philip Pfaffe | 2d4effb | 2018-11-12 11:17:07 +0000 | [diff] [blame] | 929 | for (auto &C : OptimizerLastEPCallbacks) |
| 930 | C(OptimizePM, Level); |
| 931 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 932 | // Add the core optimizing pipeline. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 933 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM))); |
| 934 | |
Michael J. Spencer | 7bb2767 | 2018-07-16 00:28:24 +0000 | [diff] [blame] | 935 | MPM.addPass(CGProfilePass()); |
| 936 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 937 | // Now we need to do some global optimization transforms. |
| 938 | // FIXME: It would seem like these should come first in the optimization |
| 939 | // pipeline and maybe be the bottom of the canonicalization pipeline? Weird |
| 940 | // ordering here. |
| 941 | MPM.addPass(GlobalDCEPass()); |
| 942 | MPM.addPass(ConstantMergePass()); |
| 943 | |
| 944 | return MPM; |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 945 | } |
| 946 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 947 | ModulePassManager |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 948 | PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, |
Vedant Kumar | 47a0c9b | 2019-02-15 18:46:44 +0000 | [diff] [blame] | 949 | bool DebugLogging, bool LTOPreLink) { |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 950 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
| 951 | |
| 952 | ModulePassManager MPM(DebugLogging); |
| 953 | |
| 954 | // Force any function attributes we want the rest of the pipeline to observe. |
| 955 | MPM.addPass(ForceFunctionAttrsPass()); |
| 956 | |
David Blaikie | 0c64f5a | 2018-01-23 01:25:20 +0000 | [diff] [blame] | 957 | // Apply module pipeline start EP callback. |
| 958 | for (auto &C : PipelineStartEPCallbacks) |
| 959 | C(MPM); |
| 960 | |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 961 | if (PGOOpt && PGOOpt->SamplePGOSupport) |
Dehao Chen | ce0842c | 2017-07-29 04:10:24 +0000 | [diff] [blame] | 962 | MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); |
| 963 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 964 | // Add the core simplification pipeline. |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 965 | MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::None, |
| 966 | DebugLogging)); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 967 | |
| 968 | // Now add the optimization pipeline. |
Vedant Kumar | 47a0c9b | 2019-02-15 18:46:44 +0000 | [diff] [blame] | 969 | MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging, LTOPreLink)); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 970 | |
| 971 | return MPM; |
| 972 | } |
| 973 | |
| 974 | ModulePassManager |
| 975 | PassBuilder::buildThinLTOPreLinkDefaultPipeline(OptimizationLevel Level, |
| 976 | bool DebugLogging) { |
| 977 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
| 978 | |
| 979 | ModulePassManager MPM(DebugLogging); |
| 980 | |
| 981 | // Force any function attributes we want the rest of the pipeline to observe. |
| 982 | MPM.addPass(ForceFunctionAttrsPass()); |
| 983 | |
Dehao Chen | 08f8831 | 2017-08-07 20:23:20 +0000 | [diff] [blame] | 984 | if (PGOOpt && PGOOpt->SamplePGOSupport) |
Dehao Chen | ce0842c | 2017-07-29 04:10:24 +0000 | [diff] [blame] | 985 | MPM.addPass(createModuleToFunctionPassAdaptor(AddDiscriminatorsPass())); |
| 986 | |
David Blaikie | 0c64f5a | 2018-01-23 01:25:20 +0000 | [diff] [blame] | 987 | // Apply module pipeline start EP callback. |
| 988 | for (auto &C : PipelineStartEPCallbacks) |
| 989 | C(MPM); |
| 990 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 991 | // If we are planning to perform ThinLTO later, we don't bloat the code with |
| 992 | // unrolling/vectorization/... now. Just simplify the module as much as we |
| 993 | // can. |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 994 | MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PreLink, |
| 995 | DebugLogging)); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 996 | |
| 997 | // Run partial inlining pass to partially inline functions that have |
| 998 | // large bodies. |
| 999 | // FIXME: It isn't clear whether this is really the right place to run this |
| 1000 | // in ThinLTO. Because there is another canonicalization and simplification |
| 1001 | // phase that will run after the thin link, running this here ends up with |
| 1002 | // less information than will be available later and it may grow functions in |
| 1003 | // ways that aren't beneficial. |
| 1004 | if (RunPartialInlining) |
| 1005 | MPM.addPass(PartialInlinerPass()); |
| 1006 | |
| 1007 | // Reduce the size of the IR as much as possible. |
| 1008 | MPM.addPass(GlobalOptPass()); |
| 1009 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1010 | return MPM; |
| 1011 | } |
| 1012 | |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1013 | ModulePassManager PassBuilder::buildThinLTODefaultPipeline( |
| 1014 | OptimizationLevel Level, bool DebugLogging, |
| 1015 | const ModuleSummaryIndex *ImportSummary) { |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1016 | ModulePassManager MPM(DebugLogging); |
| 1017 | |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1018 | if (ImportSummary) { |
| 1019 | // These passes import type identifier resolutions for whole-program |
| 1020 | // devirtualization and CFI. They must run early because other passes may |
| 1021 | // disturb the specific instruction patterns that these passes look for, |
| 1022 | // creating dependencies on resolutions that may not appear in the summary. |
| 1023 | // |
| 1024 | // For example, GVN may transform the pattern assume(type.test) appearing in |
| 1025 | // two basic blocks into assume(phi(type.test, type.test)), which would |
| 1026 | // transform a dependency on a WPD resolution into a dependency on a type |
| 1027 | // identifier resolution for CFI. |
| 1028 | // |
| 1029 | // Also, WPD has access to more precise information than ICP and can |
| 1030 | // devirtualize more effectively, so it should operate on the IR first. |
| 1031 | MPM.addPass(WholeProgramDevirtPass(nullptr, ImportSummary)); |
| 1032 | MPM.addPass(LowerTypeTestsPass(nullptr, ImportSummary)); |
| 1033 | } |
| 1034 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1035 | // Force any function attributes we want the rest of the pipeline to observe. |
| 1036 | MPM.addPass(ForceFunctionAttrsPass()); |
| 1037 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1038 | // Add the core simplification pipeline. |
Dehao Chen | 95f0030 | 2017-07-30 04:55:39 +0000 | [diff] [blame] | 1039 | MPM.addPass(buildModuleSimplificationPipeline(Level, ThinLTOPhase::PostLink, |
| 1040 | DebugLogging)); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1041 | |
| 1042 | // Now add the optimization pipeline. |
| 1043 | MPM.addPass(buildModuleOptimizationPipeline(Level, DebugLogging)); |
| 1044 | |
| 1045 | return MPM; |
| 1046 | } |
| 1047 | |
| 1048 | ModulePassManager |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1049 | PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level, |
| 1050 | bool DebugLogging) { |
| 1051 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1052 | // FIXME: We should use a customized pre-link pipeline! |
Vedant Kumar | 47a0c9b | 2019-02-15 18:46:44 +0000 | [diff] [blame] | 1053 | return buildPerModuleDefaultPipeline(Level, DebugLogging, |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 1054 | /* LTOPreLink */true); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1057 | ModulePassManager |
| 1058 | PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, bool DebugLogging, |
| 1059 | ModuleSummaryIndex *ExportSummary) { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1060 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
| 1061 | ModulePassManager MPM(DebugLogging); |
| 1062 | |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 1063 | if (PGOOpt && PGOOpt->Action == PGOOptions::SampleUse) { |
Xin Tong | 642c8d3 | 2018-11-15 18:06:42 +0000 | [diff] [blame] | 1064 | // Load sample profile before running the LTO optimization pipeline. |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 1065 | MPM.addPass(SampleProfileLoaderPass(PGOOpt->ProfileFile, |
Xin Tong | 642c8d3 | 2018-11-15 18:06:42 +0000 | [diff] [blame] | 1066 | PGOOpt->ProfileRemappingFile, |
| 1067 | false /* ThinLTOPhase::PreLink */)); |
| 1068 | } |
| 1069 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1070 | // Remove unused virtual tables to improve the quality of code generated by |
| 1071 | // whole-program devirtualization and bitset lowering. |
| 1072 | MPM.addPass(GlobalDCEPass()); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1073 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1074 | // Force any function attributes we want the rest of the pipeline to observe. |
| 1075 | MPM.addPass(ForceFunctionAttrsPass()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1076 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1077 | // Do basic inference of function attributes from known properties of system |
| 1078 | // libraries and other oracles. |
| 1079 | MPM.addPass(InferFunctionAttrsPass()); |
| 1080 | |
| 1081 | if (Level > 1) { |
Jun Bum Lim | 0c99007 | 2017-11-03 20:41:16 +0000 | [diff] [blame] | 1082 | FunctionPassManager EarlyFPM(DebugLogging); |
| 1083 | EarlyFPM.addPass(CallSiteSplittingPass()); |
| 1084 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); |
| 1085 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1086 | // Indirect call promotion. This should promote all the targets that are |
| 1087 | // left by the earlier promotion pass that promotes intra-module targets. |
| 1088 | // This two-step promotion is to save the compile time. For LTO, it should |
| 1089 | // produce the same result as if we only do promotion here. |
Dehao Chen | 2f31d0d | 2017-06-29 23:33:05 +0000 | [diff] [blame] | 1090 | MPM.addPass(PGOIndirectCallPromotion( |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 1091 | true /* InLTO */, PGOOpt && PGOOpt->Action == PGOOptions::SampleUse)); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1092 | // Propagate constants at call sites into the functions they call. This |
| 1093 | // opens opportunities for globalopt (and inlining) by substituting function |
| 1094 | // pointers passed as arguments to direct uses of functions. |
| 1095 | MPM.addPass(IPSCCPPass()); |
Matthew Simpson | cb58558 | 2017-10-25 13:40:08 +0000 | [diff] [blame] | 1096 | |
| 1097 | // Attach metadata to indirect call sites indicating the set of functions |
| 1098 | // they may target at run-time. This should follow IPSCCP. |
| 1099 | MPM.addPass(CalledValuePropagationPass()); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
| 1102 | // Now deduce any function attributes based in the current code. |
| 1103 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( |
| 1104 | PostOrderFunctionAttrsPass())); |
| 1105 | |
| 1106 | // Do RPO function attribute inference across the module to forward-propagate |
| 1107 | // attributes where applicable. |
| 1108 | // FIXME: Is this really an optimization rather than a canonicalization? |
| 1109 | MPM.addPass(ReversePostOrderFunctionAttrsPass()); |
| 1110 | |
Eric Christopher | 721eaef | 2019-02-26 20:33:22 +0000 | [diff] [blame] | 1111 | // Use in-range annotations on GEP indices to split globals where beneficial. |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1112 | MPM.addPass(GlobalSplitPass()); |
| 1113 | |
| 1114 | // Run whole program optimization of virtual call when the list of callees |
| 1115 | // is fixed. |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1116 | MPM.addPass(WholeProgramDevirtPass(ExportSummary, nullptr)); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1117 | |
| 1118 | // Stop here at -O1. |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1119 | if (Level == 1) { |
| 1120 | // The LowerTypeTestsPass needs to run to lower type metadata and the |
| 1121 | // type.test intrinsics. The pass does nothing if CFI is disabled. |
| 1122 | MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1123 | return MPM; |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1124 | } |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1125 | |
| 1126 | // Optimize globals to try and fold them into constants. |
| 1127 | MPM.addPass(GlobalOptPass()); |
| 1128 | |
| 1129 | // Promote any localized globals to SSA registers. |
| 1130 | MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); |
| 1131 | |
| 1132 | // Linking modules together can lead to duplicate global constant, only |
| 1133 | // keep one copy of each constant. |
| 1134 | MPM.addPass(ConstantMergePass()); |
| 1135 | |
| 1136 | // Remove unused arguments from functions. |
| 1137 | MPM.addPass(DeadArgumentEliminationPass()); |
| 1138 | |
| 1139 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 1140 | // simplification opportunities, and both can propagate functions through |
| 1141 | // function pointers. When this happens, we often have to resolve varargs |
| 1142 | // calls, etc, so let instcombine do this. |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1143 | FunctionPassManager PeepholeFPM(DebugLogging); |
Amjad Aboud | f1f57a3 | 2018-01-25 12:06:32 +0000 | [diff] [blame] | 1144 | if (Level == O3) |
| 1145 | PeepholeFPM.addPass(AggressiveInstCombinePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1146 | PeepholeFPM.addPass(InstCombinePass()); |
| 1147 | invokePeepholeEPCallbacks(PeepholeFPM, Level); |
| 1148 | |
| 1149 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(PeepholeFPM))); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1150 | |
| 1151 | // Note: historically, the PruneEH pass was run first to deduce nounwind and |
| 1152 | // generally clean up exception handling overhead. It isn't clear this is |
| 1153 | // valuable as the inliner doesn't currently care whether it is inlining an |
| 1154 | // invoke or a call. |
| 1155 | // Run the inliner now. |
Easwaran Raman | 8249fac | 2017-06-28 13:33:49 +0000 | [diff] [blame] | 1156 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( |
| 1157 | InlinerPass(getInlineParamsFromOptLevel(Level)))); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1158 | |
| 1159 | // Optimize globals again after we ran the inliner. |
| 1160 | MPM.addPass(GlobalOptPass()); |
| 1161 | |
| 1162 | // Garbage collect dead functions. |
| 1163 | // FIXME: Add ArgumentPromotion pass after once it's ported. |
| 1164 | MPM.addPass(GlobalDCEPass()); |
| 1165 | |
| 1166 | FunctionPassManager FPM(DebugLogging); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1167 | // The IPO Passes may leave cruft around. Clean up after them. |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1168 | FPM.addPass(InstCombinePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1169 | invokePeepholeEPCallbacks(FPM, Level); |
| 1170 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1171 | FPM.addPass(JumpThreadingPass()); |
| 1172 | |
Rong Xu | db29a3a | 2019-03-04 20:21:27 +0000 | [diff] [blame] | 1173 | // Do a post inline PGO instrumentation and use pass. This is a context |
| 1174 | // sensitive PGO pass. |
| 1175 | if (PGOOpt) { |
| 1176 | if (PGOOpt->CSAction == PGOOptions::CSIRInstr) |
| 1177 | addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ true, |
| 1178 | /* IsCS */ true, PGOOpt->CSProfileGenFile, |
| 1179 | PGOOpt->ProfileRemappingFile); |
| 1180 | else if (PGOOpt->CSAction == PGOOptions::CSIRUse) |
| 1181 | addPGOInstrPasses(MPM, DebugLogging, Level, /* RunProfileGen */ false, |
| 1182 | /* IsCS */ true, PGOOpt->ProfileFile, |
| 1183 | PGOOpt->ProfileRemappingFile); |
| 1184 | } |
| 1185 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1186 | // Break up allocas |
| 1187 | FPM.addPass(SROA()); |
| 1188 | |
Robert Lougher | f2158a8 | 2019-03-20 19:08:18 +0000 | [diff] [blame] | 1189 | // LTO provides additional opportunities for tailcall elimination due to |
| 1190 | // link-time inlining, and visibility of nocapture attribute. |
| 1191 | FPM.addPass(TailCallElimPass()); |
| 1192 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1193 | // Run a few AA driver optimizations here and now to cleanup the code. |
| 1194 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 1195 | |
| 1196 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( |
| 1197 | PostOrderFunctionAttrsPass())); |
| 1198 | // FIXME: here we run IP alias analysis in the legacy PM. |
| 1199 | |
| 1200 | FunctionPassManager MainFPM; |
| 1201 | |
| 1202 | // FIXME: once we fix LoopPass Manager, add LICM here. |
| 1203 | // FIXME: once we provide support for enabling MLSM, add it here. |
| 1204 | // FIXME: once we provide support for enabling NewGVN, add it here. |
Davide Italiano | 8a09b8e | 2017-05-22 23:41:40 +0000 | [diff] [blame] | 1205 | if (RunNewGVN) |
| 1206 | MainFPM.addPass(NewGVNPass()); |
| 1207 | else |
| 1208 | MainFPM.addPass(GVN()); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1209 | |
| 1210 | // Remove dead memcpy()'s. |
| 1211 | MainFPM.addPass(MemCpyOptPass()); |
| 1212 | |
| 1213 | // Nuke dead stores. |
| 1214 | MainFPM.addPass(DSEPass()); |
| 1215 | |
| 1216 | // FIXME: at this point, we run a bunch of loop passes: |
| 1217 | // indVarSimplify, loopDeletion, loopInterchange, loopUnrool, |
| 1218 | // loopVectorize. Enable them once the remaining issue with LPM |
| 1219 | // are sorted out. |
| 1220 | |
| 1221 | MainFPM.addPass(InstCombinePass()); |
| 1222 | MainFPM.addPass(SimplifyCFGPass()); |
| 1223 | MainFPM.addPass(SCCPPass()); |
| 1224 | MainFPM.addPass(InstCombinePass()); |
| 1225 | MainFPM.addPass(BDCEPass()); |
| 1226 | |
| 1227 | // FIXME: We may want to run SLPVectorizer here. |
| 1228 | // After vectorization, assume intrinsics may tell us more |
| 1229 | // about pointer alignments. |
| 1230 | #if 0 |
| 1231 | MainFPM.add(AlignmentFromAssumptionsPass()); |
| 1232 | #endif |
| 1233 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1234 | // FIXME: Conditionally run LoadCombine here, after it's ported |
| 1235 | // (in case we still have this pass, given its questionable usefulness). |
| 1236 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1237 | MainFPM.addPass(InstCombinePass()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1238 | invokePeepholeEPCallbacks(MainFPM, Level); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1239 | MainFPM.addPass(JumpThreadingPass()); |
| 1240 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM))); |
| 1241 | |
| 1242 | // Create a function that performs CFI checks for cross-DSO calls with |
| 1243 | // targets in the current module. |
| 1244 | MPM.addPass(CrossDSOCFIPass()); |
| 1245 | |
| 1246 | // Lower type metadata and the type.test intrinsic. This pass supports |
| 1247 | // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs |
| 1248 | // to be run at link time if CFI is enabled. This pass does nothing if |
| 1249 | // CFI is disabled. |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1250 | MPM.addPass(LowerTypeTestsPass(ExportSummary, nullptr)); |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1251 | |
Vedant Kumar | 47a0c9b | 2019-02-15 18:46:44 +0000 | [diff] [blame] | 1252 | // Enable splitting late in the FullLTO post-link pipeline. This is done in |
| 1253 | // the same stage in the old pass manager (\ref addLateLTOOptimizationPasses). |
| 1254 | if (EnableHotColdSplit) |
| 1255 | MPM.addPass(HotColdSplittingPass()); |
| 1256 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 1257 | // Add late LTO optimization passes. |
| 1258 | // Delete basic blocks, which optimization passes may have killed. |
| 1259 | MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass())); |
| 1260 | |
| 1261 | // Drop bodies of available eternally objects to improve GlobalDCE. |
| 1262 | MPM.addPass(EliminateAvailableExternallyPass()); |
| 1263 | |
| 1264 | // Now that we have optimized the program, discard unreachable functions. |
| 1265 | MPM.addPass(GlobalDCEPass()); |
| 1266 | |
| 1267 | // FIXME: Enable MergeFuncs, conditionally, after ported, maybe. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1268 | return MPM; |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1269 | } |
| 1270 | |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 1271 | AAManager PassBuilder::buildDefaultAAPipeline() { |
| 1272 | AAManager AA; |
| 1273 | |
| 1274 | // The order in which these are registered determines their priority when |
| 1275 | // being queried. |
| 1276 | |
| 1277 | // First we register the basic alias analysis that provides the majority of |
| 1278 | // per-function local AA logic. This is a stateless, on-demand local set of |
| 1279 | // AA techniques. |
| 1280 | AA.registerFunctionAnalysis<BasicAA>(); |
| 1281 | |
| 1282 | // Next we query fast, specialized alias analyses that wrap IR-embedded |
| 1283 | // information about aliasing. |
| 1284 | AA.registerFunctionAnalysis<ScopedNoAliasAA>(); |
| 1285 | AA.registerFunctionAnalysis<TypeBasedAA>(); |
| 1286 | |
| 1287 | // Add support for querying global aliasing information when available. |
Chandler Carruth | 534d644 | 2016-12-24 05:11:17 +0000 | [diff] [blame] | 1288 | // Because the `AAManager` is a function analysis and `GlobalsAA` is a module |
| 1289 | // analysis, all that the `AAManager` can do is query for any *cached* |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame] | 1290 | // results from `GlobalsAA` through a readonly proxy. |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 1291 | AA.registerModuleAnalysis<GlobalsAA>(); |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 1292 | |
| 1293 | return AA; |
| 1294 | } |
| 1295 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1296 | static Optional<int> parseRepeatPassName(StringRef Name) { |
| 1297 | if (!Name.consume_front("repeat<") || !Name.consume_back(">")) |
| 1298 | return None; |
| 1299 | int Count; |
| 1300 | if (Name.getAsInteger(0, Count) || Count <= 0) |
| 1301 | return None; |
| 1302 | return Count; |
| 1303 | } |
| 1304 | |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 1305 | static Optional<int> parseDevirtPassName(StringRef Name) { |
| 1306 | if (!Name.consume_front("devirt<") || !Name.consume_back(">")) |
| 1307 | return None; |
| 1308 | int Count; |
| 1309 | if (Name.getAsInteger(0, Count) || Count <= 0) |
| 1310 | return None; |
| 1311 | return Count; |
| 1312 | } |
| 1313 | |
Fedor Sergeev | b787140 | 2019-01-10 10:01:53 +0000 | [diff] [blame] | 1314 | static bool checkParametrizedPassName(StringRef Name, StringRef PassName) { |
| 1315 | if (!Name.consume_front(PassName)) |
| 1316 | return false; |
| 1317 | // normal pass name w/o parameters == default parameters |
| 1318 | if (Name.empty()) |
| 1319 | return true; |
| 1320 | return Name.startswith("<") && Name.endswith(">"); |
| 1321 | } |
| 1322 | |
| 1323 | namespace { |
| 1324 | |
| 1325 | /// This performs customized parsing of pass name with parameters. |
| 1326 | /// |
| 1327 | /// We do not need parametrization of passes in textual pipeline very often, |
| 1328 | /// yet on a rare occasion ability to specify parameters right there can be |
| 1329 | /// useful. |
| 1330 | /// |
| 1331 | /// \p Name - parameterized specification of a pass from a textual pipeline |
| 1332 | /// is a string in a form of : |
| 1333 | /// PassName '<' parameter-list '>' |
| 1334 | /// |
| 1335 | /// Parameter list is being parsed by the parser callable argument, \p Parser, |
| 1336 | /// It takes a string-ref of parameters and returns either StringError or a |
| 1337 | /// parameter list in a form of a custom parameters type, all wrapped into |
| 1338 | /// Expected<> template class. |
| 1339 | /// |
| 1340 | template <typename ParametersParseCallableT> |
| 1341 | auto parsePassParameters(ParametersParseCallableT &&Parser, StringRef Name, |
| 1342 | StringRef PassName) -> decltype(Parser(StringRef{})) { |
| 1343 | using ParametersT = typename decltype(Parser(StringRef{}))::value_type; |
| 1344 | |
| 1345 | StringRef Params = Name; |
| 1346 | if (!Params.consume_front(PassName)) { |
| 1347 | assert(false && |
| 1348 | "unable to strip pass name from parametrized pass specification"); |
| 1349 | } |
| 1350 | if (Params.empty()) |
| 1351 | return ParametersT{}; |
| 1352 | if (!Params.consume_front("<") || !Params.consume_back(">")) { |
| 1353 | assert(false && "invalid format for parametrized pass name"); |
| 1354 | } |
| 1355 | |
| 1356 | Expected<ParametersT> Result = Parser(Params); |
| 1357 | assert((Result || Result.template errorIsA<StringError>()) && |
| 1358 | "Pass parameter parser can only return StringErrors."); |
| 1359 | return std::move(Result); |
| 1360 | } |
| 1361 | |
| 1362 | /// Parser of parameters for LoopUnroll pass. |
| 1363 | Expected<LoopUnrollOptions> parseLoopUnrollOptions(StringRef Params) { |
| 1364 | LoopUnrollOptions UnrollOpts; |
| 1365 | while (!Params.empty()) { |
| 1366 | StringRef ParamName; |
| 1367 | std::tie(ParamName, Params) = Params.split(';'); |
| 1368 | int OptLevel = StringSwitch<int>(ParamName) |
| 1369 | .Case("O0", 0) |
| 1370 | .Case("O1", 1) |
| 1371 | .Case("O2", 2) |
| 1372 | .Case("O3", 3) |
| 1373 | .Default(-1); |
| 1374 | if (OptLevel >= 0) { |
| 1375 | UnrollOpts.setOptLevel(OptLevel); |
| 1376 | continue; |
| 1377 | } |
| 1378 | |
| 1379 | bool Enable = !ParamName.consume_front("no-"); |
| 1380 | if (ParamName == "partial") { |
| 1381 | UnrollOpts.setPartial(Enable); |
| 1382 | } else if (ParamName == "peeling") { |
| 1383 | UnrollOpts.setPeeling(Enable); |
| 1384 | } else if (ParamName == "runtime") { |
| 1385 | UnrollOpts.setRuntime(Enable); |
| 1386 | } else if (ParamName == "upperbound") { |
| 1387 | UnrollOpts.setUpperBound(Enable); |
| 1388 | } else { |
| 1389 | return make_error<StringError>( |
| 1390 | formatv("invalid LoopUnrollPass parameter '{0}' ", ParamName).str(), |
| 1391 | inconvertibleErrorCode()); |
| 1392 | } |
| 1393 | } |
| 1394 | return UnrollOpts; |
| 1395 | } |
| 1396 | |
Philip Pfaffe | 0ee6a93 | 2019-02-04 21:02:49 +0000 | [diff] [blame] | 1397 | Expected<MemorySanitizerOptions> parseMSanPassOptions(StringRef Params) { |
| 1398 | MemorySanitizerOptions Result; |
| 1399 | while (!Params.empty()) { |
| 1400 | StringRef ParamName; |
| 1401 | std::tie(ParamName, Params) = Params.split(';'); |
| 1402 | |
| 1403 | if (ParamName == "recover") { |
| 1404 | Result.Recover = true; |
| 1405 | } else if (ParamName == "kernel") { |
| 1406 | Result.Kernel = true; |
| 1407 | } else if (ParamName.consume_front("track-origins=")) { |
| 1408 | if (ParamName.getAsInteger(0, Result.TrackOrigins)) |
| 1409 | return make_error<StringError>( |
| 1410 | formatv("invalid argument to MemorySanitizer pass track-origins " |
| 1411 | "parameter: '{0}' ", |
| 1412 | ParamName) |
| 1413 | .str(), |
| 1414 | inconvertibleErrorCode()); |
| 1415 | } else { |
| 1416 | return make_error<StringError>( |
| 1417 | formatv("invalid MemorySanitizer pass parameter '{0}' ", ParamName) |
| 1418 | .str(), |
| 1419 | inconvertibleErrorCode()); |
| 1420 | } |
| 1421 | } |
| 1422 | return Result; |
| 1423 | } |
| 1424 | |
Fedor Sergeev | b787140 | 2019-01-10 10:01:53 +0000 | [diff] [blame] | 1425 | } // namespace |
| 1426 | |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1427 | /// Tests whether a pass name starts with a valid prefix for a default pipeline |
| 1428 | /// alias. |
| 1429 | static bool startsWithDefaultPipelineAliasPrefix(StringRef Name) { |
| 1430 | return Name.startswith("default") || Name.startswith("thinlto") || |
| 1431 | Name.startswith("lto"); |
| 1432 | } |
| 1433 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1434 | /// Tests whether registered callbacks will accept a given pass name. |
| 1435 | /// |
| 1436 | /// When parsing a pipeline text, the type of the outermost pipeline may be |
| 1437 | /// omitted, in which case the type is automatically determined from the first |
| 1438 | /// pass name in the text. This may be a name that is handled through one of the |
| 1439 | /// callbacks. We check this through the oridinary parsing callbacks by setting |
| 1440 | /// up a dummy PassManager in order to not force the client to also handle this |
| 1441 | /// type of query. |
| 1442 | template <typename PassManagerT, typename CallbacksT> |
| 1443 | static bool callbacksAcceptPassName(StringRef Name, CallbacksT &Callbacks) { |
| 1444 | if (!Callbacks.empty()) { |
| 1445 | PassManagerT DummyPM; |
| 1446 | for (auto &CB : Callbacks) |
| 1447 | if (CB(Name, DummyPM, {})) |
| 1448 | return true; |
| 1449 | } |
| 1450 | return false; |
| 1451 | } |
| 1452 | |
| 1453 | template <typename CallbacksT> |
| 1454 | static bool isModulePassName(StringRef Name, CallbacksT &Callbacks) { |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1455 | // Manually handle aliases for pre-configured pipeline fragments. |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1456 | if (startsWithDefaultPipelineAliasPrefix(Name)) |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1457 | return DefaultAliasRegex.match(Name); |
| 1458 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1459 | // Explicitly handle pass manager names. |
| 1460 | if (Name == "module") |
| 1461 | return true; |
| 1462 | if (Name == "cgscc") |
| 1463 | return true; |
| 1464 | if (Name == "function") |
| 1465 | return true; |
| 1466 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1467 | // Explicitly handle custom-parsed pass names. |
| 1468 | if (parseRepeatPassName(Name)) |
| 1469 | return true; |
| 1470 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 1471 | #define MODULE_PASS(NAME, CREATE_PASS) \ |
| 1472 | if (Name == NAME) \ |
| 1473 | return true; |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1474 | #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1475 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1476 | return true; |
| 1477 | #include "PassRegistry.def" |
| 1478 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1479 | return callbacksAcceptPassName<ModulePassManager>(Name, Callbacks); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1482 | template <typename CallbacksT> |
| 1483 | static bool isCGSCCPassName(StringRef Name, CallbacksT &Callbacks) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1484 | // Explicitly handle pass manager names. |
| 1485 | if (Name == "cgscc") |
| 1486 | return true; |
| 1487 | if (Name == "function") |
| 1488 | return true; |
| 1489 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1490 | // Explicitly handle custom-parsed pass names. |
| 1491 | if (parseRepeatPassName(Name)) |
| 1492 | return true; |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 1493 | if (parseDevirtPassName(Name)) |
| 1494 | return true; |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1495 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 1496 | #define CGSCC_PASS(NAME, CREATE_PASS) \ |
| 1497 | if (Name == NAME) \ |
| 1498 | return true; |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1499 | #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1500 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1501 | return true; |
| 1502 | #include "PassRegistry.def" |
| 1503 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1504 | return callbacksAcceptPassName<CGSCCPassManager>(Name, Callbacks); |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1507 | template <typename CallbacksT> |
| 1508 | static bool isFunctionPassName(StringRef Name, CallbacksT &Callbacks) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1509 | // Explicitly handle pass manager names. |
| 1510 | if (Name == "function") |
| 1511 | return true; |
| 1512 | if (Name == "loop") |
| 1513 | return true; |
| 1514 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1515 | // Explicitly handle custom-parsed pass names. |
| 1516 | if (parseRepeatPassName(Name)) |
| 1517 | return true; |
| 1518 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 1519 | #define FUNCTION_PASS(NAME, CREATE_PASS) \ |
| 1520 | if (Name == NAME) \ |
| 1521 | return true; |
Fedor Sergeev | b787140 | 2019-01-10 10:01:53 +0000 | [diff] [blame] | 1522 | #define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \ |
| 1523 | if (checkParametrizedPassName(Name, NAME)) \ |
| 1524 | return true; |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1525 | #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1526 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1527 | return true; |
| 1528 | #include "PassRegistry.def" |
| 1529 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1530 | return callbacksAcceptPassName<FunctionPassManager>(Name, Callbacks); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1533 | template <typename CallbacksT> |
| 1534 | static bool isLoopPassName(StringRef Name, CallbacksT &Callbacks) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1535 | // Explicitly handle pass manager names. |
| 1536 | if (Name == "loop") |
| 1537 | return true; |
| 1538 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1539 | // Explicitly handle custom-parsed pass names. |
| 1540 | if (parseRepeatPassName(Name)) |
| 1541 | return true; |
| 1542 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 1543 | #define LOOP_PASS(NAME, CREATE_PASS) \ |
| 1544 | if (Name == NAME) \ |
| 1545 | return true; |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1546 | #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ |
| 1547 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
| 1548 | return true; |
| 1549 | #include "PassRegistry.def" |
| 1550 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1551 | return callbacksAcceptPassName<LoopPassManager>(Name, Callbacks); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1552 | } |
| 1553 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1554 | Optional<std::vector<PassBuilder::PipelineElement>> |
| 1555 | PassBuilder::parsePipelineText(StringRef Text) { |
| 1556 | std::vector<PipelineElement> ResultPipeline; |
| 1557 | |
| 1558 | SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = { |
| 1559 | &ResultPipeline}; |
| 1560 | for (;;) { |
| 1561 | std::vector<PipelineElement> &Pipeline = *PipelineStack.back(); |
| 1562 | size_t Pos = Text.find_first_of(",()"); |
| 1563 | Pipeline.push_back({Text.substr(0, Pos), {}}); |
| 1564 | |
| 1565 | // If we have a single terminating name, we're done. |
| 1566 | if (Pos == Text.npos) |
| 1567 | break; |
| 1568 | |
| 1569 | char Sep = Text[Pos]; |
| 1570 | Text = Text.substr(Pos + 1); |
| 1571 | if (Sep == ',') |
| 1572 | // Just a name ending in a comma, continue. |
| 1573 | continue; |
| 1574 | |
| 1575 | if (Sep == '(') { |
| 1576 | // Push the inner pipeline onto the stack to continue processing. |
| 1577 | PipelineStack.push_back(&Pipeline.back().InnerPipeline); |
| 1578 | continue; |
| 1579 | } |
| 1580 | |
| 1581 | assert(Sep == ')' && "Bogus separator!"); |
| 1582 | // When handling the close parenthesis, we greedily consume them to avoid |
| 1583 | // empty strings in the pipeline. |
| 1584 | do { |
| 1585 | // If we try to pop the outer pipeline we have unbalanced parentheses. |
| 1586 | if (PipelineStack.size() == 1) |
| 1587 | return None; |
| 1588 | |
| 1589 | PipelineStack.pop_back(); |
| 1590 | } while (Text.consume_front(")")); |
| 1591 | |
| 1592 | // Check if we've finished parsing. |
| 1593 | if (Text.empty()) |
| 1594 | break; |
| 1595 | |
| 1596 | // Otherwise, the end of an inner pipeline always has to be followed by |
| 1597 | // a comma, and then we can continue. |
| 1598 | if (!Text.consume_front(",")) |
| 1599 | return None; |
| 1600 | } |
| 1601 | |
| 1602 | if (PipelineStack.size() > 1) |
| 1603 | // Unbalanced paretheses. |
| 1604 | return None; |
| 1605 | |
| 1606 | assert(PipelineStack.back() == &ResultPipeline && |
| 1607 | "Wrong pipeline at the bottom of the stack!"); |
| 1608 | return {std::move(ResultPipeline)}; |
| 1609 | } |
| 1610 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1611 | Error PassBuilder::parseModulePass(ModulePassManager &MPM, |
| 1612 | const PipelineElement &E, |
| 1613 | bool VerifyEachPass, bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1614 | auto &Name = E.Name; |
| 1615 | auto &InnerPipeline = E.InnerPipeline; |
| 1616 | |
| 1617 | // First handle complex passes like the pass managers which carry pipelines. |
| 1618 | if (!InnerPipeline.empty()) { |
| 1619 | if (Name == "module") { |
| 1620 | ModulePassManager NestedMPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1621 | if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline, |
| 1622 | VerifyEachPass, DebugLogging)) |
| 1623 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1624 | MPM.addPass(std::move(NestedMPM)); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1625 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1626 | } |
| 1627 | if (Name == "cgscc") { |
| 1628 | CGSCCPassManager CGPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1629 | if (auto Err = parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass, |
| 1630 | DebugLogging)) |
| 1631 | return Err; |
Chandler Carruth | 19913b2 | 2017-08-11 05:47:13 +0000 | [diff] [blame] | 1632 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1633 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1634 | } |
| 1635 | if (Name == "function") { |
| 1636 | FunctionPassManager FPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1637 | if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline, |
| 1638 | VerifyEachPass, DebugLogging)) |
| 1639 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1640 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1641 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1642 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1643 | if (auto Count = parseRepeatPassName(Name)) { |
| 1644 | ModulePassManager NestedMPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1645 | if (auto Err = parseModulePassPipeline(NestedMPM, InnerPipeline, |
| 1646 | VerifyEachPass, DebugLogging)) |
| 1647 | return Err; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1648 | MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1649 | return Error::success(); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1650 | } |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1651 | |
| 1652 | for (auto &C : ModulePipelineParsingCallbacks) |
| 1653 | if (C(Name, MPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1654 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1655 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1656 | // Normal passes can't have pipelines. |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1657 | return make_error<StringError>( |
| 1658 | formatv("invalid use of '{0}' pass as module pipeline", Name).str(), |
| 1659 | inconvertibleErrorCode()); |
| 1660 | ; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1661 | } |
| 1662 | |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1663 | // Manually handle aliases for pre-configured pipeline fragments. |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1664 | if (startsWithDefaultPipelineAliasPrefix(Name)) { |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1665 | SmallVector<StringRef, 3> Matches; |
| 1666 | if (!DefaultAliasRegex.match(Name, &Matches)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1667 | return make_error<StringError>( |
| 1668 | formatv("unknown default pipeline alias '{0}'", Name).str(), |
| 1669 | inconvertibleErrorCode()); |
| 1670 | |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1671 | assert(Matches.size() == 3 && "Must capture two matched strings!"); |
| 1672 | |
Jordan Rose | f85a95f | 2016-07-25 18:34:51 +0000 | [diff] [blame] | 1673 | OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2]) |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1674 | .Case("O0", O0) |
| 1675 | .Case("O1", O1) |
| 1676 | .Case("O2", O2) |
| 1677 | .Case("O3", O3) |
| 1678 | .Case("Os", Os) |
| 1679 | .Case("Oz", Oz); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1680 | if (L == O0) |
| 1681 | // At O0 we do nothing at all! |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1682 | return Error::success(); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1683 | |
| 1684 | if (Matches[1] == "default") { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1685 | MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging)); |
Chandler Carruth | 8b3be4e | 2017-06-01 11:39:39 +0000 | [diff] [blame] | 1686 | } else if (Matches[1] == "thinlto-pre-link") { |
| 1687 | MPM.addPass(buildThinLTOPreLinkDefaultPipeline(L, DebugLogging)); |
| 1688 | } else if (Matches[1] == "thinlto") { |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1689 | MPM.addPass(buildThinLTODefaultPipeline(L, DebugLogging, nullptr)); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1690 | } else if (Matches[1] == "lto-pre-link") { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1691 | MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging)); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1692 | } else { |
| 1693 | assert(Matches[1] == "lto" && "Not one of the matched options!"); |
Teresa Johnson | 28023db | 2018-07-19 14:51:32 +0000 | [diff] [blame] | 1694 | MPM.addPass(buildLTODefaultPipeline(L, DebugLogging, nullptr)); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1695 | } |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1696 | return Error::success(); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1699 | // Finally expand the basic registered passes from the .inc file. |
Chandler Carruth | 5894418 | 2014-04-21 08:08:50 +0000 | [diff] [blame] | 1700 | #define MODULE_PASS(NAME, CREATE_PASS) \ |
| 1701 | if (Name == NAME) { \ |
| 1702 | MPM.addPass(CREATE_PASS); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1703 | return Error::success(); \ |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 1704 | } |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1705 | #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ |
| 1706 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 1707 | MPM.addPass( \ |
| 1708 | RequireAnalysisPass< \ |
| 1709 | std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1710 | return Error::success(); \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1711 | } \ |
| 1712 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1713 | MPM.addPass(InvalidateAnalysisPass< \ |
| 1714 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1715 | return Error::success(); \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1716 | } |
| 1717 | #include "PassRegistry.def" |
| 1718 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1719 | for (auto &C : ModulePipelineParsingCallbacks) |
| 1720 | if (C(Name, MPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1721 | return Error::success(); |
| 1722 | return make_error<StringError>( |
| 1723 | formatv("unknown module pass '{0}'", Name).str(), |
| 1724 | inconvertibleErrorCode()); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1727 | Error PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM, |
| 1728 | const PipelineElement &E, bool VerifyEachPass, |
| 1729 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1730 | auto &Name = E.Name; |
| 1731 | auto &InnerPipeline = E.InnerPipeline; |
| 1732 | |
| 1733 | // First handle complex passes like the pass managers which carry pipelines. |
| 1734 | if (!InnerPipeline.empty()) { |
| 1735 | if (Name == "cgscc") { |
| 1736 | CGSCCPassManager NestedCGPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1737 | if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, |
| 1738 | VerifyEachPass, DebugLogging)) |
| 1739 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1740 | // Add the nested pass manager with the appropriate adaptor. |
| 1741 | CGPM.addPass(std::move(NestedCGPM)); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1742 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1743 | } |
| 1744 | if (Name == "function") { |
| 1745 | FunctionPassManager FPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1746 | if (auto Err = parseFunctionPassPipeline(FPM, InnerPipeline, |
| 1747 | VerifyEachPass, DebugLogging)) |
| 1748 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1749 | // Add the nested pass manager with the appropriate adaptor. |
Chandler Carruth | 19913b2 | 2017-08-11 05:47:13 +0000 | [diff] [blame] | 1750 | CGPM.addPass(createCGSCCToFunctionPassAdaptor(std::move(FPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1751 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1752 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1753 | if (auto Count = parseRepeatPassName(Name)) { |
| 1754 | CGSCCPassManager NestedCGPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1755 | if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, |
| 1756 | VerifyEachPass, DebugLogging)) |
| 1757 | return Err; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1758 | CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1759 | return Error::success(); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1760 | } |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 1761 | if (auto MaxRepetitions = parseDevirtPassName(Name)) { |
| 1762 | CGSCCPassManager NestedCGPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1763 | if (auto Err = parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, |
| 1764 | VerifyEachPass, DebugLogging)) |
| 1765 | return Err; |
Chandler Carruth | 19913b2 | 2017-08-11 05:47:13 +0000 | [diff] [blame] | 1766 | CGPM.addPass( |
| 1767 | createDevirtSCCRepeatedPass(std::move(NestedCGPM), *MaxRepetitions)); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1768 | return Error::success(); |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 1769 | } |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1770 | |
| 1771 | for (auto &C : CGSCCPipelineParsingCallbacks) |
| 1772 | if (C(Name, CGPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1773 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1774 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1775 | // Normal passes can't have pipelines. |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1776 | return make_error<StringError>( |
| 1777 | formatv("invalid use of '{0}' pass as cgscc pipeline", Name).str(), |
| 1778 | inconvertibleErrorCode()); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1781 | // Now expand the basic registered passes from the .inc file. |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1782 | #define CGSCC_PASS(NAME, CREATE_PASS) \ |
| 1783 | if (Name == NAME) { \ |
| 1784 | CGPM.addPass(CREATE_PASS); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1785 | return Error::success(); \ |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1786 | } |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1787 | #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ |
| 1788 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1789 | CGPM.addPass(RequireAnalysisPass< \ |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 1790 | std::remove_reference<decltype(CREATE_PASS)>::type, \ |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 1791 | LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \ |
| 1792 | CGSCCUpdateResult &>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1793 | return Error::success(); \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1794 | } \ |
| 1795 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1796 | CGPM.addPass(InvalidateAnalysisPass< \ |
| 1797 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1798 | return Error::success(); \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1799 | } |
| 1800 | #include "PassRegistry.def" |
| 1801 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1802 | for (auto &C : CGSCCPipelineParsingCallbacks) |
| 1803 | if (C(Name, CGPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1804 | return Error::success(); |
| 1805 | return make_error<StringError>( |
| 1806 | formatv("unknown cgscc pass '{0}'", Name).str(), |
| 1807 | inconvertibleErrorCode()); |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1808 | } |
| 1809 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1810 | Error PassBuilder::parseFunctionPass(FunctionPassManager &FPM, |
| 1811 | const PipelineElement &E, |
| 1812 | bool VerifyEachPass, bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1813 | auto &Name = E.Name; |
| 1814 | auto &InnerPipeline = E.InnerPipeline; |
| 1815 | |
| 1816 | // First handle complex passes like the pass managers which carry pipelines. |
| 1817 | if (!InnerPipeline.empty()) { |
| 1818 | if (Name == "function") { |
| 1819 | FunctionPassManager NestedFPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1820 | if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline, |
| 1821 | VerifyEachPass, DebugLogging)) |
| 1822 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1823 | // Add the nested pass manager with the appropriate adaptor. |
| 1824 | FPM.addPass(std::move(NestedFPM)); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1825 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1826 | } |
| 1827 | if (Name == "loop") { |
| 1828 | LoopPassManager LPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1829 | if (auto Err = parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass, |
| 1830 | DebugLogging)) |
| 1831 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1832 | // Add the nested pass manager with the appropriate adaptor. |
Fedor Sergeev | 02e7f02 | 2017-12-29 08:16:06 +0000 | [diff] [blame] | 1833 | FPM.addPass( |
| 1834 | createFunctionToLoopPassAdaptor(std::move(LPM), DebugLogging)); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1835 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1836 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1837 | if (auto Count = parseRepeatPassName(Name)) { |
| 1838 | FunctionPassManager NestedFPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1839 | if (auto Err = parseFunctionPassPipeline(NestedFPM, InnerPipeline, |
| 1840 | VerifyEachPass, DebugLogging)) |
| 1841 | return Err; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1842 | FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1843 | return Error::success(); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1844 | } |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1845 | |
| 1846 | for (auto &C : FunctionPipelineParsingCallbacks) |
| 1847 | if (C(Name, FPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1848 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1849 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1850 | // Normal passes can't have pipelines. |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1851 | return make_error<StringError>( |
| 1852 | formatv("invalid use of '{0}' pass as function pipeline", Name).str(), |
| 1853 | inconvertibleErrorCode()); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1856 | // Now expand the basic registered passes from the .inc file. |
Chandler Carruth | 5894418 | 2014-04-21 08:08:50 +0000 | [diff] [blame] | 1857 | #define FUNCTION_PASS(NAME, CREATE_PASS) \ |
| 1858 | if (Name == NAME) { \ |
| 1859 | FPM.addPass(CREATE_PASS); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1860 | return Error::success(); \ |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 1861 | } |
Fedor Sergeev | b787140 | 2019-01-10 10:01:53 +0000 | [diff] [blame] | 1862 | #define FUNCTION_PASS_WITH_PARAMS(NAME, CREATE_PASS, PARSER) \ |
| 1863 | if (checkParametrizedPassName(Name, NAME)) { \ |
| 1864 | auto Params = parsePassParameters(PARSER, Name, NAME); \ |
| 1865 | if (!Params) \ |
| 1866 | return Params.takeError(); \ |
| 1867 | FPM.addPass(CREATE_PASS(Params.get())); \ |
| 1868 | return Error::success(); \ |
| 1869 | } |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1870 | #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ |
| 1871 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 1872 | FPM.addPass( \ |
| 1873 | RequireAnalysisPass< \ |
| 1874 | std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1875 | return Error::success(); \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1876 | } \ |
| 1877 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1878 | FPM.addPass(InvalidateAnalysisPass< \ |
| 1879 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1880 | return Error::success(); \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1881 | } |
| 1882 | #include "PassRegistry.def" |
| 1883 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1884 | for (auto &C : FunctionPipelineParsingCallbacks) |
| 1885 | if (C(Name, FPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1886 | return Error::success(); |
| 1887 | return make_error<StringError>( |
| 1888 | formatv("unknown function pass '{0}'", Name).str(), |
| 1889 | inconvertibleErrorCode()); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1892 | Error PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E, |
| 1893 | bool VerifyEachPass, bool DebugLogging) { |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1894 | StringRef Name = E.Name; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1895 | auto &InnerPipeline = E.InnerPipeline; |
| 1896 | |
| 1897 | // First handle complex passes like the pass managers which carry pipelines. |
| 1898 | if (!InnerPipeline.empty()) { |
| 1899 | if (Name == "loop") { |
| 1900 | LoopPassManager NestedLPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1901 | if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline, |
| 1902 | VerifyEachPass, DebugLogging)) |
| 1903 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1904 | // Add the nested pass manager with the appropriate adaptor. |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1905 | LPM.addPass(std::move(NestedLPM)); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1906 | return Error::success(); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1907 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1908 | if (auto Count = parseRepeatPassName(Name)) { |
| 1909 | LoopPassManager NestedLPM(DebugLogging); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1910 | if (auto Err = parseLoopPassPipeline(NestedLPM, InnerPipeline, |
| 1911 | VerifyEachPass, DebugLogging)) |
| 1912 | return Err; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1913 | LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM))); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1914 | return Error::success(); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1915 | } |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1916 | |
| 1917 | for (auto &C : LoopPipelineParsingCallbacks) |
| 1918 | if (C(Name, LPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1919 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1920 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1921 | // Normal passes can't have pipelines. |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1922 | return make_error<StringError>( |
| 1923 | formatv("invalid use of '{0}' pass as loop pipeline", Name).str(), |
| 1924 | inconvertibleErrorCode()); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1927 | // Now expand the basic registered passes from the .inc file. |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1928 | #define LOOP_PASS(NAME, CREATE_PASS) \ |
| 1929 | if (Name == NAME) { \ |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1930 | LPM.addPass(CREATE_PASS); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1931 | return Error::success(); \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1932 | } |
| 1933 | #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ |
| 1934 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1935 | LPM.addPass(RequireAnalysisPass< \ |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 1936 | std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \ |
| 1937 | LoopAnalysisManager, LoopStandardAnalysisResults &, \ |
| 1938 | LPMUpdater &>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1939 | return Error::success(); \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1940 | } \ |
| 1941 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1942 | LPM.addPass(InvalidateAnalysisPass< \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1943 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1944 | return Error::success(); \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1945 | } |
| 1946 | #include "PassRegistry.def" |
| 1947 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1948 | for (auto &C : LoopPipelineParsingCallbacks) |
| 1949 | if (C(Name, LPM, InnerPipeline)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1950 | return Error::success(); |
| 1951 | return make_error<StringError>(formatv("unknown loop pass '{0}'", Name).str(), |
| 1952 | inconvertibleErrorCode()); |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1953 | } |
| 1954 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1955 | bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) { |
Chandler Carruth | 45a9c20 | 2016-03-11 09:15:11 +0000 | [diff] [blame] | 1956 | #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ |
| 1957 | if (Name == NAME) { \ |
| 1958 | AA.registerModuleAnalysis< \ |
| 1959 | std::remove_reference<decltype(CREATE_PASS)>::type>(); \ |
| 1960 | return true; \ |
| 1961 | } |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1962 | #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ |
| 1963 | if (Name == NAME) { \ |
Chandler Carruth | 58dde8c | 2016-02-26 12:17:54 +0000 | [diff] [blame] | 1964 | AA.registerFunctionAnalysis< \ |
| 1965 | std::remove_reference<decltype(CREATE_PASS)>::type>(); \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1966 | return true; \ |
| 1967 | } |
| 1968 | #include "PassRegistry.def" |
| 1969 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 1970 | for (auto &C : AAParsingCallbacks) |
| 1971 | if (C(Name, AA)) |
| 1972 | return true; |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1973 | return false; |
| 1974 | } |
| 1975 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1976 | Error PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM, |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1977 | ArrayRef<PipelineElement> Pipeline, |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1978 | bool VerifyEachPass, |
| 1979 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1980 | for (const auto &Element : Pipeline) { |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 1981 | if (auto Err = parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging)) |
| 1982 | return Err; |
| 1983 | // FIXME: No verifier support for Loop passes! |
| 1984 | } |
| 1985 | return Error::success(); |
| 1986 | } |
| 1987 | |
| 1988 | Error PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM, |
| 1989 | ArrayRef<PipelineElement> Pipeline, |
| 1990 | bool VerifyEachPass, |
| 1991 | bool DebugLogging) { |
| 1992 | for (const auto &Element : Pipeline) { |
| 1993 | if (auto Err = |
| 1994 | parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging)) |
| 1995 | return Err; |
| 1996 | if (VerifyEachPass) |
| 1997 | FPM.addPass(VerifierPass()); |
| 1998 | } |
| 1999 | return Error::success(); |
| 2000 | } |
| 2001 | |
| 2002 | Error PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM, |
| 2003 | ArrayRef<PipelineElement> Pipeline, |
| 2004 | bool VerifyEachPass, |
| 2005 | bool DebugLogging) { |
| 2006 | for (const auto &Element : Pipeline) { |
| 2007 | if (auto Err = parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging)) |
| 2008 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2009 | // FIXME: No verifier support for CGSCC passes! |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 2010 | } |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2011 | return Error::success(); |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 2012 | } |
| 2013 | |
NAKAMURA Takumi | a42f528 | 2016-05-16 10:13:37 +0000 | [diff] [blame] | 2014 | void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM, |
| 2015 | FunctionAnalysisManager &FAM, |
| 2016 | CGSCCAnalysisManager &CGAM, |
| 2017 | ModuleAnalysisManager &MAM) { |
| 2018 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 2019 | MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); }); |
NAKAMURA Takumi | a42f528 | 2016-05-16 10:13:37 +0000 | [diff] [blame] | 2020 | CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); }); |
| 2021 | FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); }); |
| 2022 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
| 2023 | FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); }); |
| 2024 | LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); }); |
| 2025 | } |
| 2026 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2027 | Error PassBuilder::parseModulePassPipeline(ModulePassManager &MPM, |
| 2028 | ArrayRef<PipelineElement> Pipeline, |
| 2029 | bool VerifyEachPass, |
| 2030 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2031 | for (const auto &Element : Pipeline) { |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2032 | if (auto Err = parseModulePass(MPM, Element, VerifyEachPass, DebugLogging)) |
| 2033 | return Err; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2034 | if (VerifyEachPass) |
| 2035 | MPM.addPass(VerifierPass()); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2036 | } |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2037 | return Error::success(); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2038 | } |
| 2039 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2040 | // Primary pass pipeline description parsing routine for a \c ModulePassManager |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2041 | // FIXME: Should this routine accept a TargetMachine or require the caller to |
| 2042 | // pre-populate the analysis managers with target-specific stuff? |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2043 | Error PassBuilder::parsePassPipeline(ModulePassManager &MPM, |
| 2044 | StringRef PipelineText, |
| 2045 | bool VerifyEachPass, bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2046 | auto Pipeline = parsePipelineText(PipelineText); |
| 2047 | if (!Pipeline || Pipeline->empty()) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2048 | return make_error<StringError>( |
| 2049 | formatv("invalid pipeline '{0}'", PipelineText).str(), |
| 2050 | inconvertibleErrorCode()); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2051 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2052 | // If the first name isn't at the module layer, wrap the pipeline up |
| 2053 | // automatically. |
| 2054 | StringRef FirstName = Pipeline->front().Name; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2055 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2056 | if (!isModulePassName(FirstName, ModulePipelineParsingCallbacks)) { |
| 2057 | if (isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2058 | Pipeline = {{"cgscc", std::move(*Pipeline)}}; |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2059 | } else if (isFunctionPassName(FirstName, |
| 2060 | FunctionPipelineParsingCallbacks)) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2061 | Pipeline = {{"function", std::move(*Pipeline)}}; |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2062 | } else if (isLoopPassName(FirstName, LoopPipelineParsingCallbacks)) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 2063 | Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}}; |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2064 | } else { |
| 2065 | for (auto &C : TopLevelPipelineParsingCallbacks) |
| 2066 | if (C(MPM, *Pipeline, VerifyEachPass, DebugLogging)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2067 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2068 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2069 | // Unknown pass or pipeline name! |
| 2070 | auto &InnerPipeline = Pipeline->front().InnerPipeline; |
| 2071 | return make_error<StringError>( |
| 2072 | formatv("unknown {0} name '{1}'", |
| 2073 | (InnerPipeline.empty() ? "pass" : "pipeline"), FirstName) |
| 2074 | .str(), |
| 2075 | inconvertibleErrorCode()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2076 | } |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2079 | if (auto Err = |
| 2080 | parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging)) |
| 2081 | return Err; |
| 2082 | return Error::success(); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 2083 | } |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 2084 | |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2085 | // Primary pass pipeline description parsing routine for a \c CGSCCPassManager |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2086 | Error PassBuilder::parsePassPipeline(CGSCCPassManager &CGPM, |
| 2087 | StringRef PipelineText, |
| 2088 | bool VerifyEachPass, bool DebugLogging) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2089 | auto Pipeline = parsePipelineText(PipelineText); |
| 2090 | if (!Pipeline || Pipeline->empty()) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2091 | return make_error<StringError>( |
| 2092 | formatv("invalid pipeline '{0}'", PipelineText).str(), |
| 2093 | inconvertibleErrorCode()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2094 | |
| 2095 | StringRef FirstName = Pipeline->front().Name; |
| 2096 | if (!isCGSCCPassName(FirstName, CGSCCPipelineParsingCallbacks)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2097 | return make_error<StringError>( |
| 2098 | formatv("unknown cgscc pass '{0}' in pipeline '{1}'", FirstName, |
| 2099 | PipelineText) |
| 2100 | .str(), |
| 2101 | inconvertibleErrorCode()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2102 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2103 | if (auto Err = |
| 2104 | parseCGSCCPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging)) |
| 2105 | return Err; |
| 2106 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2107 | } |
| 2108 | |
| 2109 | // Primary pass pipeline description parsing routine for a \c |
| 2110 | // FunctionPassManager |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2111 | Error PassBuilder::parsePassPipeline(FunctionPassManager &FPM, |
| 2112 | StringRef PipelineText, |
| 2113 | bool VerifyEachPass, bool DebugLogging) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2114 | auto Pipeline = parsePipelineText(PipelineText); |
| 2115 | if (!Pipeline || Pipeline->empty()) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2116 | return make_error<StringError>( |
| 2117 | formatv("invalid pipeline '{0}'", PipelineText).str(), |
| 2118 | inconvertibleErrorCode()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2119 | |
| 2120 | StringRef FirstName = Pipeline->front().Name; |
| 2121 | if (!isFunctionPassName(FirstName, FunctionPipelineParsingCallbacks)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2122 | return make_error<StringError>( |
| 2123 | formatv("unknown function pass '{0}' in pipeline '{1}'", FirstName, |
| 2124 | PipelineText) |
| 2125 | .str(), |
| 2126 | inconvertibleErrorCode()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2127 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2128 | if (auto Err = parseFunctionPassPipeline(FPM, *Pipeline, VerifyEachPass, |
| 2129 | DebugLogging)) |
| 2130 | return Err; |
| 2131 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | // Primary pass pipeline description parsing routine for a \c LoopPassManager |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2135 | Error PassBuilder::parsePassPipeline(LoopPassManager &CGPM, |
| 2136 | StringRef PipelineText, |
| 2137 | bool VerifyEachPass, bool DebugLogging) { |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2138 | auto Pipeline = parsePipelineText(PipelineText); |
| 2139 | if (!Pipeline || Pipeline->empty()) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2140 | return make_error<StringError>( |
| 2141 | formatv("invalid pipeline '{0}'", PipelineText).str(), |
| 2142 | inconvertibleErrorCode()); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2143 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2144 | if (auto Err = |
| 2145 | parseLoopPassPipeline(CGPM, *Pipeline, VerifyEachPass, DebugLogging)) |
| 2146 | return Err; |
| 2147 | |
| 2148 | return Error::success(); |
Philip Pfaffe | 730f2f9 | 2017-07-10 10:57:55 +0000 | [diff] [blame] | 2149 | } |
| 2150 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2151 | Error PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) { |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 2152 | // If the pipeline just consists of the word 'default' just replace the AA |
| 2153 | // manager with our default one. |
| 2154 | if (PipelineText == "default") { |
| 2155 | AA = buildDefaultAAPipeline(); |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2156 | return Error::success(); |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 2157 | } |
| 2158 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 2159 | while (!PipelineText.empty()) { |
| 2160 | StringRef Name; |
| 2161 | std::tie(Name, PipelineText) = PipelineText.split(','); |
| 2162 | if (!parseAAPassName(AA, Name)) |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2163 | return make_error<StringError>( |
| 2164 | formatv("unknown alias analysis name '{0}'", Name).str(), |
| 2165 | inconvertibleErrorCode()); |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 2166 | } |
| 2167 | |
Fedor Sergeev | bd6b213 | 2018-10-17 10:36:23 +0000 | [diff] [blame] | 2168 | return Error::success(); |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 2169 | } |