blob: 8b0e8d31b2456ddcb61e82a3e37a41ec3f9bc4b1 [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
Rafael Espindola3ea478b2011-08-02 21:50:27 +000015#include "llvm/Transforms/IPO/PassManagerBuilder.h"
Rafael Espindola07f609152011-08-09 22:17:34 +000016#include "llvm-c/Transforms/PassManagerBuilder.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000017#include "llvm/ADT/SmallVector.h"
Chandler Carruth17e0bc32015-08-06 07:33:15 +000018#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruth8b046a42015-08-14 02:42:20 +000019#include "llvm/Analysis/CFLAliasAnalysis.h"
Chandler Carruth21dcff72015-08-14 03:48:20 +000020#include "llvm/Analysis/GlobalsModRef.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000021#include "llvm/Analysis/Passes.h"
Chandler Carruth42ff4482015-08-14 02:55:50 +000022#include "llvm/Analysis/ScopedNoAliasAA.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000023#include "llvm/Analysis/TargetLibraryInfo.h"
Chandler Carruth1db22822015-08-14 03:33:48 +000024#include "llvm/Analysis/TypeBasedAliasAnalysis.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000025#include "llvm/IR/DataLayout.h"
26#include "llvm/IR/FunctionInfo.h"
27#include "llvm/IR/LegacyPassManager.h"
28#include "llvm/IR/Verifier.h"
29#include "llvm/Support/CommandLine.h"
30#include "llvm/Support/ManagedStatic.h"
Rafael Espindola7cebf362014-08-21 20:03:44 +000031#include "llvm/Target/TargetMachine.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Transforms/IPO.h"
Chandler Carruthf49f1a872015-12-27 08:13:45 +000033#include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
Chandler Carruth3a040e62015-12-27 08:41:34 +000034#include "llvm/Transforms/IPO/InferFunctionAttrs.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000035#include "llvm/Transforms/Scalar.h"
Hal Finkelc34e5112012-02-01 03:51:43 +000036#include "llvm/Transforms/Vectorize.h"
Rong Xu34abbfb2016-01-21 18:28:59 +000037#include "llvm/Transforms/Instrumentation.h"
Rafael Espindola3ea478b2011-08-02 21:50:27 +000038
39using namespace llvm;
40
Hal Finkelc34e5112012-02-01 03:51:43 +000041static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000042RunLoopVectorization("vectorize-loops", cl::Hidden,
Nadav Rotemd3df6652012-10-30 18:37:43 +000043 cl::desc("Run the Loop vectorization passes"));
Nadav Rotemc59ae202012-10-29 16:36:25 +000044
45static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000046RunSLPVectorization("vectorize-slp", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000047 cl::desc("Run the SLP vectorization passes"));
48
49static cl::opt<bool>
Nadav Rotem7f27e0b2013-10-18 23:38:13 +000050RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
Nadav Rotemd4dcc002013-04-15 05:39:58 +000051 cl::desc("Run the BB vectorization passes"));
Hal Finkelc34e5112012-02-01 03:51:43 +000052
Hal Finkel204bf532012-04-13 17:15:33 +000053static cl::opt<bool>
54UseGVNAfterVectorization("use-gvn-after-vectorization",
55 cl::init(false), cl::Hidden,
56 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
57
Chandler Carruth7b8297a2014-10-14 00:31:29 +000058static cl::opt<bool> ExtraVectorizerPasses(
59 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
60 cl::desc("Run cleanup optimization passes after vectorization."));
61
Chandler Carruth1b398ae2012-09-14 09:22:59 +000062static cl::opt<bool> UseNewSROA("use-new-sroa",
Chandler Carruth4e435992012-10-02 04:24:01 +000063 cl::init(true), cl::Hidden,
Chandler Carruth1b398ae2012-09-14 09:22:59 +000064 cl::desc("Enable the new, experimental SROA pass"));
65
Hal Finkelbf45efd2013-11-16 23:59:05 +000066static cl::opt<bool>
67RunLoopRerolling("reroll-loops", cl::Hidden,
68 cl::desc("Run the loop rerolling pass"));
69
James Molloy0cbb2a862015-03-27 10:36:57 +000070static cl::opt<bool>
71RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
72 cl::desc("Run the float2int (float demotion) pass"));
73
Michael J. Spencer289067c2014-05-29 01:55:07 +000074static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
75 cl::Hidden,
76 cl::desc("Run the load combining pass"));
77
James Molloy568da092014-08-06 12:56:19 +000078static cl::opt<bool>
79RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
James Molloy6b95d8e2014-09-04 13:23:08 +000080 cl::init(true), cl::Hidden,
James Molloy568da092014-08-06 12:56:19 +000081 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
82 "vectorizer instead of before"));
83
Hal Finkel445dda52014-09-02 22:12:54 +000084static cl::opt<bool> UseCFLAA("use-cfl-aa",
85 cl::init(false), cl::Hidden,
86 cl::desc("Enable the new, experimental CFL alias analysis"));
James Molloy568da092014-08-06 12:56:19 +000087
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000088static cl::opt<bool>
Gerolf Hoflehner008e5cd2014-09-10 20:24:03 +000089EnableMLSM("mlsm", cl::init(true), cl::Hidden,
90 cl::desc("Enable motion of merged load and store"));
Gerolf Hoflehner24815d92014-09-10 19:55:29 +000091
Karthik Bhat88db86d2015-03-06 10:11:25 +000092static cl::opt<bool> EnableLoopInterchange(
93 "enable-loopinterchange", cl::init(false), cl::Hidden,
94 cl::desc("Enable the new, experimental LoopInterchange Pass"));
95
Adam Nemet938d3d62015-05-14 12:05:18 +000096static cl::opt<bool> EnableLoopDistribute(
97 "enable-loop-distribute", cl::init(false), cl::Hidden,
98 cl::desc("Enable the new, experimental LoopDistribution Pass"));
99
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000100static cl::opt<bool> EnableNonLTOGlobalsModRef(
James Molloy40159252015-10-13 10:43:57 +0000101 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
Chandler Carruthe9ea5a62015-07-22 11:57:28 +0000102 cl::desc(
103 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
104
Adam Nemete54a4fa2015-11-03 23:50:08 +0000105static cl::opt<bool> EnableLoopLoadElim(
106 "enable-loop-load-elim", cl::init(false), cl::Hidden,
107 cl::desc("Enable the new, experimental LoopLoadElimination Pass"));
108
Rong Xu34abbfb2016-01-21 18:28:59 +0000109static cl::opt<std::string> RunPGOInstrGen(
110 "profile-generate", cl::init(""), cl::Hidden,
111 cl::desc("Enable generation phase of PGO instrumentation and specify the "
112 "path of profile data file"));
113
114static cl::opt<std::string> RunPGOInstrUse(
115 "profile-use", cl::init(""), cl::Hidden, cl::value_desc("filename"),
116 cl::desc("Enable use phase of PGO instrumentation and specify the path "
117 "of profile data file"));
118
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000119static cl::opt<bool> UseLoopVersioningLICM(
120 "enable-loop-versioning-licm", cl::init(false), cl::Hidden,
121 cl::desc("Enable the experimental Loop Versioning LICM pass"));
122
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000123PassManagerBuilder::PassManagerBuilder() {
124 OptLevel = 2;
125 SizeLevel = 0;
Craig Topperf40110f2014-04-25 05:29:35 +0000126 LibraryInfo = nullptr;
127 Inliner = nullptr;
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000128 FunctionIndex = nullptr;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000129 DisableUnitAtATime = false;
130 DisableUnrollLoops = false;
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000131 BBVectorize = RunBBVectorization;
Nadav Rotema1e5e442013-04-15 04:54:42 +0000132 SLPVectorize = RunSLPVectorization;
Nadav Rotemc59ae202012-10-29 16:36:25 +0000133 LoopVectorize = RunLoopVectorization;
Hal Finkel29aeb202013-11-17 16:02:50 +0000134 RerollLoops = RunLoopRerolling;
Michael J. Spencer289067c2014-05-29 01:55:07 +0000135 LoadCombine = RunLoadCombine;
Rafael Espindola208bc532014-08-21 13:13:17 +0000136 DisableGVNLoadPRE = false;
Rafael Espindola7cebf362014-08-21 20:03:44 +0000137 VerifyInput = false;
138 VerifyOutput = false;
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000139 MergeFunctions = false;
Teresa Johnsond3a33a12015-07-06 16:22:42 +0000140 PrepareForLTO = false;
Rong Xu34abbfb2016-01-21 18:28:59 +0000141 PGOInstrGen = RunPGOInstrGen;
142 PGOInstrUse = RunPGOInstrUse;
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000143}
144
145PassManagerBuilder::~PassManagerBuilder() {
146 delete LibraryInfo;
147 delete Inliner;
148}
149
David Chisnall719a72f2011-08-16 13:58:41 +0000150/// Set of global extensions, automatically added as part of the standard set.
151static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
152 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
153
154void PassManagerBuilder::addGlobalExtension(
155 PassManagerBuilder::ExtensionPointTy Ty,
156 PassManagerBuilder::ExtensionFn Fn) {
157 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
158}
159
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000160void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
161 Extensions.push_back(std::make_pair(Ty, Fn));
162}
163
164void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
Chandler Carruth30d69c22015-02-13 10:01:29 +0000165 legacy::PassManagerBase &PM) const {
David Chisnall719a72f2011-08-16 13:58:41 +0000166 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
167 if ((*GlobalExtensions)[i].first == ETy)
168 (*GlobalExtensions)[i].second(*this, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000169 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
170 if (Extensions[i].first == ETy)
171 Extensions[i].second(*this, PM);
172}
173
Chandler Carruth30d69c22015-02-13 10:01:29 +0000174void PassManagerBuilder::addInitialAliasAnalysisPasses(
175 legacy::PassManagerBase &PM) const {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000176 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
177 // BasicAliasAnalysis wins if they disagree. This is intended to help
178 // support "obvious" type-punning idioms.
Hal Finkel445dda52014-09-02 22:12:54 +0000179 if (UseCFLAA)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000180 PM.add(createCFLAAWrapperPass());
181 PM.add(createTypeBasedAAWrapperPass());
182 PM.add(createScopedNoAliasAAWrapperPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000183}
184
Chandler Carruth30d69c22015-02-13 10:01:29 +0000185void PassManagerBuilder::populateFunctionPassManager(
186 legacy::FunctionPassManager &FPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000187 addExtensionsToPM(EP_EarlyAsPossible, FPM);
188
189 // Add LibraryInfo if we have some.
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000190 if (LibraryInfo)
191 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000192
193 if (OptLevel == 0) return;
194
195 addInitialAliasAnalysisPasses(FPM);
196
197 FPM.add(createCFGSimplificationPass());
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000198 if (UseNewSROA)
199 FPM.add(createSROAPass());
200 else
201 FPM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000202 FPM.add(createEarlyCSEPass());
203 FPM.add(createLowerExpectIntrinsicPass());
204}
205
Rong Xu34abbfb2016-01-21 18:28:59 +0000206// Do PGO instrumentation generation or use pass as the option specified.
207void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM) {
208 if (!PGOInstrGen.empty()) {
209 MPM.add(createPGOInstrumentationGenPass());
210 // Add the profile lowering pass.
211 InstrProfOptions Options;
212 Options.InstrProfileOutput = PGOInstrGen;
213 MPM.add(createInstrProfilingPass(Options));
214 }
215 if (!PGOInstrUse.empty())
216 MPM.add(createPGOInstrumentationUsePass(PGOInstrUse));
217}
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000218void PassManagerBuilder::addFunctionSimplificationPasses(
Mehdi Amini9c1c3ac2016-02-11 22:09:11 +0000219 legacy::PassManagerBase &MPM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000220 // Start of function pass.
221 // Break up aggregate allocas, using SSAUpdater.
Chandler Carruth70b44c52012-09-15 11:43:14 +0000222 if (UseNewSROA)
Mehdi Aminid134a672015-08-23 22:15:49 +0000223 MPM.add(createSROAPass());
Chandler Carruth70b44c52012-09-15 11:43:14 +0000224 else
225 MPM.add(createScalarReplAggregatesPass(-1, false));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000226 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000227 MPM.add(createJumpThreadingPass()); // Thread jumps.
228 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
229 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
230 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
Peter Collingbourne0a437612014-05-25 10:27:02 +0000231 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000232
Akira Hatanakad9699bc2015-06-09 19:07:19 +0000233 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000234 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
235 MPM.add(createReassociatePass()); // Reassociate expressions
Roman Divackyd2b9a1b2014-11-21 19:53:24 +0000236 // Rotate Loop - disable header duplication at -Oz
237 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000238 MPM.add(createLICMPass()); // Hoist loop invariants
239 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
Michael Zolotukhin74621cc2015-09-24 03:50:17 +0000240 MPM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000241 MPM.add(createInstructionCombiningPass());
242 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
243 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
244 MPM.add(createLoopDeletionPass()); // Delete dead loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000245 if (EnableLoopInterchange) {
Karthik Bhat88db86d2015-03-06 10:11:25 +0000246 MPM.add(createLoopInterchangePass()); // Interchange loops
Karthik Bhat8210fdf2015-04-23 04:51:44 +0000247 MPM.add(createCFGSimplificationPass());
248 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000249 if (!DisableUnrollLoops)
Hal Finkel86b30642014-03-31 23:23:51 +0000250 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000251 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
252
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000253 if (OptLevel > 1) {
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000254 if (EnableMLSM)
255 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
Rafael Espindola208bc532014-08-21 13:13:17 +0000256 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
Gerolf Hoflehnerf27ae6c2014-07-18 19:13:09 +0000257 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000258 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
259 MPM.add(createSCCPPass()); // Constant prop with SCCP
260
Hal Finkel2bb61ba2015-02-17 01:36:59 +0000261 // Delete dead bit computations (instcombine runs after to fold away the dead
262 // computations, and then ADCE will run later to exploit any new DCE
263 // opportunities that creates).
264 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
265
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000266 // Run instcombine after redundancy elimination to exploit opportunities
267 // opened up by them.
268 MPM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000269 addExtensionsToPM(EP_Peephole, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000270 MPM.add(createJumpThreadingPass()); // Thread jumps
271 MPM.add(createCorrelatedValuePropagationPass());
272 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
James Molloy83570242015-02-16 18:59:54 +0000273 MPM.add(createLICMPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000274
275 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
276
Hal Finkel29aeb202013-11-17 16:02:50 +0000277 if (RerollLoops)
Hal Finkelbf45efd2013-11-16 23:59:05 +0000278 MPM.add(createLoopRerollPass());
James Molloy568da092014-08-06 12:56:19 +0000279 if (!RunSLPAfterLoopVectorization) {
280 if (SLPVectorize)
281 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Nadav Rotemd4dcc002013-04-15 05:39:58 +0000282
James Molloy568da092014-08-06 12:56:19 +0000283 if (BBVectorize) {
284 MPM.add(createBBVectorizePass());
285 MPM.add(createInstructionCombiningPass());
286 addExtensionsToPM(EP_Peephole, MPM);
287 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000288 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000289 else
290 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
Hal Finkelbf4db4f2013-01-29 00:22:49 +0000291
James Molloy568da092014-08-06 12:56:19 +0000292 // BBVectorize may have significantly shortened a loop body; unroll again.
293 if (!DisableUnrollLoops)
294 MPM.add(createLoopUnrollPass());
295 }
Hal Finkelc34e5112012-02-01 03:51:43 +0000296 }
297
Michael J. Spencer289067c2014-05-29 01:55:07 +0000298 if (LoadCombine)
299 MPM.add(createLoadCombinePass());
300
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000301 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
Tom Stellardaa664d92013-08-06 02:43:45 +0000302 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000303 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
Peter Collingbourne0a437612014-05-25 10:27:02 +0000304 addExtensionsToPM(EP_Peephole, MPM);
Mehdi Aminiec8bee12016-02-16 22:54:27 +0000305}
306
307void PassManagerBuilder::populateModulePassManager(
308 legacy::PassManagerBase &MPM) {
309 // Allow forcing function attributes as a debugging and tuning aid.
310 MPM.add(createForceFunctionAttrsLegacyPass());
311
312 // If all optimizations are disabled, just run the always-inline pass and,
313 // if enabled, the function merging pass.
314 if (OptLevel == 0) {
315 addPGOInstrPasses(MPM);
316 if (Inliner) {
317 MPM.add(Inliner);
318 Inliner = nullptr;
319 }
320
321 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
322 // creates a CGSCC pass manager, but we don't want to add extensions into
323 // that pass manager. To prevent this we insert a no-op module pass to reset
324 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
325 // builds. The function merging pass is
326 if (MergeFunctions)
327 MPM.add(createMergeFunctionsPass());
328 else if (!GlobalExtensions->empty() || !Extensions.empty())
329 MPM.add(createBarrierNoopPass());
330
331 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
332 return;
333 }
334
335 // Add LibraryInfo if we have some.
336 if (LibraryInfo)
337 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
338
339 addInitialAliasAnalysisPasses(MPM);
340
341 if (!DisableUnitAtATime) {
342 // Infer attributes about declarations if possible.
343 MPM.add(createInferFunctionAttrsLegacyPass());
344
345 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
346
347 MPM.add(createIPSCCPPass()); // IP SCCP
348 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
349 // Promote any localized global vars.
350 MPM.add(createPromoteMemoryToRegisterPass());
351
352 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
353
354 MPM.add(createInstructionCombiningPass()); // Clean up after IPCP & DAE
355 addExtensionsToPM(EP_Peephole, MPM);
356 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
357 }
358
359 addPGOInstrPasses(MPM);
360
361 if (EnableNonLTOGlobalsModRef)
362 // We add a module alias analysis pass here. In part due to bugs in the
363 // analysis infrastructure this "works" in that the analysis stays alive
364 // for the entire SCC pass run below.
365 MPM.add(createGlobalsAAWrapperPass());
366
367 // Start of CallGraph SCC passes.
368 if (!DisableUnitAtATime)
369 MPM.add(createPruneEHPass()); // Remove dead EH info
370 if (Inliner) {
371 MPM.add(Inliner);
372 Inliner = nullptr;
373 }
374 if (!DisableUnitAtATime)
375 MPM.add(createPostOrderFunctionAttrsPass());
376 if (OptLevel > 2)
377 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
378
379 addFunctionSimplificationPasses(MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000380
Renato Golin729a3ae2013-12-05 21:20:02 +0000381 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
382 // pass manager that we are specifically trying to avoid. To prevent this
383 // we must insert a no-op module pass to reset the pass manager.
384 MPM.add(createBarrierNoopPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000385
Ashutosh Nema2260a3a2016-02-11 09:23:53 +0000386 // Scheduling LoopVersioningLICM when inlining is over, because after that
Ashutosh Nemadf6763a2016-02-06 07:47:48 +0000387 // we may see more accurate aliasing. Reason to run this late is that too
388 // early versioning may prevent further inlining due to increase of code
389 // size. By placing it just after inlining other optimizations which runs
390 // later might get benefit of no-alias assumption in clone loop.
391 if (UseLoopVersioningLICM) {
392 MPM.add(createLoopVersioningLICMPass()); // Do LoopVersioningLICM
393 MPM.add(createLICMPass()); // Hoist loop invariants
394 }
395
Chandler Carruth1926b702016-01-08 10:55:52 +0000396 if (!DisableUnitAtATime)
397 MPM.add(createReversePostOrderFunctionAttrsPass());
398
Mehdi Aminic5bf5ec2016-02-11 22:09:07 +0000399 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) {
Yaron Keren611c7cf2015-09-02 06:34:11 +0000400 // Remove avail extern fns and globals definitions if we aren't
401 // compiling an object file for later LTO. For LTO we want to preserve
402 // these so they are eligible for inlining at link-time. Note if they
403 // are unreferenced they will be removed by GlobalDCE later, so
404 // this only impacts referenced available externally globals.
405 // Eventually they will be suppressed during codegen, but eliminating
406 // here enables more opportunity for GlobalDCE as it may make
407 // globals referenced by available external functions dead
408 // and saves running remaining passes on the eliminated functions.
409 MPM.add(createEliminateAvailableExternallyPass());
410 }
411
Chandler Carruth08eebe22015-07-23 09:34:01 +0000412 if (EnableNonLTOGlobalsModRef)
413 // We add a fresh GlobalsModRef run at this point. This is particularly
414 // useful as the above will have inlined, DCE'ed, and function-attr
415 // propagated everything. We should at this point have a reasonably minimal
416 // and richly annotated call graph. By computing aliasing and mod/ref
417 // information for all local globals here, the late loop passes and notably
418 // the vectorizer will be able to use them to help recognize vectorizable
419 // memory operations.
420 //
421 // Note that this relies on a bug in the pass manager which preserves
422 // a module analysis into a function pass pipeline (and throughout it) so
423 // long as the first function pass doesn't invalidate the module analysis.
424 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
425 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
426 // (doing nothing preserves it as it is required to be conservatively
427 // correct in the face of IR changes).
Chandler Carruth7b560d42015-09-09 17:55:00 +0000428 MPM.add(createGlobalsAAWrapperPass());
Chandler Carruth08eebe22015-07-23 09:34:01 +0000429
James Molloy0cbb2a862015-03-27 10:36:57 +0000430 if (RunFloat2Int)
431 MPM.add(createFloat2IntPass());
432
Tobias Grosser39a7bd12015-07-16 08:20:37 +0000433 addExtensionsToPM(EP_VectorizerStart, MPM);
434
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000435 // Re-rotate loops in all our loop nests. These may have fallout out of
436 // rotated form due to GVN or other transformations, and the vectorizer relies
Alexey Bataevda33d802015-07-10 10:37:09 +0000437 // on the rotated form. Disable header duplication at -Oz.
438 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000439
Adam Nemet938d3d62015-05-14 12:05:18 +0000440 // Distribute loops to allow partial vectorization. I.e. isolate dependences
441 // into separate loop that would otherwise inhibit vectorization.
442 if (EnableLoopDistribute)
443 MPM.add(createLoopDistributePass());
444
Renato Golin729a3ae2013-12-05 21:20:02 +0000445 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
Adam Nemete54a4fa2015-11-03 23:50:08 +0000446
447 // Eliminate loads by forwarding stores from the previous iteration to loads
448 // of the current iteration.
449 if (EnableLoopLoadElim)
450 MPM.add(createLoopLoadEliminationPass());
451
Renato Golin729a3ae2013-12-05 21:20:02 +0000452 // FIXME: Because of #pragma vectorize enable, the passes below are always
453 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
454 // on -O1 and no #pragma is found). Would be good to have these two passes
455 // as function calls, so that we can only pass them when the vectorizer
456 // changed the code.
457 MPM.add(createInstructionCombiningPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000458 if (OptLevel > 1 && ExtraVectorizerPasses) {
459 // At higher optimization levels, try to clean up any runtime overlap and
460 // alignment checks inserted by the vectorizer. We want to track correllated
461 // runtime checks for two inner loops in the same outer loop, fold any
462 // common computations, hoist loop-invariant aspects out of any outer loop,
463 // and unswitch the runtime checks if possible. Once hoisted, we may have
464 // dead (or speculatable) control flows or more combining opportunities.
465 MPM.add(createEarlyCSEPass());
466 MPM.add(createCorrelatedValuePropagationPass());
467 MPM.add(createInstructionCombiningPass());
468 MPM.add(createLICMPass());
469 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
470 MPM.add(createCFGSimplificationPass());
471 MPM.add(createInstructionCombiningPass());
472 }
James Molloy568da092014-08-06 12:56:19 +0000473
474 if (RunSLPAfterLoopVectorization) {
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000475 if (SLPVectorize) {
James Molloy568da092014-08-06 12:56:19 +0000476 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000477 if (OptLevel > 1 && ExtraVectorizerPasses) {
478 MPM.add(createEarlyCSEPass());
479 }
480 }
James Molloy568da092014-08-06 12:56:19 +0000481
482 if (BBVectorize) {
483 MPM.add(createBBVectorizePass());
484 MPM.add(createInstructionCombiningPass());
485 addExtensionsToPM(EP_Peephole, MPM);
486 if (OptLevel > 1 && UseGVNAfterVectorization)
Rafael Espindola208bc532014-08-21 13:13:17 +0000487 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
James Molloy568da092014-08-06 12:56:19 +0000488 else
489 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
490
491 // BBVectorize may have significantly shortened a loop body; unroll again.
492 if (!DisableUnrollLoops)
493 MPM.add(createLoopUnrollPass());
494 }
495 }
496
Peter Collingbourne0a437612014-05-25 10:27:02 +0000497 addExtensionsToPM(EP_Peephole, MPM);
Renato Golin729a3ae2013-12-05 21:20:02 +0000498 MPM.add(createCFGSimplificationPass());
Chandler Carruth7b8297a2014-10-14 00:31:29 +0000499 MPM.add(createInstructionCombiningPass());
Chandler Carruth08e1b872013-06-24 07:21:47 +0000500
Kevin Qin49bc7642015-03-12 05:36:01 +0000501 if (!DisableUnrollLoops) {
Hal Finkel86b30642014-03-31 23:23:51 +0000502 MPM.add(createLoopUnrollPass()); // Unroll small loops
503
Wei Mibf727ba2015-05-14 22:02:54 +0000504 // LoopUnroll may generate some redundency to cleanup.
505 MPM.add(createInstructionCombiningPass());
506
Kevin Qin49bc7642015-03-12 05:36:01 +0000507 // Runtime unrolling will introduce runtime check in loop prologue. If the
508 // unrolled loop is a inner loop, then the prologue will be inside the
509 // outer loop. LICM pass can help to promote the runtime check out if the
510 // checked value is loop invariant.
511 MPM.add(createLICMPass());
512 }
513
Hal Finkeld67e4632014-09-07 20:05:11 +0000514 // After vectorization and unrolling, assume intrinsics may tell us more
515 // about pointer alignments.
516 MPM.add(createAlignmentFromAssumptionsPass());
517
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000518 if (!DisableUnitAtATime) {
519 // FIXME: We shouldn't bother with this anymore.
520 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
521
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000522 // GlobalOpt already deletes dead functions and globals, at -O2 try a
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000523 // late pass of GlobalDCE. It is capable of deleting dead cycles.
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000524 if (OptLevel > 1) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000525 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000526 MPM.add(createConstantMergePass()); // Merge dup global constants
Evan Cheng8c6b06d2012-09-28 21:23:26 +0000527 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000528 }
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000529
530 if (MergeFunctions)
531 MPM.add(createMergeFunctionsPass());
532
Kostya Serebryanye505a5a2012-03-23 23:22:59 +0000533 addExtensionsToPM(EP_OptimizerLast, MPM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000534}
535
Chandler Carruth30d69c22015-02-13 10:01:29 +0000536void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000537 // Provide AliasAnalysis services for optimizations.
538 addInitialAliasAnalysisPasses(PM);
539
Teresa Johnson5fcbdb72015-12-07 19:21:11 +0000540 if (FunctionIndex)
541 PM.add(createFunctionImportPass(FunctionIndex));
542
Chandler Carruthf49f1a872015-12-27 08:13:45 +0000543 // Allow forcing function attributes as a debugging and tuning aid.
544 PM.add(createForceFunctionAttrsLegacyPass());
545
Chandler Carruth3a040e62015-12-27 08:41:34 +0000546 // Infer attributes about declarations if possible.
547 PM.add(createInferFunctionAttrsLegacyPass());
548
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000549 // Propagate constants at call sites into the functions they call. This
550 // opens opportunities for globalopt (and inlining) by substituting function
551 // pointers passed as arguments to direct uses of functions.
552 PM.add(createIPSCCPPass());
553
554 // Now that we internalized some globals, see if we can hack on them!
Chandler Carruth1926b702016-01-08 10:55:52 +0000555 PM.add(createPostOrderFunctionAttrsPass());
556 PM.add(createReversePostOrderFunctionAttrsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000557 PM.add(createGlobalOptimizerPass());
James Molloy6045cc82015-12-15 09:24:01 +0000558 // Promote any localized global vars.
559 PM.add(createPromoteMemoryToRegisterPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000560
561 // Linking modules together can lead to duplicated global constants, only
562 // keep one copy of each constant.
563 PM.add(createConstantMergePass());
564
565 // Remove unused arguments from functions.
566 PM.add(createDeadArgEliminationPass());
567
568 // Reduce the code after globalopt and ipsccp. Both can open up significant
569 // simplification opportunities, and both can propagate functions through
570 // function pointers. When this happens, we often have to resolve varargs
571 // calls, etc, so let instcombine do this.
572 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000573 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000574
575 // Inline small functions
Rafael Espindolae07caad2014-08-21 13:35:30 +0000576 bool RunInliner = Inliner;
577 if (RunInliner) {
578 PM.add(Inliner);
579 Inliner = nullptr;
580 }
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000581
582 PM.add(createPruneEHPass()); // Remove dead EH info.
583
584 // Optimize globals again if we ran the inliner.
585 if (RunInliner)
586 PM.add(createGlobalOptimizerPass());
587 PM.add(createGlobalDCEPass()); // Remove dead functions.
588
589 // If we didn't decide to inline a function, check to see if we can
590 // transform it to pass arguments by value instead of by reference.
591 PM.add(createArgumentPromotionPass());
592
593 // The IPO passes may leave cruft around. Clean up after them.
594 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000595 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000596 PM.add(createJumpThreadingPass());
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000597
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000598 // Break up allocas
Chandler Carruth1b398ae2012-09-14 09:22:59 +0000599 if (UseNewSROA)
600 PM.add(createSROAPass());
601 else
602 PM.add(createScalarReplAggregatesPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000603
604 // Run a few AA driven optimizations here and now, to cleanup the code.
Chandler Carruth1926b702016-01-08 10:55:52 +0000605 PM.add(createPostOrderFunctionAttrsPass()); // Add nocapture.
Chandler Carruth7b560d42015-09-09 17:55:00 +0000606 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000607
Bill Wendling932b9922012-04-02 22:16:50 +0000608 PM.add(createLICMPass()); // Hoist loop invariants.
Gerolf Hoflehner24815d92014-09-10 19:55:29 +0000609 if (EnableMLSM)
610 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
Bill Wendling932b9922012-04-02 22:16:50 +0000611 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
612 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
Bill Wendling4c0d9ad2013-08-30 00:48:37 +0000613
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000614 // Nuke dead stores.
615 PM.add(createDeadStoreEliminationPass());
616
Duncan P. N. Exon Smith2b691892014-04-15 17:48:15 +0000617 // More loops are countable; try to optimize them.
618 PM.add(createIndVarSimplifyPass());
619 PM.add(createLoopDeletionPass());
Karthik Bhat88db86d2015-03-06 10:11:25 +0000620 if (EnableLoopInterchange)
621 PM.add(createLoopInterchangePass());
622
James Molloy31f3ddd2016-01-14 15:00:09 +0000623 if (!DisableUnrollLoops)
624 PM.add(createSimpleLoopUnrollPass()); // Unroll small loops
Arnold Schwaighofereb1a38f2014-10-26 21:50:58 +0000625 PM.add(createLoopVectorizePass(true, LoopVectorize));
James Molloy31f3ddd2016-01-14 15:00:09 +0000626 // The vectorizer may have significantly shortened a loop body; unroll again.
627 if (!DisableUnrollLoops)
628 PM.add(createLoopUnrollPass());
Arnold Schwaighofer6ccda922014-02-24 18:19:31 +0000629
James Molloy6045cc82015-12-15 09:24:01 +0000630 // Now that we've optimized loops (in particular loop induction variables),
631 // we may have exposed more scalar opportunities. Run parts of the scalar
632 // optimizer again at this point.
633 PM.add(createInstructionCombiningPass()); // Initial cleanup
634 PM.add(createCFGSimplificationPass()); // if-convert
635 PM.add(createSCCPPass()); // Propagate exposed constants
636 PM.add(createInstructionCombiningPass()); // Clean up again
637 PM.add(createBitTrackingDCEPass());
638
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000639 // More scalar chains could be vectorized due to more alias information
JF Bastienf42a6ea2014-10-21 23:18:21 +0000640 if (RunSLPAfterLoopVectorization)
641 if (SLPVectorize)
642 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
Yi Jiang79eb0aa2014-05-05 23:14:46 +0000643
Hal Finkeld67e4632014-09-07 20:05:11 +0000644 // After vectorization, assume intrinsics may tell us more about pointer
645 // alignments.
646 PM.add(createAlignmentFromAssumptionsPass());
647
Michael J. Spencer289067c2014-05-29 01:55:07 +0000648 if (LoadCombine)
649 PM.add(createLoadCombinePass());
650
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000651 // Cleanup and simplify the code after the scalar optimizations.
652 PM.add(createInstructionCombiningPass());
Peter Collingbourne0a437612014-05-25 10:27:02 +0000653 addExtensionsToPM(EP_Peephole, PM);
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000654
655 PM.add(createJumpThreadingPass());
Peter Collingbourne070843d2015-03-19 22:01:00 +0000656}
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000657
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000658void PassManagerBuilder::addEarlyLTOOptimizationPasses(
659 legacy::PassManagerBase &PM) {
660 // Remove unused virtual tables to improve the quality of code generated by
661 // whole-program devirtualization and bitset lowering.
662 PM.add(createGlobalDCEPass());
663
664 // Apply whole-program devirtualization and virtual constant propagation.
665 PM.add(createWholeProgramDevirtPass());
666}
667
Peter Collingbourne070843d2015-03-19 22:01:00 +0000668void PassManagerBuilder::addLateLTOOptimizationPasses(
669 legacy::PassManagerBase &PM) {
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000670 // Delete basic blocks, which optimization passes may have killed.
Tom Stellardaa664d92013-08-06 02:43:45 +0000671 PM.add(createCFGSimplificationPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000672
Teresa Johnsonc4279a72015-08-11 16:26:41 +0000673 // Drop bodies of available externally objects to improve GlobalDCE.
674 PM.add(createEliminateAvailableExternallyPass());
675
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000676 // Now that we have optimized the program, discard unreachable functions.
677 PM.add(createGlobalDCEPass());
Nick Lewycky9e6d1842014-09-13 21:46:00 +0000678
679 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
680 // currently it damages debug info.
681 if (MergeFunctions)
682 PM.add(createMergeFunctionsPass());
Rafael Espindola3ea478b2011-08-02 21:50:27 +0000683}
Rafael Espindola07f609152011-08-09 22:17:34 +0000684
Chandler Carruth30d69c22015-02-13 10:01:29 +0000685void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
Rafael Espindola7cebf362014-08-21 20:03:44 +0000686 if (LibraryInfo)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000687 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
Rafael Espindola7cebf362014-08-21 20:03:44 +0000688
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000689 if (VerifyInput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000690 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000691
Peter Collingbournedf49d1b2016-02-09 22:50:34 +0000692 if (OptLevel != 0)
693 addEarlyLTOOptimizationPasses(PM);
694
Peter Collingbourne070843d2015-03-19 22:01:00 +0000695 if (OptLevel > 1)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000696 addLTOOptimizationPasses(PM);
697
Evgeniy Stepanov67849d52015-12-15 23:00:08 +0000698 // Create a function that performs CFI checks for cross-DSO calls with targets
699 // in the current module.
700 PM.add(createCrossDSOCFIPass());
701
Peter Collingbourne070843d2015-03-19 22:01:00 +0000702 // Lower bit sets to globals. This pass supports Clang's control flow
703 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
704 // is enabled. The pass does nothing if CFI is disabled.
705 PM.add(createLowerBitSetsPass());
706
707 if (OptLevel != 0)
708 addLateLTOOptimizationPasses(PM);
709
Duncan P. N. Exon Smithab58a562015-03-19 22:24:17 +0000710 if (VerifyOutput)
Rafael Espindola7cebf362014-08-21 20:03:44 +0000711 PM.add(createVerifierPass());
Rafael Espindola7cebf362014-08-21 20:03:44 +0000712}
713
Eric Christopher04d4e932013-04-22 22:47:22 +0000714inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
715 return reinterpret_cast<PassManagerBuilder*>(P);
716}
717
718inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
719 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
720}
721
Dmitri Gribenko0011bbf2012-11-15 16:51:49 +0000722LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
Rafael Espindola07f609152011-08-09 22:17:34 +0000723 PassManagerBuilder *PMB = new PassManagerBuilder();
724 return wrap(PMB);
725}
726
727void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
728 PassManagerBuilder *Builder = unwrap(PMB);
729 delete Builder;
730}
731
732void
733LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
734 unsigned OptLevel) {
735 PassManagerBuilder *Builder = unwrap(PMB);
736 Builder->OptLevel = OptLevel;
737}
738
739void
740LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
741 unsigned SizeLevel) {
742 PassManagerBuilder *Builder = unwrap(PMB);
743 Builder->SizeLevel = SizeLevel;
744}
745
746void
747LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
748 LLVMBool Value) {
749 PassManagerBuilder *Builder = unwrap(PMB);
750 Builder->DisableUnitAtATime = Value;
751}
752
753void
754LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
755 LLVMBool Value) {
756 PassManagerBuilder *Builder = unwrap(PMB);
757 Builder->DisableUnrollLoops = Value;
758}
759
760void
761LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
762 LLVMBool Value) {
Meador Ingedfb08a22013-06-20 19:48:07 +0000763 // NOTE: The simplify-libcalls pass has been removed.
Rafael Espindola07f609152011-08-09 22:17:34 +0000764}
765
766void
767LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
768 unsigned Threshold) {
769 PassManagerBuilder *Builder = unwrap(PMB);
770 Builder->Inliner = createFunctionInliningPass(Threshold);
771}
772
773void
774LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
775 LLVMPassManagerRef PM) {
776 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000777 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000778 Builder->populateFunctionPassManager(*FPM);
779}
780
781void
782LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
783 LLVMPassManagerRef PM) {
784 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000785 legacy::PassManagerBase *MPM = unwrap(PM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000786 Builder->populateModulePassManager(*MPM);
787}
788
789void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
790 LLVMPassManagerRef PM,
Nick Lewycky5f508542013-03-10 21:58:22 +0000791 LLVMBool Internalize,
792 LLVMBool RunInliner) {
Rafael Espindola07f609152011-08-09 22:17:34 +0000793 PassManagerBuilder *Builder = unwrap(PMB);
Chandler Carruth30d69c22015-02-13 10:01:29 +0000794 legacy::PassManagerBase *LPM = unwrap(PM);
Rafael Espindolae07caad2014-08-21 13:35:30 +0000795
796 // A small backwards compatibility hack. populateLTOPassManager used to take
797 // an RunInliner option.
798 if (RunInliner && !Builder->Inliner)
799 Builder->Inliner = createFunctionInliningPass();
800
801 Builder->populateLTOPassManager(*LPM);
Rafael Espindola07f609152011-08-09 22:17:34 +0000802}