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