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