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