3b21d3f0cb134b1ea04389bcf03f6fdace19c39b
[oota-llvm.git] / lib / Transforms / IPO / PassManagerBuilder.cpp
1 //===- 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"
17 #include "llvm-c/Transforms/PassManagerBuilder.h"
18 #include "llvm/ADT/SmallVector.h"
19 #include "llvm/Analysis/Passes.h"
20 #include "llvm/IR/DataLayout.h"
21 #include "llvm/IR/Verifier.h"
22 #include "llvm/IR/LegacyPassManager.h"
23 #include "llvm/Support/CommandLine.h"
24 #include "llvm/Support/ManagedStatic.h"
25 #include "llvm/Analysis/TargetLibraryInfo.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Transforms/IPO.h"
28 #include "llvm/Transforms/Scalar.h"
29 #include "llvm/Transforms/Vectorize.h"
30
31 using namespace llvm;
32
33 static cl::opt<bool>
34 RunLoopVectorization("vectorize-loops", cl::Hidden,
35                      cl::desc("Run the Loop vectorization passes"));
36
37 static cl::opt<bool>
38 RunSLPVectorization("vectorize-slp", cl::Hidden,
39                     cl::desc("Run the SLP vectorization passes"));
40
41 static cl::opt<bool>
42 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
43                     cl::desc("Run the BB vectorization passes"));
44
45 static cl::opt<bool>
46 UseGVNAfterVectorization("use-gvn-after-vectorization",
47   cl::init(false), cl::Hidden,
48   cl::desc("Run GVN instead of Early CSE after vectorization passes"));
49
50 static cl::opt<bool> ExtraVectorizerPasses(
51     "extra-vectorizer-passes", cl::init(false), cl::Hidden,
52     cl::desc("Run cleanup optimization passes after vectorization."));
53
54 static cl::opt<bool> UseNewSROA("use-new-sroa",
55   cl::init(true), cl::Hidden,
56   cl::desc("Enable the new, experimental SROA pass"));
57
58 static cl::opt<bool>
59 RunLoopRerolling("reroll-loops", cl::Hidden,
60                  cl::desc("Run the loop rerolling pass"));
61
62 static cl::opt<bool>
63 RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
64              cl::desc("Run the float2int (float demotion) pass"));
65
66 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
67                                     cl::Hidden,
68                                     cl::desc("Run the load combining pass"));
69
70 static cl::opt<bool>
71 RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
72   cl::init(true), cl::Hidden,
73   cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
74            "vectorizer instead of before"));
75
76 static cl::opt<bool> UseCFLAA("use-cfl-aa",
77   cl::init(false), cl::Hidden,
78   cl::desc("Enable the new, experimental CFL alias analysis"));
79
80 static cl::opt<bool>
81 EnableMLSM("mlsm", cl::init(true), cl::Hidden,
82            cl::desc("Enable motion of merged load and store"));
83
84 static cl::opt<bool> EnableLoopInterchange(
85     "enable-loopinterchange", cl::init(false), cl::Hidden,
86     cl::desc("Enable the new, experimental LoopInterchange Pass"));
87
88 static cl::opt<bool> EnableLoopDistribute(
89     "enable-loop-distribute", cl::init(false), cl::Hidden,
90     cl::desc("Enable the new, experimental LoopDistribution Pass"));
91
92 PassManagerBuilder::PassManagerBuilder() {
93     OptLevel = 2;
94     SizeLevel = 0;
95     LibraryInfo = nullptr;
96     Inliner = nullptr;
97     DisableTailCalls = false;
98     DisableUnitAtATime = false;
99     DisableUnrollLoops = false;
100     BBVectorize = RunBBVectorization;
101     SLPVectorize = RunSLPVectorization;
102     LoopVectorize = RunLoopVectorization;
103     RerollLoops = RunLoopRerolling;
104     LoadCombine = RunLoadCombine;
105     DisableGVNLoadPRE = false;
106     VerifyInput = false;
107     VerifyOutput = false;
108     MergeFunctions = false;
109 }
110
111 PassManagerBuilder::~PassManagerBuilder() {
112   delete LibraryInfo;
113   delete Inliner;
114 }
115
116 /// Set of global extensions, automatically added as part of the standard set.
117 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
118    PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
119
120 void PassManagerBuilder::addGlobalExtension(
121     PassManagerBuilder::ExtensionPointTy Ty,
122     PassManagerBuilder::ExtensionFn Fn) {
123   GlobalExtensions->push_back(std::make_pair(Ty, Fn));
124 }
125
126 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
127   Extensions.push_back(std::make_pair(Ty, Fn));
128 }
129
130 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
131                                            legacy::PassManagerBase &PM) const {
132   for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
133     if ((*GlobalExtensions)[i].first == ETy)
134       (*GlobalExtensions)[i].second(*this, PM);
135   for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
136     if (Extensions[i].first == ETy)
137       Extensions[i].second(*this, PM);
138 }
139
140 void PassManagerBuilder::addInitialAliasAnalysisPasses(
141     legacy::PassManagerBase &PM) const {
142   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
143   // BasicAliasAnalysis wins if they disagree. This is intended to help
144   // support "obvious" type-punning idioms.
145   if (UseCFLAA)
146     PM.add(createCFLAliasAnalysisPass());
147   PM.add(createTypeBasedAliasAnalysisPass());
148   PM.add(createScopedNoAliasAAPass());
149   PM.add(createBasicAliasAnalysisPass());
150 }
151
152 void PassManagerBuilder::populateFunctionPassManager(
153     legacy::FunctionPassManager &FPM) {
154   addExtensionsToPM(EP_EarlyAsPossible, FPM);
155
156   // Add LibraryInfo if we have some.
157   if (LibraryInfo)
158     FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
159
160   if (OptLevel == 0) return;
161
162   addInitialAliasAnalysisPasses(FPM);
163
164   FPM.add(createCFGSimplificationPass());
165   if (UseNewSROA)
166     FPM.add(createSROAPass());
167   else
168     FPM.add(createScalarReplAggregatesPass());
169   FPM.add(createEarlyCSEPass());
170   FPM.add(createLowerExpectIntrinsicPass());
171 }
172
173 void PassManagerBuilder::populateModulePassManager(
174     legacy::PassManagerBase &MPM) {
175   // If all optimizations are disabled, just run the always-inline pass and,
176   // if enabled, the function merging pass.
177   if (OptLevel == 0) {
178     if (Inliner) {
179       MPM.add(Inliner);
180       Inliner = nullptr;
181     }
182
183     // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
184     // creates a CGSCC pass manager, but we don't want to add extensions into
185     // that pass manager. To prevent this we insert a no-op module pass to reset
186     // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
187     // builds. The function merging pass is 
188     if (MergeFunctions)
189       MPM.add(createMergeFunctionsPass());
190     else if (!GlobalExtensions->empty() || !Extensions.empty())
191       MPM.add(createBarrierNoopPass());
192
193     addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
194     return;
195   }
196
197   // Add LibraryInfo if we have some.
198   if (LibraryInfo)
199     MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
200
201   addInitialAliasAnalysisPasses(MPM);
202
203   if (!DisableUnitAtATime) {
204     addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
205
206     MPM.add(createIPSCCPPass());              // IP SCCP
207     MPM.add(createGlobalOptimizerPass());     // Optimize out global vars
208
209     MPM.add(createDeadArgEliminationPass());  // Dead argument elimination
210
211     MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
212     addExtensionsToPM(EP_Peephole, MPM);
213     MPM.add(createCFGSimplificationPass());   // Clean up after IPCP & DAE
214   }
215
216   // Start of CallGraph SCC passes.
217   if (!DisableUnitAtATime)
218     MPM.add(createPruneEHPass());             // Remove dead EH info
219   if (Inliner) {
220     MPM.add(Inliner);
221     Inliner = nullptr;
222   }
223   if (!DisableUnitAtATime)
224     MPM.add(createFunctionAttrsPass());       // Set readonly/readnone attrs
225   if (OptLevel > 2)
226     MPM.add(createArgumentPromotionPass());   // Scalarize uninlined fn args
227
228   // Start of function pass.
229   // Break up aggregate allocas, using SSAUpdater.
230   if (UseNewSROA)
231     MPM.add(createSROAPass(/*RequiresDomTree*/ false));
232   else
233     MPM.add(createScalarReplAggregatesPass(-1, false));
234   MPM.add(createEarlyCSEPass());              // Catch trivial redundancies
235   MPM.add(createJumpThreadingPass());         // Thread jumps.
236   MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
237   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
238   MPM.add(createInstructionCombiningPass());  // Combine silly seq's
239   addExtensionsToPM(EP_Peephole, MPM);
240
241   if (!DisableTailCalls)
242     MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
243   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
244   MPM.add(createReassociatePass());           // Reassociate expressions
245   // Rotate Loop - disable header duplication at -Oz
246   MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
247   MPM.add(createLICMPass());                  // Hoist loop invariants
248   MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
249   MPM.add(createInstructionCombiningPass());
250   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
251   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
252   MPM.add(createLoopDeletionPass());          // Delete dead loops
253   if (EnableLoopInterchange) {
254     MPM.add(createLoopInterchangePass()); // Interchange loops
255     MPM.add(createCFGSimplificationPass());
256   }
257   if (!DisableUnrollLoops)
258     MPM.add(createSimpleLoopUnrollPass());    // Unroll small loops
259   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
260
261   if (OptLevel > 1) {
262     if (EnableMLSM)
263       MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
264     MPM.add(createGVNPass(DisableGVNLoadPRE));  // Remove redundancies
265   }
266   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
267   MPM.add(createSCCPPass());                  // Constant prop with SCCP
268
269   // Delete dead bit computations (instcombine runs after to fold away the dead
270   // computations, and then ADCE will run later to exploit any new DCE
271   // opportunities that creates).
272   MPM.add(createBitTrackingDCEPass());        // Delete dead bit computations
273
274   // Run instcombine after redundancy elimination to exploit opportunities
275   // opened up by them.
276   MPM.add(createInstructionCombiningPass());
277   addExtensionsToPM(EP_Peephole, MPM);
278   MPM.add(createJumpThreadingPass());         // Thread jumps
279   MPM.add(createCorrelatedValuePropagationPass());
280   MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
281   MPM.add(createLICMPass());
282
283   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
284
285   if (RerollLoops)
286     MPM.add(createLoopRerollPass());
287   if (!RunSLPAfterLoopVectorization) {
288     if (SLPVectorize)
289       MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
290
291     if (BBVectorize) {
292       MPM.add(createBBVectorizePass());
293       MPM.add(createInstructionCombiningPass());
294       addExtensionsToPM(EP_Peephole, MPM);
295       if (OptLevel > 1 && UseGVNAfterVectorization)
296         MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
297       else
298         MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
299
300       // BBVectorize may have significantly shortened a loop body; unroll again.
301       if (!DisableUnrollLoops)
302         MPM.add(createLoopUnrollPass());
303     }
304   }
305
306   if (LoadCombine)
307     MPM.add(createLoadCombinePass());
308
309   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
310   MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
311   MPM.add(createInstructionCombiningPass());  // Clean up after everything.
312   addExtensionsToPM(EP_Peephole, MPM);
313
314   // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
315   // pass manager that we are specifically trying to avoid. To prevent this
316   // we must insert a no-op module pass to reset the pass manager.
317   MPM.add(createBarrierNoopPass());
318
319   if (RunFloat2Int)
320     MPM.add(createFloat2IntPass());
321
322   // Re-rotate loops in all our loop nests. These may have fallout out of
323   // rotated form due to GVN or other transformations, and the vectorizer relies
324   // on the rotated form.
325   MPM.add(createLoopRotatePass());
326
327   // Distribute loops to allow partial vectorization.  I.e. isolate dependences
328   // into separate loop that would otherwise inhibit vectorization.
329   if (EnableLoopDistribute)
330     MPM.add(createLoopDistributePass());
331
332   MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
333   // FIXME: Because of #pragma vectorize enable, the passes below are always
334   // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
335   // on -O1 and no #pragma is found). Would be good to have these two passes
336   // as function calls, so that we can only pass them when the vectorizer
337   // changed the code.
338   MPM.add(createInstructionCombiningPass());
339   if (OptLevel > 1 && ExtraVectorizerPasses) {
340     // At higher optimization levels, try to clean up any runtime overlap and
341     // alignment checks inserted by the vectorizer. We want to track correllated
342     // runtime checks for two inner loops in the same outer loop, fold any
343     // common computations, hoist loop-invariant aspects out of any outer loop,
344     // and unswitch the runtime checks if possible. Once hoisted, we may have
345     // dead (or speculatable) control flows or more combining opportunities.
346     MPM.add(createEarlyCSEPass());
347     MPM.add(createCorrelatedValuePropagationPass());
348     MPM.add(createInstructionCombiningPass());
349     MPM.add(createLICMPass());
350     MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
351     MPM.add(createCFGSimplificationPass());
352     MPM.add(createInstructionCombiningPass());
353   }
354
355   if (RunSLPAfterLoopVectorization) {
356     if (SLPVectorize) {
357       MPM.add(createSLPVectorizerPass());   // Vectorize parallel scalar chains.
358       if (OptLevel > 1 && ExtraVectorizerPasses) {
359         MPM.add(createEarlyCSEPass());
360       }
361     }
362
363     if (BBVectorize) {
364       MPM.add(createBBVectorizePass());
365       MPM.add(createInstructionCombiningPass());
366       addExtensionsToPM(EP_Peephole, MPM);
367       if (OptLevel > 1 && UseGVNAfterVectorization)
368         MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
369       else
370         MPM.add(createEarlyCSEPass());      // Catch trivial redundancies
371
372       // BBVectorize may have significantly shortened a loop body; unroll again.
373       if (!DisableUnrollLoops)
374         MPM.add(createLoopUnrollPass());
375     }
376   }
377
378   addExtensionsToPM(EP_Peephole, MPM);
379   MPM.add(createCFGSimplificationPass());
380   MPM.add(createInstructionCombiningPass());
381
382   if (!DisableUnrollLoops) {
383     MPM.add(createLoopUnrollPass());    // Unroll small loops
384
385     // LoopUnroll may generate some redundency to cleanup.
386     MPM.add(createInstructionCombiningPass());
387
388     // This is a barrier pass to avoid combine LICM pass and loop unroll pass
389     // within same loop pass manager.
390     MPM.add(createInstructionSimplifierPass());
391
392     // Runtime unrolling will introduce runtime check in loop prologue. If the
393     // unrolled loop is a inner loop, then the prologue will be inside the
394     // outer loop. LICM pass can help to promote the runtime check out if the
395     // checked value is loop invariant.
396     MPM.add(createLICMPass());
397   }
398
399   // After vectorization and unrolling, assume intrinsics may tell us more
400   // about pointer alignments.
401   MPM.add(createAlignmentFromAssumptionsPass());
402
403   if (!DisableUnitAtATime) {
404     // FIXME: We shouldn't bother with this anymore.
405     MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
406
407     // GlobalOpt already deletes dead functions and globals, at -O2 try a
408     // late pass of GlobalDCE.  It is capable of deleting dead cycles.
409     if (OptLevel > 1) {
410       MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
411       MPM.add(createConstantMergePass());     // Merge dup global constants
412     }
413   }
414
415   if (MergeFunctions)
416     MPM.add(createMergeFunctionsPass());
417
418   addExtensionsToPM(EP_OptimizerLast, MPM);
419 }
420
421 void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
422   // Provide AliasAnalysis services for optimizations.
423   addInitialAliasAnalysisPasses(PM);
424
425   // Propagate constants at call sites into the functions they call.  This
426   // opens opportunities for globalopt (and inlining) by substituting function
427   // pointers passed as arguments to direct uses of functions.
428   PM.add(createIPSCCPPass());
429
430   // Now that we internalized some globals, see if we can hack on them!
431   PM.add(createGlobalOptimizerPass());
432
433   // Linking modules together can lead to duplicated global constants, only
434   // keep one copy of each constant.
435   PM.add(createConstantMergePass());
436
437   // Remove unused arguments from functions.
438   PM.add(createDeadArgEliminationPass());
439
440   // Reduce the code after globalopt and ipsccp.  Both can open up significant
441   // simplification opportunities, and both can propagate functions through
442   // function pointers.  When this happens, we often have to resolve varargs
443   // calls, etc, so let instcombine do this.
444   PM.add(createInstructionCombiningPass());
445   addExtensionsToPM(EP_Peephole, PM);
446
447   // Inline small functions
448   bool RunInliner = Inliner;
449   if (RunInliner) {
450     PM.add(Inliner);
451     Inliner = nullptr;
452   }
453
454   PM.add(createPruneEHPass());   // Remove dead EH info.
455
456   // Optimize globals again if we ran the inliner.
457   if (RunInliner)
458     PM.add(createGlobalOptimizerPass());
459   PM.add(createGlobalDCEPass()); // Remove dead functions.
460
461   // If we didn't decide to inline a function, check to see if we can
462   // transform it to pass arguments by value instead of by reference.
463   PM.add(createArgumentPromotionPass());
464
465   // The IPO passes may leave cruft around.  Clean up after them.
466   PM.add(createInstructionCombiningPass());
467   addExtensionsToPM(EP_Peephole, PM);
468   PM.add(createJumpThreadingPass());
469
470   // Break up allocas
471   if (UseNewSROA)
472     PM.add(createSROAPass());
473   else
474     PM.add(createScalarReplAggregatesPass());
475
476   // Run a few AA driven optimizations here and now, to cleanup the code.
477   PM.add(createFunctionAttrsPass()); // Add nocapture.
478   PM.add(createGlobalsModRefPass()); // IP alias analysis.
479
480   PM.add(createLICMPass());                 // Hoist loop invariants.
481   if (EnableMLSM)
482     PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
483   PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
484   PM.add(createMemCpyOptPass());            // Remove dead memcpys.
485
486   // Nuke dead stores.
487   PM.add(createDeadStoreEliminationPass());
488
489   // More loops are countable; try to optimize them.
490   PM.add(createIndVarSimplifyPass());
491   PM.add(createLoopDeletionPass());
492   if (EnableLoopInterchange)
493     PM.add(createLoopInterchangePass());
494
495   PM.add(createLoopVectorizePass(true, LoopVectorize));
496
497   // More scalar chains could be vectorized due to more alias information
498   if (RunSLPAfterLoopVectorization)
499     if (SLPVectorize)
500       PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
501
502   // After vectorization, assume intrinsics may tell us more about pointer
503   // alignments.
504   PM.add(createAlignmentFromAssumptionsPass());
505
506   if (LoadCombine)
507     PM.add(createLoadCombinePass());
508
509   // Cleanup and simplify the code after the scalar optimizations.
510   PM.add(createInstructionCombiningPass());
511   addExtensionsToPM(EP_Peephole, PM);
512
513   PM.add(createJumpThreadingPass());
514 }
515
516 void PassManagerBuilder::addLateLTOOptimizationPasses(
517     legacy::PassManagerBase &PM) {
518   // Delete basic blocks, which optimization passes may have killed.
519   PM.add(createCFGSimplificationPass());
520
521   // Now that we have optimized the program, discard unreachable functions.
522   PM.add(createGlobalDCEPass());
523
524   // FIXME: this is profitable (for compiler time) to do at -O0 too, but
525   // currently it damages debug info.
526   if (MergeFunctions)
527     PM.add(createMergeFunctionsPass());
528 }
529
530 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
531   if (LibraryInfo)
532     PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
533
534   if (VerifyInput)
535     PM.add(createVerifierPass());
536
537   if (OptLevel > 1)
538     addLTOOptimizationPasses(PM);
539
540   // Lower bit sets to globals. This pass supports Clang's control flow
541   // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
542   // is enabled. The pass does nothing if CFI is disabled.
543   PM.add(createLowerBitSetsPass());
544
545   if (OptLevel != 0)
546     addLateLTOOptimizationPasses(PM);
547
548   if (VerifyOutput)
549     PM.add(createVerifierPass());
550 }
551
552 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
553     return reinterpret_cast<PassManagerBuilder*>(P);
554 }
555
556 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
557   return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
558 }
559
560 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
561   PassManagerBuilder *PMB = new PassManagerBuilder();
562   return wrap(PMB);
563 }
564
565 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
566   PassManagerBuilder *Builder = unwrap(PMB);
567   delete Builder;
568 }
569
570 void
571 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
572                                   unsigned OptLevel) {
573   PassManagerBuilder *Builder = unwrap(PMB);
574   Builder->OptLevel = OptLevel;
575 }
576
577 void
578 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
579                                    unsigned SizeLevel) {
580   PassManagerBuilder *Builder = unwrap(PMB);
581   Builder->SizeLevel = SizeLevel;
582 }
583
584 void
585 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
586                                             LLVMBool Value) {
587   PassManagerBuilder *Builder = unwrap(PMB);
588   Builder->DisableUnitAtATime = Value;
589 }
590
591 void
592 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
593                                             LLVMBool Value) {
594   PassManagerBuilder *Builder = unwrap(PMB);
595   Builder->DisableUnrollLoops = Value;
596 }
597
598 void
599 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
600                                                  LLVMBool Value) {
601   // NOTE: The simplify-libcalls pass has been removed.
602 }
603
604 void
605 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
606                                               unsigned Threshold) {
607   PassManagerBuilder *Builder = unwrap(PMB);
608   Builder->Inliner = createFunctionInliningPass(Threshold);
609 }
610
611 void
612 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
613                                                   LLVMPassManagerRef PM) {
614   PassManagerBuilder *Builder = unwrap(PMB);
615   legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
616   Builder->populateFunctionPassManager(*FPM);
617 }
618
619 void
620 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
621                                                 LLVMPassManagerRef PM) {
622   PassManagerBuilder *Builder = unwrap(PMB);
623   legacy::PassManagerBase *MPM = unwrap(PM);
624   Builder->populateModulePassManager(*MPM);
625 }
626
627 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
628                                                   LLVMPassManagerRef PM,
629                                                   LLVMBool Internalize,
630                                                   LLVMBool RunInliner) {
631   PassManagerBuilder *Builder = unwrap(PMB);
632   legacy::PassManagerBase *LPM = unwrap(PM);
633
634   // A small backwards compatibility hack. populateLTOPassManager used to take
635   // an RunInliner option.
636   if (RunInliner && !Builder->Inliner)
637     Builder->Inliner = createFunctionInliningPass();
638
639   Builder->populateLTOPassManager(*LPM);
640 }