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" |
| 25 | #include "llvm/Analysis/BlockFrequencyInfoImpl.h" |
Xinliang David Li | 6e5dd41 | 2016-05-05 02:59:57 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/BranchProbabilityInfo.h" |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/CFGPrinter.h" |
George Burgess IV | bfa401e | 2016-07-06 00:26:41 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CFLAndersAliasAnalysis.h" |
| 29 | #include "llvm/Analysis/CFLSteensAliasAnalysis.h" |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/CGSCCPassManager.h" |
Chandler Carruth | 4c660f7 | 2016-03-10 11:24:11 +0000 | [diff] [blame] | 31 | #include "llvm/Analysis/CallGraph.h" |
Michael Kuperstein | de16b44 | 2016-04-18 23:55:01 +0000 | [diff] [blame] | 32 | #include "llvm/Analysis/DemandedBits.h" |
Chandler Carruth | 49c2219 | 2016-05-12 22:19:39 +0000 | [diff] [blame] | 33 | #include "llvm/Analysis/DependenceAnalysis.h" |
Hongbin Zheng | 751337f | 2016-02-25 17:54:15 +0000 | [diff] [blame] | 34 | #include "llvm/Analysis/DominanceFrontier.h" |
Chandler Carruth | 45a9c20 | 2016-03-11 09:15:11 +0000 | [diff] [blame] | 35 | #include "llvm/Analysis/GlobalsModRef.h" |
Dehao Chen | 1a44452 | 2016-07-16 22:51:33 +0000 | [diff] [blame] | 36 | #include "llvm/Analysis/IVUsers.h" |
Chandler Carruth | bf71a34 | 2014-02-06 04:37:03 +0000 | [diff] [blame] | 37 | #include "llvm/Analysis/LazyCallGraph.h" |
Sean Silva | 687019f | 2016-06-13 22:01:25 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/LazyValueInfo.h" |
Xinliang David Li | 8a02131 | 2016-07-02 21:18:40 +0000 | [diff] [blame] | 39 | #include "llvm/Analysis/LoopAccessAnalysis.h" |
Chandler Carruth | aaf0b4c | 2015-01-20 10:58:50 +0000 | [diff] [blame] | 40 | #include "llvm/Analysis/LoopInfo.h" |
Chandler Carruth | 61440d2 | 2016-03-10 00:55:30 +0000 | [diff] [blame] | 41 | #include "llvm/Analysis/MemoryDependenceAnalysis.h" |
Teresa Johnson | f93b246 | 2016-08-12 13:53:02 +0000 | [diff] [blame] | 42 | #include "llvm/Analysis/ModuleSummaryAnalysis.h" |
Adam Nemet | 79ac42a | 2016-07-18 16:29:21 +0000 | [diff] [blame] | 43 | #include "llvm/Analysis/OptimizationDiagnosticInfo.h" |
Hongbin Zheng | 3f97840 | 2016-02-25 17:54:07 +0000 | [diff] [blame] | 44 | #include "llvm/Analysis/PostDominators.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 45 | #include "llvm/Analysis/ProfileSummaryInfo.h" |
Hongbin Zheng | bc53977 | 2016-02-25 17:54:25 +0000 | [diff] [blame] | 46 | #include "llvm/Analysis/RegionInfo.h" |
Chandler Carruth | 2f1fd16 | 2015-08-17 02:08:17 +0000 | [diff] [blame] | 47 | #include "llvm/Analysis/ScalarEvolution.h" |
Chandler Carruth | 2b3d044 | 2016-02-20 04:01:45 +0000 | [diff] [blame] | 48 | #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h" |
Chandler Carruth | d6091a0 | 2016-02-20 04:03:06 +0000 | [diff] [blame] | 49 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Chandler Carruth | 8ca4322 | 2015-01-15 11:39:46 +0000 | [diff] [blame] | 50 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 51 | #include "llvm/Analysis/TargetTransformInfo.h" |
Chandler Carruth | c1dc384 | 2016-02-20 04:04:52 +0000 | [diff] [blame] | 52 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Michael Kuperstein | 82d5da5 | 2016-06-24 20:13:42 +0000 | [diff] [blame] | 53 | #include "llvm/CodeGen/PreISelIntrinsicLowering.h" |
Wei Mi | 90d195a | 2016-07-08 03:32:49 +0000 | [diff] [blame] | 54 | #include "llvm/CodeGen/UnreachableBlockElim.h" |
Chandler Carruth | 64764b4 | 2015-01-14 10:19:28 +0000 | [diff] [blame] | 55 | #include "llvm/IR/Dominators.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 56 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 57 | #include "llvm/IR/PassManager.h" |
Chandler Carruth | 4d35631 | 2014-01-20 11:34:08 +0000 | [diff] [blame] | 58 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 59 | #include "llvm/Support/Debug.h" |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 60 | #include "llvm/Support/Regex.h" |
Chandler Carruth | e038552 | 2015-02-01 10:11:22 +0000 | [diff] [blame] | 61 | #include "llvm/Target/TargetMachine.h" |
Xinliang David Li | 64dbb29 | 2016-06-05 05:12:23 +0000 | [diff] [blame] | 62 | #include "llvm/Transforms/GCOVProfiler.h" |
Chandler Carruth | 67fc52f | 2016-08-17 02:56:20 +0000 | [diff] [blame] | 63 | #include "llvm/Transforms/IPO/AlwaysInliner.h" |
Chandler Carruth | addcda4 | 2017-02-09 23:46:27 +0000 | [diff] [blame] | 64 | #include "llvm/Transforms/IPO/ArgumentPromotion.h" |
Davide Italiano | 164b9bc | 2016-05-05 00:51:09 +0000 | [diff] [blame] | 65 | #include "llvm/Transforms/IPO/ConstantMerge.h" |
Davide Italiano | 92b933a | 2016-07-09 03:25:35 +0000 | [diff] [blame] | 66 | #include "llvm/Transforms/IPO/CrossDSOCFI.h" |
Sean Silva | e3bb457 | 2016-06-12 09:16:39 +0000 | [diff] [blame] | 67 | #include "llvm/Transforms/IPO/DeadArgumentElimination.h" |
Davide Italiano | 344e838 | 2016-05-05 02:37:32 +0000 | [diff] [blame] | 68 | #include "llvm/Transforms/IPO/ElimAvailExtern.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 69 | #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 70 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Teresa Johnson | 2124157 | 2016-07-18 21:22:24 +0000 | [diff] [blame] | 71 | #include "llvm/Transforms/IPO/FunctionImport.h" |
Davide Italiano | 66228c4 | 2016-05-03 19:39:15 +0000 | [diff] [blame] | 72 | #include "llvm/Transforms/IPO/GlobalDCE.h" |
Justin Bogner | 1a07501 | 2016-04-26 00:28:01 +0000 | [diff] [blame] | 73 | #include "llvm/Transforms/IPO/GlobalOpt.h" |
Davide Italiano | 2ae76dd | 2016-11-21 00:28:23 +0000 | [diff] [blame] | 74 | #include "llvm/Transforms/IPO/GlobalSplit.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 75 | #include "llvm/Transforms/IPO/InferFunctionAttrs.h" |
Chandler Carruth | 1d96311 | 2016-12-20 03:15:32 +0000 | [diff] [blame] | 76 | #include "llvm/Transforms/IPO/Inliner.h" |
Justin Bogner | 4563a06 | 2016-04-26 20:15:52 +0000 | [diff] [blame] | 77 | #include "llvm/Transforms/IPO/Internalize.h" |
Davide Italiano | e8ae0b5 | 2016-07-11 18:10:06 +0000 | [diff] [blame] | 78 | #include "llvm/Transforms/IPO/LowerTypeTests.h" |
Easwaran Raman | 1832bf6 | 2016-06-27 16:50:18 +0000 | [diff] [blame] | 79 | #include "llvm/Transforms/IPO/PartialInlining.h" |
Davide Italiano | f54f2f0 | 2016-05-05 21:05:36 +0000 | [diff] [blame] | 80 | #include "llvm/Transforms/IPO/SCCP.h" |
Justin Bogner | 21e1537 | 2015-10-30 23:28:12 +0000 | [diff] [blame] | 81 | #include "llvm/Transforms/IPO/StripDeadPrototypes.h" |
Davide Italiano | d737dd2 | 2016-06-14 21:44:19 +0000 | [diff] [blame] | 82 | #include "llvm/Transforms/IPO/WholeProgramDevirt.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 83 | #include "llvm/Transforms/InstCombine/InstCombine.h" |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 84 | #include "llvm/Transforms/InstrProfiling.h" |
Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 85 | #include "llvm/Transforms/PGOInstrumentation.h" |
Xinliang David Li | d38392e | 2016-05-27 23:20:16 +0000 | [diff] [blame] | 86 | #include "llvm/Transforms/SampleProfile.h" |
Justin Bogner | 19b6799 | 2015-10-30 23:13:18 +0000 | [diff] [blame] | 87 | #include "llvm/Transforms/Scalar/ADCE.h" |
Sean Silva | a4c2d15 | 2016-06-15 06:18:01 +0000 | [diff] [blame] | 88 | #include "llvm/Transforms/Scalar/AlignmentFromAssumptions.h" |
Davide Italiano | 655a145 | 2016-05-25 01:57:04 +0000 | [diff] [blame] | 89 | #include "llvm/Transforms/Scalar/BDCE.h" |
Adam Nemet | 3beef41 | 2016-07-18 16:29:17 +0000 | [diff] [blame] | 90 | #include "llvm/Transforms/Scalar/ConstantHoisting.h" |
Sean Silva | b025d37 | 2016-07-06 23:26:29 +0000 | [diff] [blame] | 91 | #include "llvm/Transforms/Scalar/CorrelatedValuePropagation.h" |
Justin Bogner | 395c212 | 2016-04-22 19:40:41 +0000 | [diff] [blame] | 92 | #include "llvm/Transforms/Scalar/DCE.h" |
Justin Bogner | 594e07b | 2016-05-17 21:38:13 +0000 | [diff] [blame] | 93 | #include "llvm/Transforms/Scalar/DeadStoreElimination.h" |
Chandler Carruth | e8c686a | 2015-02-01 10:51:23 +0000 | [diff] [blame] | 94 | #include "llvm/Transforms/Scalar/EarlyCSE.h" |
Michael Kuperstein | 83b753d | 2016-06-24 23:32:02 +0000 | [diff] [blame] | 95 | #include "llvm/Transforms/Scalar/Float2Int.h" |
Easwaran Raman | 019e0bf | 2016-06-03 22:54:26 +0000 | [diff] [blame] | 96 | #include "llvm/Transforms/Scalar/GVN.h" |
Davide Italiano | 89ab89d | 2016-06-14 00:49:23 +0000 | [diff] [blame] | 97 | #include "llvm/Transforms/Scalar/GuardWidening.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 98 | #include "llvm/Transforms/Scalar/IVUsersPrinter.h" |
Sanjoy Das | 4d4339d | 2016-06-05 18:01:19 +0000 | [diff] [blame] | 99 | #include "llvm/Transforms/Scalar/IndVarSimplify.h" |
Sean Silva | 46590d5 | 2016-06-14 00:51:09 +0000 | [diff] [blame] | 100 | #include "llvm/Transforms/Scalar/JumpThreading.h" |
Dehao Chen | f400a09 | 2016-07-12 22:42:24 +0000 | [diff] [blame] | 101 | #include "llvm/Transforms/Scalar/LICM.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 102 | #include "llvm/Transforms/Scalar/LoopAccessAnalysisPrinter.h" |
Teresa Johnson | 1eca6bc | 2016-08-13 04:11:27 +0000 | [diff] [blame] | 103 | #include "llvm/Transforms/Scalar/LoopDataPrefetch.h" |
Jun Bum Lim | c837af3 | 2016-07-14 18:28:29 +0000 | [diff] [blame] | 104 | #include "llvm/Transforms/Scalar/LoopDeletion.h" |
Adam Nemet | b2593f7 | 2016-07-18 16:29:27 +0000 | [diff] [blame] | 105 | #include "llvm/Transforms/Scalar/LoopDistribute.h" |
Dehao Chen | b9f8e29 | 2016-07-12 18:45:51 +0000 | [diff] [blame] | 106 | #include "llvm/Transforms/Scalar/LoopIdiomRecognize.h" |
Dehao Chen | dcafd5e | 2016-07-15 16:42:11 +0000 | [diff] [blame] | 107 | #include "llvm/Transforms/Scalar/LoopInstSimplify.h" |
Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 108 | #include "llvm/Transforms/Scalar/LoopLoadElimination.h" |
Chandler Carruth | 3bab7e1 | 2017-01-11 09:43:56 +0000 | [diff] [blame] | 109 | #include "llvm/Transforms/Scalar/LoopPassManager.h" |
Artur Pilipenko | 8fb3d57 | 2017-01-25 16:00:44 +0000 | [diff] [blame] | 110 | #include "llvm/Transforms/Scalar/LoopPredication.h" |
Justin Bogner | d0d2341 | 2016-05-03 22:02:31 +0000 | [diff] [blame] | 111 | #include "llvm/Transforms/Scalar/LoopRotation.h" |
Justin Bogner | ab6a513 | 2016-05-03 21:47:32 +0000 | [diff] [blame] | 112 | #include "llvm/Transforms/Scalar/LoopSimplifyCFG.h" |
Chandler Carruth | e9b18e3 | 2017-01-20 08:42:19 +0000 | [diff] [blame] | 113 | #include "llvm/Transforms/Scalar/LoopSink.h" |
Dehao Chen | 6132ee8 | 2016-07-18 21:41:50 +0000 | [diff] [blame] | 114 | #include "llvm/Transforms/Scalar/LoopStrengthReduce.h" |
Sean Silva | e3c18a5 | 2016-07-19 23:54:23 +0000 | [diff] [blame] | 115 | #include "llvm/Transforms/Scalar/LoopUnrollPass.h" |
Davide Italiano | 9922344 | 2016-05-13 22:52:35 +0000 | [diff] [blame] | 116 | #include "llvm/Transforms/Scalar/LowerAtomic.h" |
Chandler Carruth | 43e590e | 2015-01-24 11:13:02 +0000 | [diff] [blame] | 117 | #include "llvm/Transforms/Scalar/LowerExpectIntrinsic.h" |
Michael Kuperstein | e45d4d9 | 2016-07-28 22:08:41 +0000 | [diff] [blame] | 118 | #include "llvm/Transforms/Scalar/LowerGuardIntrinsic.h" |
Sean Silva | 6347df0 | 2016-06-14 02:44:55 +0000 | [diff] [blame] | 119 | #include "llvm/Transforms/Scalar/MemCpyOptimizer.h" |
Davide Italiano | b49aa5c | 2016-06-17 19:10:09 +0000 | [diff] [blame] | 120 | #include "llvm/Transforms/Scalar/MergedLoadStoreMotion.h" |
Wei Mi | 1cf58f8 | 2016-07-21 22:28:52 +0000 | [diff] [blame] | 121 | #include "llvm/Transforms/Scalar/NaryReassociate.h" |
Davide Italiano | e05e330 | 2016-12-22 16:35:02 +0000 | [diff] [blame] | 122 | #include "llvm/Transforms/Scalar/NewGVN.h" |
Davide Italiano | 1021c68 | 2016-05-25 23:38:53 +0000 | [diff] [blame] | 123 | #include "llvm/Transforms/Scalar/PartiallyInlineLibCalls.h" |
Justin Bogner | c2bf63d | 2016-04-26 23:39:29 +0000 | [diff] [blame] | 124 | #include "llvm/Transforms/Scalar/Reassociate.h" |
Davide Italiano | 98f7e0e | 2016-05-18 15:18:25 +0000 | [diff] [blame] | 125 | #include "llvm/Transforms/Scalar/SCCP.h" |
Chandler Carruth | 29a18a4 | 2015-09-12 09:09:14 +0000 | [diff] [blame] | 126 | #include "llvm/Transforms/Scalar/SROA.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 127 | #include "llvm/Transforms/Scalar/SimplifyCFG.h" |
Justin Bogner | b939490 | 2016-04-22 19:54:10 +0000 | [diff] [blame] | 128 | #include "llvm/Transforms/Scalar/Sink.h" |
Michael Kuperstein | c406186 | 2016-08-01 21:48:33 +0000 | [diff] [blame] | 129 | #include "llvm/Transforms/Scalar/SpeculativeExecution.h" |
Sean Silva | 59fe82f | 2016-07-06 23:48:41 +0000 | [diff] [blame] | 130 | #include "llvm/Transforms/Scalar/TailRecursionElimination.h" |
Xinliang David Li | 1eaecef | 2016-06-15 21:51:30 +0000 | [diff] [blame] | 131 | #include "llvm/Transforms/Utils/AddDiscriminators.h" |
Wei Mi | e04d0ef | 2016-07-22 18:04:25 +0000 | [diff] [blame] | 132 | #include "llvm/Transforms/Utils/BreakCriticalEdges.h" |
Easwaran Raman | e12c487 | 2016-06-09 19:44:46 +0000 | [diff] [blame] | 133 | #include "llvm/Transforms/Utils/LCSSA.h" |
Rong Xu | 1c0e9b9 | 2016-10-18 21:36:27 +0000 | [diff] [blame] | 134 | #include "llvm/Transforms/Utils/LibCallsShrinkWrap.h" |
Davide Italiano | cd96cfd | 2016-07-09 03:03:01 +0000 | [diff] [blame] | 135 | #include "llvm/Transforms/Utils/LoopSimplify.h" |
Michael Kuperstein | 31b8399 | 2016-08-12 17:28:27 +0000 | [diff] [blame] | 136 | #include "llvm/Transforms/Utils/LowerInvoke.h" |
Davide Italiano | cccf4f0 | 2016-06-14 03:22:22 +0000 | [diff] [blame] | 137 | #include "llvm/Transforms/Utils/Mem2Reg.h" |
Geoff Berry | b96d3b2 | 2016-06-01 21:30:40 +0000 | [diff] [blame] | 138 | #include "llvm/Transforms/Utils/MemorySSA.h" |
Mehdi Amini | 27d2379 | 2016-09-16 16:56:30 +0000 | [diff] [blame] | 139 | #include "llvm/Transforms/Utils/NameAnonGlobals.h" |
Daniel Berlin | 439042b | 2017-02-07 21:10:46 +0000 | [diff] [blame] | 140 | #include "llvm/Transforms/Utils/PredicateInfo.h" |
Davide Italiano | 16284df | 2016-07-07 21:14:36 +0000 | [diff] [blame] | 141 | #include "llvm/Transforms/Utils/SimplifyInstructions.h" |
Michael Kuperstein | 39feb62 | 2016-07-25 20:52:00 +0000 | [diff] [blame] | 142 | #include "llvm/Transforms/Utils/SymbolRewriter.h" |
Sean Silva | db90d4d | 2016-07-09 22:56:50 +0000 | [diff] [blame] | 143 | #include "llvm/Transforms/Vectorize/LoopVectorize.h" |
Sean Silva | 0dacbd8 | 2016-07-09 03:11:29 +0000 | [diff] [blame] | 144 | #include "llvm/Transforms/Vectorize/SLPVectorizer.h" |
Davide Italiano | cccf4f0 | 2016-06-14 03:22:22 +0000 | [diff] [blame] | 145 | |
Chandler Carruth | 58dde8c | 2016-02-26 12:17:54 +0000 | [diff] [blame] | 146 | #include <type_traits> |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 147 | |
| 148 | using namespace llvm; |
| 149 | |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 150 | static Regex DefaultAliasRegex("^(default|lto-pre-link|lto)<(O[0123sz])>$"); |
| 151 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 152 | static bool isOptimizingForSize(PassBuilder::OptimizationLevel Level) { |
| 153 | switch (Level) { |
| 154 | case PassBuilder::O0: |
| 155 | case PassBuilder::O1: |
| 156 | case PassBuilder::O2: |
| 157 | case PassBuilder::O3: |
| 158 | return false; |
| 159 | |
| 160 | case PassBuilder::Os: |
| 161 | case PassBuilder::Oz: |
| 162 | return true; |
| 163 | } |
| 164 | llvm_unreachable("Invalid optimization level!"); |
| 165 | } |
| 166 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 167 | namespace { |
| 168 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 169 | /// \brief No-op module pass which does nothing. |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 170 | struct NoOpModulePass { |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 171 | PreservedAnalyses run(Module &M, ModuleAnalysisManager &) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 172 | return PreservedAnalyses::all(); |
| 173 | } |
Chandler Carruth | a13f27c | 2014-01-11 11:52:05 +0000 | [diff] [blame] | 174 | static StringRef name() { return "NoOpModulePass"; } |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 177 | /// \brief No-op module analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 178 | class NoOpModuleAnalysis : public AnalysisInfoMixin<NoOpModuleAnalysis> { |
| 179 | friend AnalysisInfoMixin<NoOpModuleAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 180 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 181 | |
| 182 | public: |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 183 | struct Result {}; |
Sean Silva | fd03ac6 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 184 | Result run(Module &, ModuleAnalysisManager &) { return Result(); } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 185 | static StringRef name() { return "NoOpModuleAnalysis"; } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 186 | }; |
| 187 | |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 188 | /// \brief No-op CGSCC pass which does nothing. |
| 189 | struct NoOpCGSCCPass { |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 190 | PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &, |
| 191 | LazyCallGraph &, CGSCCUpdateResult &UR) { |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 192 | return PreservedAnalyses::all(); |
| 193 | } |
| 194 | static StringRef name() { return "NoOpCGSCCPass"; } |
| 195 | }; |
| 196 | |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 197 | /// \brief No-op CGSCC analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 198 | class NoOpCGSCCAnalysis : public AnalysisInfoMixin<NoOpCGSCCAnalysis> { |
| 199 | friend AnalysisInfoMixin<NoOpCGSCCAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 200 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 201 | |
| 202 | public: |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 203 | struct Result {}; |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 204 | Result run(LazyCallGraph::SCC &, CGSCCAnalysisManager &, LazyCallGraph &G) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 205 | return Result(); |
| 206 | } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 207 | static StringRef name() { return "NoOpCGSCCAnalysis"; } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 208 | }; |
| 209 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 210 | /// \brief No-op function pass which does nothing. |
| 211 | struct NoOpFunctionPass { |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 212 | PreservedAnalyses run(Function &F, FunctionAnalysisManager &) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 213 | return PreservedAnalyses::all(); |
| 214 | } |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 215 | static StringRef name() { return "NoOpFunctionPass"; } |
| 216 | }; |
| 217 | |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 218 | /// \brief No-op function analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 219 | class NoOpFunctionAnalysis : public AnalysisInfoMixin<NoOpFunctionAnalysis> { |
| 220 | friend AnalysisInfoMixin<NoOpFunctionAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 221 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 222 | |
| 223 | public: |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 224 | struct Result {}; |
Sean Silva | 36e0d01 | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 225 | Result run(Function &, FunctionAnalysisManager &) { return Result(); } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 226 | static StringRef name() { return "NoOpFunctionAnalysis"; } |
Chandler Carruth | 0b576b3 | 2015-01-06 02:50:06 +0000 | [diff] [blame] | 227 | }; |
| 228 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 229 | /// \brief No-op loop pass which does nothing. |
| 230 | struct NoOpLoopPass { |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 231 | PreservedAnalyses run(Loop &L, LoopAnalysisManager &, |
| 232 | LoopStandardAnalysisResults &, LPMUpdater &) { |
Chandler Carruth | 164a2aa6 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 233 | return PreservedAnalyses::all(); |
| 234 | } |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 235 | static StringRef name() { return "NoOpLoopPass"; } |
| 236 | }; |
| 237 | |
| 238 | /// \brief No-op loop analysis. |
Chandler Carruth | 30a0730 | 2016-03-11 10:33:22 +0000 | [diff] [blame] | 239 | class NoOpLoopAnalysis : public AnalysisInfoMixin<NoOpLoopAnalysis> { |
| 240 | friend AnalysisInfoMixin<NoOpLoopAnalysis>; |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 241 | static AnalysisKey Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 242 | |
| 243 | public: |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 244 | struct Result {}; |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 245 | Result run(Loop &, LoopAnalysisManager &, LoopStandardAnalysisResults &) { |
| 246 | return Result(); |
| 247 | } |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 248 | static StringRef name() { return "NoOpLoopAnalysis"; } |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 249 | }; |
| 250 | |
Chandler Carruth | dab4eae | 2016-11-23 17:53:26 +0000 | [diff] [blame] | 251 | AnalysisKey NoOpModuleAnalysis::Key; |
| 252 | AnalysisKey NoOpCGSCCAnalysis::Key; |
| 253 | AnalysisKey NoOpFunctionAnalysis::Key; |
| 254 | AnalysisKey NoOpLoopAnalysis::Key; |
Chandler Carruth | b4faf13 | 2016-03-11 10:22:49 +0000 | [diff] [blame] | 255 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 256 | } // End anonymous namespace. |
| 257 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 258 | void PassBuilder::registerModuleAnalyses(ModuleAnalysisManager &MAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 259 | #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 260 | MAM.registerPass([&] { return CREATE_PASS; }); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 261 | #include "PassRegistry.def" |
| 262 | } |
| 263 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 264 | void PassBuilder::registerCGSCCAnalyses(CGSCCAnalysisManager &CGAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 265 | #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 266 | CGAM.registerPass([&] { return CREATE_PASS; }); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 267 | #include "PassRegistry.def" |
| 268 | } |
| 269 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 270 | void PassBuilder::registerFunctionAnalyses(FunctionAnalysisManager &FAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 271 | #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 272 | FAM.registerPass([&] { return CREATE_PASS; }); |
Chandler Carruth | b70f673 | 2015-01-06 02:21:37 +0000 | [diff] [blame] | 273 | #include "PassRegistry.def" |
| 274 | } |
| 275 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 276 | void PassBuilder::registerLoopAnalyses(LoopAnalysisManager &LAM) { |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 277 | #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 278 | LAM.registerPass([&] { return CREATE_PASS; }); |
| 279 | #include "PassRegistry.def" |
| 280 | } |
| 281 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 282 | FunctionPassManager |
| 283 | PassBuilder::buildFunctionSimplificationPipeline(OptimizationLevel Level, |
| 284 | bool DebugLogging) { |
| 285 | assert(Level != O0 && "Must request optimizations!"); |
| 286 | FunctionPassManager FPM(DebugLogging); |
| 287 | |
| 288 | // Form SSA out of local memory accesses after breaking apart aggregates into |
| 289 | // scalars. |
| 290 | FPM.addPass(SROA()); |
| 291 | |
| 292 | // Catch trivial redundancies |
| 293 | FPM.addPass(EarlyCSEPass()); |
| 294 | |
| 295 | // Speculative execution if the target has divergent branches; otherwise nop. |
| 296 | FPM.addPass(SpeculativeExecutionPass()); |
| 297 | |
| 298 | // Optimize based on known information about branches, and cleanup afterward. |
| 299 | FPM.addPass(JumpThreadingPass()); |
| 300 | FPM.addPass(CorrelatedValuePropagationPass()); |
| 301 | FPM.addPass(SimplifyCFGPass()); |
| 302 | FPM.addPass(InstCombinePass()); |
| 303 | |
| 304 | if (!isOptimizingForSize(Level)) |
| 305 | FPM.addPass(LibCallsShrinkWrapPass()); |
| 306 | |
| 307 | FPM.addPass(TailCallElimPass()); |
| 308 | FPM.addPass(SimplifyCFGPass()); |
| 309 | |
| 310 | // Form canonically associated expression trees, and simplify the trees using |
| 311 | // basic mathematical properties. For example, this will form (nearly) |
| 312 | // minimal multiplication trees. |
| 313 | FPM.addPass(ReassociatePass()); |
| 314 | |
| 315 | // Add the primary loop simplification pipeline. |
| 316 | // FIXME: Currently this is split into two loop pass pipelines because we run |
| 317 | // some function passes in between them. These can and should be replaced by |
| 318 | // loop pass equivalenst but those aren't ready yet. Specifically, |
| 319 | // `SimplifyCFGPass` and `InstCombinePass` are used. We have |
| 320 | // `LoopSimplifyCFGPass` which isn't yet powerful enough, and the closest to |
| 321 | // the other we have is `LoopInstSimplify`. |
| 322 | LoopPassManager LPM1(DebugLogging), LPM2(DebugLogging); |
| 323 | |
| 324 | // Rotate Loop - disable header duplication at -Oz |
| 325 | LPM1.addPass(LoopRotatePass(Level != Oz)); |
| 326 | LPM1.addPass(LICMPass()); |
Chandler Carruth | 79b733b | 2017-01-26 23:21:17 +0000 | [diff] [blame] | 327 | #if 0 |
| 328 | // The LoopUnswitch pass isn't yet ported to the new pass manager. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 329 | LPM1.addPass(LoopUnswitchPass(/* OptimizeForSize */ Level != O3)); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 330 | #endif |
Chandler Carruth | 79b733b | 2017-01-26 23:21:17 +0000 | [diff] [blame] | 331 | LPM2.addPass(IndVarSimplifyPass()); |
| 332 | LPM2.addPass(LoopIdiomRecognizePass()); |
| 333 | LPM2.addPass(LoopDeletionPass()); |
| 334 | LPM2.addPass(LoopUnrollPass::createFull()); |
| 335 | |
Chandler Carruth | 0ede22e | 2017-02-09 23:54:57 +0000 | [diff] [blame] | 336 | // We provide the opt remark emitter pass for LICM to use. We only need to do |
| 337 | // this once as it is immutable. |
| 338 | FPM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 339 | FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM1))); |
| 340 | FPM.addPass(SimplifyCFGPass()); |
| 341 | FPM.addPass(InstCombinePass()); |
| 342 | FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM2))); |
| 343 | |
| 344 | // Eliminate redundancies. |
| 345 | if (Level != O1) { |
| 346 | // These passes add substantial compile time so skip them at O1. |
| 347 | FPM.addPass(MergedLoadStoreMotionPass()); |
| 348 | FPM.addPass(GVN()); |
| 349 | } |
| 350 | |
| 351 | // Specially optimize memory movement as it doesn't look like dataflow in SSA. |
| 352 | FPM.addPass(MemCpyOptPass()); |
| 353 | |
| 354 | // Sparse conditional constant propagation. |
| 355 | // FIXME: It isn't clear why we do this *after* loop passes rather than |
| 356 | // before... |
| 357 | FPM.addPass(SCCPPass()); |
| 358 | |
| 359 | // Delete dead bit computations (instcombine runs after to fold away the dead |
| 360 | // computations, and then ADCE will run later to exploit any new DCE |
| 361 | // opportunities that creates). |
| 362 | FPM.addPass(BDCEPass()); |
| 363 | |
| 364 | // Run instcombine after redundancy and dead bit elimination to exploit |
| 365 | // opportunities opened up by them. |
| 366 | FPM.addPass(InstCombinePass()); |
| 367 | |
| 368 | // Re-consider control flow based optimizations after redundancy elimination, |
| 369 | // redo DCE, etc. |
| 370 | FPM.addPass(JumpThreadingPass()); |
| 371 | FPM.addPass(CorrelatedValuePropagationPass()); |
| 372 | FPM.addPass(DSEPass()); |
| 373 | FPM.addPass(createFunctionToLoopPassAdaptor(LICMPass())); |
| 374 | |
| 375 | // Finally, do an expensive DCE pass to catch all the dead code exposed by |
| 376 | // the simplifications and basic cleanup after all the simplifications. |
| 377 | FPM.addPass(ADCEPass()); |
| 378 | FPM.addPass(SimplifyCFGPass()); |
| 379 | FPM.addPass(InstCombinePass()); |
| 380 | |
| 381 | return FPM; |
| 382 | } |
| 383 | |
| 384 | ModulePassManager |
| 385 | PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, |
| 386 | bool DebugLogging) { |
| 387 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
| 388 | ModulePassManager MPM(DebugLogging); |
| 389 | |
| 390 | // Force any function attributes we want the rest of the pipeline te observe. |
| 391 | MPM.addPass(ForceFunctionAttrsPass()); |
| 392 | |
| 393 | // Do basic inference of function attributes from known properties of system |
| 394 | // libraries and other oracles. |
| 395 | MPM.addPass(InferFunctionAttrsPass()); |
| 396 | |
| 397 | // Create an early function pass manager to cleanup the output of the |
| 398 | // frontend. |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 399 | FunctionPassManager EarlyFPM(DebugLogging); |
| 400 | EarlyFPM.addPass(SimplifyCFGPass()); |
| 401 | EarlyFPM.addPass(SROA()); |
| 402 | EarlyFPM.addPass(EarlyCSEPass()); |
| 403 | EarlyFPM.addPass(LowerExpectIntrinsicPass()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 404 | EarlyFPM.addPass(GVNHoistPass()); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 405 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(EarlyFPM))); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 406 | |
| 407 | // Interprocedural constant propagation now that basic cleanup has occured |
| 408 | // and prior to optimizing globals. |
| 409 | // FIXME: This position in the pipeline hasn't been carefully considered in |
| 410 | // years, it should be re-analyzed. |
| 411 | MPM.addPass(IPSCCPPass()); |
| 412 | |
| 413 | // Optimize globals to try and fold them into constants. |
| 414 | MPM.addPass(GlobalOptPass()); |
| 415 | |
| 416 | // Promote any localized globals to SSA registers. |
| 417 | // FIXME: Should this instead by a run of SROA? |
| 418 | // FIXME: We should probably run instcombine and simplify-cfg afterward to |
| 419 | // delete control flows that are dead once globals have been folded to |
| 420 | // constants. |
| 421 | MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); |
| 422 | |
| 423 | // Remove any dead arguments exposed by cleanups and constand folding |
| 424 | // globals. |
| 425 | MPM.addPass(DeadArgumentEliminationPass()); |
| 426 | |
| 427 | // Create a small function pass pipeline to cleanup after all the global |
| 428 | // optimizations. |
| 429 | FunctionPassManager GlobalCleanupPM(DebugLogging); |
| 430 | GlobalCleanupPM.addPass(InstCombinePass()); |
| 431 | GlobalCleanupPM.addPass(SimplifyCFGPass()); |
| 432 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(GlobalCleanupPM))); |
| 433 | |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame^] | 434 | // Require the GlobalsAA analysis for the module so we can query it within |
| 435 | // the CGSCC pipeline. |
| 436 | MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 437 | |
| 438 | // Now begin the main postorder CGSCC pipeline. |
| 439 | // FIXME: The current CGSCC pipeline has its origins in the legacy pass |
| 440 | // manager and trying to emulate its precise behavior. Much of this doesn't |
| 441 | // make a lot of sense and we should revisit the core CGSCC structure. |
| 442 | CGSCCPassManager MainCGPipeline(DebugLogging); |
| 443 | |
| 444 | // Note: historically, the PruneEH pass was run first to deduce nounwind and |
| 445 | // generally clean up exception handling overhead. It isn't clear this is |
| 446 | // valuable as the inliner doesn't currently care whether it is inlining an |
| 447 | // invoke or a call. |
| 448 | |
| 449 | // Run the inliner first. The theory is that we are walking bottom-up and so |
| 450 | // the callees have already been fully optimized, and we want to inline them |
| 451 | // into the callers so that our optimizations can reflect that. |
| 452 | // FIXME; Customize the threshold based on optimization level. |
| 453 | MainCGPipeline.addPass(InlinerPass()); |
| 454 | |
| 455 | // Now deduce any function attributes based in the current code. |
| 456 | MainCGPipeline.addPass(PostOrderFunctionAttrsPass()); |
| 457 | |
Chandler Carruth | 0ede22e | 2017-02-09 23:54:57 +0000 | [diff] [blame] | 458 | // When at O3 add argument promotion to the pass pipeline. |
| 459 | // FIXME: It isn't at all clear why this should be limited to O3. |
| 460 | if (Level == O3) |
| 461 | MainCGPipeline.addPass(ArgumentPromotionPass()); |
| 462 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 463 | // Lastly, add the core function simplification pipeline nested inside the |
| 464 | // CGSCC walk. |
| 465 | MainCGPipeline.addPass(createCGSCCToFunctionPassAdaptor( |
| 466 | buildFunctionSimplificationPipeline(Level, DebugLogging))); |
| 467 | |
| 468 | MPM.addPass( |
| 469 | createModuleToPostOrderCGSCCPassAdaptor(std::move(MainCGPipeline))); |
| 470 | |
| 471 | // This ends the canonicalization and simplification phase of the pipeline. |
| 472 | // At this point, we expect to have canonical and simple IR which we begin |
| 473 | // *optimizing* for efficient execution going forward. |
| 474 | |
| 475 | // Eliminate externally available functions now that inlining is over -- we |
| 476 | // won't emit these anyways. |
| 477 | MPM.addPass(EliminateAvailableExternallyPass()); |
| 478 | |
| 479 | // Do RPO function attribute inference across the module to forward-propagate |
| 480 | // attributes where applicable. |
| 481 | // FIXME: Is this really an optimization rather than a canonicalization? |
| 482 | MPM.addPass(ReversePostOrderFunctionAttrsPass()); |
| 483 | |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame^] | 484 | // Re-require GloblasAA here prior to function passes. This is particularly |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 485 | // useful as the above will have inlined, DCE'ed, and function-attr |
| 486 | // propagated everything. We should at this point have a reasonably minimal |
| 487 | // and richly annotated call graph. By computing aliasing and mod/ref |
| 488 | // information for all local globals here, the late loop passes and notably |
| 489 | // the vectorizer will be able to use them to help recognize vectorizable |
| 490 | // memory operations. |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame^] | 491 | MPM.addPass(RequireAnalysisPass<GlobalsAA, Module>()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 492 | |
| 493 | FunctionPassManager OptimizePM(DebugLogging); |
| 494 | OptimizePM.addPass(Float2IntPass()); |
| 495 | // FIXME: We need to run some loop optimizations to re-rotate loops after |
| 496 | // simplify-cfg and others undo their rotation. |
| 497 | |
| 498 | // Optimize the loop execution. These passes operate on entire loop nests |
| 499 | // rather than on each loop in an inside-out manner, and so they are actually |
| 500 | // function passes. |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 501 | |
| 502 | // First rotate loops that may have been un-rotated by prior passes. |
| 503 | OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopRotatePass())); |
| 504 | |
| 505 | // Distribute loops to allow partial vectorization. I.e. isolate dependences |
| 506 | // into separate loop that would otherwise inhibit vectorization. This is |
| 507 | // currently only performed for loops marked with the metadata |
| 508 | // llvm.loop.distribute=true or when -enable-loop-distribute is specified. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 509 | OptimizePM.addPass(LoopDistributePass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 510 | |
| 511 | // Now run the core loop vectorizer. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 512 | OptimizePM.addPass(LoopVectorizePass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 513 | |
Chandler Carruth | baabda9 | 2017-01-27 01:32:26 +0000 | [diff] [blame] | 514 | // Eliminate loads by forwarding stores from the previous iteration to loads |
| 515 | // of the current iteration. |
| 516 | OptimizePM.addPass(LoopLoadEliminationPass()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 517 | |
| 518 | // Cleanup after the loop optimization passes. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 519 | OptimizePM.addPass(InstCombinePass()); |
| 520 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 521 | |
| 522 | // Now that we've formed fast to execute loop structures, we do further |
| 523 | // optimizations. These are run afterward as they might block doing complex |
| 524 | // analyses and transforms such as what are needed for loop vectorization. |
| 525 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 526 | // Optimize parallel scalar instruction chains into SIMD instructions. |
| 527 | OptimizePM.addPass(SLPVectorizerPass()); |
| 528 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 529 | // Cleanup after all of the vectorizers. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 530 | OptimizePM.addPass(SimplifyCFGPass()); |
| 531 | OptimizePM.addPass(InstCombinePass()); |
| 532 | |
| 533 | // Unroll small loops to hide loop backedge latency and saturate any parallel |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 534 | // execution resources of an out-of-order processor. We also then need to |
| 535 | // clean up redundancies and loop invariant code. |
| 536 | // FIXME: It would be really good to use a loop-integrated instruction |
| 537 | // combiner for cleanup here so that the unrolling and LICM can be pipelined |
| 538 | // across the loop nests. |
| 539 | OptimizePM.addPass(createFunctionToLoopPassAdaptor(LoopUnrollPass::create())); |
| 540 | OptimizePM.addPass(InstCombinePass()); |
Chandler Carruth | 0ede22e | 2017-02-09 23:54:57 +0000 | [diff] [blame] | 541 | OptimizePM.addPass(RequireAnalysisPass<OptimizationRemarkEmitterAnalysis, Function>()); |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 542 | OptimizePM.addPass(createFunctionToLoopPassAdaptor(LICMPass())); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 543 | |
| 544 | // Now that we've vectorized and unrolled loops, we may have more refined |
| 545 | // alignment information, try to re-derive it here. |
| 546 | OptimizePM.addPass(AlignmentFromAssumptionsPass()); |
| 547 | |
Chandler Carruth | a95ff38 | 2017-01-27 00:50:21 +0000 | [diff] [blame] | 548 | // LoopSink pass sinks instructions hoisted by LICM, which serves as a |
| 549 | // canonicalization pass that enables other optimizations. As a result, |
| 550 | // LoopSink pass needs to be a very late IR pass to avoid undoing LICM |
| 551 | // result too early. |
| 552 | OptimizePM.addPass(LoopSinkPass()); |
| 553 | |
| 554 | // And finally clean up LCSSA form before generating code. |
| 555 | OptimizePM.addPass(InstSimplifierPass()); |
| 556 | |
| 557 | // Add the core optimizing pipeline. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 558 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(OptimizePM))); |
| 559 | |
| 560 | // Now we need to do some global optimization transforms. |
| 561 | // FIXME: It would seem like these should come first in the optimization |
| 562 | // pipeline and maybe be the bottom of the canonicalization pipeline? Weird |
| 563 | // ordering here. |
| 564 | MPM.addPass(GlobalDCEPass()); |
| 565 | MPM.addPass(ConstantMergePass()); |
| 566 | |
| 567 | return MPM; |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 570 | ModulePassManager |
| 571 | PassBuilder::buildLTOPreLinkDefaultPipeline(OptimizationLevel Level, |
| 572 | bool DebugLogging) { |
| 573 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 574 | // FIXME: We should use a customized pre-link pipeline! |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 575 | return buildPerModuleDefaultPipeline(Level, DebugLogging); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 576 | } |
| 577 | |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 578 | ModulePassManager PassBuilder::buildLTODefaultPipeline(OptimizationLevel Level, |
| 579 | bool DebugLogging) { |
| 580 | assert(Level != O0 && "Must request optimizations for the default pipeline!"); |
| 581 | ModulePassManager MPM(DebugLogging); |
| 582 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 583 | // Remove unused virtual tables to improve the quality of code generated by |
| 584 | // whole-program devirtualization and bitset lowering. |
| 585 | MPM.addPass(GlobalDCEPass()); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 586 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 587 | // Force any function attributes we want the rest of the pipeline to observe. |
| 588 | MPM.addPass(ForceFunctionAttrsPass()); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 589 | |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 590 | // Do basic inference of function attributes from known properties of system |
| 591 | // libraries and other oracles. |
| 592 | MPM.addPass(InferFunctionAttrsPass()); |
| 593 | |
| 594 | if (Level > 1) { |
| 595 | // Indirect call promotion. This should promote all the targets that are |
| 596 | // left by the earlier promotion pass that promotes intra-module targets. |
| 597 | // This two-step promotion is to save the compile time. For LTO, it should |
| 598 | // produce the same result as if we only do promotion here. |
| 599 | MPM.addPass(PGOIndirectCallPromotion(true /* InLTO */)); |
| 600 | |
| 601 | // Propagate constants at call sites into the functions they call. This |
| 602 | // opens opportunities for globalopt (and inlining) by substituting function |
| 603 | // pointers passed as arguments to direct uses of functions. |
| 604 | MPM.addPass(IPSCCPPass()); |
| 605 | } |
| 606 | |
| 607 | // Now deduce any function attributes based in the current code. |
| 608 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( |
| 609 | PostOrderFunctionAttrsPass())); |
| 610 | |
| 611 | // Do RPO function attribute inference across the module to forward-propagate |
| 612 | // attributes where applicable. |
| 613 | // FIXME: Is this really an optimization rather than a canonicalization? |
| 614 | MPM.addPass(ReversePostOrderFunctionAttrsPass()); |
| 615 | |
| 616 | // Use inragne annotations on GEP indices to split globals where beneficial. |
| 617 | MPM.addPass(GlobalSplitPass()); |
| 618 | |
| 619 | // Run whole program optimization of virtual call when the list of callees |
| 620 | // is fixed. |
| 621 | MPM.addPass(WholeProgramDevirtPass()); |
| 622 | |
| 623 | // Stop here at -O1. |
| 624 | if (Level == 1) |
| 625 | return MPM; |
| 626 | |
| 627 | // Optimize globals to try and fold them into constants. |
| 628 | MPM.addPass(GlobalOptPass()); |
| 629 | |
| 630 | // Promote any localized globals to SSA registers. |
| 631 | MPM.addPass(createModuleToFunctionPassAdaptor(PromotePass())); |
| 632 | |
| 633 | // Linking modules together can lead to duplicate global constant, only |
| 634 | // keep one copy of each constant. |
| 635 | MPM.addPass(ConstantMergePass()); |
| 636 | |
| 637 | // Remove unused arguments from functions. |
| 638 | MPM.addPass(DeadArgumentEliminationPass()); |
| 639 | |
| 640 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 641 | // simplification opportunities, and both can propagate functions through |
| 642 | // function pointers. When this happens, we often have to resolve varargs |
| 643 | // calls, etc, so let instcombine do this. |
| 644 | // FIXME: add peephole extensions here as the legacy PM does. |
| 645 | MPM.addPass(createModuleToFunctionPassAdaptor(InstCombinePass())); |
| 646 | |
| 647 | // Note: historically, the PruneEH pass was run first to deduce nounwind and |
| 648 | // generally clean up exception handling overhead. It isn't clear this is |
| 649 | // valuable as the inliner doesn't currently care whether it is inlining an |
| 650 | // invoke or a call. |
| 651 | // Run the inliner now. |
| 652 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(InlinerPass())); |
| 653 | |
| 654 | // Optimize globals again after we ran the inliner. |
| 655 | MPM.addPass(GlobalOptPass()); |
| 656 | |
| 657 | // Garbage collect dead functions. |
| 658 | // FIXME: Add ArgumentPromotion pass after once it's ported. |
| 659 | MPM.addPass(GlobalDCEPass()); |
| 660 | |
| 661 | FunctionPassManager FPM(DebugLogging); |
| 662 | |
| 663 | // The IPO Passes may leave cruft around. Clean up after them. |
| 664 | // FIXME: add peephole extensions here as the legacy PM does. |
| 665 | FPM.addPass(InstCombinePass()); |
| 666 | FPM.addPass(JumpThreadingPass()); |
| 667 | |
| 668 | // Break up allocas |
| 669 | FPM.addPass(SROA()); |
| 670 | |
| 671 | // Run a few AA driver optimizations here and now to cleanup the code. |
| 672 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 673 | |
| 674 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor( |
| 675 | PostOrderFunctionAttrsPass())); |
| 676 | // FIXME: here we run IP alias analysis in the legacy PM. |
| 677 | |
| 678 | FunctionPassManager MainFPM; |
| 679 | |
| 680 | // FIXME: once we fix LoopPass Manager, add LICM here. |
| 681 | // FIXME: once we provide support for enabling MLSM, add it here. |
| 682 | // FIXME: once we provide support for enabling NewGVN, add it here. |
| 683 | MainFPM.addPass(GVN()); |
| 684 | |
| 685 | // Remove dead memcpy()'s. |
| 686 | MainFPM.addPass(MemCpyOptPass()); |
| 687 | |
| 688 | // Nuke dead stores. |
| 689 | MainFPM.addPass(DSEPass()); |
| 690 | |
| 691 | // FIXME: at this point, we run a bunch of loop passes: |
| 692 | // indVarSimplify, loopDeletion, loopInterchange, loopUnrool, |
| 693 | // loopVectorize. Enable them once the remaining issue with LPM |
| 694 | // are sorted out. |
| 695 | |
| 696 | MainFPM.addPass(InstCombinePass()); |
| 697 | MainFPM.addPass(SimplifyCFGPass()); |
| 698 | MainFPM.addPass(SCCPPass()); |
| 699 | MainFPM.addPass(InstCombinePass()); |
| 700 | MainFPM.addPass(BDCEPass()); |
| 701 | |
| 702 | // FIXME: We may want to run SLPVectorizer here. |
| 703 | // After vectorization, assume intrinsics may tell us more |
| 704 | // about pointer alignments. |
| 705 | #if 0 |
| 706 | MainFPM.add(AlignmentFromAssumptionsPass()); |
| 707 | #endif |
| 708 | |
| 709 | // FIXME: Conditionally run LoadCombine here, after it's ported |
| 710 | // (in case we still have this pass, given its questionable usefulness). |
| 711 | |
| 712 | // FIXME: add peephole extensions to the PM here. |
| 713 | MainFPM.addPass(InstCombinePass()); |
| 714 | MainFPM.addPass(JumpThreadingPass()); |
| 715 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(MainFPM))); |
| 716 | |
| 717 | // Create a function that performs CFI checks for cross-DSO calls with |
| 718 | // targets in the current module. |
| 719 | MPM.addPass(CrossDSOCFIPass()); |
| 720 | |
| 721 | // Lower type metadata and the type.test intrinsic. This pass supports |
| 722 | // clang's control flow integrity mechanisms (-fsanitize=cfi*) and needs |
| 723 | // to be run at link time if CFI is enabled. This pass does nothing if |
| 724 | // CFI is disabled. |
| 725 | // Enable once we add support for the summary in the new PM. |
| 726 | #if 0 |
Peter Collingbourne | 857aba4 | 2017-02-09 21:45:01 +0000 | [diff] [blame] | 727 | MPM.addPass(LowerTypeTestsPass(Summary ? PassSummaryAction::Export : |
| 728 | PassSummaryAction::None, |
Davide Italiano | 089a912 | 2017-01-24 00:57:39 +0000 | [diff] [blame] | 729 | Summary)); |
| 730 | #endif |
| 731 | |
| 732 | // Add late LTO optimization passes. |
| 733 | // Delete basic blocks, which optimization passes may have killed. |
| 734 | MPM.addPass(createModuleToFunctionPassAdaptor(SimplifyCFGPass())); |
| 735 | |
| 736 | // Drop bodies of available eternally objects to improve GlobalDCE. |
| 737 | MPM.addPass(EliminateAvailableExternallyPass()); |
| 738 | |
| 739 | // Now that we have optimized the program, discard unreachable functions. |
| 740 | MPM.addPass(GlobalDCEPass()); |
| 741 | |
| 742 | // FIXME: Enable MergeFuncs, conditionally, after ported, maybe. |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 743 | return MPM; |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 744 | } |
| 745 | |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 746 | AAManager PassBuilder::buildDefaultAAPipeline() { |
| 747 | AAManager AA; |
| 748 | |
| 749 | // The order in which these are registered determines their priority when |
| 750 | // being queried. |
| 751 | |
| 752 | // First we register the basic alias analysis that provides the majority of |
| 753 | // per-function local AA logic. This is a stateless, on-demand local set of |
| 754 | // AA techniques. |
| 755 | AA.registerFunctionAnalysis<BasicAA>(); |
| 756 | |
| 757 | // Next we query fast, specialized alias analyses that wrap IR-embedded |
| 758 | // information about aliasing. |
| 759 | AA.registerFunctionAnalysis<ScopedNoAliasAA>(); |
| 760 | AA.registerFunctionAnalysis<TypeBasedAA>(); |
| 761 | |
| 762 | // Add support for querying global aliasing information when available. |
Chandler Carruth | 534d644 | 2016-12-24 05:11:17 +0000 | [diff] [blame] | 763 | // Because the `AAManager` is a function analysis and `GlobalsAA` is a module |
| 764 | // analysis, all that the `AAManager` can do is query for any *cached* |
Chandler Carruth | e87fc8c | 2017-02-12 05:34:04 +0000 | [diff] [blame^] | 765 | // results from `GlobalsAA` through a readonly proxy. |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 766 | AA.registerModuleAnalysis<GlobalsAA>(); |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 767 | |
| 768 | return AA; |
| 769 | } |
| 770 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 771 | static Optional<int> parseRepeatPassName(StringRef Name) { |
| 772 | if (!Name.consume_front("repeat<") || !Name.consume_back(">")) |
| 773 | return None; |
| 774 | int Count; |
| 775 | if (Name.getAsInteger(0, Count) || Count <= 0) |
| 776 | return None; |
| 777 | return Count; |
| 778 | } |
| 779 | |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 780 | static Optional<int> parseDevirtPassName(StringRef Name) { |
| 781 | if (!Name.consume_front("devirt<") || !Name.consume_back(">")) |
| 782 | return None; |
| 783 | int Count; |
| 784 | if (Name.getAsInteger(0, Count) || Count <= 0) |
| 785 | return None; |
| 786 | return Count; |
| 787 | } |
| 788 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 789 | static bool isModulePassName(StringRef Name) { |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 790 | // Manually handle aliases for pre-configured pipeline fragments. |
| 791 | if (Name.startswith("default") || Name.startswith("lto")) |
| 792 | return DefaultAliasRegex.match(Name); |
| 793 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 794 | // Explicitly handle pass manager names. |
| 795 | if (Name == "module") |
| 796 | return true; |
| 797 | if (Name == "cgscc") |
| 798 | return true; |
| 799 | if (Name == "function") |
| 800 | return true; |
| 801 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 802 | // Explicitly handle custom-parsed pass names. |
| 803 | if (parseRepeatPassName(Name)) |
| 804 | return true; |
| 805 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 806 | #define MODULE_PASS(NAME, CREATE_PASS) \ |
| 807 | if (Name == NAME) \ |
| 808 | return true; |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 809 | #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 810 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 811 | return true; |
| 812 | #include "PassRegistry.def" |
| 813 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 814 | return false; |
| 815 | } |
| 816 | |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 817 | static bool isCGSCCPassName(StringRef Name) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 818 | // Explicitly handle pass manager names. |
| 819 | if (Name == "cgscc") |
| 820 | return true; |
| 821 | if (Name == "function") |
| 822 | return true; |
| 823 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 824 | // Explicitly handle custom-parsed pass names. |
| 825 | if (parseRepeatPassName(Name)) |
| 826 | return true; |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 827 | if (parseDevirtPassName(Name)) |
| 828 | return true; |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 829 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 830 | #define CGSCC_PASS(NAME, CREATE_PASS) \ |
| 831 | if (Name == NAME) \ |
| 832 | return true; |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 833 | #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 834 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 835 | return true; |
| 836 | #include "PassRegistry.def" |
| 837 | |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 838 | return false; |
| 839 | } |
| 840 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 841 | static bool isFunctionPassName(StringRef Name) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 842 | // Explicitly handle pass manager names. |
| 843 | if (Name == "function") |
| 844 | return true; |
| 845 | if (Name == "loop") |
| 846 | return true; |
| 847 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 848 | // Explicitly handle custom-parsed pass names. |
| 849 | if (parseRepeatPassName(Name)) |
| 850 | return true; |
| 851 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 852 | #define FUNCTION_PASS(NAME, CREATE_PASS) \ |
| 853 | if (Name == NAME) \ |
| 854 | return true; |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 855 | #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 856 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 857 | return true; |
| 858 | #include "PassRegistry.def" |
| 859 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 860 | return false; |
| 861 | } |
| 862 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 863 | static bool isLoopPassName(StringRef Name) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 864 | // Explicitly handle pass manager names. |
| 865 | if (Name == "loop") |
| 866 | return true; |
| 867 | |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 868 | // Explicitly handle custom-parsed pass names. |
| 869 | if (parseRepeatPassName(Name)) |
| 870 | return true; |
| 871 | |
Chandler Carruth | 74a8a22 | 2016-06-17 07:15:29 +0000 | [diff] [blame] | 872 | #define LOOP_PASS(NAME, CREATE_PASS) \ |
| 873 | if (Name == NAME) \ |
| 874 | return true; |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 875 | #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ |
| 876 | if (Name == "require<" NAME ">" || Name == "invalidate<" NAME ">") \ |
| 877 | return true; |
| 878 | #include "PassRegistry.def" |
| 879 | |
| 880 | return false; |
| 881 | } |
| 882 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 883 | Optional<std::vector<PassBuilder::PipelineElement>> |
| 884 | PassBuilder::parsePipelineText(StringRef Text) { |
| 885 | std::vector<PipelineElement> ResultPipeline; |
| 886 | |
| 887 | SmallVector<std::vector<PipelineElement> *, 4> PipelineStack = { |
| 888 | &ResultPipeline}; |
| 889 | for (;;) { |
| 890 | std::vector<PipelineElement> &Pipeline = *PipelineStack.back(); |
| 891 | size_t Pos = Text.find_first_of(",()"); |
| 892 | Pipeline.push_back({Text.substr(0, Pos), {}}); |
| 893 | |
| 894 | // If we have a single terminating name, we're done. |
| 895 | if (Pos == Text.npos) |
| 896 | break; |
| 897 | |
| 898 | char Sep = Text[Pos]; |
| 899 | Text = Text.substr(Pos + 1); |
| 900 | if (Sep == ',') |
| 901 | // Just a name ending in a comma, continue. |
| 902 | continue; |
| 903 | |
| 904 | if (Sep == '(') { |
| 905 | // Push the inner pipeline onto the stack to continue processing. |
| 906 | PipelineStack.push_back(&Pipeline.back().InnerPipeline); |
| 907 | continue; |
| 908 | } |
| 909 | |
| 910 | assert(Sep == ')' && "Bogus separator!"); |
| 911 | // When handling the close parenthesis, we greedily consume them to avoid |
| 912 | // empty strings in the pipeline. |
| 913 | do { |
| 914 | // If we try to pop the outer pipeline we have unbalanced parentheses. |
| 915 | if (PipelineStack.size() == 1) |
| 916 | return None; |
| 917 | |
| 918 | PipelineStack.pop_back(); |
| 919 | } while (Text.consume_front(")")); |
| 920 | |
| 921 | // Check if we've finished parsing. |
| 922 | if (Text.empty()) |
| 923 | break; |
| 924 | |
| 925 | // Otherwise, the end of an inner pipeline always has to be followed by |
| 926 | // a comma, and then we can continue. |
| 927 | if (!Text.consume_front(",")) |
| 928 | return None; |
| 929 | } |
| 930 | |
| 931 | if (PipelineStack.size() > 1) |
| 932 | // Unbalanced paretheses. |
| 933 | return None; |
| 934 | |
| 935 | assert(PipelineStack.back() == &ResultPipeline && |
| 936 | "Wrong pipeline at the bottom of the stack!"); |
| 937 | return {std::move(ResultPipeline)}; |
| 938 | } |
| 939 | |
| 940 | bool PassBuilder::parseModulePass(ModulePassManager &MPM, |
| 941 | const PipelineElement &E, bool VerifyEachPass, |
| 942 | bool DebugLogging) { |
| 943 | auto &Name = E.Name; |
| 944 | auto &InnerPipeline = E.InnerPipeline; |
| 945 | |
| 946 | // First handle complex passes like the pass managers which carry pipelines. |
| 947 | if (!InnerPipeline.empty()) { |
| 948 | if (Name == "module") { |
| 949 | ModulePassManager NestedMPM(DebugLogging); |
| 950 | if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass, |
| 951 | DebugLogging)) |
| 952 | return false; |
| 953 | MPM.addPass(std::move(NestedMPM)); |
| 954 | return true; |
| 955 | } |
| 956 | if (Name == "cgscc") { |
| 957 | CGSCCPassManager CGPM(DebugLogging); |
| 958 | if (!parseCGSCCPassPipeline(CGPM, InnerPipeline, VerifyEachPass, |
| 959 | DebugLogging)) |
| 960 | return false; |
| 961 | MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM), |
| 962 | DebugLogging)); |
| 963 | return true; |
| 964 | } |
| 965 | if (Name == "function") { |
| 966 | FunctionPassManager FPM(DebugLogging); |
| 967 | if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass, |
| 968 | DebugLogging)) |
| 969 | return false; |
| 970 | MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); |
| 971 | return true; |
| 972 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 973 | if (auto Count = parseRepeatPassName(Name)) { |
| 974 | ModulePassManager NestedMPM(DebugLogging); |
| 975 | if (!parseModulePassPipeline(NestedMPM, InnerPipeline, VerifyEachPass, |
| 976 | DebugLogging)) |
| 977 | return false; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 978 | MPM.addPass(createRepeatedPass(*Count, std::move(NestedMPM))); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 979 | return true; |
| 980 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 981 | // Normal passes can't have pipelines. |
| 982 | return false; |
| 983 | } |
| 984 | |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 985 | // Manually handle aliases for pre-configured pipeline fragments. |
| 986 | if (Name.startswith("default") || Name.startswith("lto")) { |
| 987 | SmallVector<StringRef, 3> Matches; |
| 988 | if (!DefaultAliasRegex.match(Name, &Matches)) |
| 989 | return false; |
| 990 | assert(Matches.size() == 3 && "Must capture two matched strings!"); |
| 991 | |
Jordan Rose | f85a95f | 2016-07-25 18:34:51 +0000 | [diff] [blame] | 992 | OptimizationLevel L = StringSwitch<OptimizationLevel>(Matches[2]) |
| 993 | .Case("O0", O0) |
| 994 | .Case("O1", O1) |
| 995 | .Case("O2", O2) |
| 996 | .Case("O3", O3) |
| 997 | .Case("Os", Os) |
| 998 | .Case("Oz", Oz); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 999 | if (L == O0) |
| 1000 | // At O0 we do nothing at all! |
| 1001 | return true; |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1002 | |
| 1003 | if (Matches[1] == "default") { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1004 | MPM.addPass(buildPerModuleDefaultPipeline(L, DebugLogging)); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1005 | } else if (Matches[1] == "lto-pre-link") { |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1006 | MPM.addPass(buildLTOPreLinkDefaultPipeline(L, DebugLogging)); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1007 | } else { |
| 1008 | assert(Matches[1] == "lto" && "Not one of the matched options!"); |
Chandler Carruth | e3f5064 | 2016-12-22 06:59:15 +0000 | [diff] [blame] | 1009 | MPM.addPass(buildLTODefaultPipeline(L, DebugLogging)); |
Chandler Carruth | 8b5a7419 | 2016-02-28 22:16:03 +0000 | [diff] [blame] | 1010 | } |
| 1011 | return true; |
| 1012 | } |
| 1013 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1014 | // Finally expand the basic registered passes from the .inc file. |
Chandler Carruth | 5894418 | 2014-04-21 08:08:50 +0000 | [diff] [blame] | 1015 | #define MODULE_PASS(NAME, CREATE_PASS) \ |
| 1016 | if (Name == NAME) { \ |
| 1017 | MPM.addPass(CREATE_PASS); \ |
| 1018 | return true; \ |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 1019 | } |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1020 | #define MODULE_ANALYSIS(NAME, CREATE_PASS) \ |
| 1021 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 1022 | MPM.addPass( \ |
| 1023 | RequireAnalysisPass< \ |
| 1024 | std::remove_reference<decltype(CREATE_PASS)>::type, Module>()); \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1025 | return true; \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1026 | } \ |
| 1027 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1028 | MPM.addPass(InvalidateAnalysisPass< \ |
| 1029 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1030 | return true; \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1031 | } |
| 1032 | #include "PassRegistry.def" |
| 1033 | |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1034 | return false; |
| 1035 | } |
| 1036 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1037 | bool PassBuilder::parseCGSCCPass(CGSCCPassManager &CGPM, |
| 1038 | const PipelineElement &E, bool VerifyEachPass, |
| 1039 | bool DebugLogging) { |
| 1040 | auto &Name = E.Name; |
| 1041 | auto &InnerPipeline = E.InnerPipeline; |
| 1042 | |
| 1043 | // First handle complex passes like the pass managers which carry pipelines. |
| 1044 | if (!InnerPipeline.empty()) { |
| 1045 | if (Name == "cgscc") { |
| 1046 | CGSCCPassManager NestedCGPM(DebugLogging); |
| 1047 | if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass, |
| 1048 | DebugLogging)) |
| 1049 | return false; |
| 1050 | // Add the nested pass manager with the appropriate adaptor. |
| 1051 | CGPM.addPass(std::move(NestedCGPM)); |
| 1052 | return true; |
| 1053 | } |
| 1054 | if (Name == "function") { |
| 1055 | FunctionPassManager FPM(DebugLogging); |
| 1056 | if (!parseFunctionPassPipeline(FPM, InnerPipeline, VerifyEachPass, |
| 1057 | DebugLogging)) |
| 1058 | return false; |
| 1059 | // Add the nested pass manager with the appropriate adaptor. |
| 1060 | CGPM.addPass( |
| 1061 | createCGSCCToFunctionPassAdaptor(std::move(FPM), DebugLogging)); |
| 1062 | return true; |
| 1063 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1064 | if (auto Count = parseRepeatPassName(Name)) { |
| 1065 | CGSCCPassManager NestedCGPM(DebugLogging); |
| 1066 | if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass, |
| 1067 | DebugLogging)) |
| 1068 | return false; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1069 | CGPM.addPass(createRepeatedPass(*Count, std::move(NestedCGPM))); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1070 | return true; |
| 1071 | } |
Chandler Carruth | 05ca5ac | 2016-12-28 11:07:33 +0000 | [diff] [blame] | 1072 | if (auto MaxRepetitions = parseDevirtPassName(Name)) { |
| 1073 | CGSCCPassManager NestedCGPM(DebugLogging); |
| 1074 | if (!parseCGSCCPassPipeline(NestedCGPM, InnerPipeline, VerifyEachPass, |
| 1075 | DebugLogging)) |
| 1076 | return false; |
| 1077 | CGPM.addPass(createDevirtSCCRepeatedPass(std::move(NestedCGPM), |
| 1078 | *MaxRepetitions, DebugLogging)); |
| 1079 | return true; |
| 1080 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1081 | // Normal passes can't have pipelines. |
| 1082 | return false; |
| 1083 | } |
| 1084 | |
| 1085 | // Now expand the basic registered passes from the .inc file. |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1086 | #define CGSCC_PASS(NAME, CREATE_PASS) \ |
| 1087 | if (Name == NAME) { \ |
| 1088 | CGPM.addPass(CREATE_PASS); \ |
| 1089 | return true; \ |
| 1090 | } |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1091 | #define CGSCC_ANALYSIS(NAME, CREATE_PASS) \ |
| 1092 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1093 | CGPM.addPass(RequireAnalysisPass< \ |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 1094 | std::remove_reference<decltype(CREATE_PASS)>::type, \ |
Chandler Carruth | 8882346 | 2016-08-24 09:37:14 +0000 | [diff] [blame] | 1095 | LazyCallGraph::SCC, CGSCCAnalysisManager, LazyCallGraph &, \ |
| 1096 | CGSCCUpdateResult &>()); \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1097 | return true; \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1098 | } \ |
| 1099 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1100 | CGPM.addPass(InvalidateAnalysisPass< \ |
| 1101 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1102 | return true; \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1103 | } |
| 1104 | #include "PassRegistry.def" |
| 1105 | |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1106 | return false; |
| 1107 | } |
| 1108 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1109 | bool PassBuilder::parseFunctionPass(FunctionPassManager &FPM, |
| 1110 | const PipelineElement &E, |
| 1111 | bool VerifyEachPass, bool DebugLogging) { |
| 1112 | auto &Name = E.Name; |
| 1113 | auto &InnerPipeline = E.InnerPipeline; |
| 1114 | |
| 1115 | // First handle complex passes like the pass managers which carry pipelines. |
| 1116 | if (!InnerPipeline.empty()) { |
| 1117 | if (Name == "function") { |
| 1118 | FunctionPassManager NestedFPM(DebugLogging); |
| 1119 | if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass, |
| 1120 | DebugLogging)) |
| 1121 | return false; |
| 1122 | // Add the nested pass manager with the appropriate adaptor. |
| 1123 | FPM.addPass(std::move(NestedFPM)); |
| 1124 | return true; |
| 1125 | } |
| 1126 | if (Name == "loop") { |
| 1127 | LoopPassManager LPM(DebugLogging); |
| 1128 | if (!parseLoopPassPipeline(LPM, InnerPipeline, VerifyEachPass, |
| 1129 | DebugLogging)) |
| 1130 | return false; |
| 1131 | // Add the nested pass manager with the appropriate adaptor. |
| 1132 | FPM.addPass(createFunctionToLoopPassAdaptor(std::move(LPM))); |
| 1133 | return true; |
| 1134 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1135 | if (auto Count = parseRepeatPassName(Name)) { |
| 1136 | FunctionPassManager NestedFPM(DebugLogging); |
| 1137 | if (!parseFunctionPassPipeline(NestedFPM, InnerPipeline, VerifyEachPass, |
| 1138 | DebugLogging)) |
| 1139 | return false; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1140 | FPM.addPass(createRepeatedPass(*Count, std::move(NestedFPM))); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1141 | return true; |
| 1142 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1143 | // Normal passes can't have pipelines. |
| 1144 | return false; |
| 1145 | } |
| 1146 | |
| 1147 | // Now expand the basic registered passes from the .inc file. |
Chandler Carruth | 5894418 | 2014-04-21 08:08:50 +0000 | [diff] [blame] | 1148 | #define FUNCTION_PASS(NAME, CREATE_PASS) \ |
| 1149 | if (Name == NAME) { \ |
| 1150 | FPM.addPass(CREATE_PASS); \ |
| 1151 | return true; \ |
Chandler Carruth | 52eef88 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 1152 | } |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1153 | #define FUNCTION_ANALYSIS(NAME, CREATE_PASS) \ |
| 1154 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | 9b35e6d | 2016-08-19 18:36:06 +0000 | [diff] [blame] | 1155 | FPM.addPass( \ |
| 1156 | RequireAnalysisPass< \ |
| 1157 | std::remove_reference<decltype(CREATE_PASS)>::type, Function>()); \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1158 | return true; \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1159 | } \ |
| 1160 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1161 | FPM.addPass(InvalidateAnalysisPass< \ |
| 1162 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Chandler Carruth | 3472ffb | 2015-01-06 04:49:44 +0000 | [diff] [blame] | 1163 | return true; \ |
Chandler Carruth | 628503e | 2015-01-06 02:10:51 +0000 | [diff] [blame] | 1164 | } |
| 1165 | #include "PassRegistry.def" |
| 1166 | |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 1167 | return false; |
| 1168 | } |
| 1169 | |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1170 | bool PassBuilder::parseLoopPass(LoopPassManager &LPM, const PipelineElement &E, |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1171 | bool VerifyEachPass, bool DebugLogging) { |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1172 | StringRef Name = E.Name; |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1173 | auto &InnerPipeline = E.InnerPipeline; |
| 1174 | |
| 1175 | // First handle complex passes like the pass managers which carry pipelines. |
| 1176 | if (!InnerPipeline.empty()) { |
| 1177 | if (Name == "loop") { |
| 1178 | LoopPassManager NestedLPM(DebugLogging); |
| 1179 | if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass, |
| 1180 | DebugLogging)) |
| 1181 | return false; |
| 1182 | // Add the nested pass manager with the appropriate adaptor. |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1183 | LPM.addPass(std::move(NestedLPM)); |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1184 | return true; |
| 1185 | } |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1186 | if (auto Count = parseRepeatPassName(Name)) { |
| 1187 | LoopPassManager NestedLPM(DebugLogging); |
| 1188 | if (!parseLoopPassPipeline(NestedLPM, InnerPipeline, VerifyEachPass, |
| 1189 | DebugLogging)) |
| 1190 | return false; |
Chandler Carruth | a053a88 | 2016-08-04 03:52:53 +0000 | [diff] [blame] | 1191 | LPM.addPass(createRepeatedPass(*Count, std::move(NestedLPM))); |
Chandler Carruth | 241bf24 | 2016-08-03 07:44:48 +0000 | [diff] [blame] | 1192 | return true; |
| 1193 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1194 | // Normal passes can't have pipelines. |
| 1195 | return false; |
| 1196 | } |
| 1197 | |
| 1198 | // Now expand the basic registered passes from the .inc file. |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1199 | #define LOOP_PASS(NAME, CREATE_PASS) \ |
| 1200 | if (Name == NAME) { \ |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1201 | LPM.addPass(CREATE_PASS); \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1202 | return true; \ |
| 1203 | } |
| 1204 | #define LOOP_ANALYSIS(NAME, CREATE_PASS) \ |
| 1205 | if (Name == "require<" NAME ">") { \ |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1206 | LPM.addPass(RequireAnalysisPass< \ |
Chandler Carruth | 410eaeb | 2017-01-11 06:23:21 +0000 | [diff] [blame] | 1207 | std::remove_reference<decltype(CREATE_PASS)>::type, Loop, \ |
| 1208 | LoopAnalysisManager, LoopStandardAnalysisResults &, \ |
| 1209 | LPMUpdater &>()); \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1210 | return true; \ |
| 1211 | } \ |
| 1212 | if (Name == "invalidate<" NAME ">") { \ |
Chandler Carruth | fdc6ba1 | 2016-08-03 09:14:03 +0000 | [diff] [blame] | 1213 | LPM.addPass(InvalidateAnalysisPass< \ |
Chandler Carruth | 470734b | 2016-02-26 12:30:18 +0000 | [diff] [blame] | 1214 | std::remove_reference<decltype(CREATE_PASS)>::type>()); \ |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1215 | return true; \ |
| 1216 | } |
| 1217 | #include "PassRegistry.def" |
| 1218 | |
| 1219 | return false; |
| 1220 | } |
| 1221 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1222 | bool PassBuilder::parseAAPassName(AAManager &AA, StringRef Name) { |
Chandler Carruth | 45a9c20 | 2016-03-11 09:15:11 +0000 | [diff] [blame] | 1223 | #define MODULE_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ |
| 1224 | if (Name == NAME) { \ |
| 1225 | AA.registerModuleAnalysis< \ |
| 1226 | std::remove_reference<decltype(CREATE_PASS)>::type>(); \ |
| 1227 | return true; \ |
| 1228 | } |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1229 | #define FUNCTION_ALIAS_ANALYSIS(NAME, CREATE_PASS) \ |
| 1230 | if (Name == NAME) { \ |
Chandler Carruth | 58dde8c | 2016-02-26 12:17:54 +0000 | [diff] [blame] | 1231 | AA.registerFunctionAnalysis< \ |
| 1232 | std::remove_reference<decltype(CREATE_PASS)>::type>(); \ |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1233 | return true; \ |
| 1234 | } |
| 1235 | #include "PassRegistry.def" |
| 1236 | |
| 1237 | return false; |
| 1238 | } |
| 1239 | |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1240 | bool PassBuilder::parseLoopPassPipeline(LoopPassManager &LPM, |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1241 | ArrayRef<PipelineElement> Pipeline, |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1242 | bool VerifyEachPass, |
| 1243 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1244 | for (const auto &Element : Pipeline) { |
| 1245 | if (!parseLoopPass(LPM, Element, VerifyEachPass, DebugLogging)) |
| 1246 | return false; |
| 1247 | // FIXME: No verifier support for Loop passes! |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1248 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1249 | return true; |
Justin Bogner | eecc3c8 | 2016-02-25 07:23:08 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1252 | bool PassBuilder::parseFunctionPassPipeline(FunctionPassManager &FPM, |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1253 | ArrayRef<PipelineElement> Pipeline, |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1254 | bool VerifyEachPass, |
| 1255 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1256 | for (const auto &Element : Pipeline) { |
| 1257 | if (!parseFunctionPass(FPM, Element, VerifyEachPass, DebugLogging)) |
| 1258 | return false; |
| 1259 | if (VerifyEachPass) |
| 1260 | FPM.addPass(VerifierPass()); |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 1261 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1262 | return true; |
Chandler Carruth | d833098 | 2014-01-12 09:34:22 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1265 | bool PassBuilder::parseCGSCCPassPipeline(CGSCCPassManager &CGPM, |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1266 | ArrayRef<PipelineElement> Pipeline, |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1267 | bool VerifyEachPass, |
| 1268 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1269 | for (const auto &Element : Pipeline) { |
| 1270 | if (!parseCGSCCPass(CGPM, Element, VerifyEachPass, DebugLogging)) |
| 1271 | return false; |
| 1272 | // FIXME: No verifier support for CGSCC passes! |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1273 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1274 | return true; |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
NAKAMURA Takumi | a42f528 | 2016-05-16 10:13:37 +0000 | [diff] [blame] | 1277 | void PassBuilder::crossRegisterProxies(LoopAnalysisManager &LAM, |
| 1278 | FunctionAnalysisManager &FAM, |
| 1279 | CGSCCAnalysisManager &CGAM, |
| 1280 | ModuleAnalysisManager &MAM) { |
| 1281 | MAM.registerPass([&] { return FunctionAnalysisManagerModuleProxy(FAM); }); |
| 1282 | MAM.registerPass([&] { return CGSCCAnalysisManagerModuleProxy(CGAM); }); |
NAKAMURA Takumi | a42f528 | 2016-05-16 10:13:37 +0000 | [diff] [blame] | 1283 | CGAM.registerPass([&] { return ModuleAnalysisManagerCGSCCProxy(MAM); }); |
| 1284 | FAM.registerPass([&] { return CGSCCAnalysisManagerFunctionProxy(CGAM); }); |
| 1285 | FAM.registerPass([&] { return ModuleAnalysisManagerFunctionProxy(MAM); }); |
| 1286 | FAM.registerPass([&] { return LoopAnalysisManagerFunctionProxy(LAM); }); |
| 1287 | LAM.registerPass([&] { return FunctionAnalysisManagerLoopProxy(FAM); }); |
| 1288 | } |
| 1289 | |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1290 | bool PassBuilder::parseModulePassPipeline(ModulePassManager &MPM, |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1291 | ArrayRef<PipelineElement> Pipeline, |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1292 | bool VerifyEachPass, |
| 1293 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1294 | for (const auto &Element : Pipeline) { |
| 1295 | if (!parseModulePass(MPM, Element, VerifyEachPass, DebugLogging)) |
| 1296 | return false; |
| 1297 | if (VerifyEachPass) |
| 1298 | MPM.addPass(VerifierPass()); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1299 | } |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1300 | return true; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1301 | } |
| 1302 | |
| 1303 | // Primary pass pipeline description parsing routine. |
| 1304 | // FIXME: Should this routine accept a TargetMachine or require the caller to |
| 1305 | // pre-populate the analysis managers with target-specific stuff? |
Chandler Carruth | 1ff7724 | 2015-03-07 09:02:36 +0000 | [diff] [blame] | 1306 | bool PassBuilder::parsePassPipeline(ModulePassManager &MPM, |
| 1307 | StringRef PipelineText, bool VerifyEachPass, |
| 1308 | bool DebugLogging) { |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1309 | auto Pipeline = parsePipelineText(PipelineText); |
| 1310 | if (!Pipeline || Pipeline->empty()) |
| 1311 | return false; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1312 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1313 | // If the first name isn't at the module layer, wrap the pipeline up |
| 1314 | // automatically. |
| 1315 | StringRef FirstName = Pipeline->front().Name; |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1316 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1317 | if (!isModulePassName(FirstName)) { |
| 1318 | if (isCGSCCPassName(FirstName)) |
| 1319 | Pipeline = {{"cgscc", std::move(*Pipeline)}}; |
| 1320 | else if (isFunctionPassName(FirstName)) |
| 1321 | Pipeline = {{"function", std::move(*Pipeline)}}; |
| 1322 | else if (isLoopPassName(FirstName)) |
| 1323 | Pipeline = {{"function", {{"loop", std::move(*Pipeline)}}}}; |
| 1324 | else |
| 1325 | // Unknown pass name! |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1326 | return false; |
Chandler Carruth | 572e340 | 2014-04-21 11:12:00 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Chandler Carruth | 6cb2ab2 | 2016-08-03 03:21:41 +0000 | [diff] [blame] | 1329 | return parseModulePassPipeline(MPM, *Pipeline, VerifyEachPass, DebugLogging); |
Chandler Carruth | 6644538 | 2014-01-11 08:16:35 +0000 | [diff] [blame] | 1330 | } |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1331 | |
| 1332 | bool PassBuilder::parseAAPipeline(AAManager &AA, StringRef PipelineText) { |
Chandler Carruth | 060ad61 | 2016-12-23 20:38:19 +0000 | [diff] [blame] | 1333 | // If the pipeline just consists of the word 'default' just replace the AA |
| 1334 | // manager with our default one. |
| 1335 | if (PipelineText == "default") { |
| 1336 | AA = buildDefaultAAPipeline(); |
| 1337 | return true; |
| 1338 | } |
| 1339 | |
Chandler Carruth | edf5996 | 2016-02-18 09:45:17 +0000 | [diff] [blame] | 1340 | while (!PipelineText.empty()) { |
| 1341 | StringRef Name; |
| 1342 | std::tie(Name, PipelineText) = PipelineText.split(','); |
| 1343 | if (!parseAAPassName(AA, Name)) |
| 1344 | return false; |
| 1345 | } |
| 1346 | |
| 1347 | return true; |
| 1348 | } |