Rafael Espindola | c684e83 | 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 | 69cb216 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 17 | #include "llvm-c/Transforms/PassManagerBuilder.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallVector.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 19 | #include "llvm/Analysis/Passes.h" |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 20 | #include "llvm/IR/Verifier.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 21 | #include "llvm/PassManager.h" |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 22 | #include "llvm/Support/CommandLine.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ManagedStatic.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 24 | #include "llvm/Target/TargetLibraryInfo.h" |
Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 25 | #include "llvm/Transforms/IPO.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Scalar.h" |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Vectorize.h" |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 28 | |
| 29 | using namespace llvm; |
| 30 | |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 31 | static cl::opt<bool> |
Nadav Rotem | fe16848 | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 32 | RunLoopVectorization("vectorize-loops", cl::Hidden, |
Nadav Rotem | 9210cc3 | 2012-10-30 18:37:43 +0000 | [diff] [blame] | 33 | cl::desc("Run the Loop vectorization passes")); |
Nadav Rotem | d233b78 | 2012-10-29 16:36:25 +0000 | [diff] [blame] | 34 | |
| 35 | static cl::opt<bool> |
Nadav Rotem | fe16848 | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 36 | RunSLPVectorization("vectorize-slp", cl::Hidden, |
Nadav Rotem | 1129a83 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 37 | cl::desc("Run the SLP vectorization passes")); |
| 38 | |
| 39 | static cl::opt<bool> |
Nadav Rotem | fe16848 | 2013-10-18 23:38:13 +0000 | [diff] [blame] | 40 | RunBBVectorization("vectorize-slp-aggressive", cl::Hidden, |
Nadav Rotem | 1129a83 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 41 | cl::desc("Run the BB vectorization passes")); |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 42 | |
Hal Finkel | 064551e | 2012-04-13 17:15:33 +0000 | [diff] [blame] | 43 | static cl::opt<bool> |
| 44 | UseGVNAfterVectorization("use-gvn-after-vectorization", |
| 45 | cl::init(false), cl::Hidden, |
| 46 | cl::desc("Run GVN instead of Early CSE after vectorization passes")); |
| 47 | |
Chandler Carruth | 713aa94 | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 48 | static cl::opt<bool> UseNewSROA("use-new-sroa", |
Chandler Carruth | 1fe4fae | 2012-10-02 04:24:01 +0000 | [diff] [blame] | 49 | cl::init(true), cl::Hidden, |
Chandler Carruth | 713aa94 | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 50 | cl::desc("Enable the new, experimental SROA pass")); |
| 51 | |
Hal Finkel | bebe48d | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 52 | static cl::opt<bool> |
| 53 | RunLoopRerolling("reroll-loops", cl::Hidden, |
| 54 | cl::desc("Run the loop rerolling pass")); |
| 55 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 56 | PassManagerBuilder::PassManagerBuilder() { |
| 57 | OptLevel = 2; |
| 58 | SizeLevel = 0; |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 59 | LibraryInfo = nullptr; |
| 60 | Inliner = nullptr; |
| 61 | DisableTailCalls = false; |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 62 | DisableUnitAtATime = false; |
| 63 | DisableUnrollLoops = false; |
Nadav Rotem | 1129a83 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 64 | BBVectorize = RunBBVectorization; |
Nadav Rotem | 8849838 | 2013-04-15 04:54:42 +0000 | [diff] [blame] | 65 | SLPVectorize = RunSLPVectorization; |
Nadav Rotem | d233b78 | 2012-10-29 16:36:25 +0000 | [diff] [blame] | 66 | LoopVectorize = RunLoopVectorization; |
Hal Finkel | c8dc96b | 2013-11-17 16:02:50 +0000 | [diff] [blame] | 67 | RerollLoops = RunLoopRerolling; |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | PassManagerBuilder::~PassManagerBuilder() { |
| 71 | delete LibraryInfo; |
| 72 | delete Inliner; |
| 73 | } |
| 74 | |
David Chisnall | 7a817ea | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 75 | /// Set of global extensions, automatically added as part of the standard set. |
| 76 | static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy, |
| 77 | PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions; |
| 78 | |
| 79 | void PassManagerBuilder::addGlobalExtension( |
| 80 | PassManagerBuilder::ExtensionPointTy Ty, |
| 81 | PassManagerBuilder::ExtensionFn Fn) { |
| 82 | GlobalExtensions->push_back(std::make_pair(Ty, Fn)); |
| 83 | } |
| 84 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 85 | void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) { |
| 86 | Extensions.push_back(std::make_pair(Ty, Fn)); |
| 87 | } |
| 88 | |
| 89 | void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy, |
| 90 | PassManagerBase &PM) const { |
David Chisnall | 7a817ea | 2011-08-16 13:58:41 +0000 | [diff] [blame] | 91 | for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i) |
| 92 | if ((*GlobalExtensions)[i].first == ETy) |
| 93 | (*GlobalExtensions)[i].second(*this, PM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 94 | for (unsigned i = 0, e = Extensions.size(); i != e; ++i) |
| 95 | if (Extensions[i].first == ETy) |
| 96 | Extensions[i].second(*this, PM); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const { |
| 101 | // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that |
| 102 | // BasicAliasAnalysis wins if they disagree. This is intended to help |
| 103 | // support "obvious" type-punning idioms. |
| 104 | PM.add(createTypeBasedAliasAnalysisPass()); |
| 105 | PM.add(createBasicAliasAnalysisPass()); |
| 106 | } |
| 107 | |
| 108 | void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) { |
| 109 | addExtensionsToPM(EP_EarlyAsPossible, FPM); |
| 110 | |
| 111 | // Add LibraryInfo if we have some. |
| 112 | if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo)); |
| 113 | |
| 114 | if (OptLevel == 0) return; |
| 115 | |
| 116 | addInitialAliasAnalysisPasses(FPM); |
| 117 | |
| 118 | FPM.add(createCFGSimplificationPass()); |
Chandler Carruth | 713aa94 | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 119 | if (UseNewSROA) |
| 120 | FPM.add(createSROAPass()); |
| 121 | else |
| 122 | FPM.add(createScalarReplAggregatesPass()); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 123 | FPM.add(createEarlyCSEPass()); |
| 124 | FPM.add(createLowerExpectIntrinsicPass()); |
| 125 | } |
| 126 | |
| 127 | void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) { |
| 128 | // If all optimizations are disabled, just run the always-inline pass. |
| 129 | if (OptLevel == 0) { |
| 130 | if (Inliner) { |
| 131 | MPM.add(Inliner); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 132 | Inliner = nullptr; |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 133 | } |
Chandler Carruth | 63a1eb6 | 2012-10-18 08:05:46 +0000 | [diff] [blame] | 134 | |
| 135 | // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC |
| 136 | // pass manager, but we don't want to add extensions into that pass manager. |
| 137 | // To prevent this we must insert a no-op module pass to reset the pass |
| 138 | // manager to get the same behavior as EP_OptimizerLast in non-O0 builds. |
| 139 | if (!GlobalExtensions->empty() || !Extensions.empty()) |
| 140 | MPM.add(createBarrierNoopPass()); |
| 141 | |
Kostya Serebryany | af65a8c | 2011-11-30 22:19:26 +0000 | [diff] [blame] | 142 | addExtensionsToPM(EP_EnabledOnOptLevel0, MPM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 143 | return; |
| 144 | } |
| 145 | |
| 146 | // Add LibraryInfo if we have some. |
| 147 | if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo)); |
| 148 | |
| 149 | addInitialAliasAnalysisPasses(MPM); |
| 150 | |
| 151 | if (!DisableUnitAtATime) { |
Dan Gohman | 7d4c87e | 2012-01-17 20:51:32 +0000 | [diff] [blame] | 152 | addExtensionsToPM(EP_ModuleOptimizerEarly, MPM); |
| 153 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 154 | MPM.add(createGlobalOptimizerPass()); // Optimize out global vars |
| 155 | |
| 156 | MPM.add(createIPSCCPPass()); // IP SCCP |
| 157 | MPM.add(createDeadArgEliminationPass()); // Dead argument elimination |
| 158 | |
| 159 | MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 160 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 161 | MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE |
| 162 | } |
| 163 | |
| 164 | // Start of CallGraph SCC passes. |
| 165 | if (!DisableUnitAtATime) |
| 166 | MPM.add(createPruneEHPass()); // Remove dead EH info |
| 167 | if (Inliner) { |
| 168 | MPM.add(Inliner); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 169 | Inliner = nullptr; |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 170 | } |
| 171 | if (!DisableUnitAtATime) |
| 172 | MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs |
| 173 | if (OptLevel > 2) |
| 174 | MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args |
| 175 | |
| 176 | // Start of function pass. |
| 177 | // Break up aggregate allocas, using SSAUpdater. |
Chandler Carruth | 1c8db50 | 2012-09-15 11:43:14 +0000 | [diff] [blame] | 178 | if (UseNewSROA) |
| 179 | MPM.add(createSROAPass(/*RequiresDomTree*/ false)); |
| 180 | else |
| 181 | MPM.add(createScalarReplAggregatesPass(-1, false)); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 182 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 183 | MPM.add(createJumpThreadingPass()); // Thread jumps. |
| 184 | MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals |
| 185 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
| 186 | MPM.add(createInstructionCombiningPass()); // Combine silly seq's |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 187 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 188 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 189 | if (!DisableTailCalls) |
| 190 | MPM.add(createTailCallEliminationPass()); // Eliminate tail calls |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 191 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
| 192 | MPM.add(createReassociatePass()); // Reassociate expressions |
| 193 | MPM.add(createLoopRotatePass()); // Rotate Loop |
| 194 | MPM.add(createLICMPass()); // Hoist loop invariants |
| 195 | MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3)); |
| 196 | MPM.add(createInstructionCombiningPass()); |
| 197 | MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars |
| 198 | MPM.add(createLoopIdiomPass()); // Recognize idioms like memset. |
| 199 | MPM.add(createLoopDeletionPass()); // Delete dead loops |
Nadav Rotem | d15c0c7 | 2012-10-17 18:25:06 +0000 | [diff] [blame] | 200 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 201 | if (!DisableUnrollLoops) |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 202 | MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 203 | addExtensionsToPM(EP_LoopOptimizerEnd, MPM); |
| 204 | |
| 205 | if (OptLevel > 1) |
| 206 | MPM.add(createGVNPass()); // Remove redundancies |
| 207 | MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset |
| 208 | MPM.add(createSCCPPass()); // Constant prop with SCCP |
| 209 | |
| 210 | // Run instcombine after redundancy elimination to exploit opportunities |
| 211 | // opened up by them. |
| 212 | MPM.add(createInstructionCombiningPass()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 213 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 214 | MPM.add(createJumpThreadingPass()); // Thread jumps |
| 215 | MPM.add(createCorrelatedValuePropagationPass()); |
| 216 | MPM.add(createDeadStoreEliminationPass()); // Delete dead stores |
| 217 | |
| 218 | addExtensionsToPM(EP_ScalarOptimizerLate, MPM); |
| 219 | |
Hal Finkel | c8dc96b | 2013-11-17 16:02:50 +0000 | [diff] [blame] | 220 | if (RerollLoops) |
Hal Finkel | bebe48d | 2013-11-16 23:59:05 +0000 | [diff] [blame] | 221 | MPM.add(createLoopRerollPass()); |
Nadav Rotem | f1cd798 | 2013-08-28 23:40:29 +0000 | [diff] [blame] | 222 | if (SLPVectorize) |
| 223 | MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
Nadav Rotem | 1129a83 | 2013-04-15 05:39:58 +0000 | [diff] [blame] | 224 | |
Nadav Rotem | f1cd798 | 2013-08-28 23:40:29 +0000 | [diff] [blame] | 225 | if (BBVectorize) { |
| 226 | MPM.add(createBBVectorizePass()); |
| 227 | MPM.add(createInstructionCombiningPass()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 228 | addExtensionsToPM(EP_Peephole, MPM); |
Nadav Rotem | f1cd798 | 2013-08-28 23:40:29 +0000 | [diff] [blame] | 229 | if (OptLevel > 1 && UseGVNAfterVectorization) |
| 230 | MPM.add(createGVNPass()); // Remove redundancies |
| 231 | else |
| 232 | MPM.add(createEarlyCSEPass()); // Catch trivial redundancies |
Hal Finkel | c0b3d4c | 2013-01-29 00:22:49 +0000 | [diff] [blame] | 233 | |
Nadav Rotem | f1cd798 | 2013-08-28 23:40:29 +0000 | [diff] [blame] | 234 | // BBVectorize may have significantly shortened a loop body; unroll again. |
| 235 | if (!DisableUnrollLoops) |
| 236 | MPM.add(createLoopUnrollPass()); |
Hal Finkel | de5e5ec | 2012-02-01 03:51:43 +0000 | [diff] [blame] | 237 | } |
| 238 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 239 | MPM.add(createAggressiveDCEPass()); // Delete dead instructions |
Tom Stellard | 01d7203 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 240 | MPM.add(createCFGSimplificationPass()); // Merge & remove BBs |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 241 | MPM.add(createInstructionCombiningPass()); // Clean up after everything. |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 242 | addExtensionsToPM(EP_Peephole, MPM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 243 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 244 | // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC |
| 245 | // pass manager that we are specifically trying to avoid. To prevent this |
| 246 | // we must insert a no-op module pass to reset the pass manager. |
| 247 | MPM.add(createBarrierNoopPass()); |
| 248 | MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize)); |
| 249 | // FIXME: Because of #pragma vectorize enable, the passes below are always |
| 250 | // inserted in the pipeline, even when the vectorizer doesn't run (ex. when |
| 251 | // on -O1 and no #pragma is found). Would be good to have these two passes |
| 252 | // as function calls, so that we can only pass them when the vectorizer |
| 253 | // changed the code. |
| 254 | MPM.add(createInstructionCombiningPass()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 255 | addExtensionsToPM(EP_Peephole, MPM); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 256 | MPM.add(createCFGSimplificationPass()); |
Chandler Carruth | 6bf3a05 | 2013-06-24 07:21:47 +0000 | [diff] [blame] | 257 | |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 258 | if (!DisableUnrollLoops) |
| 259 | MPM.add(createLoopUnrollPass()); // Unroll small loops |
Chandler Carruth | 6bf3a05 | 2013-06-24 07:21:47 +0000 | [diff] [blame] | 260 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 261 | if (!DisableUnitAtATime) { |
| 262 | // FIXME: We shouldn't bother with this anymore. |
| 263 | MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes |
| 264 | |
Evan Cheng | f5fdc14 | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 265 | // GlobalOpt already deletes dead functions and globals, at -O2 try a |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 266 | // late pass of GlobalDCE. It is capable of deleting dead cycles. |
Evan Cheng | f5fdc14 | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 267 | if (OptLevel > 1) { |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 268 | MPM.add(createGlobalDCEPass()); // Remove dead fns and globals. |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 269 | MPM.add(createConstantMergePass()); // Merge dup global constants |
Evan Cheng | f5fdc14 | 2012-09-28 21:23:26 +0000 | [diff] [blame] | 270 | } |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 271 | } |
Kostya Serebryany | 1db3949 | 2012-03-23 23:22:59 +0000 | [diff] [blame] | 272 | addExtensionsToPM(EP_OptimizerLast, MPM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM, |
| 276 | bool Internalize, |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 277 | bool RunInliner, |
| 278 | bool DisableGVNLoadPRE) { |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 279 | // Provide AliasAnalysis services for optimizations. |
| 280 | addInitialAliasAnalysisPasses(PM); |
| 281 | |
| 282 | // Now that composite has been compiled, scan through the module, looking |
| 283 | // for a main function. If main is defined, mark all other functions |
| 284 | // internal. |
Bill Wendling | 6e4d93b | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 285 | if (Internalize) |
Rafael Espindola | 7e667c5 | 2013-10-31 20:51:58 +0000 | [diff] [blame] | 286 | PM.add(createInternalizePass("main")); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 287 | |
| 288 | // Propagate constants at call sites into the functions they call. This |
| 289 | // opens opportunities for globalopt (and inlining) by substituting function |
| 290 | // pointers passed as arguments to direct uses of functions. |
| 291 | PM.add(createIPSCCPPass()); |
| 292 | |
| 293 | // Now that we internalized some globals, see if we can hack on them! |
| 294 | PM.add(createGlobalOptimizerPass()); |
| 295 | |
| 296 | // Linking modules together can lead to duplicated global constants, only |
| 297 | // keep one copy of each constant. |
| 298 | PM.add(createConstantMergePass()); |
| 299 | |
| 300 | // Remove unused arguments from functions. |
| 301 | PM.add(createDeadArgEliminationPass()); |
| 302 | |
| 303 | // Reduce the code after globalopt and ipsccp. Both can open up significant |
| 304 | // simplification opportunities, and both can propagate functions through |
| 305 | // function pointers. When this happens, we often have to resolve varargs |
| 306 | // calls, etc, so let instcombine do this. |
| 307 | PM.add(createInstructionCombiningPass()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 308 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 309 | |
| 310 | // Inline small functions |
| 311 | if (RunInliner) |
| 312 | PM.add(createFunctionInliningPass()); |
| 313 | |
| 314 | PM.add(createPruneEHPass()); // Remove dead EH info. |
| 315 | |
| 316 | // Optimize globals again if we ran the inliner. |
| 317 | if (RunInliner) |
| 318 | PM.add(createGlobalOptimizerPass()); |
| 319 | PM.add(createGlobalDCEPass()); // Remove dead functions. |
| 320 | |
| 321 | // If we didn't decide to inline a function, check to see if we can |
| 322 | // transform it to pass arguments by value instead of by reference. |
| 323 | PM.add(createArgumentPromotionPass()); |
| 324 | |
| 325 | // The IPO passes may leave cruft around. Clean up after them. |
| 326 | PM.add(createInstructionCombiningPass()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 327 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 328 | PM.add(createJumpThreadingPass()); |
Bill Wendling | 6e4d93b | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 329 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 330 | // Break up allocas |
Chandler Carruth | 713aa94 | 2012-09-14 09:22:59 +0000 | [diff] [blame] | 331 | if (UseNewSROA) |
| 332 | PM.add(createSROAPass()); |
| 333 | else |
| 334 | PM.add(createScalarReplAggregatesPass()); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 335 | |
| 336 | // Run a few AA driven optimizations here and now, to cleanup the code. |
| 337 | PM.add(createFunctionAttrsPass()); // Add nocapture. |
| 338 | PM.add(createGlobalsModRefPass()); // IP alias analysis. |
| 339 | |
Bill Wendling | 3197b44 | 2012-04-02 22:16:50 +0000 | [diff] [blame] | 340 | PM.add(createLICMPass()); // Hoist loop invariants. |
| 341 | PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies. |
| 342 | PM.add(createMemCpyOptPass()); // Remove dead memcpys. |
Bill Wendling | 6e4d93b | 2013-08-30 00:48:37 +0000 | [diff] [blame] | 343 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 344 | // Nuke dead stores. |
| 345 | PM.add(createDeadStoreEliminationPass()); |
| 346 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 347 | // More loops are countable; try to optimize them. |
| 348 | PM.add(createIndVarSimplifyPass()); |
| 349 | PM.add(createLoopDeletionPass()); |
Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 350 | PM.add(createLoopVectorizePass(true, true)); |
| 351 | |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 352 | // More scalar chains could be vectorized due to more alias information |
| 353 | PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains. |
| 354 | |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 355 | // Cleanup and simplify the code after the scalar optimizations. |
| 356 | PM.add(createInstructionCombiningPass()); |
Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame^] | 357 | addExtensionsToPM(EP_Peephole, PM); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 358 | |
| 359 | PM.add(createJumpThreadingPass()); |
| 360 | |
| 361 | // Delete basic blocks, which optimization passes may have killed. |
Tom Stellard | 01d7203 | 2013-08-06 02:43:45 +0000 | [diff] [blame] | 362 | PM.add(createCFGSimplificationPass()); |
Rafael Espindola | c684e83 | 2011-08-02 21:50:27 +0000 | [diff] [blame] | 363 | |
| 364 | // Now that we have optimized the program, discard unreachable functions. |
| 365 | PM.add(createGlobalDCEPass()); |
| 366 | } |
Rafael Espindola | 69cb216 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 367 | |
Eric Christopher | 3e39731 | 2013-04-22 22:47:22 +0000 | [diff] [blame] | 368 | inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) { |
| 369 | return reinterpret_cast<PassManagerBuilder*>(P); |
| 370 | } |
| 371 | |
| 372 | inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) { |
| 373 | return reinterpret_cast<LLVMPassManagerBuilderRef>(P); |
| 374 | } |
| 375 | |
Dmitri Gribenko | 79c07d2 | 2012-11-15 16:51:49 +0000 | [diff] [blame] | 376 | LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() { |
Rafael Espindola | 69cb216 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 377 | PassManagerBuilder *PMB = new PassManagerBuilder(); |
| 378 | return wrap(PMB); |
| 379 | } |
| 380 | |
| 381 | void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) { |
| 382 | PassManagerBuilder *Builder = unwrap(PMB); |
| 383 | delete Builder; |
| 384 | } |
| 385 | |
| 386 | void |
| 387 | LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB, |
| 388 | unsigned OptLevel) { |
| 389 | PassManagerBuilder *Builder = unwrap(PMB); |
| 390 | Builder->OptLevel = OptLevel; |
| 391 | } |
| 392 | |
| 393 | void |
| 394 | LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB, |
| 395 | unsigned SizeLevel) { |
| 396 | PassManagerBuilder *Builder = unwrap(PMB); |
| 397 | Builder->SizeLevel = SizeLevel; |
| 398 | } |
| 399 | |
| 400 | void |
| 401 | LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB, |
| 402 | LLVMBool Value) { |
| 403 | PassManagerBuilder *Builder = unwrap(PMB); |
| 404 | Builder->DisableUnitAtATime = Value; |
| 405 | } |
| 406 | |
| 407 | void |
| 408 | LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB, |
| 409 | LLVMBool Value) { |
| 410 | PassManagerBuilder *Builder = unwrap(PMB); |
| 411 | Builder->DisableUnrollLoops = Value; |
| 412 | } |
| 413 | |
| 414 | void |
| 415 | LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB, |
| 416 | LLVMBool Value) { |
Meador Inge | be87bce | 2013-06-20 19:48:07 +0000 | [diff] [blame] | 417 | // NOTE: The simplify-libcalls pass has been removed. |
Rafael Espindola | 69cb216 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | void |
| 421 | LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB, |
| 422 | unsigned Threshold) { |
| 423 | PassManagerBuilder *Builder = unwrap(PMB); |
| 424 | Builder->Inliner = createFunctionInliningPass(Threshold); |
| 425 | } |
| 426 | |
| 427 | void |
| 428 | LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB, |
| 429 | LLVMPassManagerRef PM) { |
| 430 | PassManagerBuilder *Builder = unwrap(PMB); |
| 431 | FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM); |
| 432 | Builder->populateFunctionPassManager(*FPM); |
| 433 | } |
| 434 | |
| 435 | void |
| 436 | LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB, |
| 437 | LLVMPassManagerRef PM) { |
| 438 | PassManagerBuilder *Builder = unwrap(PMB); |
| 439 | PassManagerBase *MPM = unwrap(PM); |
| 440 | Builder->populateModulePassManager(*MPM); |
| 441 | } |
| 442 | |
| 443 | void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB, |
| 444 | LLVMPassManagerRef PM, |
Nick Lewycky | 0ebc084 | 2013-03-10 21:58:22 +0000 | [diff] [blame] | 445 | LLVMBool Internalize, |
| 446 | LLVMBool RunInliner) { |
Rafael Espindola | 69cb216 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 447 | PassManagerBuilder *Builder = unwrap(PMB); |
| 448 | PassManagerBase *LPM = unwrap(PM); |
Nick Lewycky | 0ebc084 | 2013-03-10 21:58:22 +0000 | [diff] [blame] | 449 | Builder->populateLTOPassManager(*LPM, Internalize != 0, RunInliner != 0); |
Rafael Espindola | 69cb216 | 2011-08-09 22:17:34 +0000 | [diff] [blame] | 450 | } |