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