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