blob: 963f1bb13aafb4712bdc5317eb9c9a18fe9fe084 [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
Rafael Espindola3ea478b2011-08-02 21:50:27 +000092PassManagerBuilder::PassManagerBuilder() {
93 OptLevel = 2;
94 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000095 LibraryInfo = nullptr;
96 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000097 DisableUnitAtATime = false;
98 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000099 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000100 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000101 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000102 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000103 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000104 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000105 VerifyInput = false;
106 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000107 MergeFunctions = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000108}
109
110PassManagerBuilder::~PassManagerBuilder() {
111 delete LibraryInfo;
112 delete Inliner;
113}
114
David Chisnall719a72f2011-08-16 13:58:41 +0000115/// Set of global extensions, automatically added as part of the standard set.
116static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
117 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
118
119void PassManagerBuilder::addGlobalExtension(
120 PassManagerBuilder::ExtensionPointTy Ty,
121 PassManagerBuilder::ExtensionFn Fn) {
122 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
123}
124
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000125void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
126 Extensions.push_back(std::make_pair(Ty, Fn));
127}
128
129void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000130 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000131 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
132 if ((*GlobalExtensions)[i].first == ETy)
133 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000134 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
135 if (Extensions[i].first == ETy)
136 Extensions[i].second(*this, PM);
137}
138
Chandler Carruth30d69c22015-02-13 10:01:29 +0000139void PassManagerBuilder::addInitialAliasAnalysisPasses(
140 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000141 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
142 // BasicAliasAnalysis wins if they disagree. This is intended to help
143 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000144 if (UseCFLAA)
145 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000146 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000147 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000148 PM.add(createBasicAliasAnalysisPass());
149}
150
Chandler Carruth30d69c22015-02-13 10:01:29 +0000151void PassManagerBuilder::populateFunctionPassManager(
152 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000153 addExtensionsToPM(EP_EarlyAsPossible, FPM);
154
155 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000156 if (LibraryInfo)
157 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000158
159 if (OptLevel == 0) return;
160
161 addInitialAliasAnalysisPasses(FPM);
162
163 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000164 if (UseNewSROA)
165 FPM.add(createSROAPass());
166 else
167 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000168 FPM.add(createEarlyCSEPass());
169 FPM.add(createLowerExpectIntrinsicPass());
170}
171
Chandler Carruth30d69c22015-02-13 10:01:29 +0000172void PassManagerBuilder::populateModulePassManager(
173 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000174 // If all optimizations are disabled, just run the always-inline pass and,
175 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000176 if (OptLevel == 0) {
177 if (Inliner) {
178 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000179 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000180 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000181
Nick Lewycky592d8492014-10-23 23:49:31 +0000182 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
183 // creates a CGSCC pass manager, but we don't want to add extensions into
184 // that pass manager. To prevent this we insert a no-op module pass to reset
185 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
186 // builds. The function merging pass is
187 if (MergeFunctions)
188 MPM.add(createMergeFunctionsPass());
189 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000190 MPM.add(createBarrierNoopPass());
191
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000192 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000193 return;
194 }
195
196 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000197 if (LibraryInfo)
198 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000199
200 addInitialAliasAnalysisPasses(MPM);
201
202 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000203 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
204
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000205 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000206 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
207
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000208 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
209
210 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000211 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000212 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
213 }
214
215 // Start of CallGraph SCC passes.
216 if (!DisableUnitAtATime)
217 MPM.add(createPruneEHPass()); // Remove dead EH info
218 if (Inliner) {
219 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000220 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000221 }
222 if (!DisableUnitAtATime)
223 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
224 if (OptLevel > 2)
225 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
226
227 // Start of function pass.
228 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000229 if (UseNewSROA)
230 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
231 else
232 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000233 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000234 MPM.add(createJumpThreadingPass()); // Thread jumps.
235 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
236 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
237 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000238 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000239
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000240 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000241 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
242 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000243 // Rotate Loop - disable header duplication at -Oz
244 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000245 MPM.add(createLICMPass()); // Hoist loop invariants
246 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
247 MPM.add(createInstructionCombiningPass());
248 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
249 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
250 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000251 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000252 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000253 MPM.add(createCFGSimplificationPass());
254 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000255 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000256 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000257 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
258
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000259 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000260 if (EnableMLSM)
261 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000262 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000263 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000264 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
265 MPM.add(createSCCPPass()); // Constant prop with SCCP
266
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000267 // Delete dead bit computations (instcombine runs after to fold away the dead
268 // computations, and then ADCE will run later to exploit any new DCE
269 // opportunities that creates).
270 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
271
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000272 // Run instcombine after redundancy elimination to exploit opportunities
273 // opened up by them.
274 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000275 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000276 MPM.add(createJumpThreadingPass()); // Thread jumps
277 MPM.add(createCorrelatedValuePropagationPass());
278 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000279 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000280
281 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
282
Hal Finkel29aeb202013-11-17 16:02:50 +0000283 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000284 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000285 if (!RunSLPAfterLoopVectorization) {
286 if (SLPVectorize)
287 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000288
James Molloy568da092014-08-06 12:56:19 +0000289 if (BBVectorize) {
290 MPM.add(createBBVectorizePass());
291 MPM.add(createInstructionCombiningPass());
292 addExtensionsToPM(EP_Peephole, MPM);
293 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000294 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000295 else
296 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000297
James Molloy568da092014-08-06 12:56:19 +0000298 // BBVectorize may have significantly shortened a loop body; unroll again.
299 if (!DisableUnrollLoops)
300 MPM.add(createLoopUnrollPass());
301 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000302 }
303
Michael J. Spencer289067c2014-05-29 01:55:07 +0000304 if (LoadCombine)
305 MPM.add(createLoadCombinePass());
306
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000307 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000308 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000309 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000310 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000311
Renato Golin729a3ae2013-12-05 21:20:02 +0000312 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
313 // pass manager that we are specifically trying to avoid. To prevent this
314 // we must insert a no-op module pass to reset the pass manager.
315 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000316
James Molloy0cbb2a862015-03-27 10:36:57 +0000317 if (RunFloat2Int)
318 MPM.add(createFloat2IntPass());
319
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000320 // Re-rotate loops in all our loop nests. These may have fallout out of
321 // rotated form due to GVN or other transformations, and the vectorizer relies
322 // on the rotated form.
Michael Zolotukhin267e12f2015-03-10 19:07:41 +0000323 MPM.add(createLoopRotatePass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000324
Adam Nemet938d3d62015-05-14 12:05:18 +0000325 // Distribute loops to allow partial vectorization. I.e. isolate dependences
326 // into separate loop that would otherwise inhibit vectorization.
327 if (EnableLoopDistribute)
328 MPM.add(createLoopDistributePass());
329
Renato Golin729a3ae2013-12-05 21:20:02 +0000330 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
331 // FIXME: Because of #pragma vectorize enable, the passes below are always
332 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
333 // on -O1 and no #pragma is found). Would be good to have these two passes
334 // as function calls, so that we can only pass them when the vectorizer
335 // changed the code.
336 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000337 if (OptLevel > 1 && ExtraVectorizerPasses) {
338 // At higher optimization levels, try to clean up any runtime overlap and
339 // alignment checks inserted by the vectorizer. We want to track correllated
340 // runtime checks for two inner loops in the same outer loop, fold any
341 // common computations, hoist loop-invariant aspects out of any outer loop,
342 // and unswitch the runtime checks if possible. Once hoisted, we may have
343 // dead (or speculatable) control flows or more combining opportunities.
344 MPM.add(createEarlyCSEPass());
345 MPM.add(createCorrelatedValuePropagationPass());
346 MPM.add(createInstructionCombiningPass());
347 MPM.add(createLICMPass());
348 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
349 MPM.add(createCFGSimplificationPass());
350 MPM.add(createInstructionCombiningPass());
351 }
James Molloy568da092014-08-06 12:56:19 +0000352
353 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000354 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000355 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000356 if (OptLevel > 1 && ExtraVectorizerPasses) {
357 MPM.add(createEarlyCSEPass());
358 }
359 }
James Molloy568da092014-08-06 12:56:19 +0000360
361 if (BBVectorize) {
362 MPM.add(createBBVectorizePass());
363 MPM.add(createInstructionCombiningPass());
364 addExtensionsToPM(EP_Peephole, MPM);
365 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000366 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000367 else
368 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
369
370 // BBVectorize may have significantly shortened a loop body; unroll again.
371 if (!DisableUnrollLoops)
372 MPM.add(createLoopUnrollPass());
373 }
374 }
375
Peter Collingbourne0a437612014-05-25 10:27:02 +0000376 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000377 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000378 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000379
Kevin Qin49bc7642015-03-12 05:36:01 +0000380 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000381 MPM.add(createLoopUnrollPass()); // Unroll small loops
382
Wei Mibf727ba2015-05-14 22:02:54 +0000383 // LoopUnroll may generate some redundency to cleanup.
384 MPM.add(createInstructionCombiningPass());
385
Kevin Qin49bc7642015-03-12 05:36:01 +0000386 // Runtime unrolling will introduce runtime check in loop prologue. If the
387 // unrolled loop is a inner loop, then the prologue will be inside the
388 // outer loop. LICM pass can help to promote the runtime check out if the
389 // checked value is loop invariant.
390 MPM.add(createLICMPass());
391 }
392
Hal Finkeld67e4632014-09-07 20:05:11 +0000393 // After vectorization and unrolling, assume intrinsics may tell us more
394 // about pointer alignments.
395 MPM.add(createAlignmentFromAssumptionsPass());
396
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000397 if (!DisableUnitAtATime) {
398 // FIXME: We shouldn't bother with this anymore.
399 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
400
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000401 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000402 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000403 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000404 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000405 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000406 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000407 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000408
409 if (MergeFunctions)
410 MPM.add(createMergeFunctionsPass());
411
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000412 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000413}
414
Chandler Carruth30d69c22015-02-13 10:01:29 +0000415void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000416 // Provide AliasAnalysis services for optimizations.
417 addInitialAliasAnalysisPasses(PM);
418
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000419 // Propagate constants at call sites into the functions they call. This
420 // opens opportunities for globalopt (and inlining) by substituting function
421 // pointers passed as arguments to direct uses of functions.
422 PM.add(createIPSCCPPass());
423
424 // Now that we internalized some globals, see if we can hack on them!
425 PM.add(createGlobalOptimizerPass());
426
427 // Linking modules together can lead to duplicated global constants, only
428 // keep one copy of each constant.
429 PM.add(createConstantMergePass());
430
431 // Remove unused arguments from functions.
432 PM.add(createDeadArgEliminationPass());
433
434 // Reduce the code after globalopt and ipsccp. Both can open up significant
435 // simplification opportunities, and both can propagate functions through
436 // function pointers. When this happens, we often have to resolve varargs
437 // calls, etc, so let instcombine do this.
438 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000439 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000440
441 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000442 bool RunInliner = Inliner;
443 if (RunInliner) {
444 PM.add(Inliner);
445 Inliner = nullptr;
446 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000447
448 PM.add(createPruneEHPass()); // Remove dead EH info.
449
450 // Optimize globals again if we ran the inliner.
451 if (RunInliner)
452 PM.add(createGlobalOptimizerPass());
453 PM.add(createGlobalDCEPass()); // Remove dead functions.
454
455 // If we didn't decide to inline a function, check to see if we can
456 // transform it to pass arguments by value instead of by reference.
457 PM.add(createArgumentPromotionPass());
458
459 // The IPO passes may leave cruft around. Clean up after them.
460 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000461 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000462 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000463
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000464 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000465 if (UseNewSROA)
466 PM.add(createSROAPass());
467 else
468 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000469
470 // Run a few AA driven optimizations here and now, to cleanup the code.
471 PM.add(createFunctionAttrsPass()); // Add nocapture.
472 PM.add(createGlobalsModRefPass()); // IP alias analysis.
473
Bill Wendling932b9922012-04-02 22:16:50 +0000474 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000475 if (EnableMLSM)
476 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000477 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
478 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000479
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000480 // Nuke dead stores.
481 PM.add(createDeadStoreEliminationPass());
482
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000483 // More loops are countable; try to optimize them.
484 PM.add(createIndVarSimplifyPass());
485 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000486 if (EnableLoopInterchange)
487 PM.add(createLoopInterchangePass());
488
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000489 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000490
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000491 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000492 if (RunSLPAfterLoopVectorization)
493 if (SLPVectorize)
494 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000495
Hal Finkeld67e4632014-09-07 20:05:11 +0000496 // After vectorization, assume intrinsics may tell us more about pointer
497 // alignments.
498 PM.add(createAlignmentFromAssumptionsPass());
499
Michael J. Spencer289067c2014-05-29 01:55:07 +0000500 if (LoadCombine)
501 PM.add(createLoadCombinePass());
502
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000503 // Cleanup and simplify the code after the scalar optimizations.
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 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000508}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000509
Peter Collingbourne070843d2015-03-19 22:01:00 +0000510void PassManagerBuilder::addLateLTOOptimizationPasses(
511 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000512 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000513 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000514
515 // Now that we have optimized the program, discard unreachable functions.
516 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000517
518 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
519 // currently it damages debug info.
520 if (MergeFunctions)
521 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000522}
Rafael Espindola07f609152011-08-09 22:17:34 +0000523
Chandler Carruth30d69c22015-02-13 10:01:29 +0000524void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000525 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000526 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000527
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000528 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000529 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000530
Peter Collingbourne070843d2015-03-19 22:01:00 +0000531 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000532 addLTOOptimizationPasses(PM);
533
Peter Collingbourne070843d2015-03-19 22:01:00 +0000534 // Lower bit sets to globals. This pass supports Clang's control flow
535 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
536 // is enabled. The pass does nothing if CFI is disabled.
537 PM.add(createLowerBitSetsPass());
538
539 if (OptLevel != 0)
540 addLateLTOOptimizationPasses(PM);
541
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000542 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000543 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000544}
545
Eric Christopher04d4e932013-04-22 22:47:22 +0000546inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
547 return reinterpret_cast<PassManagerBuilder*>(P);
548}
549
550inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
551 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
552}
553
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000554LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000555 PassManagerBuilder *PMB = new PassManagerBuilder();
556 return wrap(PMB);
557}
558
559void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
560 PassManagerBuilder *Builder = unwrap(PMB);
561 delete Builder;
562}
563
564void
565LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
566 unsigned OptLevel) {
567 PassManagerBuilder *Builder = unwrap(PMB);
568 Builder->OptLevel = OptLevel;
569}
570
571void
572LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
573 unsigned SizeLevel) {
574 PassManagerBuilder *Builder = unwrap(PMB);
575 Builder->SizeLevel = SizeLevel;
576}
577
578void
579LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
580 LLVMBool Value) {
581 PassManagerBuilder *Builder = unwrap(PMB);
582 Builder->DisableUnitAtATime = Value;
583}
584
585void
586LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
587 LLVMBool Value) {
588 PassManagerBuilder *Builder = unwrap(PMB);
589 Builder->DisableUnrollLoops = Value;
590}
591
592void
593LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
594 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000595 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000596}
597
598void
599LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
600 unsigned Threshold) {
601 PassManagerBuilder *Builder = unwrap(PMB);
602 Builder->Inliner = createFunctionInliningPass(Threshold);
603}
604
605void
606LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
607 LLVMPassManagerRef PM) {
608 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000609 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000610 Builder->populateFunctionPassManager(*FPM);
611}
612
613void
614LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
615 LLVMPassManagerRef PM) {
616 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000617 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000618 Builder->populateModulePassManager(*MPM);
619}
620
621void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
622 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000623 LLVMBool Internalize,
624 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000625 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000626 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000627
628 // A small backwards compatibility hack. populateLTOPassManager used to take
629 // an RunInliner option.
630 if (RunInliner && !Builder->Inliner)
631 Builder->Inliner = createFunctionInliningPass();
632
633 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000634}