blob: b8d1b7e78e354949fb09c5028d40ed4a90f4270b [file] [log] [blame]
Rafael Espindola3ea478b2011-08-02 21:50:27 +00001//===- 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 Espindola07f609152011-08-09 22:17:34 +000017#include "llvm-c/Transforms/PassManagerBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000018#include "llvm/ADT/SmallVector.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000019#include "llvm/Analysis/Passes.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000020#include "llvm/IR/DataLayout.h"
Teresa Johnson5fcbdb72015-12-07 19:21:11 +000021#include "llvm/IR/FunctionInfo.h"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000022#include "llvm/IR/Verifier.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000023#include "llvm/IR/LegacyPassManager.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000024#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Support/ManagedStatic.h"
Chandler Carruth17e0bc32015-08-06 07:33:15 +000026#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth8b046a42015-08-14 02:42:20 +000027#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth21dcff72015-08-14 03:48:20 +000028#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth42ff4482015-08-14 02:55:50 +000029#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000030#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth1db22822015-08-14 03:33:48 +000031#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000032#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000033#include "llvm/Transforms/IPO.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000034#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000035#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000036
37using namespace llvm;
38
Hal Finkelc34e5112012-02-01 03:51:43 +000039static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000040RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000041 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000042
43static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000044RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000045 cl::desc("Run the SLP vectorization passes"));
46
47static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000048RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000049 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000050
Hal Finkel204bf532012-04-13 17:15:33 +000051static cl::opt<bool>
52UseGVNAfterVectorization("use-gvn-after-vectorization",
53 cl::init(false), cl::Hidden,
54 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
55
Chandler Carruth7b8297a2014-10-14 00:31:29 +000056static cl::opt<bool> ExtraVectorizerPasses(
57 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
58 cl::desc("Run cleanup optimization passes after vectorization."));
59
Chandler Carruth1b398ae2012-09-14 09:22:59 +000060static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000061 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000062 cl::desc("Enable the new, experimental SROA pass"));
63
Hal Finkelbf45efd2013-11-16 23:59:05 +000064static cl::opt<bool>
65RunLoopRerolling("reroll-loops", cl::Hidden,
66 cl::desc("Run the loop rerolling pass"));
67
James Molloy0cbb2a862015-03-27 10:36:57 +000068static cl::opt<bool>
69RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
70 cl::desc("Run the float2int (float demotion) pass"));
71
Michael J. Spencer289067c2014-05-29 01:55:07 +000072static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
73 cl::Hidden,
74 cl::desc("Run the load combining pass"));
75
James Molloy568da092014-08-06 12:56:19 +000076static cl::opt<bool>
77RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000078 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000079 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
80 "vectorizer instead of before"));
81
Hal Finkel445dda52014-09-02 22:12:54 +000082static cl::opt<bool> UseCFLAA("use-cfl-aa",
83 cl::init(false), cl::Hidden,
84 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000085
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000086static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000087EnableMLSM("mlsm", cl::init(true), cl::Hidden,
88 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000089
Karthik Bhat88db86d2015-03-06 10:11:25 +000090static cl::opt<bool> EnableLoopInterchange(
91 "enable-loopinterchange", cl::init(false), cl::Hidden,
92 cl::desc("Enable the new, experimental LoopInterchange Pass"));
93
Adam Nemet938d3d62015-05-14 12:05:18 +000094static cl::opt<bool> EnableLoopDistribute(
95 "enable-loop-distribute", cl::init(false), cl::Hidden,
96 cl::desc("Enable the new, experimental LoopDistribution Pass"));
97
Chandler Carruthe9ea5a62015-07-22 11:57:28 +000098static cl::opt<bool> EnableNonLTOGlobalsModRef(
James Molloy40159252015-10-13 10:43:57 +000099 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000100 cl::desc(
101 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
102
Adam Nemete54a4fa2015-11-03 23:50:08 +0000103static 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 Espindola3ea478b2011-08-02 21:50:27 +0000107PassManagerBuilder::PassManagerBuilder() {
108 OptLevel = 2;
109 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000110 LibraryInfo = nullptr;
111 Inliner = nullptr;
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000112 FunctionIndex = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000113 DisableUnitAtATime = false;
114 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000115 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000116 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000117 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000118 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000119 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000120 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000121 VerifyInput = false;
122 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000123 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000124 PrepareForLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000125}
126
127PassManagerBuilder::~PassManagerBuilder() {
128 delete LibraryInfo;
129 delete Inliner;
130}
131
David Chisnall719a72f2011-08-16 13:58:41 +0000132/// Set of global extensions, automatically added as part of the standard set.
133static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
134 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
135
136void PassManagerBuilder::addGlobalExtension(
137 PassManagerBuilder::ExtensionPointTy Ty,
138 PassManagerBuilder::ExtensionFn Fn) {
139 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
140}
141
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000142void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
143 Extensions.push_back(std::make_pair(Ty, Fn));
144}
145
146void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000147 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000148 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
149 if ((*GlobalExtensions)[i].first == ETy)
150 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000151 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 Carruth30d69c22015-02-13 10:01:29 +0000156void PassManagerBuilder::addInitialAliasAnalysisPasses(
157 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000158 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
159 // BasicAliasAnalysis wins if they disagree. This is intended to help
160 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000161 if (UseCFLAA)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000162 PM.add(createCFLAAWrapperPass());
163 PM.add(createTypeBasedAAWrapperPass());
164 PM.add(createScopedNoAliasAAWrapperPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000165}
166
Chandler Carruth30d69c22015-02-13 10:01:29 +0000167void PassManagerBuilder::populateFunctionPassManager(
168 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000169 addExtensionsToPM(EP_EarlyAsPossible, FPM);
170
171 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000172 if (LibraryInfo)
173 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000174
175 if (OptLevel == 0) return;
176
177 addInitialAliasAnalysisPasses(FPM);
178
179 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000180 if (UseNewSROA)
181 FPM.add(createSROAPass());
182 else
183 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000184 FPM.add(createEarlyCSEPass());
185 FPM.add(createLowerExpectIntrinsicPass());
186}
187
Chandler Carruth30d69c22015-02-13 10:01:29 +0000188void PassManagerBuilder::populateModulePassManager(
189 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000190 // If all optimizations are disabled, just run the always-inline pass and,
191 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000192 if (OptLevel == 0) {
193 if (Inliner) {
194 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000195 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000196 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000197
Nick Lewycky592d8492014-10-23 23:49:31 +0000198 // 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 Carruthe8479e12012-10-18 08:05:46 +0000206 MPM.add(createBarrierNoopPass());
207
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000208 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000209 return;
210 }
211
212 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000213 if (LibraryInfo)
214 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000215
216 addInitialAliasAnalysisPasses(MPM);
217
218 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000219 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
220
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000221 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000222 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
223
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000224 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
225
226 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000227 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000228 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
229 }
230
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000231 if (EnableNonLTOGlobalsModRef)
232 // We add a module alias analysis pass here. In part due to bugs in the
233 // analysis infrastructure this "works" in that the analysis stays alive
234 // for the entire SCC pass run below.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000235 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000236
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000237 // Start of CallGraph SCC passes.
238 if (!DisableUnitAtATime)
239 MPM.add(createPruneEHPass()); // Remove dead EH info
240 if (Inliner) {
241 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000242 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000243 }
244 if (!DisableUnitAtATime)
245 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
246 if (OptLevel > 2)
247 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
248
249 // Start of function pass.
250 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000251 if (UseNewSROA)
Mehdi Aminid134a672015-08-23 22:15:49 +0000252 MPM.add(createSROAPass());
Chandler Carruth70b44c52012-09-15 11:43:14 +0000253 else
254 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000255 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000256 MPM.add(createJumpThreadingPass()); // Thread jumps.
257 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
258 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
259 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000260 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000261
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000262 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000263 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
264 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000265 // Rotate Loop - disable header duplication at -Oz
266 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000267 MPM.add(createLICMPass()); // Hoist loop invariants
268 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
Michael Zolotukhin74621cc2015-09-24 03:50:17 +0000269 MPM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000270 MPM.add(createInstructionCombiningPass());
271 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
272 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
273 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000274 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000275 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000276 MPM.add(createCFGSimplificationPass());
277 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000278 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000279 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000280 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
281
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000282 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000283 if (EnableMLSM)
284 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000285 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000286 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000287 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
288 MPM.add(createSCCPPass()); // Constant prop with SCCP
289
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000290 // Delete dead bit computations (instcombine runs after to fold away the dead
291 // computations, and then ADCE will run later to exploit any new DCE
292 // opportunities that creates).
293 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
294
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000295 // Run instcombine after redundancy elimination to exploit opportunities
296 // opened up by them.
297 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000298 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000299 MPM.add(createJumpThreadingPass()); // Thread jumps
300 MPM.add(createCorrelatedValuePropagationPass());
301 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000302 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000303
304 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
305
Hal Finkel29aeb202013-11-17 16:02:50 +0000306 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000307 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000308 if (!RunSLPAfterLoopVectorization) {
309 if (SLPVectorize)
310 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000311
James Molloy568da092014-08-06 12:56:19 +0000312 if (BBVectorize) {
313 MPM.add(createBBVectorizePass());
314 MPM.add(createInstructionCombiningPass());
315 addExtensionsToPM(EP_Peephole, MPM);
316 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000317 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000318 else
319 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000320
James Molloy568da092014-08-06 12:56:19 +0000321 // BBVectorize may have significantly shortened a loop body; unroll again.
322 if (!DisableUnrollLoops)
323 MPM.add(createLoopUnrollPass());
324 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000325 }
326
Michael J. Spencer289067c2014-05-29 01:55:07 +0000327 if (LoadCombine)
328 MPM.add(createLoadCombinePass());
329
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000330 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000331 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000332 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000333 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000334
Renato Golin729a3ae2013-12-05 21:20:02 +0000335 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
336 // pass manager that we are specifically trying to avoid. To prevent this
337 // we must insert a no-op module pass to reset the pass manager.
338 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000339
Yaron Keren611c7cf2015-09-02 06:34:11 +0000340 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) {
341 // Remove avail extern fns and globals definitions if we aren't
342 // compiling an object file for later LTO. For LTO we want to preserve
343 // these so they are eligible for inlining at link-time. Note if they
344 // are unreferenced they will be removed by GlobalDCE later, so
345 // this only impacts referenced available externally globals.
346 // Eventually they will be suppressed during codegen, but eliminating
347 // here enables more opportunity for GlobalDCE as it may make
348 // globals referenced by available external functions dead
349 // and saves running remaining passes on the eliminated functions.
350 MPM.add(createEliminateAvailableExternallyPass());
351 }
352
Chandler Carruth08eebe22015-07-23 09:34:01 +0000353 if (EnableNonLTOGlobalsModRef)
354 // We add a fresh GlobalsModRef run at this point. This is particularly
355 // useful as the above will have inlined, DCE'ed, and function-attr
356 // propagated everything. We should at this point have a reasonably minimal
357 // and richly annotated call graph. By computing aliasing and mod/ref
358 // information for all local globals here, the late loop passes and notably
359 // the vectorizer will be able to use them to help recognize vectorizable
360 // memory operations.
361 //
362 // Note that this relies on a bug in the pass manager which preserves
363 // a module analysis into a function pass pipeline (and throughout it) so
364 // long as the first function pass doesn't invalidate the module analysis.
365 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
366 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
367 // (doing nothing preserves it as it is required to be conservatively
368 // correct in the face of IR changes).
Chandler Carruth7b560d42015-09-09 17:55:00 +0000369 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruth08eebe22015-07-23 09:34:01 +0000370
James Molloy0cbb2a862015-03-27 10:36:57 +0000371 if (RunFloat2Int)
372 MPM.add(createFloat2IntPass());
373
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000374 addExtensionsToPM(EP_VectorizerStart, MPM);
375
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000376 // Re-rotate loops in all our loop nests. These may have fallout out of
377 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000378 // on the rotated form. Disable header duplication at -Oz.
379 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000380
Adam Nemet938d3d62015-05-14 12:05:18 +0000381 // Distribute loops to allow partial vectorization. I.e. isolate dependences
382 // into separate loop that would otherwise inhibit vectorization.
383 if (EnableLoopDistribute)
384 MPM.add(createLoopDistributePass());
385
Renato Golin729a3ae2013-12-05 21:20:02 +0000386 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000387
388 // Eliminate loads by forwarding stores from the previous iteration to loads
389 // of the current iteration.
390 if (EnableLoopLoadElim)
391 MPM.add(createLoopLoadEliminationPass());
392
Renato Golin729a3ae2013-12-05 21:20:02 +0000393 // FIXME: Because of #pragma vectorize enable, the passes below are always
394 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
395 // on -O1 and no #pragma is found). Would be good to have these two passes
396 // as function calls, so that we can only pass them when the vectorizer
397 // changed the code.
398 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000399 if (OptLevel > 1 && ExtraVectorizerPasses) {
400 // At higher optimization levels, try to clean up any runtime overlap and
401 // alignment checks inserted by the vectorizer. We want to track correllated
402 // runtime checks for two inner loops in the same outer loop, fold any
403 // common computations, hoist loop-invariant aspects out of any outer loop,
404 // and unswitch the runtime checks if possible. Once hoisted, we may have
405 // dead (or speculatable) control flows or more combining opportunities.
406 MPM.add(createEarlyCSEPass());
407 MPM.add(createCorrelatedValuePropagationPass());
408 MPM.add(createInstructionCombiningPass());
409 MPM.add(createLICMPass());
410 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
411 MPM.add(createCFGSimplificationPass());
412 MPM.add(createInstructionCombiningPass());
413 }
James Molloy568da092014-08-06 12:56:19 +0000414
415 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000416 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000417 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000418 if (OptLevel > 1 && ExtraVectorizerPasses) {
419 MPM.add(createEarlyCSEPass());
420 }
421 }
James Molloy568da092014-08-06 12:56:19 +0000422
423 if (BBVectorize) {
424 MPM.add(createBBVectorizePass());
425 MPM.add(createInstructionCombiningPass());
426 addExtensionsToPM(EP_Peephole, MPM);
427 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000428 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000429 else
430 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
431
432 // BBVectorize may have significantly shortened a loop body; unroll again.
433 if (!DisableUnrollLoops)
434 MPM.add(createLoopUnrollPass());
435 }
436 }
437
Peter Collingbourne0a437612014-05-25 10:27:02 +0000438 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000439 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000440 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000441
Kevin Qin49bc7642015-03-12 05:36:01 +0000442 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000443 MPM.add(createLoopUnrollPass()); // Unroll small loops
444
Wei Mibf727ba2015-05-14 22:02:54 +0000445 // LoopUnroll may generate some redundency to cleanup.
446 MPM.add(createInstructionCombiningPass());
447
Kevin Qin49bc7642015-03-12 05:36:01 +0000448 // Runtime unrolling will introduce runtime check in loop prologue. If the
449 // unrolled loop is a inner loop, then the prologue will be inside the
450 // outer loop. LICM pass can help to promote the runtime check out if the
451 // checked value is loop invariant.
452 MPM.add(createLICMPass());
453 }
454
Hal Finkeld67e4632014-09-07 20:05:11 +0000455 // After vectorization and unrolling, assume intrinsics may tell us more
456 // about pointer alignments.
457 MPM.add(createAlignmentFromAssumptionsPass());
458
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000459 if (!DisableUnitAtATime) {
460 // FIXME: We shouldn't bother with this anymore.
461 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
462
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000463 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000464 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000465 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000466 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000467 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000468 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000469 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000470
471 if (MergeFunctions)
472 MPM.add(createMergeFunctionsPass());
473
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000474 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000475}
476
Chandler Carruth30d69c22015-02-13 10:01:29 +0000477void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000478 // Provide AliasAnalysis services for optimizations.
479 addInitialAliasAnalysisPasses(PM);
480
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000481 if (FunctionIndex)
482 PM.add(createFunctionImportPass(FunctionIndex));
483
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000484 // Propagate constants at call sites into the functions they call. This
485 // opens opportunities for globalopt (and inlining) by substituting function
486 // pointers passed as arguments to direct uses of functions.
487 PM.add(createIPSCCPPass());
488
489 // Now that we internalized some globals, see if we can hack on them!
James Molloy9ad4f222015-11-18 11:24:42 +0000490 PM.add(createFunctionAttrsPass()); // Add norecurse if possible.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000491 PM.add(createGlobalOptimizerPass());
492
493 // Linking modules together can lead to duplicated global constants, only
494 // keep one copy of each constant.
495 PM.add(createConstantMergePass());
496
497 // Remove unused arguments from functions.
498 PM.add(createDeadArgEliminationPass());
499
500 // Reduce the code after globalopt and ipsccp. Both can open up significant
501 // simplification opportunities, and both can propagate functions through
502 // function pointers. When this happens, we often have to resolve varargs
503 // calls, etc, so let instcombine do this.
504 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000505 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000506
507 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000508 bool RunInliner = Inliner;
509 if (RunInliner) {
510 PM.add(Inliner);
511 Inliner = nullptr;
512 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000513
514 PM.add(createPruneEHPass()); // Remove dead EH info.
515
516 // Optimize globals again if we ran the inliner.
517 if (RunInliner)
518 PM.add(createGlobalOptimizerPass());
519 PM.add(createGlobalDCEPass()); // Remove dead functions.
520
521 // If we didn't decide to inline a function, check to see if we can
522 // transform it to pass arguments by value instead of by reference.
523 PM.add(createArgumentPromotionPass());
524
525 // The IPO passes may leave cruft around. Clean up after them.
526 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000527 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000528 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000529
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000530 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000531 if (UseNewSROA)
532 PM.add(createSROAPass());
533 else
534 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000535
536 // Run a few AA driven optimizations here and now, to cleanup the code.
537 PM.add(createFunctionAttrsPass()); // Add nocapture.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000538 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000539
Bill Wendling932b9922012-04-02 22:16:50 +0000540 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000541 if (EnableMLSM)
542 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000543 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
544 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000545
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000546 // Nuke dead stores.
547 PM.add(createDeadStoreEliminationPass());
548
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000549 // More loops are countable; try to optimize them.
550 PM.add(createIndVarSimplifyPass());
551 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000552 if (EnableLoopInterchange)
553 PM.add(createLoopInterchangePass());
554
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000555 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000556
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000557 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000558 if (RunSLPAfterLoopVectorization)
559 if (SLPVectorize)
560 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000561
Hal Finkeld67e4632014-09-07 20:05:11 +0000562 // After vectorization, assume intrinsics may tell us more about pointer
563 // alignments.
564 PM.add(createAlignmentFromAssumptionsPass());
565
Michael J. Spencer289067c2014-05-29 01:55:07 +0000566 if (LoadCombine)
567 PM.add(createLoadCombinePass());
568
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000569 // Cleanup and simplify the code after the scalar optimizations.
570 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000571 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000572
573 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000574}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000575
Peter Collingbourne070843d2015-03-19 22:01:00 +0000576void PassManagerBuilder::addLateLTOOptimizationPasses(
577 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000578 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000579 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000580
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000581 // Drop bodies of available externally objects to improve GlobalDCE.
582 PM.add(createEliminateAvailableExternallyPass());
583
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000584 // Now that we have optimized the program, discard unreachable functions.
585 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000586
587 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
588 // currently it damages debug info.
589 if (MergeFunctions)
590 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000591}
Rafael Espindola07f609152011-08-09 22:17:34 +0000592
Chandler Carruth30d69c22015-02-13 10:01:29 +0000593void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000594 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000595 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000596
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000597 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000598 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000599
Peter Collingbourne070843d2015-03-19 22:01:00 +0000600 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000601 addLTOOptimizationPasses(PM);
602
Peter Collingbourne070843d2015-03-19 22:01:00 +0000603 // Lower bit sets to globals. This pass supports Clang's control flow
604 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
605 // is enabled. The pass does nothing if CFI is disabled.
606 PM.add(createLowerBitSetsPass());
607
608 if (OptLevel != 0)
609 addLateLTOOptimizationPasses(PM);
610
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000611 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000612 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000613}
614
Eric Christopher04d4e932013-04-22 22:47:22 +0000615inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
616 return reinterpret_cast<PassManagerBuilder*>(P);
617}
618
619inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
620 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
621}
622
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000623LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000624 PassManagerBuilder *PMB = new PassManagerBuilder();
625 return wrap(PMB);
626}
627
628void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
629 PassManagerBuilder *Builder = unwrap(PMB);
630 delete Builder;
631}
632
633void
634LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
635 unsigned OptLevel) {
636 PassManagerBuilder *Builder = unwrap(PMB);
637 Builder->OptLevel = OptLevel;
638}
639
640void
641LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
642 unsigned SizeLevel) {
643 PassManagerBuilder *Builder = unwrap(PMB);
644 Builder->SizeLevel = SizeLevel;
645}
646
647void
648LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
649 LLVMBool Value) {
650 PassManagerBuilder *Builder = unwrap(PMB);
651 Builder->DisableUnitAtATime = Value;
652}
653
654void
655LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
656 LLVMBool Value) {
657 PassManagerBuilder *Builder = unwrap(PMB);
658 Builder->DisableUnrollLoops = Value;
659}
660
661void
662LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
663 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000664 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000665}
666
667void
668LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
669 unsigned Threshold) {
670 PassManagerBuilder *Builder = unwrap(PMB);
671 Builder->Inliner = createFunctionInliningPass(Threshold);
672}
673
674void
675LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
676 LLVMPassManagerRef PM) {
677 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000678 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000679 Builder->populateFunctionPassManager(*FPM);
680}
681
682void
683LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
684 LLVMPassManagerRef PM) {
685 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000686 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000687 Builder->populateModulePassManager(*MPM);
688}
689
690void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
691 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000692 LLVMBool Internalize,
693 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000694 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000695 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000696
697 // A small backwards compatibility hack. populateLTOPassManager used to take
698 // an RunInliner option.
699 if (RunInliner && !Builder->Inliner)
700 Builder->Inliner = createFunctionInliningPass();
701
702 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000703}