1 //===- PassManagerBuilder.cpp - Build Standard Pass -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the PassManagerBuilder class, which is used to set up a
10 // "standard" optimization sequence suitable for languages like C and C++.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
15 #include "llvm-c/Transforms/PassManagerBuilder.h"
16 #include "llvm/ADT/SmallVector.h"
17 #include "llvm/Analysis/BasicAliasAnalysis.h"
18 #include "llvm/Analysis/CFLAndersAliasAnalysis.h"
19 #include "llvm/Analysis/CFLSteensAliasAnalysis.h"
20 #include "llvm/Analysis/GlobalsModRef.h"
21 #include "llvm/Analysis/InlineCost.h"
22 #include "llvm/Analysis/Passes.h"
23 #include "llvm/Analysis/ScopedNoAliasAA.h"
24 #include "llvm/Analysis/TargetLibraryInfo.h"
25 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
26 #include "llvm/IR/DataLayout.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/Transforms/AggressiveInstCombine/AggressiveInstCombine.h"
32 #include "llvm/Transforms/IPO.h"
33 #include "llvm/Transforms/IPO/Attributor.h"
34 #include "llvm/Transforms/IPO/ForceFunctionAttrs.h"
35 #include "llvm/Transforms/IPO/FunctionAttrs.h"
36 #include "llvm/Transforms/IPO/InferFunctionAttrs.h"
37 #include "llvm/Transforms/InstCombine/InstCombine.h"
38 #include "llvm/Transforms/Instrumentation.h"
39 #include "llvm/Transforms/Scalar.h"
40 #include "llvm/Transforms/Scalar/GVN.h"
41 #include "llvm/Transforms/Scalar/InstSimplifyPass.h"
42 #include "llvm/Transforms/Scalar/LICM.h"
43 #include "llvm/Transforms/Scalar/LoopUnrollPass.h"
44 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h"
45 #include "llvm/Transforms/Utils.h"
46 #include "llvm/Transforms/Vectorize.h"
47 #include "llvm/Transforms/Vectorize/LoopVectorize.h"
48 #include "llvm/Transforms/Vectorize/SLPVectorizer.h"
49 
50 using namespace llvm;
51 
52 static cl::opt<bool>
53     RunPartialInlining("enable-partial-inlining", cl::init(false), cl::Hidden,
54                        cl::ZeroOrMore, cl::desc("Run Partial inlinining pass"));
55 
56 static cl::opt<bool>
57 UseGVNAfterVectorization("use-gvn-after-vectorization",
58   cl::init(false), cl::Hidden,
59   cl::desc("Run GVN instead of Early CSE after vectorization passes"));
60 
61 static cl::opt<bool> ExtraVectorizerPasses(
62     "extra-vectorizer-passes", cl::init(false), cl::Hidden,
63     cl::desc("Run cleanup optimization passes after vectorization."));
64 
65 static cl::opt<bool>
66 RunLoopRerolling("reroll-loops", cl::Hidden,
67                  cl::desc("Run the loop rerolling pass"));
68 
69 static cl::opt<bool> RunNewGVN("enable-newgvn", cl::init(false), cl::Hidden,
70                                cl::desc("Run the NewGVN pass"));
71 
72 // Experimental option to use CFL-AA
73 enum class CFLAAType { None, Steensgaard, Andersen, Both };
74 static cl::opt<CFLAAType>
75     UseCFLAA("use-cfl-aa", cl::init(CFLAAType::None), cl::Hidden,
76              cl::desc("Enable the new, experimental CFL alias analysis"),
77              cl::values(clEnumValN(CFLAAType::None, "none", "Disable CFL-AA"),
78                         clEnumValN(CFLAAType::Steensgaard, "steens",
79                                    "Enable unification-based CFL-AA"),
80                         clEnumValN(CFLAAType::Andersen, "anders",
81                                    "Enable inclusion-based CFL-AA"),
82                         clEnumValN(CFLAAType::Both, "both",
83                                    "Enable both variants of CFL-AA")));
84 
85 static cl::opt<bool> EnableLoopInterchange(
86     "enable-loopinterchange", cl::init(false), cl::Hidden,
87     cl::desc("Enable the new, experimental LoopInterchange Pass"));
88 
89 static cl::opt<bool> EnableUnrollAndJam("enable-unroll-and-jam",
90                                         cl::init(false), cl::Hidden,
91                                         cl::desc("Enable Unroll And Jam Pass"));
92 
93 static cl::opt<bool>
94     EnablePrepareForThinLTO("prepare-for-thinlto", cl::init(false), cl::Hidden,
95                             cl::desc("Enable preparation for ThinLTO."));
96 
97 static cl::opt<bool>
98     EnablePerformThinLTO("perform-thinlto", cl::init(false), cl::Hidden,
99                          cl::desc("Enable performing ThinLTO."));
100 
101 cl::opt<bool> EnableHotColdSplit("hot-cold-split", cl::init(false), cl::Hidden,
102     cl::desc("Enable hot-cold splitting pass"));
103 
104 static cl::opt<bool> UseLoopVersioningLICM(
105     "enable-loop-versioning-licm", cl::init(false), cl::Hidden,
106     cl::desc("Enable the experimental Loop Versioning LICM pass"));
107 
108 static cl::opt<bool>
109     DisablePreInliner("disable-preinline", cl::init(false), cl::Hidden,
110                       cl::desc("Disable pre-instrumentation inliner"));
111 
112 static cl::opt<int> PreInlineThreshold(
113     "preinline-threshold", cl::Hidden, cl::init(75), cl::ZeroOrMore,
114     cl::desc("Control the amount of inlining in pre-instrumentation inliner "
115              "(default = 75)"));
116 
117 static cl::opt<bool> EnableGVNHoist(
118     "enable-gvn-hoist", cl::init(false), cl::Hidden,
119     cl::desc("Enable the GVN hoisting pass (default = off)"));
120 
121 static cl::opt<bool>
122     DisableLibCallsShrinkWrap("disable-libcalls-shrinkwrap", cl::init(false),
123                               cl::Hidden,
124                               cl::desc("Disable shrink-wrap library calls"));
125 
126 static cl::opt<bool> EnableSimpleLoopUnswitch(
127     "enable-simple-loop-unswitch", cl::init(false), cl::Hidden,
128     cl::desc("Enable the simple loop unswitch pass. Also enables independent "
129              "cleanup passes integrated into the loop pass manager pipeline."));
130 
131 static cl::opt<bool> EnableGVNSink(
132     "enable-gvn-sink", cl::init(false), cl::Hidden,
133     cl::desc("Enable the GVN sinking pass (default = off)"));
134 
135 // This option is used in simplifying testing SampleFDO optimizations for
136 // profile loading.
137 static cl::opt<bool>
138     EnableCHR("enable-chr", cl::init(true), cl::Hidden,
139               cl::desc("Enable control height reduction optimization (CHR)"));
140 
141 cl::opt<bool> FlattenedProfileUsed(
142     "flattened-profile-used", cl::init(false), cl::Hidden,
143     cl::desc("Indicate the sample profile being used is flattened, i.e., "
144              "no inline hierachy exists in the profile. "));
145 
146 cl::opt<bool> EnableOrderFileInstrumentation(
147     "enable-order-file-instrumentation", cl::init(false), cl::Hidden,
148     cl::desc("Enable order file instrumentation (default = off)"));
149 
150 static cl::opt<bool>
151     EnableMatrix("enable-matrix", cl::init(false), cl::Hidden,
152                  cl::desc("Enable lowering of the matrix intrinsics"));
153 
154 PassManagerBuilder::PassManagerBuilder() {
155     OptLevel = 2;
156     SizeLevel = 0;
157     LibraryInfo = nullptr;
158     Inliner = nullptr;
159     DisableUnrollLoops = false;
160     SLPVectorize = RunSLPVectorization;
161     LoopVectorize = EnableLoopVectorization;
162     LoopsInterleaved = EnableLoopInterleaving;
163     RerollLoops = RunLoopRerolling;
164     NewGVN = RunNewGVN;
165     LicmMssaOptCap = SetLicmMssaOptCap;
166     LicmMssaNoAccForPromotionCap = SetLicmMssaNoAccForPromotionCap;
167     DisableGVNLoadPRE = false;
168     ForgetAllSCEVInLoopUnroll = ForgetSCEVInLoopUnroll;
169     VerifyInput = false;
170     VerifyOutput = false;
171     MergeFunctions = false;
172     PrepareForLTO = false;
173     EnablePGOInstrGen = false;
174     EnablePGOCSInstrGen = false;
175     EnablePGOCSInstrUse = false;
176     PGOInstrGen = "";
177     PGOInstrUse = "";
178     PGOSampleUse = "";
179     PrepareForThinLTO = EnablePrepareForThinLTO;
180     PerformThinLTO = EnablePerformThinLTO;
181     DivergentTarget = false;
182 }
183 
184 PassManagerBuilder::~PassManagerBuilder() {
185   delete LibraryInfo;
186   delete Inliner;
187 }
188 
189 /// Set of global extensions, automatically added as part of the standard set.
190 static ManagedStatic<SmallVector<std::pair<PassManagerBuilder::ExtensionPointTy,
191    PassManagerBuilder::ExtensionFn>, 8> > GlobalExtensions;
192 
193 /// Check if GlobalExtensions is constructed and not empty.
194 /// Since GlobalExtensions is a managed static, calling 'empty()' will trigger
195 /// the construction of the object.
196 static bool GlobalExtensionsNotEmpty() {
197   return GlobalExtensions.isConstructed() && !GlobalExtensions->empty();
198 }
199 
200 void PassManagerBuilder::addGlobalExtension(
201     PassManagerBuilder::ExtensionPointTy Ty,
202     PassManagerBuilder::ExtensionFn Fn) {
203   GlobalExtensions->push_back(std::make_pair(Ty, std::move(Fn)));
204 }
205 
206 void PassManagerBuilder::addExtension(ExtensionPointTy Ty, ExtensionFn Fn) {
207   Extensions.push_back(std::make_pair(Ty, std::move(Fn)));
208 }
209 
210 void PassManagerBuilder::addExtensionsToPM(ExtensionPointTy ETy,
211                                            legacy::PassManagerBase &PM) const {
212   if (GlobalExtensionsNotEmpty()) {
213     for (auto &Ext : *GlobalExtensions) {
214       if (Ext.first == ETy)
215         Ext.second(*this, PM);
216     }
217   }
218   for (unsigned i = 0, e = Extensions.size(); i != e; ++i)
219     if (Extensions[i].first == ETy)
220       Extensions[i].second(*this, PM);
221 }
222 
223 void PassManagerBuilder::addInitialAliasAnalysisPasses(
224     legacy::PassManagerBase &PM) const {
225   switch (UseCFLAA) {
226   case CFLAAType::Steensgaard:
227     PM.add(createCFLSteensAAWrapperPass());
228     break;
229   case CFLAAType::Andersen:
230     PM.add(createCFLAndersAAWrapperPass());
231     break;
232   case CFLAAType::Both:
233     PM.add(createCFLSteensAAWrapperPass());
234     PM.add(createCFLAndersAAWrapperPass());
235     break;
236   default:
237     break;
238   }
239 
240   // Add TypeBasedAliasAnalysis before BasicAliasAnalysis so that
241   // BasicAliasAnalysis wins if they disagree. This is intended to help
242   // support "obvious" type-punning idioms.
243   PM.add(createTypeBasedAAWrapperPass());
244   PM.add(createScopedNoAliasAAWrapperPass());
245 }
246 
247 void PassManagerBuilder::addInstructionCombiningPass(
248     legacy::PassManagerBase &PM) const {
249   bool ExpensiveCombines = OptLevel > 2;
250   PM.add(createInstructionCombiningPass(ExpensiveCombines));
251 }
252 
253 void PassManagerBuilder::populateFunctionPassManager(
254     legacy::FunctionPassManager &FPM) {
255   addExtensionsToPM(EP_EarlyAsPossible, FPM);
256   FPM.add(createEntryExitInstrumenterPass());
257 
258   // Add LibraryInfo if we have some.
259   if (LibraryInfo)
260     FPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
261 
262   if (OptLevel == 0) return;
263 
264   addInitialAliasAnalysisPasses(FPM);
265 
266   FPM.add(createCFGSimplificationPass());
267   FPM.add(createSROAPass());
268   FPM.add(createEarlyCSEPass());
269   FPM.add(createLowerExpectIntrinsicPass());
270 }
271 
272 // Do PGO instrumentation generation or use pass as the option specified.
273 void PassManagerBuilder::addPGOInstrPasses(legacy::PassManagerBase &MPM,
274                                            bool IsCS = false) {
275   if (IsCS) {
276     if (!EnablePGOCSInstrGen && !EnablePGOCSInstrUse)
277       return;
278   } else if (!EnablePGOInstrGen && PGOInstrUse.empty() && PGOSampleUse.empty())
279     return;
280 
281   // Perform the preinline and cleanup passes for O1 and above.
282   // And avoid doing them if optimizing for size.
283   // We will not do this inline for context sensitive PGO (when IsCS is true).
284   if (OptLevel > 0 && SizeLevel == 0 && !DisablePreInliner &&
285       PGOSampleUse.empty() && !IsCS) {
286     // Create preinline pass. We construct an InlineParams object and specify
287     // the threshold here to avoid the command line options of the regular
288     // inliner to influence pre-inlining. The only fields of InlineParams we
289     // care about are DefaultThreshold and HintThreshold.
290     InlineParams IP;
291     IP.DefaultThreshold = PreInlineThreshold;
292     // FIXME: The hint threshold has the same value used by the regular inliner.
293     // This should probably be lowered after performance testing.
294     IP.HintThreshold = 325;
295 
296     MPM.add(createFunctionInliningPass(IP));
297     MPM.add(createSROAPass());
298     MPM.add(createEarlyCSEPass());             // Catch trivial redundancies
299     MPM.add(createCFGSimplificationPass());    // Merge & remove BBs
300     MPM.add(createInstructionCombiningPass()); // Combine silly seq's
301     addExtensionsToPM(EP_Peephole, MPM);
302   }
303   if ((EnablePGOInstrGen && !IsCS) || (EnablePGOCSInstrGen && IsCS)) {
304     MPM.add(createPGOInstrumentationGenLegacyPass(IsCS));
305     // Add the profile lowering pass.
306     InstrProfOptions Options;
307     if (!PGOInstrGen.empty())
308       Options.InstrProfileOutput = PGOInstrGen;
309     Options.DoCounterPromotion = true;
310     Options.UseBFIInPromotion = IsCS;
311     MPM.add(createLoopRotatePass());
312     MPM.add(createInstrProfilingLegacyPass(Options, IsCS));
313   }
314   if (!PGOInstrUse.empty())
315     MPM.add(createPGOInstrumentationUseLegacyPass(PGOInstrUse, IsCS));
316   // Indirect call promotion that promotes intra-module targets only.
317   // For ThinLTO this is done earlier due to interactions with globalopt
318   // for imported functions. We don't run this at -O0.
319   if (OptLevel > 0 && !IsCS)
320     MPM.add(
321         createPGOIndirectCallPromotionLegacyPass(false, !PGOSampleUse.empty()));
322 }
323 void PassManagerBuilder::addFunctionSimplificationPasses(
324     legacy::PassManagerBase &MPM) {
325   // Start of function pass.
326   // Break up aggregate allocas, using SSAUpdater.
327   assert(OptLevel >= 1 && "Calling function optimizer with no optimization level!");
328   MPM.add(createSROAPass());
329   MPM.add(createEarlyCSEPass(true /* Enable mem-ssa. */)); // Catch trivial redundancies
330 
331   if (OptLevel > 1) {
332     if (EnableGVNHoist)
333       MPM.add(createGVNHoistPass());
334     if (EnableGVNSink) {
335       MPM.add(createGVNSinkPass());
336       MPM.add(createCFGSimplificationPass());
337     }
338   }
339 
340   if (OptLevel > 1) {
341     // Speculative execution if the target has divergent branches; otherwise nop.
342     MPM.add(createSpeculativeExecutionIfHasBranchDivergencePass());
343 
344     MPM.add(createJumpThreadingPass());         // Thread jumps.
345     MPM.add(createCorrelatedValuePropagationPass()); // Propagate conditionals
346   }
347   MPM.add(createCFGSimplificationPass());     // Merge & remove BBs
348   // Combine silly seq's
349   if (OptLevel > 2)
350     MPM.add(createAggressiveInstCombinerPass());
351   addInstructionCombiningPass(MPM);
352   if (SizeLevel == 0 && !DisableLibCallsShrinkWrap)
353     MPM.add(createLibCallsShrinkWrapPass());
354   addExtensionsToPM(EP_Peephole, MPM);
355 
356   // Optimize memory intrinsic calls based on the profiled size information.
357   if (SizeLevel == 0)
358     MPM.add(createPGOMemOPSizeOptLegacyPass());
359 
360   // TODO: Investigate the cost/benefit of tail call elimination on debugging.
361   if (OptLevel > 1)
362     MPM.add(createTailCallEliminationPass()); // Eliminate tail calls
363   MPM.add(createCFGSimplificationPass());      // Merge & remove BBs
364   MPM.add(createReassociatePass());           // Reassociate expressions
365 
366   // Begin the loop pass pipeline.
367   if (EnableSimpleLoopUnswitch) {
368     // The simple loop unswitch pass relies on separate cleanup passes. Schedule
369     // them first so when we re-process a loop they run before other loop
370     // passes.
371     MPM.add(createLoopInstSimplifyPass());
372     MPM.add(createLoopSimplifyCFGPass());
373   }
374   // Rotate Loop - disable header duplication at -Oz
375   MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
376   // TODO: Investigate promotion cap for O1.
377   MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap));
378   if (EnableSimpleLoopUnswitch)
379     MPM.add(createSimpleLoopUnswitchLegacyPass());
380   else
381     MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3, DivergentTarget));
382   // FIXME: We break the loop pass pipeline here in order to do full
383   // simplify-cfg. Eventually loop-simplifycfg should be enhanced to replace the
384   // need for this.
385   MPM.add(createCFGSimplificationPass());
386   addInstructionCombiningPass(MPM);
387   // We resume loop passes creating a second loop pipeline here.
388   MPM.add(createIndVarSimplifyPass());        // Canonicalize indvars
389   MPM.add(createLoopIdiomPass());             // Recognize idioms like memset.
390   addExtensionsToPM(EP_LateLoopOptimizations, MPM);
391   MPM.add(createLoopDeletionPass());          // Delete dead loops
392 
393   if (EnableLoopInterchange)
394     MPM.add(createLoopInterchangePass()); // Interchange loops
395 
396   // Unroll small loops
397   MPM.add(createSimpleLoopUnrollPass(OptLevel, DisableUnrollLoops,
398                                      ForgetAllSCEVInLoopUnroll));
399   addExtensionsToPM(EP_LoopOptimizerEnd, MPM);
400   // This ends the loop pass pipelines.
401 
402   if (OptLevel > 1) {
403     MPM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds
404     MPM.add(NewGVN ? createNewGVNPass()
405                    : createGVNPass(DisableGVNLoadPRE)); // Remove redundancies
406   }
407   MPM.add(createMemCpyOptPass());             // Remove memcpy / form memset
408   MPM.add(createSCCPPass());                  // Constant prop with SCCP
409 
410   // Delete dead bit computations (instcombine runs after to fold away the dead
411   // computations, and then ADCE will run later to exploit any new DCE
412   // opportunities that creates).
413   MPM.add(createBitTrackingDCEPass());        // Delete dead bit computations
414 
415   // Run instcombine after redundancy elimination to exploit opportunities
416   // opened up by them.
417   addInstructionCombiningPass(MPM);
418   addExtensionsToPM(EP_Peephole, MPM);
419   if (OptLevel > 1) {
420     MPM.add(createJumpThreadingPass());         // Thread jumps
421     MPM.add(createCorrelatedValuePropagationPass());
422     MPM.add(createDeadStoreEliminationPass());  // Delete dead stores
423     MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap));
424   }
425 
426   addExtensionsToPM(EP_ScalarOptimizerLate, MPM);
427 
428   if (RerollLoops)
429     MPM.add(createLoopRerollPass());
430 
431   // TODO: Investigate if this is too expensive at O1.
432   MPM.add(createAggressiveDCEPass());         // Delete dead instructions
433   MPM.add(createCFGSimplificationPass()); // Merge & remove BBs
434   // Clean up after everything.
435   addInstructionCombiningPass(MPM);
436   addExtensionsToPM(EP_Peephole, MPM);
437 
438   if (EnableCHR && OptLevel >= 3 &&
439       (!PGOInstrUse.empty() || !PGOSampleUse.empty() || EnablePGOCSInstrGen))
440     MPM.add(createControlHeightReductionLegacyPass());
441 }
442 
443 void PassManagerBuilder::populateModulePassManager(
444     legacy::PassManagerBase &MPM) {
445   // Whether this is a default or *LTO pre-link pipeline. The FullLTO post-link
446   // is handled separately, so just check this is not the ThinLTO post-link.
447   bool DefaultOrPreLinkPipeline = !PerformThinLTO;
448 
449   if (!PGOSampleUse.empty()) {
450     MPM.add(createPruneEHPass());
451     // In ThinLTO mode, when flattened profile is used, all the available
452     // profile information will be annotated in PreLink phase so there is
453     // no need to load the profile again in PostLink.
454     if (!(FlattenedProfileUsed && PerformThinLTO))
455       MPM.add(createSampleProfileLoaderPass(PGOSampleUse));
456   }
457 
458   // Allow forcing function attributes as a debugging and tuning aid.
459   MPM.add(createForceFunctionAttrsLegacyPass());
460 
461   // If all optimizations are disabled, just run the always-inline pass and,
462   // if enabled, the function merging pass.
463   if (OptLevel == 0) {
464     addPGOInstrPasses(MPM);
465     if (Inliner) {
466       MPM.add(Inliner);
467       Inliner = nullptr;
468     }
469 
470     // FIXME: The BarrierNoopPass is a HACK! The inliner pass above implicitly
471     // creates a CGSCC pass manager, but we don't want to add extensions into
472     // that pass manager. To prevent this we insert a no-op module pass to reset
473     // the pass manager to get the same behavior as EP_OptimizerLast in non-O0
474     // builds. The function merging pass is
475     if (MergeFunctions)
476       MPM.add(createMergeFunctionsPass());
477     else if (GlobalExtensionsNotEmpty() || !Extensions.empty())
478       MPM.add(createBarrierNoopPass());
479 
480     if (PerformThinLTO) {
481       // Drop available_externally and unreferenced globals. This is necessary
482       // with ThinLTO in order to avoid leaving undefined references to dead
483       // globals in the object file.
484       MPM.add(createEliminateAvailableExternallyPass());
485       MPM.add(createGlobalDCEPass());
486     }
487 
488     addExtensionsToPM(EP_EnabledOnOptLevel0, MPM);
489 
490     if (PrepareForLTO || PrepareForThinLTO) {
491       MPM.add(createCanonicalizeAliasesPass());
492       // Rename anon globals to be able to export them in the summary.
493       // This has to be done after we add the extensions to the pass manager
494       // as there could be passes (e.g. Adddress sanitizer) which introduce
495       // new unnamed globals.
496       MPM.add(createNameAnonGlobalPass());
497     }
498     return;
499   }
500 
501   // Add LibraryInfo if we have some.
502   if (LibraryInfo)
503     MPM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
504 
505   addInitialAliasAnalysisPasses(MPM);
506 
507   // For ThinLTO there are two passes of indirect call promotion. The
508   // first is during the compile phase when PerformThinLTO=false and
509   // intra-module indirect call targets are promoted. The second is during
510   // the ThinLTO backend when PerformThinLTO=true, when we promote imported
511   // inter-module indirect calls. For that we perform indirect call promotion
512   // earlier in the pass pipeline, here before globalopt. Otherwise imported
513   // available_externally functions look unreferenced and are removed.
514   if (PerformThinLTO)
515     MPM.add(createPGOIndirectCallPromotionLegacyPass(/*InLTO = */ true,
516                                                      !PGOSampleUse.empty()));
517 
518   // For SamplePGO in ThinLTO compile phase, we do not want to unroll loops
519   // as it will change the CFG too much to make the 2nd profile annotation
520   // in backend more difficult.
521   bool PrepareForThinLTOUsingPGOSampleProfile =
522       PrepareForThinLTO && !PGOSampleUse.empty();
523   if (PrepareForThinLTOUsingPGOSampleProfile)
524     DisableUnrollLoops = true;
525 
526   // Infer attributes about declarations if possible.
527   MPM.add(createInferFunctionAttrsLegacyPass());
528 
529   addExtensionsToPM(EP_ModuleOptimizerEarly, MPM);
530 
531   if (OptLevel > 2)
532     MPM.add(createCallSiteSplittingPass());
533 
534   MPM.add(createIPSCCPPass());          // IP SCCP
535   MPM.add(createCalledValuePropagationPass());
536 
537   // Infer attributes on declarations, call sites, arguments, etc.
538   MPM.add(createAttributorLegacyPass());
539 
540   MPM.add(createGlobalOptimizerPass()); // Optimize out global vars
541   // Promote any localized global vars.
542   MPM.add(createPromoteMemoryToRegisterPass());
543 
544   MPM.add(createDeadArgEliminationPass()); // Dead argument elimination
545 
546   addInstructionCombiningPass(MPM); // Clean up after IPCP & DAE
547   addExtensionsToPM(EP_Peephole, MPM);
548   MPM.add(createCFGSimplificationPass()); // Clean up after IPCP & DAE
549 
550   // For SamplePGO in ThinLTO compile phase, we do not want to do indirect
551   // call promotion as it will change the CFG too much to make the 2nd
552   // profile annotation in backend more difficult.
553   // PGO instrumentation is added during the compile phase for ThinLTO, do
554   // not run it a second time
555   if (DefaultOrPreLinkPipeline && !PrepareForThinLTOUsingPGOSampleProfile)
556     addPGOInstrPasses(MPM);
557 
558   // Create profile COMDAT variables. Lld linker wants to see all variables
559   // before the LTO/ThinLTO link since it needs to resolve symbols/comdats.
560   if (!PerformThinLTO && EnablePGOCSInstrGen)
561     MPM.add(createPGOInstrumentationGenCreateVarLegacyPass(PGOInstrGen));
562 
563   // We add a module alias analysis pass here. In part due to bugs in the
564   // analysis infrastructure this "works" in that the analysis stays alive
565   // for the entire SCC pass run below.
566   MPM.add(createGlobalsAAWrapperPass());
567 
568   // Start of CallGraph SCC passes.
569   MPM.add(createPruneEHPass()); // Remove dead EH info
570   bool RunInliner = false;
571   if (Inliner) {
572     MPM.add(Inliner);
573     Inliner = nullptr;
574     RunInliner = true;
575   }
576 
577   MPM.add(createPostOrderFunctionAttrsLegacyPass());
578   if (OptLevel > 2)
579     MPM.add(createArgumentPromotionPass()); // Scalarize uninlined fn args
580 
581   addExtensionsToPM(EP_CGSCCOptimizerLate, MPM);
582   addFunctionSimplificationPasses(MPM);
583 
584   // FIXME: This is a HACK! The inliner pass above implicitly creates a CGSCC
585   // pass manager that we are specifically trying to avoid. To prevent this
586   // we must insert a no-op module pass to reset the pass manager.
587   MPM.add(createBarrierNoopPass());
588 
589   if (RunPartialInlining)
590     MPM.add(createPartialInliningPass());
591 
592   if (OptLevel > 1 && !PrepareForLTO && !PrepareForThinLTO)
593     // Remove avail extern fns and globals definitions if we aren't
594     // compiling an object file for later LTO. For LTO we want to preserve
595     // these so they are eligible for inlining at link-time. Note if they
596     // are unreferenced they will be removed by GlobalDCE later, so
597     // this only impacts referenced available externally globals.
598     // Eventually they will be suppressed during codegen, but eliminating
599     // here enables more opportunity for GlobalDCE as it may make
600     // globals referenced by available external functions dead
601     // and saves running remaining passes on the eliminated functions.
602     MPM.add(createEliminateAvailableExternallyPass());
603 
604   // CSFDO instrumentation and use pass. Don't invoke this for Prepare pass
605   // for LTO and ThinLTO -- The actual pass will be called after all inlines
606   // are performed.
607   // Need to do this after COMDAT variables have been eliminated,
608   // (i.e. after EliminateAvailableExternallyPass).
609   if (!(PrepareForLTO || PrepareForThinLTO))
610     addPGOInstrPasses(MPM, /* IsCS */ true);
611 
612   if (EnableOrderFileInstrumentation)
613     MPM.add(createInstrOrderFilePass());
614 
615   MPM.add(createReversePostOrderFunctionAttrsPass());
616 
617   // The inliner performs some kind of dead code elimination as it goes,
618   // but there are cases that are not really caught by it. We might
619   // at some point consider teaching the inliner about them, but it
620   // is OK for now to run GlobalOpt + GlobalDCE in tandem as their
621   // benefits generally outweight the cost, making the whole pipeline
622   // faster.
623   if (RunInliner) {
624     MPM.add(createGlobalOptimizerPass());
625     MPM.add(createGlobalDCEPass());
626   }
627 
628   // If we are planning to perform ThinLTO later, let's not bloat the code with
629   // unrolling/vectorization/... now. We'll first run the inliner + CGSCC passes
630   // during ThinLTO and perform the rest of the optimizations afterward.
631   if (PrepareForThinLTO) {
632     // Ensure we perform any last passes, but do so before renaming anonymous
633     // globals in case the passes add any.
634     addExtensionsToPM(EP_OptimizerLast, MPM);
635     MPM.add(createCanonicalizeAliasesPass());
636     // Rename anon globals to be able to export them in the summary.
637     MPM.add(createNameAnonGlobalPass());
638     return;
639   }
640 
641   if (PerformThinLTO)
642     // Optimize globals now when performing ThinLTO, this enables more
643     // optimizations later.
644     MPM.add(createGlobalOptimizerPass());
645 
646   // Scheduling LoopVersioningLICM when inlining is over, because after that
647   // we may see more accurate aliasing. Reason to run this late is that too
648   // early versioning may prevent further inlining due to increase of code
649   // size. By placing it just after inlining other optimizations which runs
650   // later might get benefit of no-alias assumption in clone loop.
651   if (UseLoopVersioningLICM) {
652     MPM.add(createLoopVersioningLICMPass());    // Do LoopVersioningLICM
653     MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap));
654   }
655 
656   // We add a fresh GlobalsModRef run at this point. This is particularly
657   // useful as the above will have inlined, DCE'ed, and function-attr
658   // propagated everything. We should at this point have a reasonably minimal
659   // and richly annotated call graph. By computing aliasing and mod/ref
660   // information for all local globals here, the late loop passes and notably
661   // the vectorizer will be able to use them to help recognize vectorizable
662   // memory operations.
663   //
664   // Note that this relies on a bug in the pass manager which preserves
665   // a module analysis into a function pass pipeline (and throughout it) so
666   // long as the first function pass doesn't invalidate the module analysis.
667   // Thus both Float2Int and LoopRotate have to preserve AliasAnalysis for
668   // this to work. Fortunately, it is trivial to preserve AliasAnalysis
669   // (doing nothing preserves it as it is required to be conservatively
670   // correct in the face of IR changes).
671   MPM.add(createGlobalsAAWrapperPass());
672 
673   MPM.add(createFloat2IntPass());
674   MPM.add(createLowerConstantIntrinsicsPass());
675 
676   if (EnableMatrix) {
677     MPM.add(createLowerMatrixIntrinsicsPass());
678     // CSE the pointer arithmetic of the column vectors.  This allows alias
679     // analysis to establish no-aliasing between loads and stores of different
680     // columns of the same matrix.
681     MPM.add(createEarlyCSEPass(false));
682   }
683 
684   addExtensionsToPM(EP_VectorizerStart, MPM);
685 
686   // Re-rotate loops in all our loop nests. These may have fallout out of
687   // rotated form due to GVN or other transformations, and the vectorizer relies
688   // on the rotated form. Disable header duplication at -Oz.
689   MPM.add(createLoopRotatePass(SizeLevel == 2 ? 0 : -1));
690 
691   // Distribute loops to allow partial vectorization.  I.e. isolate dependences
692   // into separate loop that would otherwise inhibit vectorization.  This is
693   // currently only performed for loops marked with the metadata
694   // llvm.loop.distribute=true or when -enable-loop-distribute is specified.
695   MPM.add(createLoopDistributePass());
696 
697   MPM.add(createLoopVectorizePass(!LoopsInterleaved, !LoopVectorize));
698 
699   // Eliminate loads by forwarding stores from the previous iteration to loads
700   // of the current iteration.
701   MPM.add(createLoopLoadEliminationPass());
702 
703   // FIXME: Because of #pragma vectorize enable, the passes below are always
704   // inserted in the pipeline, even when the vectorizer doesn't run (ex. when
705   // on -O1 and no #pragma is found). Would be good to have these two passes
706   // as function calls, so that we can only pass them when the vectorizer
707   // changed the code.
708   addInstructionCombiningPass(MPM);
709   if (OptLevel > 1 && ExtraVectorizerPasses) {
710     // At higher optimization levels, try to clean up any runtime overlap and
711     // alignment checks inserted by the vectorizer. We want to track correllated
712     // runtime checks for two inner loops in the same outer loop, fold any
713     // common computations, hoist loop-invariant aspects out of any outer loop,
714     // and unswitch the runtime checks if possible. Once hoisted, we may have
715     // dead (or speculatable) control flows or more combining opportunities.
716     MPM.add(createEarlyCSEPass());
717     MPM.add(createCorrelatedValuePropagationPass());
718     addInstructionCombiningPass(MPM);
719     MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap));
720     MPM.add(createLoopUnswitchPass(SizeLevel || OptLevel < 3, DivergentTarget));
721     MPM.add(createCFGSimplificationPass());
722     addInstructionCombiningPass(MPM);
723   }
724 
725   // Cleanup after loop vectorization, etc. Simplification passes like CVP and
726   // GVN, loop transforms, and others have already run, so it's now better to
727   // convert to more optimized IR using more aggressive simplify CFG options.
728   // The extra sinking transform can create larger basic blocks, so do this
729   // before SLP vectorization.
730   MPM.add(createCFGSimplificationPass(1, true, true, false, true));
731 
732   if (SLPVectorize) {
733     MPM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
734     if (OptLevel > 1 && ExtraVectorizerPasses) {
735       MPM.add(createEarlyCSEPass());
736     }
737   }
738 
739   addExtensionsToPM(EP_Peephole, MPM);
740   addInstructionCombiningPass(MPM);
741 
742   if (EnableUnrollAndJam && !DisableUnrollLoops) {
743     // Unroll and Jam. We do this before unroll but need to be in a separate
744     // loop pass manager in order for the outer loop to be processed by
745     // unroll and jam before the inner loop is unrolled.
746     MPM.add(createLoopUnrollAndJamPass(OptLevel));
747   }
748 
749   // Unroll small loops
750   MPM.add(createLoopUnrollPass(OptLevel, DisableUnrollLoops,
751                                ForgetAllSCEVInLoopUnroll));
752 
753   if (!DisableUnrollLoops) {
754     // LoopUnroll may generate some redundency to cleanup.
755     addInstructionCombiningPass(MPM);
756 
757     // Runtime unrolling will introduce runtime check in loop prologue. If the
758     // unrolled loop is a inner loop, then the prologue will be inside the
759     // outer loop. LICM pass can help to promote the runtime check out if the
760     // checked value is loop invariant.
761     MPM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap));
762   }
763 
764   MPM.add(createWarnMissedTransformationsPass());
765 
766   // After vectorization and unrolling, assume intrinsics may tell us more
767   // about pointer alignments.
768   MPM.add(createAlignmentFromAssumptionsPass());
769 
770   // FIXME: We shouldn't bother with this anymore.
771   MPM.add(createStripDeadPrototypesPass()); // Get rid of dead prototypes
772 
773   // GlobalOpt already deletes dead functions and globals, at -O2 try a
774   // late pass of GlobalDCE.  It is capable of deleting dead cycles.
775   if (OptLevel > 1) {
776     MPM.add(createGlobalDCEPass());         // Remove dead fns and globals.
777     MPM.add(createConstantMergePass());     // Merge dup global constants
778   }
779 
780   // See comment in the new PM for justification of scheduling splitting at
781   // this stage (\ref buildModuleSimplificationPipeline).
782   if (EnableHotColdSplit && !(PrepareForLTO || PrepareForThinLTO))
783     MPM.add(createHotColdSplittingPass());
784 
785   if (MergeFunctions)
786     MPM.add(createMergeFunctionsPass());
787 
788   // LoopSink pass sinks instructions hoisted by LICM, which serves as a
789   // canonicalization pass that enables other optimizations. As a result,
790   // LoopSink pass needs to be a very late IR pass to avoid undoing LICM
791   // result too early.
792   MPM.add(createLoopSinkPass());
793   // Get rid of LCSSA nodes.
794   MPM.add(createInstSimplifyLegacyPass());
795 
796   // This hoists/decomposes div/rem ops. It should run after other sink/hoist
797   // passes to avoid re-sinking, but before SimplifyCFG because it can allow
798   // flattening of blocks.
799   MPM.add(createDivRemPairsPass());
800 
801   // LoopSink (and other loop passes since the last simplifyCFG) might have
802   // resulted in single-entry-single-exit or empty blocks. Clean up the CFG.
803   MPM.add(createCFGSimplificationPass());
804 
805   addExtensionsToPM(EP_OptimizerLast, MPM);
806 
807   if (PrepareForLTO) {
808     MPM.add(createCanonicalizeAliasesPass());
809     // Rename anon globals to be able to handle them in the summary
810     MPM.add(createNameAnonGlobalPass());
811   }
812 }
813 
814 void PassManagerBuilder::addLTOOptimizationPasses(legacy::PassManagerBase &PM) {
815   // Load sample profile before running the LTO optimization pipeline.
816   if (!PGOSampleUse.empty()) {
817     PM.add(createPruneEHPass());
818     PM.add(createSampleProfileLoaderPass(PGOSampleUse));
819   }
820 
821   // Remove unused virtual tables to improve the quality of code generated by
822   // whole-program devirtualization and bitset lowering.
823   PM.add(createGlobalDCEPass());
824 
825   // Provide AliasAnalysis services for optimizations.
826   addInitialAliasAnalysisPasses(PM);
827 
828   // Allow forcing function attributes as a debugging and tuning aid.
829   PM.add(createForceFunctionAttrsLegacyPass());
830 
831   // Infer attributes about declarations if possible.
832   PM.add(createInferFunctionAttrsLegacyPass());
833 
834   if (OptLevel > 1) {
835     // Split call-site with more constrained arguments.
836     PM.add(createCallSiteSplittingPass());
837 
838     // Indirect call promotion. This should promote all the targets that are
839     // left by the earlier promotion pass that promotes intra-module targets.
840     // This two-step promotion is to save the compile time. For LTO, it should
841     // produce the same result as if we only do promotion here.
842     PM.add(
843         createPGOIndirectCallPromotionLegacyPass(true, !PGOSampleUse.empty()));
844 
845     // Propagate constants at call sites into the functions they call.  This
846     // opens opportunities for globalopt (and inlining) by substituting function
847     // pointers passed as arguments to direct uses of functions.
848     PM.add(createIPSCCPPass());
849 
850     // Attach metadata to indirect call sites indicating the set of functions
851     // they may target at run-time. This should follow IPSCCP.
852     PM.add(createCalledValuePropagationPass());
853 
854     // Infer attributes on declarations, call sites, arguments, etc.
855     PM.add(createAttributorLegacyPass());
856   }
857 
858   // Infer attributes about definitions. The readnone attribute in particular is
859   // required for virtual constant propagation.
860   PM.add(createPostOrderFunctionAttrsLegacyPass());
861   PM.add(createReversePostOrderFunctionAttrsPass());
862 
863   // Split globals using inrange annotations on GEP indices. This can help
864   // improve the quality of generated code when virtual constant propagation or
865   // control flow integrity are enabled.
866   PM.add(createGlobalSplitPass());
867 
868   // Apply whole-program devirtualization and virtual constant propagation.
869   PM.add(createWholeProgramDevirtPass(ExportSummary, nullptr));
870 
871   // That's all we need at opt level 1.
872   if (OptLevel == 1)
873     return;
874 
875   // Now that we internalized some globals, see if we can hack on them!
876   PM.add(createGlobalOptimizerPass());
877   // Promote any localized global vars.
878   PM.add(createPromoteMemoryToRegisterPass());
879 
880   // Linking modules together can lead to duplicated global constants, only
881   // keep one copy of each constant.
882   PM.add(createConstantMergePass());
883 
884   // Remove unused arguments from functions.
885   PM.add(createDeadArgEliminationPass());
886 
887   // Reduce the code after globalopt and ipsccp.  Both can open up significant
888   // simplification opportunities, and both can propagate functions through
889   // function pointers.  When this happens, we often have to resolve varargs
890   // calls, etc, so let instcombine do this.
891   if (OptLevel > 2)
892     PM.add(createAggressiveInstCombinerPass());
893   addInstructionCombiningPass(PM);
894   addExtensionsToPM(EP_Peephole, PM);
895 
896   // Inline small functions
897   bool RunInliner = Inliner;
898   if (RunInliner) {
899     PM.add(Inliner);
900     Inliner = nullptr;
901   }
902 
903   PM.add(createPruneEHPass());   // Remove dead EH info.
904 
905   // CSFDO instrumentation and use pass.
906   addPGOInstrPasses(PM, /* IsCS */ true);
907 
908   // Optimize globals again if we ran the inliner.
909   if (RunInliner)
910     PM.add(createGlobalOptimizerPass());
911   PM.add(createGlobalDCEPass()); // Remove dead functions.
912 
913   // If we didn't decide to inline a function, check to see if we can
914   // transform it to pass arguments by value instead of by reference.
915   PM.add(createArgumentPromotionPass());
916 
917   // The IPO passes may leave cruft around.  Clean up after them.
918   addInstructionCombiningPass(PM);
919   addExtensionsToPM(EP_Peephole, PM);
920   PM.add(createJumpThreadingPass());
921 
922   // Break up allocas
923   PM.add(createSROAPass());
924 
925   // LTO provides additional opportunities for tailcall elimination due to
926   // link-time inlining, and visibility of nocapture attribute.
927   if (OptLevel > 1)
928     PM.add(createTailCallEliminationPass());
929 
930   // Infer attributes on declarations, call sites, arguments, etc.
931   PM.add(createPostOrderFunctionAttrsLegacyPass()); // Add nocapture.
932   // Run a few AA driven optimizations here and now, to cleanup the code.
933   PM.add(createGlobalsAAWrapperPass()); // IP alias analysis.
934 
935   PM.add(createLICMPass(LicmMssaOptCap, LicmMssaNoAccForPromotionCap));
936   PM.add(createMergedLoadStoreMotionPass()); // Merge ld/st in diamonds.
937   PM.add(NewGVN ? createNewGVNPass()
938                 : createGVNPass(DisableGVNLoadPRE)); // Remove redundancies.
939   PM.add(createMemCpyOptPass());            // Remove dead memcpys.
940 
941   // Nuke dead stores.
942   PM.add(createDeadStoreEliminationPass());
943 
944   // More loops are countable; try to optimize them.
945   PM.add(createIndVarSimplifyPass());
946   PM.add(createLoopDeletionPass());
947   if (EnableLoopInterchange)
948     PM.add(createLoopInterchangePass());
949 
950   // Unroll small loops
951   PM.add(createSimpleLoopUnrollPass(OptLevel, DisableUnrollLoops,
952                                     ForgetAllSCEVInLoopUnroll));
953   PM.add(createLoopVectorizePass(true, !LoopVectorize));
954   // The vectorizer may have significantly shortened a loop body; unroll again.
955   PM.add(createLoopUnrollPass(OptLevel, DisableUnrollLoops,
956                               ForgetAllSCEVInLoopUnroll));
957 
958   PM.add(createWarnMissedTransformationsPass());
959 
960   // Now that we've optimized loops (in particular loop induction variables),
961   // we may have exposed more scalar opportunities. Run parts of the scalar
962   // optimizer again at this point.
963   addInstructionCombiningPass(PM); // Initial cleanup
964   PM.add(createCFGSimplificationPass()); // if-convert
965   PM.add(createSCCPPass()); // Propagate exposed constants
966   addInstructionCombiningPass(PM); // Clean up again
967   PM.add(createBitTrackingDCEPass());
968 
969   // More scalar chains could be vectorized due to more alias information
970   if (SLPVectorize)
971     PM.add(createSLPVectorizerPass()); // Vectorize parallel scalar chains.
972 
973   // After vectorization, assume intrinsics may tell us more about pointer
974   // alignments.
975   PM.add(createAlignmentFromAssumptionsPass());
976 
977   // Cleanup and simplify the code after the scalar optimizations.
978   addInstructionCombiningPass(PM);
979   addExtensionsToPM(EP_Peephole, PM);
980 
981   PM.add(createJumpThreadingPass());
982 }
983 
984 void PassManagerBuilder::addLateLTOOptimizationPasses(
985     legacy::PassManagerBase &PM) {
986   // See comment in the new PM for justification of scheduling splitting at
987   // this stage (\ref buildLTODefaultPipeline).
988   if (EnableHotColdSplit)
989     PM.add(createHotColdSplittingPass());
990 
991   // Delete basic blocks, which optimization passes may have killed.
992   PM.add(createCFGSimplificationPass());
993 
994   // Drop bodies of available externally objects to improve GlobalDCE.
995   PM.add(createEliminateAvailableExternallyPass());
996 
997   // Now that we have optimized the program, discard unreachable functions.
998   PM.add(createGlobalDCEPass());
999 
1000   // FIXME: this is profitable (for compiler time) to do at -O0 too, but
1001   // currently it damages debug info.
1002   if (MergeFunctions)
1003     PM.add(createMergeFunctionsPass());
1004 }
1005 
1006 void PassManagerBuilder::populateThinLTOPassManager(
1007     legacy::PassManagerBase &PM) {
1008   PerformThinLTO = true;
1009   if (LibraryInfo)
1010     PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
1011 
1012   if (VerifyInput)
1013     PM.add(createVerifierPass());
1014 
1015   if (ImportSummary) {
1016     // These passes import type identifier resolutions for whole-program
1017     // devirtualization and CFI. They must run early because other passes may
1018     // disturb the specific instruction patterns that these passes look for,
1019     // creating dependencies on resolutions that may not appear in the summary.
1020     //
1021     // For example, GVN may transform the pattern assume(type.test) appearing in
1022     // two basic blocks into assume(phi(type.test, type.test)), which would
1023     // transform a dependency on a WPD resolution into a dependency on a type
1024     // identifier resolution for CFI.
1025     //
1026     // Also, WPD has access to more precise information than ICP and can
1027     // devirtualize more effectively, so it should operate on the IR first.
1028     PM.add(createWholeProgramDevirtPass(nullptr, ImportSummary));
1029     PM.add(createLowerTypeTestsPass(nullptr, ImportSummary));
1030   }
1031 
1032   populateModulePassManager(PM);
1033 
1034   if (VerifyOutput)
1035     PM.add(createVerifierPass());
1036   PerformThinLTO = false;
1037 }
1038 
1039 void PassManagerBuilder::populateLTOPassManager(legacy::PassManagerBase &PM) {
1040   if (LibraryInfo)
1041     PM.add(new TargetLibraryInfoWrapperPass(*LibraryInfo));
1042 
1043   if (VerifyInput)
1044     PM.add(createVerifierPass());
1045 
1046   addExtensionsToPM(EP_FullLinkTimeOptimizationEarly, PM);
1047 
1048   if (OptLevel != 0)
1049     addLTOOptimizationPasses(PM);
1050   else {
1051     // The whole-program-devirt pass needs to run at -O0 because only it knows
1052     // about the llvm.type.checked.load intrinsic: it needs to both lower the
1053     // intrinsic itself and handle it in the summary.
1054     PM.add(createWholeProgramDevirtPass(ExportSummary, nullptr));
1055   }
1056 
1057   // Create a function that performs CFI checks for cross-DSO calls with targets
1058   // in the current module.
1059   PM.add(createCrossDSOCFIPass());
1060 
1061   // Lower type metadata and the type.test intrinsic. This pass supports Clang's
1062   // control flow integrity mechanisms (-fsanitize=cfi*) and needs to run at
1063   // link time if CFI is enabled. The pass does nothing if CFI is disabled.
1064   PM.add(createLowerTypeTestsPass(ExportSummary, nullptr));
1065 
1066   if (OptLevel != 0)
1067     addLateLTOOptimizationPasses(PM);
1068 
1069   addExtensionsToPM(EP_FullLinkTimeOptimizationLast, PM);
1070 
1071   if (VerifyOutput)
1072     PM.add(createVerifierPass());
1073 }
1074 
1075 inline PassManagerBuilder *unwrap(LLVMPassManagerBuilderRef P) {
1076     return reinterpret_cast<PassManagerBuilder*>(P);
1077 }
1078 
1079 inline LLVMPassManagerBuilderRef wrap(PassManagerBuilder *P) {
1080   return reinterpret_cast<LLVMPassManagerBuilderRef>(P);
1081 }
1082 
1083 LLVMPassManagerBuilderRef LLVMPassManagerBuilderCreate() {
1084   PassManagerBuilder *PMB = new PassManagerBuilder();
1085   return wrap(PMB);
1086 }
1087 
1088 void LLVMPassManagerBuilderDispose(LLVMPassManagerBuilderRef PMB) {
1089   PassManagerBuilder *Builder = unwrap(PMB);
1090   delete Builder;
1091 }
1092 
1093 void
1094 LLVMPassManagerBuilderSetOptLevel(LLVMPassManagerBuilderRef PMB,
1095                                   unsigned OptLevel) {
1096   PassManagerBuilder *Builder = unwrap(PMB);
1097   Builder->OptLevel = OptLevel;
1098 }
1099 
1100 void
1101 LLVMPassManagerBuilderSetSizeLevel(LLVMPassManagerBuilderRef PMB,
1102                                    unsigned SizeLevel) {
1103   PassManagerBuilder *Builder = unwrap(PMB);
1104   Builder->SizeLevel = SizeLevel;
1105 }
1106 
1107 void
1108 LLVMPassManagerBuilderSetDisableUnitAtATime(LLVMPassManagerBuilderRef PMB,
1109                                             LLVMBool Value) {
1110   // NOTE: The DisableUnitAtATime switch has been removed.
1111 }
1112 
1113 void
1114 LLVMPassManagerBuilderSetDisableUnrollLoops(LLVMPassManagerBuilderRef PMB,
1115                                             LLVMBool Value) {
1116   PassManagerBuilder *Builder = unwrap(PMB);
1117   Builder->DisableUnrollLoops = Value;
1118 }
1119 
1120 void
1121 LLVMPassManagerBuilderSetDisableSimplifyLibCalls(LLVMPassManagerBuilderRef PMB,
1122                                                  LLVMBool Value) {
1123   // NOTE: The simplify-libcalls pass has been removed.
1124 }
1125 
1126 void
1127 LLVMPassManagerBuilderUseInlinerWithThreshold(LLVMPassManagerBuilderRef PMB,
1128                                               unsigned Threshold) {
1129   PassManagerBuilder *Builder = unwrap(PMB);
1130   Builder->Inliner = createFunctionInliningPass(Threshold);
1131 }
1132 
1133 void
1134 LLVMPassManagerBuilderPopulateFunctionPassManager(LLVMPassManagerBuilderRef PMB,
1135                                                   LLVMPassManagerRef PM) {
1136   PassManagerBuilder *Builder = unwrap(PMB);
1137   legacy::FunctionPassManager *FPM = unwrap<legacy::FunctionPassManager>(PM);
1138   Builder->populateFunctionPassManager(*FPM);
1139 }
1140 
1141 void
1142 LLVMPassManagerBuilderPopulateModulePassManager(LLVMPassManagerBuilderRef PMB,
1143                                                 LLVMPassManagerRef PM) {
1144   PassManagerBuilder *Builder = unwrap(PMB);
1145   legacy::PassManagerBase *MPM = unwrap(PM);
1146   Builder->populateModulePassManager(*MPM);
1147 }
1148 
1149 void LLVMPassManagerBuilderPopulateLTOPassManager(LLVMPassManagerBuilderRef PMB,
1150                                                   LLVMPassManagerRef PM,
1151                                                   LLVMBool Internalize,
1152                                                   LLVMBool RunInliner) {
1153   PassManagerBuilder *Builder = unwrap(PMB);
1154   legacy::PassManagerBase *LPM = unwrap(PM);
1155 
1156   // A small backwards compatibility hack. populateLTOPassManager used to take
1157   // an RunInliner option.
1158   if (RunInliner && !Builder->Inliner)
1159     Builder->Inliner = createFunctionInliningPass();
1160 
1161   Builder->populateLTOPassManager(*LPM);
1162 }
1163