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