Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 1 | //===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===// |
| 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 | // |
| 10 | // This file defines the PassManagerBuilder class, which is used to set up a |
| 11 | // "standard" optimization sequence suitable for languages like C and C++. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 15 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 16 | #include "llvm-c/Transforms/PassManagerBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 17 | #include "llvm/ADT/SmallVector.h" |
Chandler Carruth | 17e0bc3 | 2015-08-06 07:33:15 +0000 | [diff] [blame] | 18 | #include "llvm/Analysis/BasicAliasAnalysis.h" |
Chandler Carruth | 8b046a4 | 2015-08-14 02:42:20 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/CFLAliasAnalysis.h" |
Chandler Carruth | 21dcff7 | 2015-08-14 03:48:20 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/GlobalsModRef.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/Passes.h" |
Chandler Carruth | 42ff448 | 2015-08-14 02:55:50 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/ScopedNoAliasAA.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 23 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Chandler Carruth | 1db2282 | 2015-08-14 03:33:48 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/TypeBasedAliasAnalysis.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 25 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 26 | #include "llvm/IR/LegacyPassManager.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 27 | #include "llvm/IR/ModuleSummaryIndex.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 28 | #include "llvm/IR/Verifier.h" |
| 29 | #include "llvm/Support/CommandLine.h" |
| 30 | #include "llvm/Support/ManagedStatic.h" |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 31 | #include "llvm/Target/TargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 32 | #include "llvm/Transforms/IPO.h" |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/IPO/ForceFunctionAttrs.h" |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/IPO/FunctionAttrs.h" |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/IPO/InferFunctionAttrs.h" |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 36 | #include "llvm/Transforms/Instrumentation.h" |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 37 | #include "llvm/Transforms/Scalar.h" |
Chandler Carruth | 89c45a1 | 2016-03-11 08:50:55 +0000 | [diff] [blame] | 38 | #include "llvm/Transforms/Scalar/GVN.h" |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 39 | #include "llvm/Transforms/Vectorize.h" |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 40 | |
| 41 | using namespace llvm; |
| 42 | |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 44 | RunLoopVectorization("vectorize-loops", cl::Hidden, |
Nadav Rotem | d3df665 | 2012-10-30 18:37:43 +0000 | [diff] [blame] | 45 | cl::desc("Run the Loop vectorization passes")); |
Nadav Rotem | c59ae20 | 2012-10-29 16:36:25 +0000 | [diff] [blame] | 46 | |
| 47 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 48 | RunSLPVectorization("vectorize-slp", cl::Hidden, |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 49 | cl::desc("Run the SLP vectorization passes")); |
| 50 | |
| 51 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 52 | RunBBVectorization("vectorize-slp-aggressive", cl::Hidden, |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 53 | cl::desc("Run the BB vectorization passes")); |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 54 | |
Hal Finkel | 204bf53 | 2012-04-13 17:15:33 +0000 | [diff] [blame] | 55 | static cl::opt<bool> |
| 56 | UseGVNAfterVectorization("use-gvn-after-vectorization", |
| 57 | cl::init(false), cl::Hidden, |
| 58 | cl::desc("Run GVN instead of Early CSE after vectorization passes")); |
| 59 | |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 60 | static cl::opt<bool> ExtraVectorizerPasses( |
| 61 | "extra-vectorizer-passes", cl::init(false), cl::Hidden, |
| 62 | cl::desc("Run cleanup optimization passes after vectorization.")); |
| 63 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 64 | static cl::opt<bool> |
| 65 | RunLoopRerolling("reroll-loops", cl::Hidden, |
| 66 | cl::desc("Run the loop rerolling pass")); |
| 67 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 68 | static cl::opt<bool> |
| 69 | RunFloat2Int("float-to-int", cl::Hidden, cl::init(true), |
| 70 | cl::desc("Run the float2int (float demotion) pass")); |
| 71 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 72 | static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false), |
| 73 | cl::Hidden, |
| 74 | cl::desc("Run the load combining pass")); |
| 75 | |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 76 | static cl::opt<bool> |
| 77 | RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization", |
James Molloy | 6b95d8e | 2014-09-04 13:23:08 +0000 | [diff] [blame] | 78 | cl::init(true), cl::Hidden, |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 79 | cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop " |
| 80 | "vectorizer instead of before")); |
| 81 | |
Hal Finkel | 445dda5 | 2014-09-02 22:12:54 +0000 | [diff] [blame] | 82 | static cl::opt<bool> UseCFLAA("use-cfl-aa", |
| 83 | cl::init(false), cl::Hidden, |
| 84 | cl::desc("Enable the new, experimental CFL alias analysis")); |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 85 | |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 86 | static cl::opt<bool> |
Gerolf Hoflehner | 008e5cd | 2014-09-10 20:24:03 +0000 | [diff] [blame] | 87 | EnableMLSM("mlsm", cl::init(true), cl::Hidden, |
| 88 | cl::desc("Enable motion of merged load and store")); |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 89 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 90 | static cl::opt<bool> EnableLoopInterchange( |
| 91 | "enable-loopinterchange", cl::init(false), cl::Hidden, |
| 92 | cl::desc("Enable the new, experimental LoopInterchange Pass")); |
| 93 | |
Chandler Carruth | e9ea5a6 | 2015-07-22 11:57:28 +0000 | [diff] [blame] | 94 | static cl::opt<bool> EnableNonLTOGlobalsModRef( |
James Molloy | 4015925 | 2015-10-13 10:43:57 +0000 | [diff] [blame] | 95 | "enable-non-lto-gmr", cl::init(true), cl::Hidden, |
Chandler Carruth | e9ea5a6 | 2015-07-22 11:57:28 +0000 | [diff] [blame] | 96 | cl::desc( |
| 97 | "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline.")); |
| 98 | |
Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 99 | static cl::opt<bool> EnableLoopLoadElim( |
Adam Nemet | c979c6e | 2016-03-15 22:26:12 +0000 | [diff] [blame] | 100 | "enable-loop-load-elim", cl::init(true), cl::Hidden, |
| 101 | cl::desc("Enable the LoopLoadElimination Pass")); |
Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 102 | |
Rong Xu | 34abbfb | 2016-01-21 18:28:59 +0000 | [diff] [blame] | 103 | static cl::opt<std::string> RunPGOInstrGen( |
| 104 | "profile-generate", cl::init(""), cl::Hidden, |
| 105 | cl::desc("Enable generation phase of PGO instrumentation and specify the " |
| 106 | "path of profile data file")); |
| 107 | |
| 108 | static cl::opt<std::string> RunPGOInstrUse( |
| 109 | "profile-use", cl::init(""), cl::Hidden, cl::value_desc("filename"), |
| 110 | cl::desc("Enable use phase of PGO instrumentation and specify the path " |
| 111 | "of profile data file")); |
| 112 | |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 113 | static cl::opt<bool> UseLoopVersioningLICM( |
| 114 | "enable-loop-versioning-licm", cl::init(false), cl::Hidden, |
| 115 | cl::desc("Enable the experimental Loop Versioning LICM pass")); |
| 116 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 117 | PassManagerBuilder::PassManagerBuilder() { |
| 118 | OptLevel = 2; |
| 119 | SizeLevel = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 120 | LibraryInfo = nullptr; |
| 121 | Inliner = nullptr; |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 122 | ModuleSummary = nullptr; |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 123 | DisableUnitAtATime = false; |
| 124 | DisableUnrollLoops = false; |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 125 | BBVectorize = RunBBVectorization; |
Nadav Rotem | a1e5e44 | 2013-04-15 04:54:42 +0000 | [diff] [blame] | 126 | SLPVectorize = RunSLPVectorization; |
Nadav Rotem | c59ae20 | 2012-10-29 16:36:25 +0000 | [diff] [blame] | 127 | LoopVectorize = RunLoopVectorization; |
Hal Finkel | 29aeb20 | 2013-11-17 16:02:50 +0000 | [diff] [blame] | 128 | RerollLoops = RunLoopRerolling; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 129 | LoadCombine = RunLoadCombine; |
Rafael Espindola | 208bc53 | 2014-08-21 13:13:17 +0000 | [diff] [blame] | 130 | DisableGVNLoadPRE = false; |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 131 | VerifyInput = false; |
| 132 | VerifyOutput = false; |
Nick Lewycky | 9e6d184 | 2014-09-13 21:46:00 +0000 | [diff] [blame] | 133 | MergeFunctions = false; |
Teresa Johnson | d3a33a1 | 2015-07-06 16:22:42 +0000 | [diff] [blame] | 134 | PrepareForLTO = false; |
Rong Xu | 34abbfb | 2016-01-21 18:28:59 +0000 | [diff] [blame] | 135 | PGOInstrGen = RunPGOInstrGen; |
| 136 | PGOInstrUse = RunPGOInstrUse; |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 137 | PrepareForThinLTO = false; |
| 138 | PerformThinLTO = false; |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | PassManagerBuilder::~PassManagerBuilder() { |
| 142 | delete LibraryInfo; |
| 143 | delete Inliner; |
| 144 | } |
| 145 | |
David Chisnall | 719a72f | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 146 | /// Set of global extensions, automatically added as part of the standard set. |
| 147 | static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, |
| 148 | PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; |
| 149 | |
| 150 | void PassManagerBuilder::addGlobalExtension( |
| 151 | PassManagerBuilder::ExtensionPointTy Ty, |
| 152 | PassManagerBuilder::ExtensionFn Fn) { |
Justin Lebar | 2fe1323 | 2016-03-30 20:39:29 +0000 | [diff] [blame] | 153 | GlobalExtensions->push_back(std::make_pair(Ty, std::move(Fn))); |
David Chisnall | 719a72f | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 156 | void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { |
Justin Lebar | 2fe1323 | 2016-03-30 20:39:29 +0000 | [diff] [blame] | 157 | Extensions.push_back(std::make_pair(Ty, std::move(Fn))); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 161 | legacy::PassManagerBase &PM) const { |
David Chisnall | 719a72f | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 162 | for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) |
| 163 | if ((*GlobalExtensions)[i].first == ETy) |
| 164 | (*GlobalExtensions)[i].second(*this, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 165 | for (unsigned i = 0, e = Extensions.size(); i != e; ++i) |
| 166 | if (Extensions[i].first == ETy) |
| 167 | Extensions[i].second(*this, PM); |
| 168 | } |
| 169 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 170 | void PassManagerBuilder::addInitialAliasAnalysisPasses( |
| 171 | legacy::PassManagerBase &PM) const { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 172 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 173 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 174 | // support "obvious" type-punning idioms. |
Hal Finkel | 445dda5 | 2014-09-02 22:12:54 +0000 | [diff] [blame] | 175 | if (UseCFLAA) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 176 | PM.add(createCFLAAWrapperPass()); |
| 177 | PM.add(createTypeBasedAAWrapperPass()); |
| 178 | PM.add(createScopedNoAliasAAWrapperPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 181 | void PassManagerBuilder::addInstructionCombiningPass( |
| 182 | legacy::PassManagerBase &PM) const { |
| 183 | bool ExpensiveCombines = OptLevel > 2; |
| 184 | PM.add(createInstructionCombiningPass(ExpensiveCombines)); |
| 185 | } |
| 186 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 187 | void PassManagerBuilder::populateFunctionPassManager( |
| 188 | legacy::FunctionPassManager &FPM) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 189 | addExtensionsToPM(EP_EarlyAsPossible, FPM); |
| 190 | |
| 191 | // Add LibraryInfo if we have some. |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 192 | if (LibraryInfo) |
| 193 | FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 194 | |
| 195 | if (OptLevel == 0) return; |
| 196 | |
| 197 | addInitialAliasAnalysisPasses(FPM); |
| 198 | |
| 199 | FPM.add(createCFGSimplificationPass()); |
David Majnemer | cbf614a | 2016-06-15 00:19:09 +0000 | [diff] [blame^] | 200 | FPM.add(createSROAPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 201 | FPM.add(createEarlyCSEPass()); |
| 202 | FPM.add(createLowerExpectIntrinsicPass()); |
| 203 | } |
| 204 | |
Rong Xu | 34abbfb | 2016-01-21 18:28:59 +0000 | [diff] [blame] | 205 | // Do PGO instrumentation generation or use pass as the option specified. |
| 206 | void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) { |
| 207 | if (!PGOInstrGen.empty()) { |
Xinliang David Li | 8aebf44 | 2016-05-06 05:49:19 +0000 | [diff] [blame] | 208 | MPM.add(createPGOInstrumentationGenLegacyPass()); |
Rong Xu | 34abbfb | 2016-01-21 18:28:59 +0000 | [diff] [blame] | 209 | // Add the profile lowering pass. |
| 210 | InstrProfOptions Options; |
| 211 | Options.InstrProfileOutput = PGOInstrGen; |
Xinliang David Li | e6b8929 | 2016-04-18 17:47:38 +0000 | [diff] [blame] | 212 | MPM.add(createInstrProfilingLegacyPass(Options)); |
Rong Xu | 34abbfb | 2016-01-21 18:28:59 +0000 | [diff] [blame] | 213 | } |
| 214 | if (!PGOInstrUse.empty()) |
Xinliang David Li | d55827f | 2016-05-07 05:39:12 +0000 | [diff] [blame] | 215 | MPM.add(createPGOInstrumentationUseLegacyPass(PGOInstrUse)); |
Rong Xu | 34abbfb | 2016-01-21 18:28:59 +0000 | [diff] [blame] | 216 | } |
Mehdi Amini | ec8bee1 | 2016-02-16 22:54:27 +0000 | [diff] [blame] | 217 | void PassManagerBuilder::addFunctionSimplificationPasses( |
Mehdi Amini | 9c1c3ac | 2016-02-11 22:09:11 +0000 | [diff] [blame] | 218 | legacy::PassManagerBase &MPM) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 219 | // Start of function pass. |
| 220 | // Break up aggregate allocas, using SSAUpdater. |
David Majnemer | cbf614a | 2016-06-15 00:19:09 +0000 | [diff] [blame^] | 221 | MPM.add(createSROAPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 222 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
Justin Lebar | cf63b64 | 2016-04-15 00:32:12 +0000 | [diff] [blame] | 223 | // Speculative execution if the target has divergent branches; otherwise nop. |
| 224 | MPM.add(createSpeculativeExecutionIfHasBranchDivergencePass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 225 | MPM.add(createJumpThreadingPass()); // Thread jumps. |
| 226 | MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals |
| 227 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 228 | // Combine silly seq's |
| 229 | addInstructionCombiningPass(MPM); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 230 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 231 | |
Akira Hatanaka | d9699bc | 2015-06-09 19:07:19 +0000 | [diff] [blame] | 232 | MPM.add(createTailCallEliminationPass()); // Eliminate tail calls |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 233 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
| 234 | MPM.add(createReassociatePass()); // Reassociate expressions |
Roman Divacky | d2b9a1b | 2014-11-21 19:53:24 +0000 | [diff] [blame] | 235 | // Rotate Loop - disable header duplication at -Oz |
| 236 | MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 237 | MPM.add(createLICMPass()); // Hoist loop invariants |
| 238 | MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); |
Michael Zolotukhin | 74621cc | 2015-09-24 03:50:17 +0000 | [diff] [blame] | 239 | MPM.add(createCFGSimplificationPass()); |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 240 | addInstructionCombiningPass(MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 241 | MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars |
| 242 | MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. |
| 243 | MPM.add(createLoopDeletionPass()); // Delete dead loops |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 244 | if (EnableLoopInterchange) { |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 245 | MPM.add(createLoopInterchangePass()); // Interchange loops |
Karthik Bhat | 8210fdf | 2015-04-23 04:51:44 +0000 | [diff] [blame] | 246 | MPM.add(createCFGSimplificationPass()); |
| 247 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 248 | if (!DisableUnrollLoops) |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 249 | MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 250 | addExtensionsToPM(EP_LoopOptimizerEnd, MPM); |
| 251 | |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 252 | if (OptLevel > 1) { |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 253 | if (EnableMLSM) |
| 254 | MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds |
Rafael Espindola | 208bc53 | 2014-08-21 13:13:17 +0000 | [diff] [blame] | 255 | MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies |
Gerolf Hoflehner | f27ae6c | 2014-07-18 19:13:09 +0000 | [diff] [blame] | 256 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 257 | MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset |
| 258 | MPM.add(createSCCPPass()); // Constant prop with SCCP |
| 259 | |
Hal Finkel | 2bb61ba | 2015-02-17 01:36:59 +0000 | [diff] [blame] | 260 | // Delete dead bit computations (instcombine runs after to fold away the dead |
| 261 | // computations, and then ADCE will run later to exploit any new DCE |
| 262 | // opportunities that creates). |
| 263 | MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations |
| 264 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 265 | // Run instcombine after redundancy elimination to exploit opportunities |
| 266 | // opened up by them. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 267 | addInstructionCombiningPass(MPM); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 268 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 269 | MPM.add(createJumpThreadingPass()); // Thread jumps |
| 270 | MPM.add(createCorrelatedValuePropagationPass()); |
| 271 | MPM.add(createDeadStoreEliminationPass()); // Delete dead stores |
James Molloy | 8357024 | 2015-02-16 18:59:54 +0000 | [diff] [blame] | 272 | MPM.add(createLICMPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 273 | |
| 274 | addExtensionsToPM(EP_ScalarOptimizerLate, MPM); |
| 275 | |
Hal Finkel | 29aeb20 | 2013-11-17 16:02:50 +0000 | [diff] [blame] | 276 | if (RerollLoops) |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 277 | MPM.add(createLoopRerollPass()); |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 278 | if (!RunSLPAfterLoopVectorization) { |
| 279 | if (SLPVectorize) |
| 280 | MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 281 | |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 282 | if (BBVectorize) { |
| 283 | MPM.add(createBBVectorizePass()); |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 284 | addInstructionCombiningPass(MPM); |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 285 | addExtensionsToPM(EP_Peephole, MPM); |
| 286 | if (OptLevel > 1 && UseGVNAfterVectorization) |
Rafael Espindola | 208bc53 | 2014-08-21 13:13:17 +0000 | [diff] [blame] | 287 | MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 288 | else |
| 289 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
Hal Finkel | bf4db4f | 2013-01-29 00:22:49 +0000 | [diff] [blame] | 290 | |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 291 | // BBVectorize may have significantly shortened a loop body; unroll again. |
| 292 | if (!DisableUnrollLoops) |
| 293 | MPM.add(createLoopUnrollPass()); |
| 294 | } |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 297 | if (LoadCombine) |
| 298 | MPM.add(createLoadCombinePass()); |
| 299 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 300 | MPM.add(createAggressiveDCEPass()); // Delete dead instructions |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 301 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 302 | // Clean up after everything. |
| 303 | addInstructionCombiningPass(MPM); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 304 | addExtensionsToPM(EP_Peephole, MPM); |
Mehdi Amini | ec8bee1 | 2016-02-16 22:54:27 +0000 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | void PassManagerBuilder::populateModulePassManager( |
| 308 | legacy::PassManagerBase &MPM) { |
| 309 | // Allow forcing function attributes as a debugging and tuning aid. |
| 310 | MPM.add(createForceFunctionAttrsLegacyPass()); |
| 311 | |
| 312 | // If all optimizations are disabled, just run the always-inline pass and, |
| 313 | // if enabled, the function merging pass. |
| 314 | if (OptLevel == 0) { |
| 315 | addPGOInstrPasses(MPM); |
| 316 | if (Inliner) { |
| 317 | MPM.add(Inliner); |
| 318 | Inliner = nullptr; |
| 319 | } |
| 320 | |
| 321 | // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly |
| 322 | // creates a CGSCC pass manager, but we don't want to add extensions into |
| 323 | // that pass manager. To prevent this we insert a no-op module pass to reset |
| 324 | // the pass manager to get the same behavior as EP_OptimizerLast in non-O0 |
| 325 | // builds. The function merging pass is |
| 326 | if (MergeFunctions) |
| 327 | MPM.add(createMergeFunctionsPass()); |
| 328 | else if (!GlobalExtensions->empty() || !Extensions.empty()) |
| 329 | MPM.add(createBarrierNoopPass()); |
| 330 | |
| 331 | addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | // Add LibraryInfo if we have some. |
| 336 | if (LibraryInfo) |
| 337 | MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); |
| 338 | |
| 339 | addInitialAliasAnalysisPasses(MPM); |
| 340 | |
| 341 | if (!DisableUnitAtATime) { |
| 342 | // Infer attributes about declarations if possible. |
| 343 | MPM.add(createInferFunctionAttrsLegacyPass()); |
| 344 | |
| 345 | addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); |
| 346 | |
| 347 | MPM.add(createIPSCCPPass()); // IP SCCP |
| 348 | MPM.add(createGlobalOptimizerPass()); // Optimize out global vars |
| 349 | // Promote any localized global vars. |
| 350 | MPM.add(createPromoteMemoryToRegisterPass()); |
| 351 | |
| 352 | MPM.add(createDeadArgEliminationPass()); // Dead argument elimination |
| 353 | |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 354 | addInstructionCombiningPass(MPM); // Clean up after IPCP & DAE |
Mehdi Amini | ec8bee1 | 2016-02-16 22:54:27 +0000 | [diff] [blame] | 355 | addExtensionsToPM(EP_Peephole, MPM); |
| 356 | MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE |
| 357 | } |
| 358 | |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 359 | if (!PerformThinLTO) { |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 360 | /// PGO instrumentation is added during the compile phase for ThinLTO, do |
| 361 | /// not run it a second time |
| 362 | addPGOInstrPasses(MPM); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 363 | // Indirect call promotion that promotes intra-module targets only. |
Xinliang David Li | 7261618 | 2016-05-15 01:04:24 +0000 | [diff] [blame] | 364 | MPM.add(createPGOIndirectCallPromotionLegacyPass()); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 365 | } |
Mehdi Amini | ec8bee1 | 2016-02-16 22:54:27 +0000 | [diff] [blame] | 366 | |
| 367 | if (EnableNonLTOGlobalsModRef) |
| 368 | // We add a module alias analysis pass here. In part due to bugs in the |
| 369 | // analysis infrastructure this "works" in that the analysis stays alive |
| 370 | // for the entire SCC pass run below. |
| 371 | MPM.add(createGlobalsAAWrapperPass()); |
| 372 | |
| 373 | // Start of CallGraph SCC passes. |
| 374 | if (!DisableUnitAtATime) |
| 375 | MPM.add(createPruneEHPass()); // Remove dead EH info |
| 376 | if (Inliner) { |
| 377 | MPM.add(Inliner); |
| 378 | Inliner = nullptr; |
| 379 | } |
| 380 | if (!DisableUnitAtATime) |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 381 | MPM.add(createPostOrderFunctionAttrsLegacyPass()); |
Mehdi Amini | ec8bee1 | 2016-02-16 22:54:27 +0000 | [diff] [blame] | 382 | if (OptLevel > 2) |
| 383 | MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args |
| 384 | |
| 385 | addFunctionSimplificationPasses(MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 386 | |
Mehdi Amini | 45c7b3e | 2016-05-02 16:53:16 +0000 | [diff] [blame] | 387 | // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC |
| 388 | // pass manager that we are specifically trying to avoid. To prevent this |
| 389 | // we must insert a no-op module pass to reset the pass manager. |
| 390 | MPM.add(createBarrierNoopPass()); |
| 391 | |
Mehdi Amini | 7f7d8be | 2016-05-03 15:46:00 +0000 | [diff] [blame] | 392 | if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO && |
| 393 | !PrepareForThinLTO) |
| 394 | // Remove avail extern fns and globals definitions if we aren't |
| 395 | // compiling an object file for later LTO. For LTO we want to preserve |
| 396 | // these so they are eligible for inlining at link-time. Note if they |
| 397 | // are unreferenced they will be removed by GlobalDCE later, so |
| 398 | // this only impacts referenced available externally globals. |
| 399 | // Eventually they will be suppressed during codegen, but eliminating |
| 400 | // here enables more opportunity for GlobalDCE as it may make |
| 401 | // globals referenced by available external functions dead |
| 402 | // and saves running remaining passes on the eliminated functions. |
| 403 | MPM.add(createEliminateAvailableExternallyPass()); |
| 404 | |
Mehdi Amini | 45c7b3e | 2016-05-02 16:53:16 +0000 | [diff] [blame] | 405 | if (!DisableUnitAtATime) |
| 406 | MPM.add(createReversePostOrderFunctionAttrsPass()); |
| 407 | |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 408 | // If we are planning to perform ThinLTO later, let's not bloat the code with |
| 409 | // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes |
| 410 | // during ThinLTO and perform the rest of the optimizations afterward. |
Mehdi Amini | f72ca86b | 2016-04-25 08:47:37 +0000 | [diff] [blame] | 411 | if (PrepareForThinLTO) { |
Mehdi Amini | bf4513b | 2016-04-25 08:47:49 +0000 | [diff] [blame] | 412 | // Reduce the size of the IR as much as possible. |
| 413 | MPM.add(createGlobalOptimizerPass()); |
Mehdi Amini | f72ca86b | 2016-04-25 08:47:37 +0000 | [diff] [blame] | 414 | // Rename anon function to be able to export them in the summary. |
| 415 | MPM.add(createNameAnonFunctionPass()); |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 416 | return; |
Mehdi Amini | f72ca86b | 2016-04-25 08:47:37 +0000 | [diff] [blame] | 417 | } |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 418 | |
Mehdi Amini | 31407ba | 2016-05-06 18:17:03 +0000 | [diff] [blame] | 419 | if (PerformThinLTO) |
| 420 | // Optimize globals now when performing ThinLTO, this enables more |
| 421 | // optimizations later. |
| 422 | MPM.add(createGlobalOptimizerPass()); |
| 423 | |
Ashutosh Nema | 2260a3a | 2016-02-11 09:23:53 +0000 | [diff] [blame] | 424 | // Scheduling LoopVersioningLICM when inlining is over, because after that |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 425 | // we may see more accurate aliasing. Reason to run this late is that too |
| 426 | // early versioning may prevent further inlining due to increase of code |
Mehdi Amini | 31407ba | 2016-05-06 18:17:03 +0000 | [diff] [blame] | 427 | // size. By placing it just after inlining other optimizations which runs |
| 428 | // later might get benefit of no-alias assumption in clone loop. |
Ashutosh Nema | df6763a | 2016-02-06 07:47:48 +0000 | [diff] [blame] | 429 | if (UseLoopVersioningLICM) { |
| 430 | MPM.add(createLoopVersioningLICMPass()); // Do LoopVersioningLICM |
| 431 | MPM.add(createLICMPass()); // Hoist loop invariants |
| 432 | } |
| 433 | |
Chandler Carruth | 08eebe2 | 2015-07-23 09:34:01 +0000 | [diff] [blame] | 434 | if (EnableNonLTOGlobalsModRef) |
| 435 | // We add a fresh GlobalsModRef run at this point. This is particularly |
| 436 | // useful as the above will have inlined, DCE'ed, and function-attr |
| 437 | // propagated everything. We should at this point have a reasonably minimal |
| 438 | // and richly annotated call graph. By computing aliasing and mod/ref |
| 439 | // information for all local globals here, the late loop passes and notably |
| 440 | // the vectorizer will be able to use them to help recognize vectorizable |
| 441 | // memory operations. |
| 442 | // |
| 443 | // Note that this relies on a bug in the pass manager which preserves |
| 444 | // a module analysis into a function pass pipeline (and throughout it) so |
| 445 | // long as the first function pass doesn't invalidate the module analysis. |
| 446 | // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for |
| 447 | // this to work. Fortunately, it is trivial to preserve AliasAnalysis |
| 448 | // (doing nothing preserves it as it is required to be conservatively |
| 449 | // correct in the face of IR changes). |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 450 | MPM.add(createGlobalsAAWrapperPass()); |
Chandler Carruth | 08eebe2 | 2015-07-23 09:34:01 +0000 | [diff] [blame] | 451 | |
James Molloy | 0cbb2a86 | 2015-03-27 10:36:57 +0000 | [diff] [blame] | 452 | if (RunFloat2Int) |
| 453 | MPM.add(createFloat2IntPass()); |
| 454 | |
Tobias Grosser | 39a7bd1 | 2015-07-16 08:20:37 +0000 | [diff] [blame] | 455 | addExtensionsToPM(EP_VectorizerStart, MPM); |
| 456 | |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 457 | // Re-rotate loops in all our loop nests. These may have fallout out of |
| 458 | // rotated form due to GVN or other transformations, and the vectorizer relies |
Alexey Bataev | da33d80 | 2015-07-10 10:37:09 +0000 | [diff] [blame] | 459 | // on the rotated form. Disable header duplication at -Oz. |
| 460 | MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 461 | |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 462 | // Distribute loops to allow partial vectorization. I.e. isolate dependences |
Adam Nemet | d2fa414 | 2016-04-27 05:28:18 +0000 | [diff] [blame] | 463 | // into separate loop that would otherwise inhibit vectorization. This is |
| 464 | // currently only performed for loops marked with the metadata |
| 465 | // llvm.loop.distribute=true or when -enable-loop-distribute is specified. |
| 466 | MPM.add(createLoopDistributePass(/*ProcessAllLoopsByDefault=*/false)); |
Adam Nemet | 938d3d6 | 2015-05-14 12:05:18 +0000 | [diff] [blame] | 467 | |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 468 | MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize)); |
Adam Nemet | e54a4fa | 2015-11-03 23:50:08 +0000 | [diff] [blame] | 469 | |
| 470 | // Eliminate loads by forwarding stores from the previous iteration to loads |
| 471 | // of the current iteration. |
| 472 | if (EnableLoopLoadElim) |
| 473 | MPM.add(createLoopLoadEliminationPass()); |
| 474 | |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 475 | // FIXME: Because of #pragma vectorize enable, the passes below are always |
| 476 | // inserted in the pipeline, even when the vectorizer doesn't run (ex. when |
| 477 | // on -O1 and no #pragma is found). Would be good to have these two passes |
| 478 | // as function calls, so that we can only pass them when the vectorizer |
| 479 | // changed the code. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 480 | addInstructionCombiningPass(MPM); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 481 | if (OptLevel > 1 && ExtraVectorizerPasses) { |
| 482 | // At higher optimization levels, try to clean up any runtime overlap and |
| 483 | // alignment checks inserted by the vectorizer. We want to track correllated |
| 484 | // runtime checks for two inner loops in the same outer loop, fold any |
| 485 | // common computations, hoist loop-invariant aspects out of any outer loop, |
| 486 | // and unswitch the runtime checks if possible. Once hoisted, we may have |
| 487 | // dead (or speculatable) control flows or more combining opportunities. |
| 488 | MPM.add(createEarlyCSEPass()); |
| 489 | MPM.add(createCorrelatedValuePropagationPass()); |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 490 | addInstructionCombiningPass(MPM); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 491 | MPM.add(createLICMPass()); |
| 492 | MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); |
| 493 | MPM.add(createCFGSimplificationPass()); |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 494 | addInstructionCombiningPass(MPM); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 495 | } |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 496 | |
| 497 | if (RunSLPAfterLoopVectorization) { |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 498 | if (SLPVectorize) { |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 499 | MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 500 | if (OptLevel > 1 && ExtraVectorizerPasses) { |
| 501 | MPM.add(createEarlyCSEPass()); |
| 502 | } |
| 503 | } |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 504 | |
| 505 | if (BBVectorize) { |
| 506 | MPM.add(createBBVectorizePass()); |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 507 | addInstructionCombiningPass(MPM); |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 508 | addExtensionsToPM(EP_Peephole, MPM); |
| 509 | if (OptLevel > 1 && UseGVNAfterVectorization) |
Rafael Espindola | 208bc53 | 2014-08-21 13:13:17 +0000 | [diff] [blame] | 510 | MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 511 | else |
| 512 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
| 513 | |
| 514 | // BBVectorize may have significantly shortened a loop body; unroll again. |
| 515 | if (!DisableUnrollLoops) |
| 516 | MPM.add(createLoopUnrollPass()); |
| 517 | } |
| 518 | } |
| 519 | |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 520 | addExtensionsToPM(EP_Peephole, MPM); |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 521 | MPM.add(createCFGSimplificationPass()); |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 522 | addInstructionCombiningPass(MPM); |
Chandler Carruth | 08e1b87 | 2013-06-24 07:21:47 +0000 | [diff] [blame] | 523 | |
Kevin Qin | 49bc764 | 2015-03-12 05:36:01 +0000 | [diff] [blame] | 524 | if (!DisableUnrollLoops) { |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 525 | MPM.add(createLoopUnrollPass()); // Unroll small loops |
| 526 | |
Wei Mi | bf727ba | 2015-05-14 22:02:54 +0000 | [diff] [blame] | 527 | // LoopUnroll may generate some redundency to cleanup. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 528 | addInstructionCombiningPass(MPM); |
Wei Mi | bf727ba | 2015-05-14 22:02:54 +0000 | [diff] [blame] | 529 | |
Kevin Qin | 49bc764 | 2015-03-12 05:36:01 +0000 | [diff] [blame] | 530 | // Runtime unrolling will introduce runtime check in loop prologue. If the |
| 531 | // unrolled loop is a inner loop, then the prologue will be inside the |
| 532 | // outer loop. LICM pass can help to promote the runtime check out if the |
| 533 | // checked value is loop invariant. |
| 534 | MPM.add(createLICMPass()); |
Manuel Jacob | a485984 | 2016-06-02 22:14:26 +0000 | [diff] [blame] | 535 | |
| 536 | // Get rid of LCSSA nodes. |
| 537 | MPM.add(createInstructionSimplifierPass()); |
Kevin Qin | 49bc764 | 2015-03-12 05:36:01 +0000 | [diff] [blame] | 538 | } |
| 539 | |
Hal Finkel | d67e463 | 2014-09-07 20:05:11 +0000 | [diff] [blame] | 540 | // After vectorization and unrolling, assume intrinsics may tell us more |
| 541 | // about pointer alignments. |
| 542 | MPM.add(createAlignmentFromAssumptionsPass()); |
| 543 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 544 | if (!DisableUnitAtATime) { |
| 545 | // FIXME: We shouldn't bother with this anymore. |
| 546 | MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes |
| 547 | |
Evan Cheng | 8c6b06d | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 548 | // GlobalOpt already deletes dead functions and globals, at -O2 try a |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 549 | // late pass of GlobalDCE. It is capable of deleting dead cycles. |
Evan Cheng | 8c6b06d | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 550 | if (OptLevel > 1) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 551 | MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 552 | MPM.add(createConstantMergePass()); // Merge dup global constants |
Evan Cheng | 8c6b06d | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 553 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 554 | } |
Nick Lewycky | 9e6d184 | 2014-09-13 21:46:00 +0000 | [diff] [blame] | 555 | |
| 556 | if (MergeFunctions) |
| 557 | MPM.add(createMergeFunctionsPass()); |
| 558 | |
Kostya Serebryany | e505a5a | 2012-03-23 23:22:59 +0000 | [diff] [blame] | 559 | addExtensionsToPM(EP_OptimizerLast, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 560 | } |
| 561 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 562 | void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) { |
Peter Collingbourne | fad596a | 2016-05-25 21:26:14 +0000 | [diff] [blame] | 563 | // Remove unused virtual tables to improve the quality of code generated by |
| 564 | // whole-program devirtualization and bitset lowering. |
| 565 | PM.add(createGlobalDCEPass()); |
| 566 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 567 | // Provide AliasAnalysis services for optimizations. |
| 568 | addInitialAliasAnalysisPasses(PM); |
| 569 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 570 | if (ModuleSummary) |
| 571 | PM.add(createFunctionImportPass(ModuleSummary)); |
Teresa Johnson | 5fcbdb7 | 2015-12-07 19:21:11 +0000 | [diff] [blame] | 572 | |
Chandler Carruth | f49f1a87 | 2015-12-27 08:13:45 +0000 | [diff] [blame] | 573 | // Allow forcing function attributes as a debugging and tuning aid. |
| 574 | PM.add(createForceFunctionAttrsLegacyPass()); |
| 575 | |
Chandler Carruth | 3a040e6 | 2015-12-27 08:41:34 +0000 | [diff] [blame] | 576 | // Infer attributes about declarations if possible. |
| 577 | PM.add(createInferFunctionAttrsLegacyPass()); |
| 578 | |
Peter Collingbourne | fad596a | 2016-05-25 21:26:14 +0000 | [diff] [blame] | 579 | if (OptLevel > 1) { |
| 580 | // Indirect call promotion. This should promote all the targets that are |
| 581 | // left by the earlier promotion pass that promotes intra-module targets. |
| 582 | // This two-step promotion is to save the compile time. For LTO, it should |
| 583 | // produce the same result as if we only do promotion here. |
| 584 | PM.add(createPGOIndirectCallPromotionLegacyPass(true)); |
Rong Xu | 6e34c49 | 2016-04-27 23:20:27 +0000 | [diff] [blame] | 585 | |
Peter Collingbourne | fad596a | 2016-05-25 21:26:14 +0000 | [diff] [blame] | 586 | // Propagate constants at call sites into the functions they call. This |
| 587 | // opens opportunities for globalopt (and inlining) by substituting function |
| 588 | // pointers passed as arguments to direct uses of functions. |
| 589 | PM.add(createIPSCCPPass()); |
| 590 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 591 | |
Peter Collingbourne | fad596a | 2016-05-25 21:26:14 +0000 | [diff] [blame] | 592 | // Infer attributes about definitions. The readnone attribute in particular is |
| 593 | // required for virtual constant propagation. |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 594 | PM.add(createPostOrderFunctionAttrsLegacyPass()); |
Chandler Carruth | 1926b70 | 2016-01-08 10:55:52 +0000 | [diff] [blame] | 595 | PM.add(createReversePostOrderFunctionAttrsPass()); |
Peter Collingbourne | fad596a | 2016-05-25 21:26:14 +0000 | [diff] [blame] | 596 | |
| 597 | // Apply whole-program devirtualization and virtual constant propagation. |
| 598 | PM.add(createWholeProgramDevirtPass()); |
| 599 | |
| 600 | // That's all we need at opt level 1. |
| 601 | if (OptLevel == 1) |
| 602 | return; |
| 603 | |
| 604 | // Now that we internalized some globals, see if we can hack on them! |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 605 | PM.add(createGlobalOptimizerPass()); |
James Molloy | 6045cc8 | 2015-12-15 09:24:01 +0000 | [diff] [blame] | 606 | // Promote any localized global vars. |
| 607 | PM.add(createPromoteMemoryToRegisterPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 608 | |
| 609 | // Linking modules together can lead to duplicated global constants, only |
| 610 | // keep one copy of each constant. |
| 611 | PM.add(createConstantMergePass()); |
| 612 | |
| 613 | // Remove unused arguments from functions. |
| 614 | PM.add(createDeadArgEliminationPass()); |
| 615 | |
| 616 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 617 | // simplification opportunities, and both can propagate functions through |
| 618 | // function pointers. When this happens, we often have to resolve varargs |
| 619 | // calls, etc, so let instcombine do this. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 620 | addInstructionCombiningPass(PM); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 621 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 622 | |
| 623 | // Inline small functions |
Rafael Espindola | e07caad | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 624 | bool RunInliner = Inliner; |
| 625 | if (RunInliner) { |
| 626 | PM.add(Inliner); |
| 627 | Inliner = nullptr; |
| 628 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 629 | |
| 630 | PM.add(createPruneEHPass()); // Remove dead EH info. |
| 631 | |
| 632 | // Optimize globals again if we ran the inliner. |
| 633 | if (RunInliner) |
| 634 | PM.add(createGlobalOptimizerPass()); |
| 635 | PM.add(createGlobalDCEPass()); // Remove dead functions. |
| 636 | |
| 637 | // If we didn't decide to inline a function, check to see if we can |
| 638 | // transform it to pass arguments by value instead of by reference. |
| 639 | PM.add(createArgumentPromotionPass()); |
| 640 | |
| 641 | // The IPO passes may leave cruft around. Clean up after them. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 642 | addInstructionCombiningPass(PM); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 643 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 644 | PM.add(createJumpThreadingPass()); |
Bill Wendling | 4c0d9ad | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 645 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 646 | // Break up allocas |
David Majnemer | cbf614a | 2016-06-15 00:19:09 +0000 | [diff] [blame^] | 647 | PM.add(createSROAPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 648 | |
| 649 | // Run a few AA driven optimizations here and now, to cleanup the code. |
Chandler Carruth | 9c4ed17 | 2016-02-18 11:03:11 +0000 | [diff] [blame] | 650 | PM.add(createPostOrderFunctionAttrsLegacyPass()); // Add nocapture. |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 651 | PM.add(createGlobalsAAWrapperPass()); // IP alias analysis. |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 652 | |
Bill Wendling | 932b992 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 653 | PM.add(createLICMPass()); // Hoist loop invariants. |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 654 | if (EnableMLSM) |
| 655 | PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds. |
Bill Wendling | 932b992 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 656 | PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. |
| 657 | PM.add(createMemCpyOptPass()); // Remove dead memcpys. |
Bill Wendling | 4c0d9ad | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 658 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 659 | // Nuke dead stores. |
| 660 | PM.add(createDeadStoreEliminationPass()); |
| 661 | |
Duncan P. N. Exon Smith | 2b69189 | 2014-04-15 17:48:15 +0000 | [diff] [blame] | 662 | // More loops are countable; try to optimize them. |
| 663 | PM.add(createIndVarSimplifyPass()); |
| 664 | PM.add(createLoopDeletionPass()); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 665 | if (EnableLoopInterchange) |
| 666 | PM.add(createLoopInterchangePass()); |
| 667 | |
James Molloy | 31f3ddd | 2016-01-14 15:00:09 +0000 | [diff] [blame] | 668 | if (!DisableUnrollLoops) |
| 669 | PM.add(createSimpleLoopUnrollPass()); // Unroll small loops |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 670 | PM.add(createLoopVectorizePass(true, LoopVectorize)); |
James Molloy | 31f3ddd | 2016-01-14 15:00:09 +0000 | [diff] [blame] | 671 | // The vectorizer may have significantly shortened a loop body; unroll again. |
| 672 | if (!DisableUnrollLoops) |
| 673 | PM.add(createLoopUnrollPass()); |
Arnold Schwaighofer | 6ccda92 | 2014-02-24 18:19:31 +0000 | [diff] [blame] | 674 | |
James Molloy | 6045cc8 | 2015-12-15 09:24:01 +0000 | [diff] [blame] | 675 | // Now that we've optimized loops (in particular loop induction variables), |
| 676 | // we may have exposed more scalar opportunities. Run parts of the scalar |
| 677 | // optimizer again at this point. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 678 | addInstructionCombiningPass(PM); // Initial cleanup |
James Molloy | 6045cc8 | 2015-12-15 09:24:01 +0000 | [diff] [blame] | 679 | PM.add(createCFGSimplificationPass()); // if-convert |
| 680 | PM.add(createSCCPPass()); // Propagate exposed constants |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 681 | addInstructionCombiningPass(PM); // Clean up again |
James Molloy | 6045cc8 | 2015-12-15 09:24:01 +0000 | [diff] [blame] | 682 | PM.add(createBitTrackingDCEPass()); |
| 683 | |
Yi Jiang | 79eb0aa | 2014-05-05 23:14:46 +0000 | [diff] [blame] | 684 | // More scalar chains could be vectorized due to more alias information |
JF Bastien | f42a6ea | 2014-10-21 23:18:21 +0000 | [diff] [blame] | 685 | if (RunSLPAfterLoopVectorization) |
| 686 | if (SLPVectorize) |
| 687 | PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
Yi Jiang | 79eb0aa | 2014-05-05 23:14:46 +0000 | [diff] [blame] | 688 | |
Hal Finkel | d67e463 | 2014-09-07 20:05:11 +0000 | [diff] [blame] | 689 | // After vectorization, assume intrinsics may tell us more about pointer |
| 690 | // alignments. |
| 691 | PM.add(createAlignmentFromAssumptionsPass()); |
| 692 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 693 | if (LoadCombine) |
| 694 | PM.add(createLoadCombinePass()); |
| 695 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 696 | // Cleanup and simplify the code after the scalar optimizations. |
Matthias Braun | c31032d | 2016-03-09 18:47:11 +0000 | [diff] [blame] | 697 | addInstructionCombiningPass(PM); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 698 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 699 | |
| 700 | PM.add(createJumpThreadingPass()); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 701 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 702 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 703 | void PassManagerBuilder::addLateLTOOptimizationPasses( |
| 704 | legacy::PassManagerBase &PM) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 705 | // Delete basic blocks, which optimization passes may have killed. |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 706 | PM.add(createCFGSimplificationPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 707 | |
Teresa Johnson | c4279a7 | 2015-08-11 16:26:41 +0000 | [diff] [blame] | 708 | // Drop bodies of available externally objects to improve GlobalDCE. |
| 709 | PM.add(createEliminateAvailableExternallyPass()); |
| 710 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 711 | // Now that we have optimized the program, discard unreachable functions. |
| 712 | PM.add(createGlobalDCEPass()); |
Nick Lewycky | 9e6d184 | 2014-09-13 21:46:00 +0000 | [diff] [blame] | 713 | |
| 714 | // FIXME: this is profitable (for compiler time) to do at -O0 too, but |
| 715 | // currently it damages debug info. |
| 716 | if (MergeFunctions) |
| 717 | PM.add(createMergeFunctionsPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 718 | } |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 719 | |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 720 | void PassManagerBuilder::populateThinLTOPassManager( |
| 721 | legacy::PassManagerBase &PM) { |
| 722 | PerformThinLTO = true; |
| 723 | |
| 724 | if (VerifyInput) |
| 725 | PM.add(createVerifierPass()); |
| 726 | |
Teresa Johnson | 26ab577 | 2016-03-15 00:04:37 +0000 | [diff] [blame] | 727 | if (ModuleSummary) |
| 728 | PM.add(createFunctionImportPass(ModuleSummary)); |
Mehdi Amini | 1db10ac | 2016-02-16 23:02:29 +0000 | [diff] [blame] | 729 | |
| 730 | populateModulePassManager(PM); |
| 731 | |
| 732 | if (VerifyOutput) |
| 733 | PM.add(createVerifierPass()); |
| 734 | PerformThinLTO = false; |
| 735 | } |
| 736 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 737 | void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) { |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 738 | if (LibraryInfo) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 739 | PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 740 | |
Duncan P. N. Exon Smith | ab58a56 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 741 | if (VerifyInput) |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 742 | PM.add(createVerifierPass()); |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 743 | |
Peter Collingbourne | df49d1b | 2016-02-09 22:50:34 +0000 | [diff] [blame] | 744 | if (OptLevel != 0) |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 745 | addLTOOptimizationPasses(PM); |
| 746 | |
Evgeniy Stepanov | 67849d5 | 2015-12-15 23:00:08 +0000 | [diff] [blame] | 747 | // Create a function that performs CFI checks for cross-DSO calls with targets |
| 748 | // in the current module. |
| 749 | PM.add(createCrossDSOCFIPass()); |
| 750 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame] | 751 | // Lower bit sets to globals. This pass supports Clang's control flow |
| 752 | // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI |
| 753 | // is enabled. The pass does nothing if CFI is disabled. |
| 754 | PM.add(createLowerBitSetsPass()); |
| 755 | |
| 756 | if (OptLevel != 0) |
| 757 | addLateLTOOptimizationPasses(PM); |
| 758 | |
Duncan P. N. Exon Smith | ab58a56 | 2015-03-19 22:24:17 +0000 | [diff] [blame] | 759 | if (VerifyOutput) |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 760 | PM.add(createVerifierPass()); |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 763 | inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { |
| 764 | return reinterpret_cast<PassManagerBuilder*>(P); |
| 765 | } |
| 766 | |
| 767 | inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { |
| 768 | return reinterpret_cast<LLVMPassManagerBuilderRef>(P); |
| 769 | } |
| 770 | |
Dmitri Gribenko | 0011bbf | 2012-11-15 16:51:49 +0000 | [diff] [blame] | 771 | LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 772 | PassManagerBuilder *PMB = new PassManagerBuilder(); |
| 773 | return wrap(PMB); |
| 774 | } |
| 775 | |
| 776 | void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { |
| 777 | PassManagerBuilder *Builder = unwrap(PMB); |
| 778 | delete Builder; |
| 779 | } |
| 780 | |
| 781 | void |
| 782 | LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, |
| 783 | unsigned OptLevel) { |
| 784 | PassManagerBuilder *Builder = unwrap(PMB); |
| 785 | Builder->OptLevel = OptLevel; |
| 786 | } |
| 787 | |
| 788 | void |
| 789 | LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, |
| 790 | unsigned SizeLevel) { |
| 791 | PassManagerBuilder *Builder = unwrap(PMB); |
| 792 | Builder->SizeLevel = SizeLevel; |
| 793 | } |
| 794 | |
| 795 | void |
| 796 | LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, |
| 797 | LLVMBool Value) { |
| 798 | PassManagerBuilder *Builder = unwrap(PMB); |
| 799 | Builder->DisableUnitAtATime = Value; |
| 800 | } |
| 801 | |
| 802 | void |
| 803 | LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, |
| 804 | LLVMBool Value) { |
| 805 | PassManagerBuilder *Builder = unwrap(PMB); |
| 806 | Builder->DisableUnrollLoops = Value; |
| 807 | } |
| 808 | |
| 809 | void |
| 810 | LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, |
| 811 | LLVMBool Value) { |
Meador Inge | dfb08a2 | 2013-06-20 19:48:07 +0000 | [diff] [blame] | 812 | // NOTE: The simplify-libcalls pass has been removed. |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | void |
| 816 | LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, |
| 817 | unsigned Threshold) { |
| 818 | PassManagerBuilder *Builder = unwrap(PMB); |
| 819 | Builder->Inliner = createFunctionInliningPass(Threshold); |
| 820 | } |
| 821 | |
| 822 | void |
| 823 | LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, |
| 824 | LLVMPassManagerRef PM) { |
| 825 | PassManagerBuilder *Builder = unwrap(PMB); |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 826 | legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM); |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 827 | Builder->populateFunctionPassManager(*FPM); |
| 828 | } |
| 829 | |
| 830 | void |
| 831 | LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, |
| 832 | LLVMPassManagerRef PM) { |
| 833 | PassManagerBuilder *Builder = unwrap(PMB); |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 834 | legacy::PassManagerBase *MPM = unwrap(PM); |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 835 | Builder->populateModulePassManager(*MPM); |
| 836 | } |
| 837 | |
| 838 | void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, |
| 839 | LLVMPassManagerRef PM, |
Nick Lewycky | 5f50854 | 2013-03-10 21:58:22 +0000 | [diff] [blame] | 840 | LLVMBool Internalize, |
| 841 | LLVMBool RunInliner) { |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 842 | PassManagerBuilder *Builder = unwrap(PMB); |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 843 | legacy::PassManagerBase *LPM = unwrap(PM); |
Rafael Espindola | e07caad | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 844 | |
| 845 | // A small backwards compatibility hack. populateLTOPassManager used to take |
| 846 | // an RunInliner option. |
| 847 | if (RunInliner && !Builder->Inliner) |
| 848 | Builder->Inliner = createFunctionInliningPass(); |
| 849 | |
| 850 | Builder->populateLTOPassManager(*LPM); |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 851 | } |