blob: 36ae6b2d717913b9553041ae35d759423b7f7b6e [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"
Chandler Carruth5ad5f152014-01-13 09:26:24 +000021#include "llvm/IR/Verifier.h"
Chandler Carruth30d69c22015-02-13 10:01:29 +000022#include "llvm/IR/LegacyPassManager.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000023#include "llvm/Support/CommandLine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Support/ManagedStatic.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000025#include "llvm/Analysis/TargetLibraryInfo.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000026#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Transforms/IPO.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000028#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000029#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000030
31using namespace llvm;
32
Hal Finkelc34e5112012-02-01 03:51:43 +000033static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000034RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000035 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000036
37static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000038RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000039 cl::desc("Run the SLP vectorization passes"));
40
41static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000042RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000043 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000044
Hal Finkel204bf532012-04-13 17:15:33 +000045static cl::opt<bool>
46UseGVNAfterVectorization("use-gvn-after-vectorization",
47 cl::init(false), cl::Hidden,
48 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
49
Chandler Carruth7b8297a2014-10-14 00:31:29 +000050static cl::opt<bool> ExtraVectorizerPasses(
51 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
52 cl::desc("Run cleanup optimization passes after vectorization."));
53
Chandler Carruth1b398ae2012-09-14 09:22:59 +000054static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000055 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000056 cl::desc("Enable the new, experimental SROA pass"));
57
Hal Finkelbf45efd2013-11-16 23:59:05 +000058static cl::opt<bool>
59RunLoopRerolling("reroll-loops", cl::Hidden,
60 cl::desc("Run the loop rerolling pass"));
61
James Molloy0cbb2a862015-03-27 10:36:57 +000062static cl::opt<bool>
63RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
64 cl::desc("Run the float2int (float demotion) pass"));
65
Michael J. Spencer289067c2014-05-29 01:55:07 +000066static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
67 cl::Hidden,
68 cl::desc("Run the load combining pass"));
69
James Molloy568da092014-08-06 12:56:19 +000070static cl::opt<bool>
71RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000072 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000073 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
74 "vectorizer instead of before"));
75
Hal Finkel445dda52014-09-02 22:12:54 +000076static cl::opt<bool> UseCFLAA("use-cfl-aa",
77 cl::init(false), cl::Hidden,
78 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000079
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000080static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000081EnableMLSM("mlsm", cl::init(true), cl::Hidden,
82 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000083
Karthik Bhat88db86d2015-03-06 10:11:25 +000084static cl::opt<bool> EnableLoopInterchange(
85 "enable-loopinterchange", cl::init(false), cl::Hidden,
86 cl::desc("Enable the new, experimental LoopInterchange Pass"));
87
Adam Nemet938d3d62015-05-14 12:05:18 +000088static cl::opt<bool> EnableLoopDistribute(
89 "enable-loop-distribute", cl::init(false), cl::Hidden,
90 cl::desc("Enable the new, experimental LoopDistribution Pass"));
91
Chandler Carruthe9ea5a62015-07-22 11:57:28 +000092static cl::opt<bool> EnableNonLTOGlobalsModRef(
93 "enable-non-lto-gmr", cl::init(false), cl::Hidden,
94 cl::desc(
95 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
96
Rafael Espindola3ea478b2011-08-02 21:50:27 +000097PassManagerBuilder::PassManagerBuilder() {
98 OptLevel = 2;
99 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000100 LibraryInfo = nullptr;
101 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000102 DisableUnitAtATime = false;
103 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000104 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000105 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000106 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000107 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000108 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000109 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000110 VerifyInput = false;
111 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000112 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000113 PrepareForLTO = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000114}
115
116PassManagerBuilder::~PassManagerBuilder() {
117 delete LibraryInfo;
118 delete Inliner;
119}
120
David Chisnall719a72f2011-08-16 13:58:41 +0000121/// Set of global extensions, automatically added as part of the standard set.
122static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
123 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
124
125void PassManagerBuilder::addGlobalExtension(
126 PassManagerBuilder::ExtensionPointTy Ty,
127 PassManagerBuilder::ExtensionFn Fn) {
128 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
129}
130
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000131void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
132 Extensions.push_back(std::make_pair(Ty, Fn));
133}
134
135void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000136 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000137 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
138 if ((*GlobalExtensions)[i].first == ETy)
139 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000140 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
141 if (Extensions[i].first == ETy)
142 Extensions[i].second(*this, PM);
143}
144
Chandler Carruth30d69c22015-02-13 10:01:29 +0000145void PassManagerBuilder::addInitialAliasAnalysisPasses(
146 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000147 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
148 // BasicAliasAnalysis wins if they disagree. This is intended to help
149 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000150 if (UseCFLAA)
151 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000152 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000153 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000154 PM.add(createBasicAliasAnalysisPass());
155}
156
Chandler Carruth30d69c22015-02-13 10:01:29 +0000157void PassManagerBuilder::populateFunctionPassManager(
158 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000159 addExtensionsToPM(EP_EarlyAsPossible, FPM);
160
161 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000162 if (LibraryInfo)
163 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000164
165 if (OptLevel == 0) return;
166
167 addInitialAliasAnalysisPasses(FPM);
168
169 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000170 if (UseNewSROA)
171 FPM.add(createSROAPass());
172 else
173 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000174 FPM.add(createEarlyCSEPass());
175 FPM.add(createLowerExpectIntrinsicPass());
176}
177
Chandler Carruth30d69c22015-02-13 10:01:29 +0000178void PassManagerBuilder::populateModulePassManager(
179 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000180 // If all optimizations are disabled, just run the always-inline pass and,
181 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000182 if (OptLevel == 0) {
183 if (Inliner) {
184 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000185 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000186 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000187
Nick Lewycky592d8492014-10-23 23:49:31 +0000188 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
189 // creates a CGSCC pass manager, but we don't want to add extensions into
190 // that pass manager. To prevent this we insert a no-op module pass to reset
191 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
192 // builds. The function merging pass is
193 if (MergeFunctions)
194 MPM.add(createMergeFunctionsPass());
195 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000196 MPM.add(createBarrierNoopPass());
197
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000198 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000199 return;
200 }
201
202 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000203 if (LibraryInfo)
204 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000205
206 addInitialAliasAnalysisPasses(MPM);
207
208 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000209 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
210
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000211 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000212 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
213
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000214 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
215
216 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000217 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000218 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
219 }
220
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000221 if (EnableNonLTOGlobalsModRef)
222 // We add a module alias analysis pass here. In part due to bugs in the
223 // analysis infrastructure this "works" in that the analysis stays alive
224 // for the entire SCC pass run below.
225 MPM.add(createGlobalsModRefPass());
226
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000227 // Start of CallGraph SCC passes.
228 if (!DisableUnitAtATime)
229 MPM.add(createPruneEHPass()); // Remove dead EH info
230 if (Inliner) {
231 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000232 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000233 }
234 if (!DisableUnitAtATime)
235 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
236 if (OptLevel > 2)
237 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
238
239 // Start of function pass.
240 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000241 if (UseNewSROA)
242 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
243 else
244 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000245 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000246 MPM.add(createJumpThreadingPass()); // Thread jumps.
247 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
248 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
249 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000250 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000251
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000252 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000253 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
254 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000255 // Rotate Loop - disable header duplication at -Oz
256 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000257 MPM.add(createLICMPass()); // Hoist loop invariants
258 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
259 MPM.add(createInstructionCombiningPass());
260 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
261 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
262 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000263 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000264 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000265 MPM.add(createCFGSimplificationPass());
266 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000267 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000268 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000269 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
270
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000271 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000272 if (EnableMLSM)
273 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000274 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000275 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000276 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
277 MPM.add(createSCCPPass()); // Constant prop with SCCP
278
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000279 // Delete dead bit computations (instcombine runs after to fold away the dead
280 // computations, and then ADCE will run later to exploit any new DCE
281 // opportunities that creates).
282 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
283
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000284 // Run instcombine after redundancy elimination to exploit opportunities
285 // opened up by them.
286 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000287 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000288 MPM.add(createJumpThreadingPass()); // Thread jumps
289 MPM.add(createCorrelatedValuePropagationPass());
290 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000291 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000292
293 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
294
Hal Finkel29aeb202013-11-17 16:02:50 +0000295 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000296 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000297 if (!RunSLPAfterLoopVectorization) {
298 if (SLPVectorize)
299 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000300
James Molloy568da092014-08-06 12:56:19 +0000301 if (BBVectorize) {
302 MPM.add(createBBVectorizePass());
303 MPM.add(createInstructionCombiningPass());
304 addExtensionsToPM(EP_Peephole, MPM);
305 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000306 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000307 else
308 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000309
James Molloy568da092014-08-06 12:56:19 +0000310 // BBVectorize may have significantly shortened a loop body; unroll again.
311 if (!DisableUnrollLoops)
312 MPM.add(createLoopUnrollPass());
313 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000314 }
315
Michael J. Spencer289067c2014-05-29 01:55:07 +0000316 if (LoadCombine)
317 MPM.add(createLoadCombinePass());
318
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000319 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000320 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000321 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000322 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000323
Renato Golin729a3ae2013-12-05 21:20:02 +0000324 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
325 // pass manager that we are specifically trying to avoid. To prevent this
326 // we must insert a no-op module pass to reset the pass manager.
327 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000328
Chandler Carruth08eebe22015-07-23 09:34:01 +0000329 if (EnableNonLTOGlobalsModRef)
330 // We add a fresh GlobalsModRef run at this point. This is particularly
331 // useful as the above will have inlined, DCE'ed, and function-attr
332 // propagated everything. We should at this point have a reasonably minimal
333 // and richly annotated call graph. By computing aliasing and mod/ref
334 // information for all local globals here, the late loop passes and notably
335 // the vectorizer will be able to use them to help recognize vectorizable
336 // memory operations.
337 //
338 // Note that this relies on a bug in the pass manager which preserves
339 // a module analysis into a function pass pipeline (and throughout it) so
340 // long as the first function pass doesn't invalidate the module analysis.
341 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
342 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
343 // (doing nothing preserves it as it is required to be conservatively
344 // correct in the face of IR changes).
345 MPM.add(createGlobalsModRefPass());
346
James Molloy0cbb2a862015-03-27 10:36:57 +0000347 if (RunFloat2Int)
348 MPM.add(createFloat2IntPass());
349
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000350 addExtensionsToPM(EP_VectorizerStart, MPM);
351
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000352 // Re-rotate loops in all our loop nests. These may have fallout out of
353 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000354 // on the rotated form. Disable header duplication at -Oz.
355 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000356
Adam Nemet938d3d62015-05-14 12:05:18 +0000357 // Distribute loops to allow partial vectorization. I.e. isolate dependences
358 // into separate loop that would otherwise inhibit vectorization.
359 if (EnableLoopDistribute)
360 MPM.add(createLoopDistributePass());
361
Renato Golin729a3ae2013-12-05 21:20:02 +0000362 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
363 // FIXME: Because of #pragma vectorize enable, the passes below are always
364 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
365 // on -O1 and no #pragma is found). Would be good to have these two passes
366 // as function calls, so that we can only pass them when the vectorizer
367 // changed the code.
368 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000369 if (OptLevel > 1 && ExtraVectorizerPasses) {
370 // At higher optimization levels, try to clean up any runtime overlap and
371 // alignment checks inserted by the vectorizer. We want to track correllated
372 // runtime checks for two inner loops in the same outer loop, fold any
373 // common computations, hoist loop-invariant aspects out of any outer loop,
374 // and unswitch the runtime checks if possible. Once hoisted, we may have
375 // dead (or speculatable) control flows or more combining opportunities.
376 MPM.add(createEarlyCSEPass());
377 MPM.add(createCorrelatedValuePropagationPass());
378 MPM.add(createInstructionCombiningPass());
379 MPM.add(createLICMPass());
380 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
381 MPM.add(createCFGSimplificationPass());
382 MPM.add(createInstructionCombiningPass());
383 }
James Molloy568da092014-08-06 12:56:19 +0000384
385 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000386 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000387 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000388 if (OptLevel > 1 && ExtraVectorizerPasses) {
389 MPM.add(createEarlyCSEPass());
390 }
391 }
James Molloy568da092014-08-06 12:56:19 +0000392
393 if (BBVectorize) {
394 MPM.add(createBBVectorizePass());
395 MPM.add(createInstructionCombiningPass());
396 addExtensionsToPM(EP_Peephole, MPM);
397 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000398 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000399 else
400 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
401
402 // BBVectorize may have significantly shortened a loop body; unroll again.
403 if (!DisableUnrollLoops)
404 MPM.add(createLoopUnrollPass());
405 }
406 }
407
Peter Collingbourne0a437612014-05-25 10:27:02 +0000408 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000409 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000410 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000411
Kevin Qin49bc7642015-03-12 05:36:01 +0000412 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000413 MPM.add(createLoopUnrollPass()); // Unroll small loops
414
Wei Mibf727ba2015-05-14 22:02:54 +0000415 // LoopUnroll may generate some redundency to cleanup.
416 MPM.add(createInstructionCombiningPass());
417
Kevin Qin49bc7642015-03-12 05:36:01 +0000418 // Runtime unrolling will introduce runtime check in loop prologue. If the
419 // unrolled loop is a inner loop, then the prologue will be inside the
420 // outer loop. LICM pass can help to promote the runtime check out if the
421 // checked value is loop invariant.
422 MPM.add(createLICMPass());
423 }
424
Hal Finkeld67e4632014-09-07 20:05:11 +0000425 // After vectorization and unrolling, assume intrinsics may tell us more
426 // about pointer alignments.
427 MPM.add(createAlignmentFromAssumptionsPass());
428
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000429 if (!DisableUnitAtATime) {
430 // FIXME: We shouldn't bother with this anymore.
431 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
432
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000433 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000434 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000435 if (OptLevel > 1) {
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000436 if (!PrepareForLTO) {
437 // Remove avail extern fns and globals definitions if we aren't
438 // compiling an object file for later LTO. For LTO we want to preserve
439 // these so they are eligible for inlining at link-time. Note if they
440 // are unreferenced they will be removed by GlobalDCE below, so
441 // this only impacts referenced available externally globals.
442 // Eventually they will be suppressed during codegen, but eliminating
443 // here enables more opportunity for GlobalDCE as it may make
444 // globals referenced by available external functions dead.
445 MPM.add(createEliminateAvailableExternallyPass());
446 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000447 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000448 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000449 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000450 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000451
452 if (MergeFunctions)
453 MPM.add(createMergeFunctionsPass());
454
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000455 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000456}
457
Chandler Carruth30d69c22015-02-13 10:01:29 +0000458void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000459 // Provide AliasAnalysis services for optimizations.
460 addInitialAliasAnalysisPasses(PM);
461
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000462 // Propagate constants at call sites into the functions they call. This
463 // opens opportunities for globalopt (and inlining) by substituting function
464 // pointers passed as arguments to direct uses of functions.
465 PM.add(createIPSCCPPass());
466
467 // Now that we internalized some globals, see if we can hack on them!
468 PM.add(createGlobalOptimizerPass());
469
470 // Linking modules together can lead to duplicated global constants, only
471 // keep one copy of each constant.
472 PM.add(createConstantMergePass());
473
474 // Remove unused arguments from functions.
475 PM.add(createDeadArgEliminationPass());
476
477 // Reduce the code after globalopt and ipsccp. Both can open up significant
478 // simplification opportunities, and both can propagate functions through
479 // function pointers. When this happens, we often have to resolve varargs
480 // calls, etc, so let instcombine do this.
481 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000482 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000483
484 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000485 bool RunInliner = Inliner;
486 if (RunInliner) {
487 PM.add(Inliner);
488 Inliner = nullptr;
489 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000490
491 PM.add(createPruneEHPass()); // Remove dead EH info.
492
493 // Optimize globals again if we ran the inliner.
494 if (RunInliner)
495 PM.add(createGlobalOptimizerPass());
496 PM.add(createGlobalDCEPass()); // Remove dead functions.
497
498 // If we didn't decide to inline a function, check to see if we can
499 // transform it to pass arguments by value instead of by reference.
500 PM.add(createArgumentPromotionPass());
501
502 // The IPO passes may leave cruft around. Clean up after them.
503 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000504 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000505 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000506
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000507 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000508 if (UseNewSROA)
509 PM.add(createSROAPass());
510 else
511 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000512
513 // Run a few AA driven optimizations here and now, to cleanup the code.
514 PM.add(createFunctionAttrsPass()); // Add nocapture.
515 PM.add(createGlobalsModRefPass()); // IP alias analysis.
516
Bill Wendling932b9922012-04-02 22:16:50 +0000517 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000518 if (EnableMLSM)
519 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000520 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
521 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000522
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000523 // Nuke dead stores.
524 PM.add(createDeadStoreEliminationPass());
525
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000526 // More loops are countable; try to optimize them.
527 PM.add(createIndVarSimplifyPass());
528 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000529 if (EnableLoopInterchange)
530 PM.add(createLoopInterchangePass());
531
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000532 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000533
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000534 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000535 if (RunSLPAfterLoopVectorization)
536 if (SLPVectorize)
537 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000538
Hal Finkeld67e4632014-09-07 20:05:11 +0000539 // After vectorization, assume intrinsics may tell us more about pointer
540 // alignments.
541 PM.add(createAlignmentFromAssumptionsPass());
542
Michael J. Spencer289067c2014-05-29 01:55:07 +0000543 if (LoadCombine)
544 PM.add(createLoadCombinePass());
545
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000546 // Cleanup and simplify the code after the scalar optimizations.
547 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000548 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000549
550 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000551}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000552
Peter Collingbourne070843d2015-03-19 22:01:00 +0000553void PassManagerBuilder::addLateLTOOptimizationPasses(
554 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000555 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000556 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000557
558 // Now that we have optimized the program, discard unreachable functions.
559 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000560
561 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
562 // currently it damages debug info.
563 if (MergeFunctions)
564 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000565}
Rafael Espindola07f609152011-08-09 22:17:34 +0000566
Chandler Carruth30d69c22015-02-13 10:01:29 +0000567void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000568 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000569 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000570
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000571 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000572 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000573
Peter Collingbourne070843d2015-03-19 22:01:00 +0000574 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000575 addLTOOptimizationPasses(PM);
576
Peter Collingbourne070843d2015-03-19 22:01:00 +0000577 // Lower bit sets to globals. This pass supports Clang's control flow
578 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
579 // is enabled. The pass does nothing if CFI is disabled.
580 PM.add(createLowerBitSetsPass());
581
582 if (OptLevel != 0)
583 addLateLTOOptimizationPasses(PM);
584
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000585 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000586 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000587}
588
Eric Christopher04d4e932013-04-22 22:47:22 +0000589inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
590 return reinterpret_cast<PassManagerBuilder*>(P);
591}
592
593inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
594 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
595}
596
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000597LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000598 PassManagerBuilder *PMB = new PassManagerBuilder();
599 return wrap(PMB);
600}
601
602void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
603 PassManagerBuilder *Builder = unwrap(PMB);
604 delete Builder;
605}
606
607void
608LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
609 unsigned OptLevel) {
610 PassManagerBuilder *Builder = unwrap(PMB);
611 Builder->OptLevel = OptLevel;
612}
613
614void
615LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
616 unsigned SizeLevel) {
617 PassManagerBuilder *Builder = unwrap(PMB);
618 Builder->SizeLevel = SizeLevel;
619}
620
621void
622LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
623 LLVMBool Value) {
624 PassManagerBuilder *Builder = unwrap(PMB);
625 Builder->DisableUnitAtATime = Value;
626}
627
628void
629LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
630 LLVMBool Value) {
631 PassManagerBuilder *Builder = unwrap(PMB);
632 Builder->DisableUnrollLoops = Value;
633}
634
635void
636LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
637 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000638 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000639}
640
641void
642LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
643 unsigned Threshold) {
644 PassManagerBuilder *Builder = unwrap(PMB);
645 Builder->Inliner = createFunctionInliningPass(Threshold);
646}
647
648void
649LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
650 LLVMPassManagerRef PM) {
651 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000652 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000653 Builder->populateFunctionPassManager(*FPM);
654}
655
656void
657LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
658 LLVMPassManagerRef PM) {
659 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000660 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000661 Builder->populateModulePassManager(*MPM);
662}
663
664void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
665 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000666 LLVMBool Internalize,
667 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000668 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000669 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000670
671 // A small backwards compatibility hack. populateLTOPassManager used to take
672 // an RunInliner option.
673 if (RunInliner && !Builder->Inliner)
674 Builder->Inliner = createFunctionInliningPass();
675
676 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000677}