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