blob: 621bdd030eb5304f6b57813b378eaa7e0e92cfca [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 Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/PassManager.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"
27#include "llvm/Target/TargetSubtargetInfo.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Transforms/IPO.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000029#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000030#include "llvm/Transforms/Vectorize.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000031
32using namespace llvm;
33
Hal Finkelc34e5112012-02-01 03:51:43 +000034static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000035RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000036 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000037
38static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000039RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000040 cl::desc("Run the SLP vectorization passes"));
41
42static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000043RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000044 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000045
Hal Finkel204bf532012-04-13 17:15:33 +000046static cl::opt<bool>
47UseGVNAfterVectorization("use-gvn-after-vectorization",
48 cl::init(false), cl::Hidden,
49 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
50
Chandler Carruth7b8297a2014-10-14 00:31:29 +000051static cl::opt<bool> ExtraVectorizerPasses(
52 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
53 cl::desc("Run cleanup optimization passes after vectorization."));
54
Chandler Carruth1b398ae2012-09-14 09:22:59 +000055static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000056 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000057 cl::desc("Enable the new, experimental SROA pass"));
58
Hal Finkelbf45efd2013-11-16 23:59:05 +000059static cl::opt<bool>
60RunLoopRerolling("reroll-loops", cl::Hidden,
61 cl::desc("Run the loop rerolling pass"));
62
Michael J. Spencer289067c2014-05-29 01:55:07 +000063static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
64 cl::Hidden,
65 cl::desc("Run the load combining pass"));
66
James Molloy568da092014-08-06 12:56:19 +000067static cl::opt<bool>
68RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000069 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000070 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
71 "vectorizer instead of before"));
72
Hal Finkel445dda52014-09-02 22:12:54 +000073static cl::opt<bool> UseCFLAA("use-cfl-aa",
74 cl::init(false), cl::Hidden,
75 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000076
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000077static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000078EnableMLSM("mlsm", cl::init(true), cl::Hidden,
79 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000080
Rafael Espindola3ea478b2011-08-02 21:50:27 +000081PassManagerBuilder::PassManagerBuilder() {
82 OptLevel = 2;
83 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000084 LibraryInfo = nullptr;
85 Inliner = nullptr;
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +000086 DisableTailCalls = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000087 DisableUnitAtATime = false;
88 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000089 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +000090 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +000091 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +000092 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +000093 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +000094 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +000095 VerifyInput = false;
96 VerifyOutput = false;
97 StripDebug = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +000098 MergeFunctions = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000099}
100
101PassManagerBuilder::~PassManagerBuilder() {
102 delete LibraryInfo;
103 delete Inliner;
104}
105
David Chisnall719a72f2011-08-16 13:58:41 +0000106/// Set of global extensions, automatically added as part of the standard set.
107static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
108 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
109
110void PassManagerBuilder::addGlobalExtension(
111 PassManagerBuilder::ExtensionPointTy Ty,
112 PassManagerBuilder::ExtensionFn Fn) {
113 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
114}
115
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000116void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
117 Extensions.push_back(std::make_pair(Ty, Fn));
118}
119
120void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
121 PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000122 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
123 if ((*GlobalExtensions)[i].first == ETy)
124 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000125 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
126 if (Extensions[i].first == ETy)
127 Extensions[i].second(*this, PM);
128}
129
130void
131PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
132 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
133 // BasicAliasAnalysis wins if they disagree. This is intended to help
134 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000135 if (UseCFLAA)
136 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000137 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000138 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000139 PM.add(createBasicAliasAnalysisPass());
140}
141
142void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
143 addExtensionsToPM(EP_EarlyAsPossible, FPM);
144
145 // Add LibraryInfo if we have some.
146 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
147
148 if (OptLevel == 0) return;
149
150 addInitialAliasAnalysisPasses(FPM);
151
152 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000153 if (UseNewSROA)
154 FPM.add(createSROAPass());
155 else
156 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000157 FPM.add(createEarlyCSEPass());
158 FPM.add(createLowerExpectIntrinsicPass());
159}
160
161void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000162 // If all optimizations are disabled, just run the always-inline pass and,
163 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000164 if (OptLevel == 0) {
165 if (Inliner) {
166 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000167 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000168 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000169
Nick Lewycky592d8492014-10-23 23:49:31 +0000170 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
171 // creates a CGSCC pass manager, but we don't want to add extensions into
172 // that pass manager. To prevent this we insert a no-op module pass to reset
173 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
174 // builds. The function merging pass is
175 if (MergeFunctions)
176 MPM.add(createMergeFunctionsPass());
177 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000178 MPM.add(createBarrierNoopPass());
179
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000180 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000181 return;
182 }
183
184 // Add LibraryInfo if we have some.
185 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
186
187 addInitialAliasAnalysisPasses(MPM);
188
189 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000190 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
191
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000192 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000193 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
194
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000195 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
196
197 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000198 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000199 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
200 }
201
202 // Start of CallGraph SCC passes.
203 if (!DisableUnitAtATime)
204 MPM.add(createPruneEHPass()); // Remove dead EH info
205 if (Inliner) {
206 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000207 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000208 }
209 if (!DisableUnitAtATime)
210 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
211 if (OptLevel > 2)
212 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
213
214 // Start of function pass.
215 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000216 if (UseNewSROA)
217 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
218 else
219 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000220 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000221 MPM.add(createJumpThreadingPass()); // Thread jumps.
222 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
223 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
224 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000225 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000226
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +0000227 if (!DisableTailCalls)
228 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000229 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
230 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000231 // Rotate Loop - disable header duplication at -Oz
232 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000233 MPM.add(createLICMPass()); // Hoist loop invariants
234 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
235 MPM.add(createInstructionCombiningPass());
236 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
237 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
238 MPM.add(createLoopDeletionPass()); // Delete dead loops
Nadav Rotem6b94c2a2012-10-17 18:25:06 +0000239
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000240 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000241 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000242 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
243
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000244 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000245 if (EnableMLSM)
246 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000247 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000248 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000249 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
250 MPM.add(createSCCPPass()); // Constant prop with SCCP
251
252 // Run instcombine after redundancy elimination to exploit opportunities
253 // opened up by them.
254 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000255 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000256 MPM.add(createJumpThreadingPass()); // Thread jumps
257 MPM.add(createCorrelatedValuePropagationPass());
258 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
259
260 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
261
Hal Finkel29aeb202013-11-17 16:02:50 +0000262 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000263 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000264 if (!RunSLPAfterLoopVectorization) {
265 if (SLPVectorize)
266 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000267
James Molloy568da092014-08-06 12:56:19 +0000268 if (BBVectorize) {
269 MPM.add(createBBVectorizePass());
270 MPM.add(createInstructionCombiningPass());
271 addExtensionsToPM(EP_Peephole, MPM);
272 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000273 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000274 else
275 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000276
James Molloy568da092014-08-06 12:56:19 +0000277 // BBVectorize may have significantly shortened a loop body; unroll again.
278 if (!DisableUnrollLoops)
279 MPM.add(createLoopUnrollPass());
280 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000281 }
282
Michael J. Spencer289067c2014-05-29 01:55:07 +0000283 if (LoadCombine)
284 MPM.add(createLoadCombinePass());
285
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000286 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000287 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000288 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000289 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000290
Renato Golin729a3ae2013-12-05 21:20:02 +0000291 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
292 // pass manager that we are specifically trying to avoid. To prevent this
293 // we must insert a no-op module pass to reset the pass manager.
294 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000295
296 // Re-rotate loops in all our loop nests. These may have fallout out of
297 // rotated form due to GVN or other transformations, and the vectorizer relies
298 // on the rotated form.
299 if (ExtraVectorizerPasses)
300 MPM.add(createLoopRotatePass());
301
Renato Golin729a3ae2013-12-05 21:20:02 +0000302 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
303 // FIXME: Because of #pragma vectorize enable, the passes below are always
304 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
305 // on -O1 and no #pragma is found). Would be good to have these two passes
306 // as function calls, so that we can only pass them when the vectorizer
307 // changed the code.
308 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000309 if (OptLevel > 1 && ExtraVectorizerPasses) {
310 // At higher optimization levels, try to clean up any runtime overlap and
311 // alignment checks inserted by the vectorizer. We want to track correllated
312 // runtime checks for two inner loops in the same outer loop, fold any
313 // common computations, hoist loop-invariant aspects out of any outer loop,
314 // and unswitch the runtime checks if possible. Once hoisted, we may have
315 // dead (or speculatable) control flows or more combining opportunities.
316 MPM.add(createEarlyCSEPass());
317 MPM.add(createCorrelatedValuePropagationPass());
318 MPM.add(createInstructionCombiningPass());
319 MPM.add(createLICMPass());
320 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
321 MPM.add(createCFGSimplificationPass());
322 MPM.add(createInstructionCombiningPass());
323 }
James Molloy568da092014-08-06 12:56:19 +0000324
325 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000326 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000327 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000328 if (OptLevel > 1 && ExtraVectorizerPasses) {
329 MPM.add(createEarlyCSEPass());
330 }
331 }
James Molloy568da092014-08-06 12:56:19 +0000332
333 if (BBVectorize) {
334 MPM.add(createBBVectorizePass());
335 MPM.add(createInstructionCombiningPass());
336 addExtensionsToPM(EP_Peephole, MPM);
337 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000338 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000339 else
340 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
341
342 // BBVectorize may have significantly shortened a loop body; unroll again.
343 if (!DisableUnrollLoops)
344 MPM.add(createLoopUnrollPass());
345 }
346 }
347
Peter Collingbourne0a437612014-05-25 10:27:02 +0000348 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000349 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000350 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000351
Hal Finkel86b30642014-03-31 23:23:51 +0000352 if (!DisableUnrollLoops)
353 MPM.add(createLoopUnrollPass()); // Unroll small loops
354
Hal Finkeld67e4632014-09-07 20:05:11 +0000355 // After vectorization and unrolling, assume intrinsics may tell us more
356 // about pointer alignments.
357 MPM.add(createAlignmentFromAssumptionsPass());
358
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000359 if (!DisableUnitAtATime) {
360 // FIXME: We shouldn't bother with this anymore.
361 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
362
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000363 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000364 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000365 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000366 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000367 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000368 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000369 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000370
371 if (MergeFunctions)
372 MPM.add(createMergeFunctionsPass());
373
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000374 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000375}
376
Rafael Espindola7cebf362014-08-21 20:03:44 +0000377void PassManagerBuilder::addLTOOptimizationPasses(PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000378 // Provide AliasAnalysis services for optimizations.
379 addInitialAliasAnalysisPasses(PM);
380
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000381 // Propagate constants at call sites into the functions they call. This
382 // opens opportunities for globalopt (and inlining) by substituting function
383 // pointers passed as arguments to direct uses of functions.
384 PM.add(createIPSCCPPass());
385
386 // Now that we internalized some globals, see if we can hack on them!
387 PM.add(createGlobalOptimizerPass());
388
389 // Linking modules together can lead to duplicated global constants, only
390 // keep one copy of each constant.
391 PM.add(createConstantMergePass());
392
393 // Remove unused arguments from functions.
394 PM.add(createDeadArgEliminationPass());
395
396 // Reduce the code after globalopt and ipsccp. Both can open up significant
397 // simplification opportunities, and both can propagate functions through
398 // function pointers. When this happens, we often have to resolve varargs
399 // calls, etc, so let instcombine do this.
400 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000401 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000402
403 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000404 bool RunInliner = Inliner;
405 if (RunInliner) {
406 PM.add(Inliner);
407 Inliner = nullptr;
408 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000409
410 PM.add(createPruneEHPass()); // Remove dead EH info.
411
412 // Optimize globals again if we ran the inliner.
413 if (RunInliner)
414 PM.add(createGlobalOptimizerPass());
415 PM.add(createGlobalDCEPass()); // Remove dead functions.
416
417 // If we didn't decide to inline a function, check to see if we can
418 // transform it to pass arguments by value instead of by reference.
419 PM.add(createArgumentPromotionPass());
420
421 // The IPO passes may leave cruft around. Clean up after them.
422 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000423 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000424 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000425
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000426 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000427 if (UseNewSROA)
428 PM.add(createSROAPass());
429 else
430 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000431
432 // Run a few AA driven optimizations here and now, to cleanup the code.
433 PM.add(createFunctionAttrsPass()); // Add nocapture.
434 PM.add(createGlobalsModRefPass()); // IP alias analysis.
435
Bill Wendling932b9922012-04-02 22:16:50 +0000436 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000437 if (EnableMLSM)
438 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000439 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
440 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000441
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000442 // Nuke dead stores.
443 PM.add(createDeadStoreEliminationPass());
444
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000445 // More loops are countable; try to optimize them.
446 PM.add(createIndVarSimplifyPass());
447 PM.add(createLoopDeletionPass());
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000448 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000449
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000450 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000451 if (RunSLPAfterLoopVectorization)
452 if (SLPVectorize)
453 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000454
Hal Finkeld67e4632014-09-07 20:05:11 +0000455 // After vectorization, assume intrinsics may tell us more about pointer
456 // alignments.
457 PM.add(createAlignmentFromAssumptionsPass());
458
Michael J. Spencer289067c2014-05-29 01:55:07 +0000459 if (LoadCombine)
460 PM.add(createLoadCombinePass());
461
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000462 // Cleanup and simplify the code after the scalar optimizations.
463 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000464 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000465
466 PM.add(createJumpThreadingPass());
467
468 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000469 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000470
471 // Now that we have optimized the program, discard unreachable functions.
472 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000473
474 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
475 // currently it damages debug info.
476 if (MergeFunctions)
477 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000478}
Rafael Espindola07f609152011-08-09 22:17:34 +0000479
Rafael Espindola7cebf362014-08-21 20:03:44 +0000480void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
481 TargetMachine *TM) {
482 if (TM) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000483 PM.add(new DataLayoutPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000484 TM->addAnalysisPasses(PM);
485 }
486
487 if (LibraryInfo)
488 PM.add(new TargetLibraryInfo(*LibraryInfo));
489
490 if (VerifyInput)
491 PM.add(createVerifierPass());
492
493 if (StripDebug)
494 PM.add(createStripSymbolsPass(true));
495
496 if (VerifyInput)
497 PM.add(createDebugInfoVerifierPass());
498
499 if (OptLevel != 0)
500 addLTOOptimizationPasses(PM);
501
502 if (VerifyOutput) {
503 PM.add(createVerifierPass());
504 PM.add(createDebugInfoVerifierPass());
505 }
506}
507
Eric Christopher04d4e932013-04-22 22:47:22 +0000508inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
509 return reinterpret_cast<PassManagerBuilder*>(P);
510}
511
512inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
513 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
514}
515
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000516LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000517 PassManagerBuilder *PMB = new PassManagerBuilder();
518 return wrap(PMB);
519}
520
521void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
522 PassManagerBuilder *Builder = unwrap(PMB);
523 delete Builder;
524}
525
526void
527LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
528 unsigned OptLevel) {
529 PassManagerBuilder *Builder = unwrap(PMB);
530 Builder->OptLevel = OptLevel;
531}
532
533void
534LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
535 unsigned SizeLevel) {
536 PassManagerBuilder *Builder = unwrap(PMB);
537 Builder->SizeLevel = SizeLevel;
538}
539
540void
541LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
542 LLVMBool Value) {
543 PassManagerBuilder *Builder = unwrap(PMB);
544 Builder->DisableUnitAtATime = Value;
545}
546
547void
548LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
549 LLVMBool Value) {
550 PassManagerBuilder *Builder = unwrap(PMB);
551 Builder->DisableUnrollLoops = Value;
552}
553
554void
555LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
556 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000557 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000558}
559
560void
561LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
562 unsigned Threshold) {
563 PassManagerBuilder *Builder = unwrap(PMB);
564 Builder->Inliner = createFunctionInliningPass(Threshold);
565}
566
567void
568LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
569 LLVMPassManagerRef PM) {
570 PassManagerBuilder *Builder = unwrap(PMB);
571 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
572 Builder->populateFunctionPassManager(*FPM);
573}
574
575void
576LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
577 LLVMPassManagerRef PM) {
578 PassManagerBuilder *Builder = unwrap(PMB);
579 PassManagerBase *MPM = unwrap(PM);
580 Builder->populateModulePassManager(*MPM);
581}
582
583void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
584 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000585 LLVMBool Internalize,
586 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000587 PassManagerBuilder *Builder = unwrap(PMB);
588 PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000589
590 // A small backwards compatibility hack. populateLTOPassManager used to take
591 // an RunInliner option.
592 if (RunInliner && !Builder->Inliner)
593 Builder->Inliner = createFunctionInliningPass();
594
595 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000596}