1 //===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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++.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
16 #include "llvm-c/Transforms/PassManagerBuilder.h"
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Analysis/BasicAliasAnalysis.h"
19 #include "llvm/Analysis/CFLAliasAnalysis.h"
20 #include "llvm/Analysis/GlobalsModRef.h"
21 #include "llvm/Analysis/Passes.h"
22 #include "llvm/Analysis/ScopedNoAliasAA.h"
23 #include "llvm/Analysis/TargetLibraryInfo.h"
24 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
25 #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"
31 #include "llvm/Target/TargetMachine.h"
32 #include "llvm/Transforms/IPO.h"
33 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
34 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
35 #include "llvm/Transforms/Scalar.h"
36 #include "llvm/Transforms/Vectorize.h"
41 RunLoopVectorization("vectorize-loops", cl::Hidden,
42 cl::desc("Run the Loop vectorization passes"));
45 RunSLPVectorization("vectorize-slp", cl::Hidden,
46 cl::desc("Run the SLP vectorization passes"));
49 RunBBVectorization("vectorize-slp-aggressive", cl::Hidden,
50 cl::desc("Run the BB vectorization passes"));
53 UseGVNAfterVectorization("use-gvn-after-vectorization",
54 cl::init(false), cl::Hidden,
55 cl::desc("Run GVN instead of Early CSE after vectorization passes"));
57 static cl::opt<bool> ExtraVectorizerPasses(
58 "extra-vectorizer-passes", cl::init(false), cl::Hidden,
59 cl::desc("Run cleanup optimization passes after vectorization."));
61 static cl::opt<bool> UseNewSROA("use-new-sroa",
62 cl::init(true), cl::Hidden,
63 cl::desc("Enable the new, experimental SROA pass"));
66 RunLoopRerolling("reroll-loops", cl::Hidden,
67 cl::desc("Run the loop rerolling pass"));
70 RunFloat2Int("float-to-int", cl::Hidden, cl::init(true),
71 cl::desc("Run the float2int (float demotion) pass"));
73 static cl::opt<bool> RunLoadCombine("combine-loads", cl::init(false),
75 cl::desc("Run the load combining pass"));
78 RunSLPAfterLoopVectorization("run-slp-after-loop-vectorization",
79 cl::init(true), cl::Hidden,
80 cl::desc("Run the SLP vectorizer (and BB vectorizer) after the Loop "
81 "vectorizer instead of before"));
83 static cl::opt<bool> UseCFLAA("use-cfl-aa",
84 cl::init(false), cl::Hidden,
85 cl::desc("Enable the new, experimental CFL alias analysis"));
88 EnableMLSM("mlsm", cl::init(true), cl::Hidden,
89 cl::desc("Enable motion of merged load and store"));
91 static cl::opt<bool> EnableLoopInterchange(
92 "enable-loopinterchange", cl::init(false), cl::Hidden,
93 cl::desc("Enable the new, experimental LoopInterchange Pass"));
95 static cl::opt<bool> EnableLoopDistribute(
96 "enable-loop-distribute", cl::init(false), cl::Hidden,
97 cl::desc("Enable the new, experimental LoopDistribution Pass"));
99 static cl::opt<bool> EnableNonLTOGlobalsModRef(
100 "enable-non-lto-gmr", cl::init(true), cl::Hidden,
102 "Enable the GlobalsModRef AliasAnalysis outside of the LTO pipeline."));
104 static cl::opt<bool> EnableLoopLoadElim(
105 "enable-loop-load-elim", cl::init(false), cl::Hidden,
106 cl::desc("Enable the new, experimental LoopLoadElimination Pass"));
108 PassManagerBuilder::PassManagerBuilder() {
111 LibraryInfo = nullptr;
113 FunctionIndex = nullptr;
114 DisableUnitAtATime = false;
115 DisableUnrollLoops = false;
116 BBVectorize = RunBBVectorization;
117 SLPVectorize = RunSLPVectorization;
118 LoopVectorize = RunLoopVectorization;
119 RerollLoops = RunLoopRerolling;
120 LoadCombine = RunLoadCombine;
121 DisableGVNLoadPRE = false;
123 VerifyOutput = false;
124 MergeFunctions = false;
125 PrepareForLTO = false;
128 PassManagerBuilder::~PassManagerBuilder() {
133 /// Set of global extensions, automatically added as part of the standard set.
134 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
135 PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
137 void PassManagerBuilder::addGlobalExtension(
138 PassManagerBuilder::ExtensionPointTy Ty,
139 PassManagerBuilder::ExtensionFn Fn) {
140 GlobalExtensions->push_back(std::make_pair(Ty, Fn));
143 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
144 Extensions.push_back(std::make_pair(Ty, Fn));
147 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
148 legacy::PassManagerBase &PM) const {
149 for (unsigned i = 0, e = GlobalExtensions->size(); i != e; ++i)
150 if ((*GlobalExtensions)[i].first == ETy)
151 (*GlobalExtensions)[i].second(*this, PM);
152 for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
153 if (Extensions[i].first == ETy)
154 Extensions[i].second(*this, PM);
157 void PassManagerBuilder::addInitialAliasAnalysisPasses(
158 legacy::PassManagerBase &PM) const {
159 // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
160 // BasicAliasAnalysis wins if they disagree. This is intended to help
161 // support "obvious" type-punning idioms.
163 PM.add(createCFLAAWrapperPass());
164 PM.add(createTypeBasedAAWrapperPass());
165 PM.add(createScopedNoAliasAAWrapperPass());
168 void PassManagerBuilder::populateFunctionPassManager(
169 legacy::FunctionPassManager &FPM) {
170 addExtensionsToPM(EP_EarlyAsPossible, FPM);
172 // Add LibraryInfo if we have some.
174 FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
176 if (OptLevel == 0) return;
178 addInitialAliasAnalysisPasses(FPM);
180 FPM.add(createCFGSimplificationPass());
182 FPM.add(createSROAPass());
184 FPM.add(createScalarReplAggregatesPass());
185 FPM.add(createEarlyCSEPass());
186 FPM.add(createLowerExpectIntrinsicPass());
189 void PassManagerBuilder::populateModulePassManager(
190 legacy::PassManagerBase &MPM) {
191 // Allow forcing function attributes as a debugging and tuning aid.
192 MPM.add(createForceFunctionAttrsLegacyPass());
194 // If all optimizations are disabled, just run the always-inline pass and,
195 // if enabled, the function merging pass.
202 // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
203 // creates a CGSCC pass manager, but we don't want to add extensions into
204 // that pass manager. To prevent this we insert a no-op module pass to reset
205 // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
206 // builds. The function merging pass is
208 MPM.add(createMergeFunctionsPass());
209 else if (!GlobalExtensions->empty() || !Extensions.empty())
210 MPM.add(createBarrierNoopPass());
212 addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
216 // Add LibraryInfo if we have some.
218 MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
220 addInitialAliasAnalysisPasses(MPM);
222 if (!DisableUnitAtATime) {
223 // Infer attributes about declarations if possible.
224 MPM.add(createInferFunctionAttrsLegacyPass());
226 addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
228 MPM.add(createIPSCCPPass()); // IP SCCP
229 MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
230 // Promote any localized global vars
231 MPM.add(createPromoteMemoryToRegisterPass());
233 MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
235 MPM.add(createInstructionCombiningPass());// Clean up after IPCP & DAE
236 addExtensionsToPM(EP_Peephole, MPM);
237 MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
240 if (EnableNonLTOGlobalsModRef)
241 // We add a module alias analysis pass here. In part due to bugs in the
242 // analysis infrastructure this "works" in that the analysis stays alive
243 // for the entire SCC pass run below.
244 MPM.add(createGlobalsAAWrapperPass());
246 // Start of CallGraph SCC passes.
247 if (!DisableUnitAtATime)
248 MPM.add(createPruneEHPass()); // Remove dead EH info
253 if (!DisableUnitAtATime)
254 MPM.add(createFunctionAttrsPass()); // Set readonly/readnone attrs
256 MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
258 // Start of function pass.
259 // Break up aggregate allocas, using SSAUpdater.
261 MPM.add(createSROAPass());
263 MPM.add(createScalarReplAggregatesPass(-1, false));
264 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
265 MPM.add(createJumpThreadingPass()); // Thread jumps.
266 MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
267 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
268 MPM.add(createInstructionCombiningPass()); // Combine silly seq's
269 addExtensionsToPM(EP_Peephole, MPM);
271 MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
272 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
273 MPM.add(createReassociatePass()); // Reassociate expressions
274 // Rotate Loop - disable header duplication at -Oz
275 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
276 MPM.add(createLICMPass()); // Hoist loop invariants
277 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
278 MPM.add(createCFGSimplificationPass());
279 MPM.add(createInstructionCombiningPass());
280 MPM.add(createIndVarSimplifyPass()); // Canonicalize indvars
281 MPM.add(createLoopIdiomPass()); // Recognize idioms like memset.
282 MPM.add(createLoopDeletionPass()); // Delete dead loops
283 if (EnableLoopInterchange) {
284 MPM.add(createLoopInterchangePass()); // Interchange loops
285 MPM.add(createCFGSimplificationPass());
287 if (!DisableUnrollLoops)
288 MPM.add(createSimpleLoopUnrollPass()); // Unroll small loops
289 addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
293 MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
294 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
296 MPM.add(createMemCpyOptPass()); // Remove memcpy / form memset
297 MPM.add(createSCCPPass()); // Constant prop with SCCP
299 // Delete dead bit computations (instcombine runs after to fold away the dead
300 // computations, and then ADCE will run later to exploit any new DCE
301 // opportunities that creates).
302 MPM.add(createBitTrackingDCEPass()); // Delete dead bit computations
304 // Run instcombine after redundancy elimination to exploit opportunities
305 // opened up by them.
306 MPM.add(createInstructionCombiningPass());
307 addExtensionsToPM(EP_Peephole, MPM);
308 MPM.add(createJumpThreadingPass()); // Thread jumps
309 MPM.add(createCorrelatedValuePropagationPass());
310 MPM.add(createDeadStoreEliminationPass()); // Delete dead stores
311 MPM.add(createLICMPass());
313 addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
316 MPM.add(createLoopRerollPass());
317 if (!RunSLPAfterLoopVectorization) {
319 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
322 MPM.add(createBBVectorizePass());
323 MPM.add(createInstructionCombiningPass());
324 addExtensionsToPM(EP_Peephole, MPM);
325 if (OptLevel > 1 && UseGVNAfterVectorization)
326 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
328 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
330 // BBVectorize may have significantly shortened a loop body; unroll again.
331 if (!DisableUnrollLoops)
332 MPM.add(createLoopUnrollPass());
337 MPM.add(createLoadCombinePass());
339 MPM.add(createAggressiveDCEPass()); // Delete dead instructions
340 MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
341 MPM.add(createInstructionCombiningPass()); // Clean up after everything.
342 addExtensionsToPM(EP_Peephole, MPM);
344 // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
345 // pass manager that we are specifically trying to avoid. To prevent this
346 // we must insert a no-op module pass to reset the pass manager.
347 MPM.add(createBarrierNoopPass());
349 if (!DisableUnitAtATime && OptLevel > 1 && !PrepareForLTO) {
350 // Remove avail extern fns and globals definitions if we aren't
351 // compiling an object file for later LTO. For LTO we want to preserve
352 // these so they are eligible for inlining at link-time. Note if they
353 // are unreferenced they will be removed by GlobalDCE later, so
354 // this only impacts referenced available externally globals.
355 // Eventually they will be suppressed during codegen, but eliminating
356 // here enables more opportunity for GlobalDCE as it may make
357 // globals referenced by available external functions dead
358 // and saves running remaining passes on the eliminated functions.
359 MPM.add(createEliminateAvailableExternallyPass());
362 if (EnableNonLTOGlobalsModRef)
363 // We add a fresh GlobalsModRef run at this point. This is particularly
364 // useful as the above will have inlined, DCE'ed, and function-attr
365 // propagated everything. We should at this point have a reasonably minimal
366 // and richly annotated call graph. By computing aliasing and mod/ref
367 // information for all local globals here, the late loop passes and notably
368 // the vectorizer will be able to use them to help recognize vectorizable
369 // memory operations.
371 // Note that this relies on a bug in the pass manager which preserves
372 // a module analysis into a function pass pipeline (and throughout it) so
373 // long as the first function pass doesn't invalidate the module analysis.
374 // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
375 // this to work. Fortunately, it is trivial to preserve AliasAnalysis
376 // (doing nothing preserves it as it is required to be conservatively
377 // correct in the face of IR changes).
378 MPM.add(createGlobalsAAWrapperPass());
381 MPM.add(createFloat2IntPass());
383 addExtensionsToPM(EP_VectorizerStart, MPM);
385 // Re-rotate loops in all our loop nests. These may have fallout out of
386 // rotated form due to GVN or other transformations, and the vectorizer relies
387 // on the rotated form. Disable header duplication at -Oz.
388 MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
390 // Distribute loops to allow partial vectorization. I.e. isolate dependences
391 // into separate loop that would otherwise inhibit vectorization.
392 if (EnableLoopDistribute)
393 MPM.add(createLoopDistributePass());
395 MPM.add(createLoopVectorizePass(DisableUnrollLoops, LoopVectorize));
397 // Eliminate loads by forwarding stores from the previous iteration to loads
398 // of the current iteration.
399 if (EnableLoopLoadElim)
400 MPM.add(createLoopLoadEliminationPass());
402 // FIXME: Because of #pragma vectorize enable, the passes below are always
403 // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
404 // on -O1 and no #pragma is found). Would be good to have these two passes
405 // as function calls, so that we can only pass them when the vectorizer
407 MPM.add(createInstructionCombiningPass());
408 if (OptLevel > 1 && ExtraVectorizerPasses) {
409 // At higher optimization levels, try to clean up any runtime overlap and
410 // alignment checks inserted by the vectorizer. We want to track correllated
411 // runtime checks for two inner loops in the same outer loop, fold any
412 // common computations, hoist loop-invariant aspects out of any outer loop,
413 // and unswitch the runtime checks if possible. Once hoisted, we may have
414 // dead (or speculatable) control flows or more combining opportunities.
415 MPM.add(createEarlyCSEPass());
416 MPM.add(createCorrelatedValuePropagationPass());
417 MPM.add(createInstructionCombiningPass());
418 MPM.add(createLICMPass());
419 MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3));
420 MPM.add(createCFGSimplificationPass());
421 MPM.add(createInstructionCombiningPass());
424 if (RunSLPAfterLoopVectorization) {
426 MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
427 if (OptLevel > 1 && ExtraVectorizerPasses) {
428 MPM.add(createEarlyCSEPass());
433 MPM.add(createBBVectorizePass());
434 MPM.add(createInstructionCombiningPass());
435 addExtensionsToPM(EP_Peephole, MPM);
436 if (OptLevel > 1 && UseGVNAfterVectorization)
437 MPM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
439 MPM.add(createEarlyCSEPass()); // Catch trivial redundancies
441 // BBVectorize may have significantly shortened a loop body; unroll again.
442 if (!DisableUnrollLoops)
443 MPM.add(createLoopUnrollPass());
447 addExtensionsToPM(EP_Peephole, MPM);
448 MPM.add(createCFGSimplificationPass());
449 MPM.add(createInstructionCombiningPass());
451 if (!DisableUnrollLoops) {
452 MPM.add(createLoopUnrollPass()); // Unroll small loops
454 // LoopUnroll may generate some redundency to cleanup.
455 MPM.add(createInstructionCombiningPass());
457 // Runtime unrolling will introduce runtime check in loop prologue. If the
458 // unrolled loop is a inner loop, then the prologue will be inside the
459 // outer loop. LICM pass can help to promote the runtime check out if the
460 // checked value is loop invariant.
461 MPM.add(createLICMPass());
464 // After vectorization and unrolling, assume intrinsics may tell us more
465 // about pointer alignments.
466 MPM.add(createAlignmentFromAssumptionsPass());
468 if (!DisableUnitAtATime) {
469 // FIXME: We shouldn't bother with this anymore.
470 MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
472 // GlobalOpt already deletes dead functions and globals, at -O2 try a
473 // late pass of GlobalDCE. It is capable of deleting dead cycles.
475 MPM.add(createGlobalDCEPass()); // Remove dead fns and globals.
476 MPM.add(createConstantMergePass()); // Merge dup global constants
481 MPM.add(createMergeFunctionsPass());
483 addExtensionsToPM(EP_OptimizerLast, MPM);
486 void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
487 // Provide AliasAnalysis services for optimizations.
488 addInitialAliasAnalysisPasses(PM);
491 PM.add(createFunctionImportPass(FunctionIndex));
493 // Allow forcing function attributes as a debugging and tuning aid.
494 PM.add(createForceFunctionAttrsLegacyPass());
496 // Infer attributes about declarations if possible.
497 PM.add(createInferFunctionAttrsLegacyPass());
499 // Propagate constants at call sites into the functions they call. This
500 // opens opportunities for globalopt (and inlining) by substituting function
501 // pointers passed as arguments to direct uses of functions.
502 PM.add(createIPSCCPPass());
504 // Now that we internalized some globals, see if we can hack on them!
505 PM.add(createFunctionAttrsPass()); // Add norecurse if possible.
506 PM.add(createGlobalOptimizerPass());
507 // Promote any localized global vars.
508 PM.add(createPromoteMemoryToRegisterPass());
510 // Linking modules together can lead to duplicated global constants, only
511 // keep one copy of each constant.
512 PM.add(createConstantMergePass());
514 // Remove unused arguments from functions.
515 PM.add(createDeadArgEliminationPass());
517 // Reduce the code after globalopt and ipsccp. Both can open up significant
518 // simplification opportunities, and both can propagate functions through
519 // function pointers. When this happens, we often have to resolve varargs
520 // calls, etc, so let instcombine do this.
521 PM.add(createInstructionCombiningPass());
522 addExtensionsToPM(EP_Peephole, PM);
524 // Inline small functions
525 bool RunInliner = Inliner;
531 PM.add(createPruneEHPass()); // Remove dead EH info.
533 // Optimize globals again if we ran the inliner.
535 PM.add(createGlobalOptimizerPass());
536 PM.add(createGlobalDCEPass()); // Remove dead functions.
538 // If we didn't decide to inline a function, check to see if we can
539 // transform it to pass arguments by value instead of by reference.
540 PM.add(createArgumentPromotionPass());
542 // The IPO passes may leave cruft around. Clean up after them.
543 PM.add(createInstructionCombiningPass());
544 addExtensionsToPM(EP_Peephole, PM);
545 PM.add(createJumpThreadingPass());
549 PM.add(createSROAPass());
551 PM.add(createScalarReplAggregatesPass());
553 // Run a few AA driven optimizations here and now, to cleanup the code.
554 PM.add(createFunctionAttrsPass()); // Add nocapture.
555 PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
557 PM.add(createLICMPass()); // Hoist loop invariants.
559 PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
560 PM.add(createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
561 PM.add(createMemCpyOptPass()); // Remove dead memcpys.
564 PM.add(createDeadStoreEliminationPass());
566 // More loops are countable; try to optimize them.
567 PM.add(createIndVarSimplifyPass());
568 PM.add(createLoopDeletionPass());
569 if (EnableLoopInterchange)
570 PM.add(createLoopInterchangePass());
572 PM.add(createLoopVectorizePass(true, LoopVectorize));
574 // Now that we've optimized loops (in particular loop induction variables),
575 // we may have exposed more scalar opportunities. Run parts of the scalar
576 // optimizer again at this point.
577 PM.add(createInstructionCombiningPass()); // Initial cleanup
578 PM.add(createCFGSimplificationPass()); // if-convert
579 PM.add(createSCCPPass()); // Propagate exposed constants
580 PM.add(createInstructionCombiningPass()); // Clean up again
581 PM.add(createBitTrackingDCEPass());
583 // More scalar chains could be vectorized due to more alias information
584 if (RunSLPAfterLoopVectorization)
586 PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
588 // After vectorization, assume intrinsics may tell us more about pointer
590 PM.add(createAlignmentFromAssumptionsPass());
593 PM.add(createLoadCombinePass());
595 // Cleanup and simplify the code after the scalar optimizations.
596 PM.add(createInstructionCombiningPass());
597 addExtensionsToPM(EP_Peephole, PM);
599 PM.add(createJumpThreadingPass());
602 void PassManagerBuilder::addLateLTOOptimizationPasses(
603 legacy::PassManagerBase &PM) {
604 // Delete basic blocks, which optimization passes may have killed.
605 PM.add(createCFGSimplificationPass());
607 // Drop bodies of available externally objects to improve GlobalDCE.
608 PM.add(createEliminateAvailableExternallyPass());
610 // Now that we have optimized the program, discard unreachable functions.
611 PM.add(createGlobalDCEPass());
613 // FIXME: this is profitable (for compiler time) to do at -O0 too, but
614 // currently it damages debug info.
616 PM.add(createMergeFunctionsPass());
619 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
621 PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
624 PM.add(createVerifierPass());
627 addLTOOptimizationPasses(PM);
629 // Create a function that performs CFI checks for cross-DSO calls with targets
630 // in the current module.
631 PM.add(createCrossDSOCFIPass());
633 // Lower bit sets to globals. This pass supports Clang's control flow
634 // integrity mechanisms (-fsanitize=cfi*) and needs to run at link time if CFI
635 // is enabled. The pass does nothing if CFI is disabled.
636 PM.add(createLowerBitSetsPass());
639 addLateLTOOptimizationPasses(PM);
642 PM.add(createVerifierPass());
645 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
646 return reinterpret_cast<PassManagerBuilder*>(P);
649 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
650 return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
653 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
654 PassManagerBuilder *PMB = new PassManagerBuilder();
658 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
659 PassManagerBuilder *Builder = unwrap(PMB);
664 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
666 PassManagerBuilder *Builder = unwrap(PMB);
667 Builder->OptLevel = OptLevel;
671 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
672 unsigned SizeLevel) {
673 PassManagerBuilder *Builder = unwrap(PMB);
674 Builder->SizeLevel = SizeLevel;
678 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
680 PassManagerBuilder *Builder = unwrap(PMB);
681 Builder->DisableUnitAtATime = Value;
685 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
687 PassManagerBuilder *Builder = unwrap(PMB);
688 Builder->DisableUnrollLoops = Value;
692 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
694 // NOTE: The simplify-libcalls pass has been removed.
698 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
699 unsigned Threshold) {
700 PassManagerBuilder *Builder = unwrap(PMB);
701 Builder->Inliner = createFunctionInliningPass(Threshold);
705 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
706 LLVMPassManagerRef PM) {
707 PassManagerBuilder *Builder = unwrap(PMB);
708 legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
709 Builder->populateFunctionPassManager(*FPM);
713 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
714 LLVMPassManagerRef PM) {
715 PassManagerBuilder *Builder = unwrap(PMB);
716 legacy::PassManagerBase *MPM = unwrap(PM);
717 Builder->populateModulePassManager(*MPM);
720 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
721 LLVMPassManagerRef PM,
722 LLVMBool Internalize,
723 LLVMBool RunInliner) {
724 PassManagerBuilder *Builder = unwrap(PMB);
725 legacy::PassManagerBase *LPM = unwrap(PM);
727 // A small backwards compatibility hack. populateLTOPassManager used to take
728 // an RunInliner option.
729 if (RunInliner && !Builder->Inliner)
730 Builder->Inliner = createFunctionInliningPass();
732 Builder->populateLTOPassManager(*LPM);