blob: 00b8c7cbbd7c87e43ee3ad52fbdd35a94a340a02 [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
Michael J. Spencer289067c2014-05-29 01:55:07 +000062static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
63 cl::Hidden,
64 cl::desc("Run the load combining pass"));
65
James Molloy568da092014-08-06 12:56:19 +000066static cl::opt<bool>
67RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000068 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000069 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
70 "vectorizer instead of before"));
71
Hal Finkel445dda52014-09-02 22:12:54 +000072static cl::opt<bool> UseCFLAA("use-cfl-aa",
73 cl::init(false), cl::Hidden,
74 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000075
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000076static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000077EnableMLSM("mlsm", cl::init(true), cl::Hidden,
78 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000079
Rafael Espindola3ea478b2011-08-02 21:50:27 +000080PassManagerBuilder::PassManagerBuilder() {
81 OptLevel = 2;
82 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000083 LibraryInfo = nullptr;
84 Inliner = nullptr;
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +000085 DisableTailCalls = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000086 DisableUnitAtATime = false;
87 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000088 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +000089 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +000090 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +000091 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +000092 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +000093 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +000094 VerifyInput = false;
95 VerifyOutput = false;
96 StripDebug = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +000097 MergeFunctions = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000098}
99
100PassManagerBuilder::~PassManagerBuilder() {
101 delete LibraryInfo;
102 delete Inliner;
103}
104
David Chisnall719a72f2011-08-16 13:58:41 +0000105/// Set of global extensions, automatically added as part of the standard set.
106static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
107 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
108
109void PassManagerBuilder::addGlobalExtension(
110 PassManagerBuilder::ExtensionPointTy Ty,
111 PassManagerBuilder::ExtensionFn Fn) {
112 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
113}
114
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000115void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
116 Extensions.push_back(std::make_pair(Ty, Fn));
117}
118
119void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000120 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000121 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
122 if ((*GlobalExtensions)[i].first == ETy)
123 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000124 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
125 if (Extensions[i].first == ETy)
126 Extensions[i].second(*this, PM);
127}
128
Chandler Carruth30d69c22015-02-13 10:01:29 +0000129void PassManagerBuilder::addInitialAliasAnalysisPasses(
130 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000131 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
132 // BasicAliasAnalysis wins if they disagree. This is intended to help
133 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000134 if (UseCFLAA)
135 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000136 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000137 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000138 PM.add(createBasicAliasAnalysisPass());
139}
140
Chandler Carruth30d69c22015-02-13 10:01:29 +0000141void PassManagerBuilder::populateFunctionPassManager(
142 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000143 addExtensionsToPM(EP_EarlyAsPossible, FPM);
144
145 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000146 if (LibraryInfo)
147 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000148
149 if (OptLevel == 0) return;
150
151 addInitialAliasAnalysisPasses(FPM);
152
153 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000154 if (UseNewSROA)
155 FPM.add(createSROAPass());
156 else
157 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000158 FPM.add(createEarlyCSEPass());
159 FPM.add(createLowerExpectIntrinsicPass());
160}
161
Chandler Carruth30d69c22015-02-13 10:01:29 +0000162void PassManagerBuilder::populateModulePassManager(
163 legacy::PassManagerBase &MPM) {
Nick Lewycky592d8492014-10-23 23:49:31 +0000164 // If all optimizations are disabled, just run the always-inline pass and,
165 // if enabled, the function merging pass.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000166 if (OptLevel == 0) {
167 if (Inliner) {
168 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000169 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000170 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000171
Nick Lewycky592d8492014-10-23 23:49:31 +0000172 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
173 // creates a CGSCC pass manager, but we don't want to add extensions into
174 // that pass manager. To prevent this we insert a no-op module pass to reset
175 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
176 // builds. The function merging pass is
177 if (MergeFunctions)
178 MPM.add(createMergeFunctionsPass());
179 else if (!GlobalExtensions->empty() || !Extensions.empty())
Chandler Carruthe8479e12012-10-18 08:05:46 +0000180 MPM.add(createBarrierNoopPass());
181
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000182 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000183 return;
184 }
185
186 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000187 if (LibraryInfo)
188 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000189
190 addInitialAliasAnalysisPasses(MPM);
191
192 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000193 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
194
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000195 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000196 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
197
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000198 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
199
200 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000201 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000202 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
203 }
204
205 // Start of CallGraph SCC passes.
206 if (!DisableUnitAtATime)
207 MPM.add(createPruneEHPass()); // Remove dead EH info
208 if (Inliner) {
209 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000210 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000211 }
212 if (!DisableUnitAtATime)
213 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
214 if (OptLevel > 2)
215 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
216
217 // Start of function pass.
218 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000219 if (UseNewSROA)
220 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
221 else
222 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000223 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000224 MPM.add(createJumpThreadingPass()); // Thread jumps.
225 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
226 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
227 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000228 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000229
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +0000230 if (!DisableTailCalls)
231 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000232 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
233 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000234 // Rotate Loop - disable header duplication at -Oz
235 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000236 MPM.add(createLICMPass()); // Hoist loop invariants
237 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
238 MPM.add(createInstructionCombiningPass());
239 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
240 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
241 MPM.add(createLoopDeletionPass()); // Delete dead loops
Nadav Rotem6b94c2a2012-10-17 18:25:06 +0000242
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000243 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000244 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000245 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
246
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000247 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000248 if (EnableMLSM)
249 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000250 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000251 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000252 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
253 MPM.add(createSCCPPass()); // Constant prop with SCCP
254
255 // Run instcombine after redundancy elimination to exploit opportunities
256 // opened up by them.
257 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000258 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000259 MPM.add(createJumpThreadingPass()); // Thread jumps
260 MPM.add(createCorrelatedValuePropagationPass());
261 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000262 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000263
264 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
265
Hal Finkel29aeb202013-11-17 16:02:50 +0000266 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000267 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000268 if (!RunSLPAfterLoopVectorization) {
269 if (SLPVectorize)
270 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000271
James Molloy568da092014-08-06 12:56:19 +0000272 if (BBVectorize) {
273 MPM.add(createBBVectorizePass());
274 MPM.add(createInstructionCombiningPass());
275 addExtensionsToPM(EP_Peephole, MPM);
276 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000277 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000278 else
279 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000280
James Molloy568da092014-08-06 12:56:19 +0000281 // BBVectorize may have significantly shortened a loop body; unroll again.
282 if (!DisableUnrollLoops)
283 MPM.add(createLoopUnrollPass());
284 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000285 }
286
Michael J. Spencer289067c2014-05-29 01:55:07 +0000287 if (LoadCombine)
288 MPM.add(createLoadCombinePass());
289
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000290 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000291 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000292 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000293 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000294
Renato Golin729a3ae2013-12-05 21:20:02 +0000295 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
296 // pass manager that we are specifically trying to avoid. To prevent this
297 // we must insert a no-op module pass to reset the pass manager.
298 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000299
300 // Re-rotate loops in all our loop nests. These may have fallout out of
301 // rotated form due to GVN or other transformations, and the vectorizer relies
302 // on the rotated form.
303 if (ExtraVectorizerPasses)
304 MPM.add(createLoopRotatePass());
305
Renato Golin729a3ae2013-12-05 21:20:02 +0000306 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
307 // FIXME: Because of #pragma vectorize enable, the passes below are always
308 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
309 // on -O1 and no #pragma is found). Would be good to have these two passes
310 // as function calls, so that we can only pass them when the vectorizer
311 // changed the code.
312 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000313 if (OptLevel > 1 && ExtraVectorizerPasses) {
314 // At higher optimization levels, try to clean up any runtime overlap and
315 // alignment checks inserted by the vectorizer. We want to track correllated
316 // runtime checks for two inner loops in the same outer loop, fold any
317 // common computations, hoist loop-invariant aspects out of any outer loop,
318 // and unswitch the runtime checks if possible. Once hoisted, we may have
319 // dead (or speculatable) control flows or more combining opportunities.
320 MPM.add(createEarlyCSEPass());
321 MPM.add(createCorrelatedValuePropagationPass());
322 MPM.add(createInstructionCombiningPass());
323 MPM.add(createLICMPass());
324 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
325 MPM.add(createCFGSimplificationPass());
326 MPM.add(createInstructionCombiningPass());
327 }
James Molloy568da092014-08-06 12:56:19 +0000328
329 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000330 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000331 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000332 if (OptLevel > 1 && ExtraVectorizerPasses) {
333 MPM.add(createEarlyCSEPass());
334 }
335 }
James Molloy568da092014-08-06 12:56:19 +0000336
337 if (BBVectorize) {
338 MPM.add(createBBVectorizePass());
339 MPM.add(createInstructionCombiningPass());
340 addExtensionsToPM(EP_Peephole, MPM);
341 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000342 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000343 else
344 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
345
346 // BBVectorize may have significantly shortened a loop body; unroll again.
347 if (!DisableUnrollLoops)
348 MPM.add(createLoopUnrollPass());
349 }
350 }
351
Peter Collingbourne0a437612014-05-25 10:27:02 +0000352 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000353 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000354 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000355
Hal Finkel86b30642014-03-31 23:23:51 +0000356 if (!DisableUnrollLoops)
357 MPM.add(createLoopUnrollPass()); // Unroll small loops
358
Hal Finkeld67e4632014-09-07 20:05:11 +0000359 // After vectorization and unrolling, assume intrinsics may tell us more
360 // about pointer alignments.
361 MPM.add(createAlignmentFromAssumptionsPass());
362
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000363 if (!DisableUnitAtATime) {
364 // FIXME: We shouldn't bother with this anymore.
365 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
366
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000367 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000368 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000369 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000370 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000371 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000372 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000373 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000374
375 if (MergeFunctions)
376 MPM.add(createMergeFunctionsPass());
377
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000378 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000379}
380
Chandler Carruth30d69c22015-02-13 10:01:29 +0000381void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000382 // Provide AliasAnalysis services for optimizations.
383 addInitialAliasAnalysisPasses(PM);
384
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000385 // Propagate constants at call sites into the functions they call. This
386 // opens opportunities for globalopt (and inlining) by substituting function
387 // pointers passed as arguments to direct uses of functions.
388 PM.add(createIPSCCPPass());
389
390 // Now that we internalized some globals, see if we can hack on them!
391 PM.add(createGlobalOptimizerPass());
392
393 // Linking modules together can lead to duplicated global constants, only
394 // keep one copy of each constant.
395 PM.add(createConstantMergePass());
396
397 // Remove unused arguments from functions.
398 PM.add(createDeadArgEliminationPass());
399
400 // Reduce the code after globalopt and ipsccp. Both can open up significant
401 // simplification opportunities, and both can propagate functions through
402 // function pointers. When this happens, we often have to resolve varargs
403 // calls, etc, so let instcombine do this.
404 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000405 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000406
407 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000408 bool RunInliner = Inliner;
409 if (RunInliner) {
410 PM.add(Inliner);
411 Inliner = nullptr;
412 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000413
414 PM.add(createPruneEHPass()); // Remove dead EH info.
415
416 // Optimize globals again if we ran the inliner.
417 if (RunInliner)
418 PM.add(createGlobalOptimizerPass());
419 PM.add(createGlobalDCEPass()); // Remove dead functions.
420
421 // If we didn't decide to inline a function, check to see if we can
422 // transform it to pass arguments by value instead of by reference.
423 PM.add(createArgumentPromotionPass());
424
425 // The IPO passes may leave cruft around. Clean up after them.
426 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000427 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000428 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000429
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000430 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000431 if (UseNewSROA)
432 PM.add(createSROAPass());
433 else
434 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000435
436 // Run a few AA driven optimizations here and now, to cleanup the code.
437 PM.add(createFunctionAttrsPass()); // Add nocapture.
438 PM.add(createGlobalsModRefPass()); // IP alias analysis.
439
Bill Wendling932b9922012-04-02 22:16:50 +0000440 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000441 if (EnableMLSM)
442 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000443 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
444 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000445
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000446 // Nuke dead stores.
447 PM.add(createDeadStoreEliminationPass());
448
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000449 // More loops are countable; try to optimize them.
450 PM.add(createIndVarSimplifyPass());
451 PM.add(createLoopDeletionPass());
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000452 PM.add(createLoopVectorizePass(true, LoopVectorize));
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000453
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000454 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000455 if (RunSLPAfterLoopVectorization)
456 if (SLPVectorize)
457 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000458
Hal Finkeld67e4632014-09-07 20:05:11 +0000459 // After vectorization, assume intrinsics may tell us more about pointer
460 // alignments.
461 PM.add(createAlignmentFromAssumptionsPass());
462
Michael J. Spencer289067c2014-05-29 01:55:07 +0000463 if (LoadCombine)
464 PM.add(createLoadCombinePass());
465
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000466 // Cleanup and simplify the code after the scalar optimizations.
467 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000468 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000469
470 PM.add(createJumpThreadingPass());
471
472 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000473 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000474
475 // Now that we have optimized the program, discard unreachable functions.
476 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000477
478 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
479 // currently it damages debug info.
480 if (MergeFunctions)
481 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000482}
Rafael Espindola07f609152011-08-09 22:17:34 +0000483
Chandler Carruth30d69c22015-02-13 10:01:29 +0000484void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000485 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000486 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000487
488 if (VerifyInput)
489 PM.add(createVerifierPass());
490
491 if (StripDebug)
492 PM.add(createStripSymbolsPass(true));
493
494 if (VerifyInput)
495 PM.add(createDebugInfoVerifierPass());
496
497 if (OptLevel != 0)
498 addLTOOptimizationPasses(PM);
499
500 if (VerifyOutput) {
501 PM.add(createVerifierPass());
502 PM.add(createDebugInfoVerifierPass());
503 }
504}
505
Eric Christopher04d4e932013-04-22 22:47:22 +0000506inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
507 return reinterpret_cast<PassManagerBuilder*>(P);
508}
509
510inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
511 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
512}
513
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000514LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000515 PassManagerBuilder *PMB = new PassManagerBuilder();
516 return wrap(PMB);
517}
518
519void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
520 PassManagerBuilder *Builder = unwrap(PMB);
521 delete Builder;
522}
523
524void
525LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
526 unsigned OptLevel) {
527 PassManagerBuilder *Builder = unwrap(PMB);
528 Builder->OptLevel = OptLevel;
529}
530
531void
532LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
533 unsigned SizeLevel) {
534 PassManagerBuilder *Builder = unwrap(PMB);
535 Builder->SizeLevel = SizeLevel;
536}
537
538void
539LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
540 LLVMBool Value) {
541 PassManagerBuilder *Builder = unwrap(PMB);
542 Builder->DisableUnitAtATime = Value;
543}
544
545void
546LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
547 LLVMBool Value) {
548 PassManagerBuilder *Builder = unwrap(PMB);
549 Builder->DisableUnrollLoops = Value;
550}
551
552void
553LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
554 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000555 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000556}
557
558void
559LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
560 unsigned Threshold) {
561 PassManagerBuilder *Builder = unwrap(PMB);
562 Builder->Inliner = createFunctionInliningPass(Threshold);
563}
564
565void
566LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
567 LLVMPassManagerRef PM) {
568 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000569 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000570 Builder->populateFunctionPassManager(*FPM);
571}
572
573void
574LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
575 LLVMPassManagerRef PM) {
576 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000577 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000578 Builder->populateModulePassManager(*MPM);
579}
580
581void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
582 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000583 LLVMBool Internalize,
584 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000585 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000586 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000587
588 // A small backwards compatibility hack. populateLTOPassManager used to take
589 // an RunInliner option.
590 if (RunInliner && !Builder->Inliner)
591 Builder->Inliner = createFunctionInliningPass();
592
593 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000594}