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 | |
| 15 | |
| 16 | #include "llvm/Transforms/IPO/PassManagerBuilder.h" |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 17 | #include "llvm-c/Transforms/PassManagerBuilder.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 20 | #include "llvm/IR/DataLayout.h" |
Chandler Carruth | 5ad5f15 | 2014-01-13 09:26:24 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 22 | #include "llvm/IR/LegacyPassManager.h" |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 23 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 24 | #include "llvm/Support/ManagedStatic.h" |
Chandler Carruth | 62d4215 | 2015-01-15 02:16:27 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/TargetLibraryInfo.h" |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetMachine.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/IPO.h" |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Scalar.h" |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 29 | #include "llvm/Transforms/Vectorize.h" |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
| 32 | |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 33 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 34 | RunLoopVectorization("vectorize-loops", cl::Hidden, |
Nadav Rotem | d3df665 | 2012-10-30 18:37:43 +0000 | [diff] [blame] | 35 | cl::desc("Run the Loop vectorization passes")); |
Nadav Rotem | c59ae20 | 2012-10-29 16:36:25 +0000 | [diff] [blame] | 36 | |
| 37 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 38 | RunSLPVectorization("vectorize-slp", cl::Hidden, |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 39 | cl::desc("Run the SLP vectorization passes")); |
| 40 | |
| 41 | static cl::opt<bool> |
Nadav Rotem | 7f27e0b | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 42 | RunBBVectorization("vectorize-slp-aggressive", cl::Hidden, |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 43 | cl::desc("Run the BB vectorization passes")); |
Hal Finkel | c34e511 | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 44 | |
Hal Finkel | 204bf53 | 2012-04-13 17:15:33 +0000 | [diff] [blame] | 45 | static cl::opt<bool> |
| 46 | UseGVNAfterVectorization("use-gvn-after-vectorization", |
| 47 | cl::init(false), cl::Hidden, |
| 48 | cl::desc("Run GVN instead of Early CSE after vectorization passes")); |
| 49 | |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 50 | static cl::opt<bool> ExtraVectorizerPasses( |
| 51 | "extra-vectorizer-passes", cl::init(false), cl::Hidden, |
| 52 | cl::desc("Run cleanup optimization passes after vectorization.")); |
| 53 | |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 54 | static cl::opt<bool> UseNewSROA("use-new-sroa", |
Chandler Carruth | 4e43599 | 2012-10-02 04:24:01 +0000 | [diff] [blame] | 55 | cl::init(true), cl::Hidden, |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 56 | cl::desc("Enable the new, experimental SROA pass")); |
| 57 | |
Hal Finkel | bf45efd | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 58 | static cl::opt<bool> |
| 59 | RunLoopRerolling("reroll-loops", cl::Hidden, |
| 60 | cl::desc("Run the loop rerolling pass")); |
| 61 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 62 | static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false), |
| 63 | cl::Hidden, |
| 64 | cl::desc("Run the load combining pass")); |
| 65 | |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 66 | static cl::opt<bool> |
| 67 | RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization", |
James Molloy | 6b95d8e | 2014-09-04 13:23:08 +0000 | [diff] [blame] | 68 | cl::init(true), cl::Hidden, |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 69 | cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop " |
| 70 | "vectorizer instead of before")); |
| 71 | |
Hal Finkel | 445dda5 | 2014-09-02 22:12:54 +0000 | [diff] [blame] | 72 | static cl::opt<bool> UseCFLAA("use-cfl-aa", |
| 73 | cl::init(false), cl::Hidden, |
| 74 | cl::desc("Enable the new, experimental CFL alias analysis")); |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 75 | |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 76 | static cl::opt<bool> |
Gerolf Hoflehner | 008e5cd | 2014-09-10 20:24:03 +0000 | [diff] [blame] | 77 | EnableMLSM("mlsm", cl::init(true), cl::Hidden, |
| 78 | cl::desc("Enable motion of merged load and store")); |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 79 | |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 80 | static cl::opt<bool> EnableLoopInterchange( |
| 81 | "enable-loopinterchange", cl::init(false), cl::Hidden, |
| 82 | cl::desc("Enable the new, experimental LoopInterchange Pass")); |
| 83 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 84 | PassManagerBuilder::PassManagerBuilder() { |
| 85 | OptLevel = 2; |
| 86 | SizeLevel = 0; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 87 | LibraryInfo = nullptr; |
| 88 | Inliner = nullptr; |
Duncan P. N. Exon Smith | 49f3ec8 | 2014-04-18 01:05:15 +0000 | [diff] [blame] | 89 | DisableTailCalls = false; |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 90 | DisableUnitAtATime = false; |
| 91 | DisableUnrollLoops = false; |
Nadav Rotem | d4dcc00 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 92 | BBVectorize = RunBBVectorization; |
Nadav Rotem | a1e5e44 | 2013-04-15 04:54:42 +0000 | [diff] [blame] | 93 | SLPVectorize = RunSLPVectorization; |
Nadav Rotem | c59ae20 | 2012-10-29 16:36:25 +0000 | [diff] [blame] | 94 | LoopVectorize = RunLoopVectorization; |
Hal Finkel | 29aeb20 | 2013-11-17 16:02:50 +0000 | [diff] [blame] | 95 | RerollLoops = RunLoopRerolling; |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 96 | LoadCombine = RunLoadCombine; |
Rafael Espindola | 208bc53 | 2014-08-21 13:13:17 +0000 | [diff] [blame] | 97 | DisableGVNLoadPRE = false; |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 98 | VerifyInput = false; |
| 99 | VerifyOutput = false; |
Nick Lewycky | 9e6d184 | 2014-09-13 21:46:00 +0000 | [diff] [blame] | 100 | MergeFunctions = false; |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | PassManagerBuilder::~PassManagerBuilder() { |
| 104 | delete LibraryInfo; |
| 105 | delete Inliner; |
| 106 | } |
| 107 | |
David Chisnall | 719a72f | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 108 | /// Set of global extensions, automatically added as part of the standard set. |
| 109 | static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, |
| 110 | PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; |
| 111 | |
| 112 | void PassManagerBuilder::addGlobalExtension( |
| 113 | PassManagerBuilder::ExtensionPointTy Ty, |
| 114 | PassManagerBuilder::ExtensionFn Fn) { |
| 115 | GlobalExtensions->push_back(std::make_pair(Ty, Fn)); |
| 116 | } |
| 117 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 118 | void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { |
| 119 | Extensions.push_back(std::make_pair(Ty, Fn)); |
| 120 | } |
| 121 | |
| 122 | void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 123 | legacy::PassManagerBase &PM) const { |
David Chisnall | 719a72f | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 124 | for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) |
| 125 | if ((*GlobalExtensions)[i].first == ETy) |
| 126 | (*GlobalExtensions)[i].second(*this, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 127 | for (unsigned i = 0, e = Extensions.size(); i != e; ++i) |
| 128 | if (Extensions[i].first == ETy) |
| 129 | Extensions[i].second(*this, PM); |
| 130 | } |
| 131 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 132 | void PassManagerBuilder::addInitialAliasAnalysisPasses( |
| 133 | legacy::PassManagerBase &PM) const { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 134 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 135 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 136 | // support "obvious" type-punning idioms. |
Hal Finkel | 445dda5 | 2014-09-02 22:12:54 +0000 | [diff] [blame] | 137 | if (UseCFLAA) |
| 138 | PM.add(createCFLAliasAnalysisPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 139 | PM.add(createTypeBasedAliasAnalysisPass()); |
Hal Finkel | 9414665 | 2014-07-24 14:25:39 +0000 | [diff] [blame] | 140 | PM.add(createScopedNoAliasAAPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 141 | PM.add(createBasicAliasAnalysisPass()); |
| 142 | } |
| 143 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 144 | void PassManagerBuilder::populateFunctionPassManager( |
| 145 | legacy::FunctionPassManager &FPM) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 146 | addExtensionsToPM(EP_EarlyAsPossible, FPM); |
| 147 | |
| 148 | // Add LibraryInfo if we have some. |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 149 | if (LibraryInfo) |
| 150 | FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 151 | |
| 152 | if (OptLevel == 0) return; |
| 153 | |
| 154 | addInitialAliasAnalysisPasses(FPM); |
| 155 | |
| 156 | FPM.add(createCFGSimplificationPass()); |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 157 | if (UseNewSROA) |
| 158 | FPM.add(createSROAPass()); |
| 159 | else |
| 160 | FPM.add(createScalarReplAggregatesPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 161 | FPM.add(createEarlyCSEPass()); |
| 162 | FPM.add(createLowerExpectIntrinsicPass()); |
| 163 | } |
| 164 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 165 | void PassManagerBuilder::populateModulePassManager( |
| 166 | legacy::PassManagerBase &MPM) { |
Nick Lewycky | 592d849 | 2014-10-23 23:49:31 +0000 | [diff] [blame] | 167 | // If all optimizations are disabled, just run the always-inline pass and, |
| 168 | // if enabled, the function merging pass. |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 169 | if (OptLevel == 0) { |
| 170 | if (Inliner) { |
| 171 | MPM.add(Inliner); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 172 | Inliner = nullptr; |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 173 | } |
Chandler Carruth | e8479e1 | 2012-10-18 08:05:46 +0000 | [diff] [blame] | 174 | |
Nick Lewycky | 592d849 | 2014-10-23 23:49:31 +0000 | [diff] [blame] | 175 | // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly |
| 176 | // creates a CGSCC pass manager, but we don't want to add extensions into |
| 177 | // that pass manager. To prevent this we insert a no-op module pass to reset |
| 178 | // the pass manager to get the same behavior as EP_OptimizerLast in non-O0 |
| 179 | // builds. The function merging pass is |
| 180 | if (MergeFunctions) |
| 181 | MPM.add(createMergeFunctionsPass()); |
| 182 | else if (!GlobalExtensions->empty() || !Extensions.empty()) |
Chandler Carruth | e8479e1 | 2012-10-18 08:05:46 +0000 | [diff] [blame] | 183 | MPM.add(createBarrierNoopPass()); |
| 184 | |
Kostya Serebryany | dc436f9 | 2011-11-30 22:19:26 +0000 | [diff] [blame] | 185 | addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
| 189 | // Add LibraryInfo if we have some. |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 190 | if (LibraryInfo) |
| 191 | MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 192 | |
| 193 | addInitialAliasAnalysisPasses(MPM); |
| 194 | |
| 195 | if (!DisableUnitAtATime) { |
Dan Gohman | b993629 | 2012-01-17 20:51:32 +0000 | [diff] [blame] | 196 | addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); |
| 197 | |
Gerolf Hoflehner | 65b1332 | 2014-07-03 19:28:15 +0000 | [diff] [blame] | 198 | MPM.add(createIPSCCPPass()); // IP SCCP |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 199 | MPM.add(createGlobalOptimizerPass()); // Optimize out global vars |
| 200 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 201 | MPM.add(createDeadArgEliminationPass()); // Dead argument elimination |
| 202 | |
| 203 | MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 204 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 205 | MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE |
| 206 | } |
| 207 | |
| 208 | // Start of CallGraph SCC passes. |
| 209 | if (!DisableUnitAtATime) |
| 210 | MPM.add(createPruneEHPass()); // Remove dead EH info |
| 211 | if (Inliner) { |
| 212 | MPM.add(Inliner); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 213 | Inliner = nullptr; |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 214 | } |
| 215 | if (!DisableUnitAtATime) |
| 216 | MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs |
| 217 | if (OptLevel > 2) |
| 218 | MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args |
| 219 | |
| 220 | // Start of function pass. |
| 221 | // Break up aggregate allocas, using SSAUpdater. |
Chandler Carruth | 70b44c5 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 222 | if (UseNewSROA) |
| 223 | MPM.add(createSROAPass(/*RequiresDomTree*/ false)); |
| 224 | else |
| 225 | MPM.add(createScalarReplAggregatesPass(-1, false)); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 226 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 227 | MPM.add(createJumpThreadingPass()); // Thread jumps. |
| 228 | MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals |
| 229 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
| 230 | MPM.add(createInstructionCombiningPass()); // Combine silly seq's |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 231 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 232 | |
Duncan P. N. Exon Smith | 49f3ec8 | 2014-04-18 01:05:15 +0000 | [diff] [blame] | 233 | if (!DisableTailCalls) |
| 234 | MPM.add(createTailCallEliminationPass()); // Eliminate tail calls |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 235 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
| 236 | MPM.add(createReassociatePass()); // Reassociate expressions |
Roman Divacky | d2b9a1b | 2014-11-21 19:53:24 +0000 | [diff] [blame] | 237 | // Rotate Loop - disable header duplication at -Oz |
| 238 | MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1)); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 239 | MPM.add(createLICMPass()); // Hoist loop invariants |
| 240 | MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); |
| 241 | MPM.add(createInstructionCombiningPass()); |
| 242 | MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars |
| 243 | MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. |
| 244 | MPM.add(createLoopDeletionPass()); // Delete dead loops |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 245 | if (EnableLoopInterchange) |
| 246 | MPM.add(createLoopInterchangePass()); // Interchange loops |
Nadav Rotem | 6b94c2a | 2012-10-17 18:25:06 +0000 | [diff] [blame] | 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. |
| 267 | MPM.add(createInstructionCombiningPass()); |
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()); |
| 284 | MPM.add(createInstructionCombiningPass()); |
| 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 |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 302 | MPM.add(createInstructionCombiningPass()); // Clean up after everything. |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 303 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 304 | |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 305 | // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC |
| 306 | // pass manager that we are specifically trying to avoid. To prevent this |
| 307 | // we must insert a no-op module pass to reset the pass manager. |
| 308 | MPM.add(createBarrierNoopPass()); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 309 | |
| 310 | // Re-rotate loops in all our loop nests. These may have fallout out of |
| 311 | // rotated form due to GVN or other transformations, and the vectorizer relies |
| 312 | // on the rotated form. |
Michael Zolotukhin | 267e12f | 2015-03-10 19:07:41 +0000 | [diff] [blame] | 313 | MPM.add(createLoopRotatePass()); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 314 | |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 315 | MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize)); |
| 316 | // FIXME: Because of #pragma vectorize enable, the passes below are always |
| 317 | // inserted in the pipeline, even when the vectorizer doesn't run (ex. when |
| 318 | // on -O1 and no #pragma is found). Would be good to have these two passes |
| 319 | // as function calls, so that we can only pass them when the vectorizer |
| 320 | // changed the code. |
| 321 | MPM.add(createInstructionCombiningPass()); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 322 | if (OptLevel > 1 && ExtraVectorizerPasses) { |
| 323 | // At higher optimization levels, try to clean up any runtime overlap and |
| 324 | // alignment checks inserted by the vectorizer. We want to track correllated |
| 325 | // runtime checks for two inner loops in the same outer loop, fold any |
| 326 | // common computations, hoist loop-invariant aspects out of any outer loop, |
| 327 | // and unswitch the runtime checks if possible. Once hoisted, we may have |
| 328 | // dead (or speculatable) control flows or more combining opportunities. |
| 329 | MPM.add(createEarlyCSEPass()); |
| 330 | MPM.add(createCorrelatedValuePropagationPass()); |
| 331 | MPM.add(createInstructionCombiningPass()); |
| 332 | MPM.add(createLICMPass()); |
| 333 | MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); |
| 334 | MPM.add(createCFGSimplificationPass()); |
| 335 | MPM.add(createInstructionCombiningPass()); |
| 336 | } |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 337 | |
| 338 | if (RunSLPAfterLoopVectorization) { |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 339 | if (SLPVectorize) { |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 340 | MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 341 | if (OptLevel > 1 && ExtraVectorizerPasses) { |
| 342 | MPM.add(createEarlyCSEPass()); |
| 343 | } |
| 344 | } |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 345 | |
| 346 | if (BBVectorize) { |
| 347 | MPM.add(createBBVectorizePass()); |
| 348 | MPM.add(createInstructionCombiningPass()); |
| 349 | addExtensionsToPM(EP_Peephole, MPM); |
| 350 | if (OptLevel > 1 && UseGVNAfterVectorization) |
Rafael Espindola | 208bc53 | 2014-08-21 13:13:17 +0000 | [diff] [blame] | 351 | MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies |
James Molloy | 568da09 | 2014-08-06 12:56:19 +0000 | [diff] [blame] | 352 | else |
| 353 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
| 354 | |
| 355 | // BBVectorize may have significantly shortened a loop body; unroll again. |
| 356 | if (!DisableUnrollLoops) |
| 357 | MPM.add(createLoopUnrollPass()); |
| 358 | } |
| 359 | } |
| 360 | |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 361 | addExtensionsToPM(EP_Peephole, MPM); |
Renato Golin | 729a3ae | 2013-12-05 21:20:02 +0000 | [diff] [blame] | 362 | MPM.add(createCFGSimplificationPass()); |
Chandler Carruth | 7b8297a | 2014-10-14 00:31:29 +0000 | [diff] [blame] | 363 | MPM.add(createInstructionCombiningPass()); |
Chandler Carruth | 08e1b87 | 2013-06-24 07:21:47 +0000 | [diff] [blame] | 364 | |
Kevin Qin | 49bc764 | 2015-03-12 05:36:01 +0000 | [diff] [blame] | 365 | if (!DisableUnrollLoops) { |
Hal Finkel | 86b3064 | 2014-03-31 23:23:51 +0000 | [diff] [blame] | 366 | MPM.add(createLoopUnrollPass()); // Unroll small loops |
| 367 | |
Kevin Qin | 49bc764 | 2015-03-12 05:36:01 +0000 | [diff] [blame] | 368 | // This is a barrier pass to avoid combine LICM pass and loop unroll pass |
| 369 | // within same loop pass manager. |
| 370 | MPM.add(createInstructionSimplifierPass()); |
| 371 | |
| 372 | // Runtime unrolling will introduce runtime check in loop prologue. If the |
| 373 | // unrolled loop is a inner loop, then the prologue will be inside the |
| 374 | // outer loop. LICM pass can help to promote the runtime check out if the |
| 375 | // checked value is loop invariant. |
| 376 | MPM.add(createLICMPass()); |
| 377 | } |
| 378 | |
Hal Finkel | d67e463 | 2014-09-07 20:05:11 +0000 | [diff] [blame] | 379 | // After vectorization and unrolling, assume intrinsics may tell us more |
| 380 | // about pointer alignments. |
| 381 | MPM.add(createAlignmentFromAssumptionsPass()); |
| 382 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 383 | if (!DisableUnitAtATime) { |
| 384 | // FIXME: We shouldn't bother with this anymore. |
| 385 | MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes |
| 386 | |
Evan Cheng | 8c6b06d | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 387 | // GlobalOpt already deletes dead functions and globals, at -O2 try a |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 388 | // late pass of GlobalDCE. It is capable of deleting dead cycles. |
Evan Cheng | 8c6b06d | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 389 | if (OptLevel > 1) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 390 | MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 391 | MPM.add(createConstantMergePass()); // Merge dup global constants |
Evan Cheng | 8c6b06d | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 392 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 393 | } |
Nick Lewycky | 9e6d184 | 2014-09-13 21:46:00 +0000 | [diff] [blame] | 394 | |
| 395 | if (MergeFunctions) |
| 396 | MPM.add(createMergeFunctionsPass()); |
| 397 | |
Kostya Serebryany | e505a5a | 2012-03-23 23:22:59 +0000 | [diff] [blame] | 398 | addExtensionsToPM(EP_OptimizerLast, MPM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 401 | void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 402 | // Provide AliasAnalysis services for optimizations. |
| 403 | addInitialAliasAnalysisPasses(PM); |
| 404 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 405 | // Propagate constants at call sites into the functions they call. This |
| 406 | // opens opportunities for globalopt (and inlining) by substituting function |
| 407 | // pointers passed as arguments to direct uses of functions. |
| 408 | PM.add(createIPSCCPPass()); |
| 409 | |
| 410 | // Now that we internalized some globals, see if we can hack on them! |
| 411 | PM.add(createGlobalOptimizerPass()); |
| 412 | |
| 413 | // Linking modules together can lead to duplicated global constants, only |
| 414 | // keep one copy of each constant. |
| 415 | PM.add(createConstantMergePass()); |
| 416 | |
| 417 | // Remove unused arguments from functions. |
| 418 | PM.add(createDeadArgEliminationPass()); |
| 419 | |
| 420 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 421 | // simplification opportunities, and both can propagate functions through |
| 422 | // function pointers. When this happens, we often have to resolve varargs |
| 423 | // calls, etc, so let instcombine do this. |
| 424 | PM.add(createInstructionCombiningPass()); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 425 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 426 | |
| 427 | // Inline small functions |
Rafael Espindola | e07caad | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 428 | bool RunInliner = Inliner; |
| 429 | if (RunInliner) { |
| 430 | PM.add(Inliner); |
| 431 | Inliner = nullptr; |
| 432 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 433 | |
| 434 | PM.add(createPruneEHPass()); // Remove dead EH info. |
| 435 | |
| 436 | // Optimize globals again if we ran the inliner. |
| 437 | if (RunInliner) |
| 438 | PM.add(createGlobalOptimizerPass()); |
| 439 | PM.add(createGlobalDCEPass()); // Remove dead functions. |
| 440 | |
| 441 | // If we didn't decide to inline a function, check to see if we can |
| 442 | // transform it to pass arguments by value instead of by reference. |
| 443 | PM.add(createArgumentPromotionPass()); |
| 444 | |
| 445 | // The IPO passes may leave cruft around. Clean up after them. |
| 446 | PM.add(createInstructionCombiningPass()); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 447 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 448 | PM.add(createJumpThreadingPass()); |
Bill Wendling | 4c0d9ad | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 449 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 450 | // Break up allocas |
Chandler Carruth | 1b398ae | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 451 | if (UseNewSROA) |
| 452 | PM.add(createSROAPass()); |
| 453 | else |
| 454 | PM.add(createScalarReplAggregatesPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 455 | |
| 456 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 457 | PM.add(createFunctionAttrsPass()); // Add nocapture. |
| 458 | PM.add(createGlobalsModRefPass()); // IP alias analysis. |
| 459 | |
Bill Wendling | 932b992 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 460 | PM.add(createLICMPass()); // Hoist loop invariants. |
Gerolf Hoflehner | 24815d9 | 2014-09-10 19:55:29 +0000 | [diff] [blame] | 461 | if (EnableMLSM) |
| 462 | PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds. |
Bill Wendling | 932b992 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 463 | PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. |
| 464 | PM.add(createMemCpyOptPass()); // Remove dead memcpys. |
Bill Wendling | 4c0d9ad | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 465 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 466 | // Nuke dead stores. |
| 467 | PM.add(createDeadStoreEliminationPass()); |
| 468 | |
Duncan P. N. Exon Smith | 2b69189 | 2014-04-15 17:48:15 +0000 | [diff] [blame] | 469 | // More loops are countable; try to optimize them. |
| 470 | PM.add(createIndVarSimplifyPass()); |
| 471 | PM.add(createLoopDeletionPass()); |
Karthik Bhat | 88db86d | 2015-03-06 10:11:25 +0000 | [diff] [blame] | 472 | if (EnableLoopInterchange) |
| 473 | PM.add(createLoopInterchangePass()); |
| 474 | |
Arnold Schwaighofer | eb1a38f | 2014-10-26 21:50:58 +0000 | [diff] [blame] | 475 | PM.add(createLoopVectorizePass(true, LoopVectorize)); |
Arnold Schwaighofer | 6ccda92 | 2014-02-24 18:19:31 +0000 | [diff] [blame] | 476 | |
Yi Jiang | 79eb0aa | 2014-05-05 23:14:46 +0000 | [diff] [blame] | 477 | // More scalar chains could be vectorized due to more alias information |
JF Bastien | f42a6ea | 2014-10-21 23:18:21 +0000 | [diff] [blame] | 478 | if (RunSLPAfterLoopVectorization) |
| 479 | if (SLPVectorize) |
| 480 | PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
Yi Jiang | 79eb0aa | 2014-05-05 23:14:46 +0000 | [diff] [blame] | 481 | |
Hal Finkel | d67e463 | 2014-09-07 20:05:11 +0000 | [diff] [blame] | 482 | // After vectorization, assume intrinsics may tell us more about pointer |
| 483 | // alignments. |
| 484 | PM.add(createAlignmentFromAssumptionsPass()); |
| 485 | |
Michael J. Spencer | 289067c | 2014-05-29 01:55:07 +0000 | [diff] [blame] | 486 | if (LoadCombine) |
| 487 | PM.add(createLoadCombinePass()); |
| 488 | |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 489 | // Cleanup and simplify the code after the scalar optimizations. |
| 490 | PM.add(createInstructionCombiningPass()); |
Peter Collingbourne | 0a43761 | 2014-05-25 10:27:02 +0000 | [diff] [blame] | 491 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 492 | |
| 493 | PM.add(createJumpThreadingPass()); |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame^] | 494 | } |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 495 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame^] | 496 | void PassManagerBuilder::addLateLTOOptimizationPasses( |
| 497 | legacy::PassManagerBase &PM) { |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 498 | // Delete basic blocks, which optimization passes may have killed. |
Tom Stellard | aa664d9 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 499 | PM.add(createCFGSimplificationPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 500 | |
| 501 | // Now that we have optimized the program, discard unreachable functions. |
| 502 | PM.add(createGlobalDCEPass()); |
Nick Lewycky | 9e6d184 | 2014-09-13 21:46:00 +0000 | [diff] [blame] | 503 | |
| 504 | // FIXME: this is profitable (for compiler time) to do at -O0 too, but |
| 505 | // currently it damages debug info. |
| 506 | if (MergeFunctions) |
| 507 | PM.add(createMergeFunctionsPass()); |
Rafael Espindola | 3ea478b | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 508 | } |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 509 | |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 510 | void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) { |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 511 | if (LibraryInfo) |
Chandler Carruth | b98f63d | 2015-01-15 10:41:28 +0000 | [diff] [blame] | 512 | PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo)); |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 513 | |
Duncan P. N. Exon Smith | 0a93e2d | 2015-03-19 21:37:17 +0000 | [diff] [blame] | 514 | if (VerifyInput) { |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 515 | PM.add(createVerifierPass()); |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 516 | PM.add(createDebugInfoVerifierPass()); |
Duncan P. N. Exon Smith | 0a93e2d | 2015-03-19 21:37:17 +0000 | [diff] [blame] | 517 | } |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 518 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame^] | 519 | if (OptLevel > 1) |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 520 | addLTOOptimizationPasses(PM); |
| 521 | |
Peter Collingbourne | 070843d | 2015-03-19 22:01:00 +0000 | [diff] [blame^] | 522 | // Lower bit sets to globals. This pass supports Clang's control flow |
| 523 | // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI |
| 524 | // is enabled. The pass does nothing if CFI is disabled. |
| 525 | PM.add(createLowerBitSetsPass()); |
| 526 | |
| 527 | if (OptLevel != 0) |
| 528 | addLateLTOOptimizationPasses(PM); |
| 529 | |
Rafael Espindola | 7cebf36 | 2014-08-21 20:03:44 +0000 | [diff] [blame] | 530 | if (VerifyOutput) { |
| 531 | PM.add(createVerifierPass()); |
| 532 | PM.add(createDebugInfoVerifierPass()); |
| 533 | } |
| 534 | } |
| 535 | |
Eric Christopher | 04d4e93 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 536 | inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { |
| 537 | return reinterpret_cast<PassManagerBuilder*>(P); |
| 538 | } |
| 539 | |
| 540 | inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { |
| 541 | return reinterpret_cast<LLVMPassManagerBuilderRef>(P); |
| 542 | } |
| 543 | |
Dmitri Gribenko | 0011bbf | 2012-11-15 16:51:49 +0000 | [diff] [blame] | 544 | LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 545 | PassManagerBuilder *PMB = new PassManagerBuilder(); |
| 546 | return wrap(PMB); |
| 547 | } |
| 548 | |
| 549 | void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { |
| 550 | PassManagerBuilder *Builder = unwrap(PMB); |
| 551 | delete Builder; |
| 552 | } |
| 553 | |
| 554 | void |
| 555 | LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, |
| 556 | unsigned OptLevel) { |
| 557 | PassManagerBuilder *Builder = unwrap(PMB); |
| 558 | Builder->OptLevel = OptLevel; |
| 559 | } |
| 560 | |
| 561 | void |
| 562 | LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, |
| 563 | unsigned SizeLevel) { |
| 564 | PassManagerBuilder *Builder = unwrap(PMB); |
| 565 | Builder->SizeLevel = SizeLevel; |
| 566 | } |
| 567 | |
| 568 | void |
| 569 | LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, |
| 570 | LLVMBool Value) { |
| 571 | PassManagerBuilder *Builder = unwrap(PMB); |
| 572 | Builder->DisableUnitAtATime = Value; |
| 573 | } |
| 574 | |
| 575 | void |
| 576 | LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, |
| 577 | LLVMBool Value) { |
| 578 | PassManagerBuilder *Builder = unwrap(PMB); |
| 579 | Builder->DisableUnrollLoops = Value; |
| 580 | } |
| 581 | |
| 582 | void |
| 583 | LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, |
| 584 | LLVMBool Value) { |
Meador Inge | dfb08a2 | 2013-06-20 19:48:07 +0000 | [diff] [blame] | 585 | // NOTE: The simplify-libcalls pass has been removed. |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 586 | } |
| 587 | |
| 588 | void |
| 589 | LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, |
| 590 | unsigned Threshold) { |
| 591 | PassManagerBuilder *Builder = unwrap(PMB); |
| 592 | Builder->Inliner = createFunctionInliningPass(Threshold); |
| 593 | } |
| 594 | |
| 595 | void |
| 596 | LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, |
| 597 | LLVMPassManagerRef PM) { |
| 598 | PassManagerBuilder *Builder = unwrap(PMB); |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 599 | legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM); |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 600 | Builder->populateFunctionPassManager(*FPM); |
| 601 | } |
| 602 | |
| 603 | void |
| 604 | LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, |
| 605 | LLVMPassManagerRef PM) { |
| 606 | PassManagerBuilder *Builder = unwrap(PMB); |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 607 | legacy::PassManagerBase *MPM = unwrap(PM); |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 608 | Builder->populateModulePassManager(*MPM); |
| 609 | } |
| 610 | |
| 611 | void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, |
| 612 | LLVMPassManagerRef PM, |
Nick Lewycky | 5f50854 | 2013-03-10 21:58:22 +0000 | [diff] [blame] | 613 | LLVMBool Internalize, |
| 614 | LLVMBool RunInliner) { |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 615 | PassManagerBuilder *Builder = unwrap(PMB); |
Chandler Carruth | 30d69c2 | 2015-02-13 10:01:29 +0000 | [diff] [blame] | 616 | legacy::PassManagerBase *LPM = unwrap(PM); |
Rafael Espindola | e07caad | 2014-08-21 13:35:30 +0000 | [diff] [blame] | 617 | |
| 618 | // A small backwards compatibility hack. populateLTOPassManager used to take |
| 619 | // an RunInliner option. |
| 620 | if (RunInliner && !Builder->Inliner) |
| 621 | Builder->Inliner = createFunctionInliningPass(); |
| 622 | |
| 623 | Builder->populateLTOPassManager(*LPM); |
Rafael Espindola | 07f60915 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 624 | } |