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