blob: d02e6975a19f6991feb7c7af3953110f4b72c935 [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"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000025#include "llvm/Target/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 Carruth1b398ae2012-09-14 09:22:59 +000051static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000052 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000053 cl::desc("Enable the new, experimental SROA pass"));
54
Hal Finkelbf45efd2013-11-16 23:59:05 +000055static cl::opt<bool>
56RunLoopRerolling("reroll-loops", cl::Hidden,
57 cl::desc("Run the loop rerolling pass"));
58
Michael J. Spencer289067c2014-05-29 01:55:07 +000059static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
60 cl::Hidden,
61 cl::desc("Run the load combining pass"));
62
James Molloy568da092014-08-06 12:56:19 +000063static cl::opt<bool>
64RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000065 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000066 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
67 "vectorizer instead of before"));
68
Hal Finkel445dda52014-09-02 22:12:54 +000069static cl::opt<bool> UseCFLAA("use-cfl-aa",
70 cl::init(false), cl::Hidden,
71 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000072
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000073static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000074EnableMLSM("mlsm", cl::init(true), cl::Hidden,
75 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000076
Rafael Espindola3ea478b2011-08-02 21:50:27 +000077PassManagerBuilder::PassManagerBuilder() {
78 OptLevel = 2;
79 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +000080 LibraryInfo = nullptr;
81 Inliner = nullptr;
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +000082 DisableTailCalls = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000083 DisableUnitAtATime = false;
84 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +000085 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +000086 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +000087 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +000088 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +000089 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +000090 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +000091 VerifyInput = false;
92 VerifyOutput = false;
93 StripDebug = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +000094 MergeFunctions = false;
Rafael Espindola3ea478b2011-08-02 21:50:27 +000095}
96
97PassManagerBuilder::~PassManagerBuilder() {
98 delete LibraryInfo;
99 delete Inliner;
100}
101
David Chisnall719a72f2011-08-16 13:58:41 +0000102/// Set of global extensions, automatically added as part of the standard set.
103static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
104 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
105
106void PassManagerBuilder::addGlobalExtension(
107 PassManagerBuilder::ExtensionPointTy Ty,
108 PassManagerBuilder::ExtensionFn Fn) {
109 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
110}
111
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000112void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
113 Extensions.push_back(std::make_pair(Ty, Fn));
114}
115
116void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
117 PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000118 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
119 if ((*GlobalExtensions)[i].first == ETy)
120 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000121 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
122 if (Extensions[i].first == ETy)
123 Extensions[i].second(*this, PM);
124}
125
126void
127PassManagerBuilder::addInitialAliasAnalysisPasses(PassManagerBase &PM) const {
128 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
129 // BasicAliasAnalysis wins if they disagree. This is intended to help
130 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000131 if (UseCFLAA)
132 PM.add(createCFLAliasAnalysisPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000133 PM.add(createTypeBasedAliasAnalysisPass());
Hal Finkel94146652014-07-24 14:25:39 +0000134 PM.add(createScopedNoAliasAAPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000135 PM.add(createBasicAliasAnalysisPass());
136}
137
138void PassManagerBuilder::populateFunctionPassManager(FunctionPassManager &FPM) {
139 addExtensionsToPM(EP_EarlyAsPossible, FPM);
140
141 // Add LibraryInfo if we have some.
142 if (LibraryInfo) FPM.add(new TargetLibraryInfo(*LibraryInfo));
143
144 if (OptLevel == 0) return;
145
146 addInitialAliasAnalysisPasses(FPM);
147
148 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000149 if (UseNewSROA)
150 FPM.add(createSROAPass());
151 else
152 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000153 FPM.add(createEarlyCSEPass());
154 FPM.add(createLowerExpectIntrinsicPass());
155}
156
157void PassManagerBuilder::populateModulePassManager(PassManagerBase &MPM) {
158 // If all optimizations are disabled, just run the always-inline pass.
159 if (OptLevel == 0) {
160 if (Inliner) {
161 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000162 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000163 }
Chandler Carruthe8479e12012-10-18 08:05:46 +0000164
165 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
166 // pass manager, but we don't want to add extensions into that pass manager.
167 // To prevent this we must insert a no-op module pass to reset the pass
168 // manager to get the same behavior as EP_OptimizerLast in non-O0 builds.
169 if (!GlobalExtensions->empty() || !Extensions.empty())
170 MPM.add(createBarrierNoopPass());
171
Kostya Serebryanydc436f92011-11-30 22:19:26 +0000172 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000173 return;
174 }
175
176 // Add LibraryInfo if we have some.
177 if (LibraryInfo) MPM.add(new TargetLibraryInfo(*LibraryInfo));
178
179 addInitialAliasAnalysisPasses(MPM);
180
181 if (!DisableUnitAtATime) {
Dan Gohmanb9936292012-01-17 20:51:32 +0000182 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
183
Gerolf Hoflehner65b13322014-07-03 19:28:15 +0000184 MPM.add(createIPSCCPPass()); // IP SCCP
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000185 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
186
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000187 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
188
189 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
Peter Collingbourne0a437612014-05-25 10:27:02 +0000190 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000191 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
192 }
193
194 // Start of CallGraph SCC passes.
195 if (!DisableUnitAtATime)
196 MPM.add(createPruneEHPass()); // Remove dead EH info
197 if (Inliner) {
198 MPM.add(Inliner);
Craig Topperf40110f2014-04-25 05:29:35 +0000199 Inliner = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000200 }
201 if (!DisableUnitAtATime)
202 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
203 if (OptLevel > 2)
204 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
205
206 // Start of function pass.
207 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000208 if (UseNewSROA)
209 MPM.add(createSROAPass(/*RequiresDomTree*/ false));
210 else
211 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000212 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000213 MPM.add(createJumpThreadingPass()); // Thread jumps.
214 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
215 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
216 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000217 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000218
Duncan P. N. Exon Smith49f3ec82014-04-18 01:05:15 +0000219 if (!DisableTailCalls)
220 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000221 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
222 MPM.add(createReassociatePass()); // Reassociate expressions
223 MPM.add(createLoopRotatePass()); // Rotate Loop
224 MPM.add(createLICMPass()); // Hoist loop invariants
225 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
226 MPM.add(createInstructionCombiningPass());
227 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
228 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
229 MPM.add(createLoopDeletionPass()); // Delete dead loops
Nadav Rotem6b94c2a2012-10-17 18:25:06 +0000230
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000231 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000232 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000233 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
234
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000235 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000236 if (EnableMLSM)
237 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000238 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000239 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000240 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
241 MPM.add(createSCCPPass()); // Constant prop with SCCP
242
243 // Run instcombine after redundancy elimination to exploit opportunities
244 // opened up by them.
245 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000246 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000247 MPM.add(createJumpThreadingPass()); // Thread jumps
248 MPM.add(createCorrelatedValuePropagationPass());
249 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
250
251 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
252
Hal Finkel29aeb202013-11-17 16:02:50 +0000253 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000254 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000255 if (!RunSLPAfterLoopVectorization) {
256 if (SLPVectorize)
257 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000258
James Molloy568da092014-08-06 12:56:19 +0000259 if (BBVectorize) {
260 MPM.add(createBBVectorizePass());
261 MPM.add(createInstructionCombiningPass());
262 addExtensionsToPM(EP_Peephole, MPM);
263 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000264 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000265 else
266 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000267
James Molloy568da092014-08-06 12:56:19 +0000268 // BBVectorize may have significantly shortened a loop body; unroll again.
269 if (!DisableUnrollLoops)
270 MPM.add(createLoopUnrollPass());
271 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000272 }
273
Michael J. Spencer289067c2014-05-29 01:55:07 +0000274 if (LoadCombine)
275 MPM.add(createLoadCombinePass());
276
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000277 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000278 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000279 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000280 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000281
Renato Golin729a3ae2013-12-05 21:20:02 +0000282 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
283 // pass manager that we are specifically trying to avoid. To prevent this
284 // we must insert a no-op module pass to reset the pass manager.
285 MPM.add(createBarrierNoopPass());
286 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
287 // FIXME: Because of #pragma vectorize enable, the passes below are always
288 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
289 // on -O1 and no #pragma is found). Would be good to have these two passes
290 // as function calls, so that we can only pass them when the vectorizer
291 // changed the code.
292 MPM.add(createInstructionCombiningPass());
James Molloy568da092014-08-06 12:56:19 +0000293
294 if (RunSLPAfterLoopVectorization) {
295 if (SLPVectorize)
296 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
297
298 if (BBVectorize) {
299 MPM.add(createBBVectorizePass());
300 MPM.add(createInstructionCombiningPass());
301 addExtensionsToPM(EP_Peephole, MPM);
302 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000303 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000304 else
305 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
306
307 // BBVectorize may have significantly shortened a loop body; unroll again.
308 if (!DisableUnrollLoops)
309 MPM.add(createLoopUnrollPass());
310 }
311 }
312
Peter Collingbourne0a437612014-05-25 10:27:02 +0000313 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000314 MPM.add(createCFGSimplificationPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000315
Hal Finkel86b30642014-03-31 23:23:51 +0000316 if (!DisableUnrollLoops)
317 MPM.add(createLoopUnrollPass()); // Unroll small loops
318
Hal Finkeld67e4632014-09-07 20:05:11 +0000319 // After vectorization and unrolling, assume intrinsics may tell us more
320 // about pointer alignments.
321 MPM.add(createAlignmentFromAssumptionsPass());
322
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000323 if (!DisableUnitAtATime) {
324 // FIXME: We shouldn't bother with this anymore.
325 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
326
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000327 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000328 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000329 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000330 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000331 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000332 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000333 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000334
335 if (MergeFunctions)
336 MPM.add(createMergeFunctionsPass());
337
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000338 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000339}
340
Rafael Espindola7cebf362014-08-21 20:03:44 +0000341void PassManagerBuilder::addLTOOptimizationPasses(PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000342 // Provide AliasAnalysis services for optimizations.
343 addInitialAliasAnalysisPasses(PM);
344
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000345 // Propagate constants at call sites into the functions they call. This
346 // opens opportunities for globalopt (and inlining) by substituting function
347 // pointers passed as arguments to direct uses of functions.
348 PM.add(createIPSCCPPass());
349
350 // Now that we internalized some globals, see if we can hack on them!
351 PM.add(createGlobalOptimizerPass());
352
353 // Linking modules together can lead to duplicated global constants, only
354 // keep one copy of each constant.
355 PM.add(createConstantMergePass());
356
357 // Remove unused arguments from functions.
358 PM.add(createDeadArgEliminationPass());
359
360 // Reduce the code after globalopt and ipsccp. Both can open up significant
361 // simplification opportunities, and both can propagate functions through
362 // function pointers. When this happens, we often have to resolve varargs
363 // calls, etc, so let instcombine do this.
364 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000365 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000366
367 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000368 bool RunInliner = Inliner;
369 if (RunInliner) {
370 PM.add(Inliner);
371 Inliner = nullptr;
372 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000373
374 PM.add(createPruneEHPass()); // Remove dead EH info.
375
376 // Optimize globals again if we ran the inliner.
377 if (RunInliner)
378 PM.add(createGlobalOptimizerPass());
379 PM.add(createGlobalDCEPass()); // Remove dead functions.
380
381 // If we didn't decide to inline a function, check to see if we can
382 // transform it to pass arguments by value instead of by reference.
383 PM.add(createArgumentPromotionPass());
384
385 // The IPO passes may leave cruft around. Clean up after them.
386 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000387 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000388 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000389
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000390 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000391 if (UseNewSROA)
392 PM.add(createSROAPass());
393 else
394 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000395
396 // Run a few AA driven optimizations here and now, to cleanup the code.
397 PM.add(createFunctionAttrsPass()); // Add nocapture.
398 PM.add(createGlobalsModRefPass()); // IP alias analysis.
399
Bill Wendling932b9922012-04-02 22:16:50 +0000400 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000401 if (EnableMLSM)
402 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000403 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
404 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000405
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000406 // Nuke dead stores.
407 PM.add(createDeadStoreEliminationPass());
408
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000409 // More loops are countable; try to optimize them.
410 PM.add(createIndVarSimplifyPass());
411 PM.add(createLoopDeletionPass());
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000412 PM.add(createLoopVectorizePass(true, true));
413
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000414 // More scalar chains could be vectorized due to more alias information
415 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
416
Hal Finkeld67e4632014-09-07 20:05:11 +0000417 // After vectorization, assume intrinsics may tell us more about pointer
418 // alignments.
419 PM.add(createAlignmentFromAssumptionsPass());
420
Michael J. Spencer289067c2014-05-29 01:55:07 +0000421 if (LoadCombine)
422 PM.add(createLoadCombinePass());
423
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000424 // Cleanup and simplify the code after the scalar optimizations.
425 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000426 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000427
428 PM.add(createJumpThreadingPass());
429
430 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000431 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000432
433 // Now that we have optimized the program, discard unreachable functions.
434 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000435
436 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
437 // currently it damages debug info.
438 if (MergeFunctions)
439 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000440}
Rafael Espindola07f609152011-08-09 22:17:34 +0000441
Rafael Espindola7cebf362014-08-21 20:03:44 +0000442void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
443 TargetMachine *TM) {
444 if (TM) {
Rafael Espindolac435adc2014-09-10 21:27:43 +0000445 PM.add(new DataLayoutPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000446 TM->addAnalysisPasses(PM);
447 }
448
449 if (LibraryInfo)
450 PM.add(new TargetLibraryInfo(*LibraryInfo));
451
452 if (VerifyInput)
453 PM.add(createVerifierPass());
454
455 if (StripDebug)
456 PM.add(createStripSymbolsPass(true));
457
458 if (VerifyInput)
459 PM.add(createDebugInfoVerifierPass());
460
461 if (OptLevel != 0)
462 addLTOOptimizationPasses(PM);
463
464 if (VerifyOutput) {
465 PM.add(createVerifierPass());
466 PM.add(createDebugInfoVerifierPass());
467 }
468}
469
Eric Christopher04d4e932013-04-22 22:47:22 +0000470inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
471 return reinterpret_cast<PassManagerBuilder*>(P);
472}
473
474inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
475 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
476}
477
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000478LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000479 PassManagerBuilder *PMB = new PassManagerBuilder();
480 return wrap(PMB);
481}
482
483void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
484 PassManagerBuilder *Builder = unwrap(PMB);
485 delete Builder;
486}
487
488void
489LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
490 unsigned OptLevel) {
491 PassManagerBuilder *Builder = unwrap(PMB);
492 Builder->OptLevel = OptLevel;
493}
494
495void
496LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
497 unsigned SizeLevel) {
498 PassManagerBuilder *Builder = unwrap(PMB);
499 Builder->SizeLevel = SizeLevel;
500}
501
502void
503LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
504 LLVMBool Value) {
505 PassManagerBuilder *Builder = unwrap(PMB);
506 Builder->DisableUnitAtATime = Value;
507}
508
509void
510LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
511 LLVMBool Value) {
512 PassManagerBuilder *Builder = unwrap(PMB);
513 Builder->DisableUnrollLoops = Value;
514}
515
516void
517LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
518 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000519 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000520}
521
522void
523LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
524 unsigned Threshold) {
525 PassManagerBuilder *Builder = unwrap(PMB);
526 Builder->Inliner = createFunctionInliningPass(Threshold);
527}
528
529void
530LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
531 LLVMPassManagerRef PM) {
532 PassManagerBuilder *Builder = unwrap(PMB);
533 FunctionPassManager *FPM = unwrap<FunctionPassManager>(PM);
534 Builder->populateFunctionPassManager(*FPM);
535}
536
537void
538LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
539 LLVMPassManagerRef PM) {
540 PassManagerBuilder *Builder = unwrap(PMB);
541 PassManagerBase *MPM = unwrap(PM);
542 Builder->populateModulePassManager(*MPM);
543}
544
545void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
546 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000547 LLVMBool Internalize,
548 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000549 PassManagerBuilder *Builder = unwrap(PMB);
550 PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000551
552 // A small backwards compatibility hack. populateLTOPassManager used to take
553 // an RunInliner option.
554 if (RunInliner && !Builder->Inliner)
555 Builder->Inliner = createFunctionInliningPass();
556
557 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000558}