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