1 //===--- BackendUtil.cpp - LLVM Backend Utilities -------------------------===//
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 #include "clang/CodeGen/BackendUtil.h"
10 #include "clang/Basic/CodeGenOptions.h"
11 #include "clang/Basic/Diagnostic.h"
12 #include "clang/Basic/LangOptions.h"
13 #include "clang/Basic/TargetOptions.h"
14 #include "clang/Frontend/FrontendDiagnostic.h"
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Lex/HeaderSearchOptions.h"
17 #include "llvm/ADT/SmallSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/ADT/Triple.h"
21 #include "llvm/Analysis/StackSafetyAnalysis.h"
22 #include "llvm/Analysis/TargetLibraryInfo.h"
23 #include "llvm/Analysis/TargetTransformInfo.h"
24 #include "llvm/Bitcode/BitcodeReader.h"
25 #include "llvm/Bitcode/BitcodeWriter.h"
26 #include "llvm/Bitcode/BitcodeWriterPass.h"
27 #include "llvm/CodeGen/RegAllocRegistry.h"
28 #include "llvm/CodeGen/SchedulerRegistry.h"
29 #include "llvm/CodeGen/TargetSubtargetInfo.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/IRPrintingPasses.h"
32 #include "llvm/IR/LegacyPassManager.h"
33 #include "llvm/IR/Module.h"
34 #include "llvm/IR/ModuleSummaryIndex.h"
35 #include "llvm/IR/PassManager.h"
36 #include "llvm/IR/Verifier.h"
37 #include "llvm/LTO/LTOBackend.h"
38 #include "llvm/MC/MCAsmInfo.h"
39 #include "llvm/MC/SubtargetFeature.h"
40 #include "llvm/Passes/PassBuilder.h"
41 #include "llvm/Passes/PassPlugin.h"
42 #include "llvm/Passes/StandardInstrumentations.h"
43 #include "llvm/Support/BuryPointer.h"
44 #include "llvm/Support/CommandLine.h"
45 #include "llvm/Support/MemoryBuffer.h"
46 #include "llvm/Support/PrettyStackTrace.h"
47 #include "llvm/Support/TargetRegistry.h"
48 #include "llvm/Support/TimeProfiler.h"
49 #include "llvm/Support/Timer.h"
50 #include "llvm/Support/ToolOutputFile.h"
51 #include "llvm/Support/raw_ostream.h"
52 #include "llvm/Target/TargetMachine.h"
53 #include "llvm/Target/TargetOptions.h"
54 #include "llvm/Transforms/Coroutines.h"
55 #include "llvm/Transforms/Coroutines/CoroCleanup.h"
56 #include "llvm/Transforms/Coroutines/CoroEarly.h"
57 #include "llvm/Transforms/Coroutines/CoroElide.h"
58 #include "llvm/Transforms/Coroutines/CoroSplit.h"
59 #include "llvm/Transforms/IPO.h"
60 #include "llvm/Transforms/IPO/AlwaysInliner.h"
61 #include "llvm/Transforms/IPO/LowerTypeTests.h"
62 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
63 #include "llvm/Transforms/IPO/ThinLTOBitcodeWriter.h"
64 #include "llvm/Transforms/InstCombine/InstCombine.h"
65 #include "llvm/Transforms/Instrumentation.h"
66 #include "llvm/Transforms/Instrumentation/AddressSanitizer.h"
67 #include "llvm/Transforms/Instrumentation/BoundsChecking.h"
68 #include "llvm/Transforms/Instrumentation/GCOVProfiler.h"
69 #include "llvm/Transforms/Instrumentation/HWAddressSanitizer.h"
70 #include "llvm/Transforms/Instrumentation/InstrProfiling.h"
71 #include "llvm/Transforms/Instrumentation/MemorySanitizer.h"
72 #include "llvm/Transforms/Instrumentation/SanitizerCoverage.h"
73 #include "llvm/Transforms/Instrumentation/ThreadSanitizer.h"
74 #include "llvm/Transforms/ObjCARC.h"
75 #include "llvm/Transforms/Scalar.h"
76 #include "llvm/Transforms/Scalar/GVN.h"
77 #include "llvm/Transforms/Utils.h"
78 #include "llvm/Transforms/Utils/CanonicalizeAliases.h"
79 #include "llvm/Transforms/Utils/EntryExitInstrumenter.h"
80 #include "llvm/Transforms/Utils/NameAnonGlobals.h"
81 #include "llvm/Transforms/Utils/SymbolRewriter.h"
82 #include "llvm/Transforms/Utils/UniqueInternalLinkageNames.h"
83 #include <memory>
84 using namespace clang;
85 using namespace llvm;
86 
87 #define HANDLE_EXTENSION(Ext)                                                  \
88   llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
89 #include "llvm/Support/Extension.def"
90 
91 namespace {
92 
93 // Default filename used for profile generation.
94 static constexpr StringLiteral DefaultProfileGenName = "default_%m.profraw";
95 
96 class EmitAssemblyHelper {
97   DiagnosticsEngine &Diags;
98   const HeaderSearchOptions &HSOpts;
99   const CodeGenOptions &CodeGenOpts;
100   const clang::TargetOptions &TargetOpts;
101   const LangOptions &LangOpts;
102   Module *TheModule;
103 
104   Timer CodeGenerationTime;
105 
106   std::unique_ptr<raw_pwrite_stream> OS;
107 
108   TargetIRAnalysis getTargetIRAnalysis() const {
109     if (TM)
110       return TM->getTargetIRAnalysis();
111 
112     return TargetIRAnalysis();
113   }
114 
115   void CreatePasses(legacy::PassManager &MPM, legacy::FunctionPassManager &FPM);
116 
117   /// Generates the TargetMachine.
118   /// Leaves TM unchanged if it is unable to create the target machine.
119   /// Some of our clang tests specify triples which are not built
120   /// into clang. This is okay because these tests check the generated
121   /// IR, and they require DataLayout which depends on the triple.
122   /// In this case, we allow this method to fail and not report an error.
123   /// When MustCreateTM is used, we print an error if we are unable to load
124   /// the requested target.
125   void CreateTargetMachine(bool MustCreateTM);
126 
127   /// Add passes necessary to emit assembly or LLVM IR.
128   ///
129   /// \return True on success.
130   bool AddEmitPasses(legacy::PassManager &CodeGenPasses, BackendAction Action,
131                      raw_pwrite_stream &OS, raw_pwrite_stream *DwoOS);
132 
133   std::unique_ptr<llvm::ToolOutputFile> openOutputFile(StringRef Path) {
134     std::error_code EC;
135     auto F = std::make_unique<llvm::ToolOutputFile>(Path, EC,
136                                                      llvm::sys::fs::OF_None);
137     if (EC) {
138       Diags.Report(diag::err_fe_unable_to_open_output) << Path << EC.message();
139       F.reset();
140     }
141     return F;
142   }
143 
144 public:
145   EmitAssemblyHelper(DiagnosticsEngine &_Diags,
146                      const HeaderSearchOptions &HeaderSearchOpts,
147                      const CodeGenOptions &CGOpts,
148                      const clang::TargetOptions &TOpts,
149                      const LangOptions &LOpts, Module *M)
150       : Diags(_Diags), HSOpts(HeaderSearchOpts), CodeGenOpts(CGOpts),
151         TargetOpts(TOpts), LangOpts(LOpts), TheModule(M),
152         CodeGenerationTime("codegen", "Code Generation Time") {}
153 
154   ~EmitAssemblyHelper() {
155     if (CodeGenOpts.DisableFree)
156       BuryPointer(std::move(TM));
157   }
158 
159   std::unique_ptr<TargetMachine> TM;
160 
161   void EmitAssembly(BackendAction Action,
162                     std::unique_ptr<raw_pwrite_stream> OS);
163 
164   void EmitAssemblyWithNewPassManager(BackendAction Action,
165                                       std::unique_ptr<raw_pwrite_stream> OS);
166 };
167 
168 // We need this wrapper to access LangOpts and CGOpts from extension functions
169 // that we add to the PassManagerBuilder.
170 class PassManagerBuilderWrapper : public PassManagerBuilder {
171 public:
172   PassManagerBuilderWrapper(const Triple &TargetTriple,
173                             const CodeGenOptions &CGOpts,
174                             const LangOptions &LangOpts)
175       : PassManagerBuilder(), TargetTriple(TargetTriple), CGOpts(CGOpts),
176         LangOpts(LangOpts) {}
177   const Triple &getTargetTriple() const { return TargetTriple; }
178   const CodeGenOptions &getCGOpts() const { return CGOpts; }
179   const LangOptions &getLangOpts() const { return LangOpts; }
180 
181 private:
182   const Triple &TargetTriple;
183   const CodeGenOptions &CGOpts;
184   const LangOptions &LangOpts;
185 };
186 }
187 
188 static void addObjCARCAPElimPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
189   if (Builder.OptLevel > 0)
190     PM.add(createObjCARCAPElimPass());
191 }
192 
193 static void addObjCARCExpandPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
194   if (Builder.OptLevel > 0)
195     PM.add(createObjCARCExpandPass());
196 }
197 
198 static void addObjCARCOptPass(const PassManagerBuilder &Builder, PassManagerBase &PM) {
199   if (Builder.OptLevel > 0)
200     PM.add(createObjCARCOptPass());
201 }
202 
203 static void addAddDiscriminatorsPass(const PassManagerBuilder &Builder,
204                                      legacy::PassManagerBase &PM) {
205   PM.add(createAddDiscriminatorsPass());
206 }
207 
208 static void addBoundsCheckingPass(const PassManagerBuilder &Builder,
209                                   legacy::PassManagerBase &PM) {
210   PM.add(createBoundsCheckingLegacyPass());
211 }
212 
213 static SanitizerCoverageOptions
214 getSancovOptsFromCGOpts(const CodeGenOptions &CGOpts) {
215   SanitizerCoverageOptions Opts;
216   Opts.CoverageType =
217       static_cast<SanitizerCoverageOptions::Type>(CGOpts.SanitizeCoverageType);
218   Opts.IndirectCalls = CGOpts.SanitizeCoverageIndirectCalls;
219   Opts.TraceBB = CGOpts.SanitizeCoverageTraceBB;
220   Opts.TraceCmp = CGOpts.SanitizeCoverageTraceCmp;
221   Opts.TraceDiv = CGOpts.SanitizeCoverageTraceDiv;
222   Opts.TraceGep = CGOpts.SanitizeCoverageTraceGep;
223   Opts.Use8bitCounters = CGOpts.SanitizeCoverage8bitCounters;
224   Opts.TracePC = CGOpts.SanitizeCoverageTracePC;
225   Opts.TracePCGuard = CGOpts.SanitizeCoverageTracePCGuard;
226   Opts.NoPrune = CGOpts.SanitizeCoverageNoPrune;
227   Opts.Inline8bitCounters = CGOpts.SanitizeCoverageInline8bitCounters;
228   Opts.InlineBoolFlag = CGOpts.SanitizeCoverageInlineBoolFlag;
229   Opts.PCTable = CGOpts.SanitizeCoveragePCTable;
230   Opts.StackDepth = CGOpts.SanitizeCoverageStackDepth;
231   return Opts;
232 }
233 
234 static void addSanitizerCoveragePass(const PassManagerBuilder &Builder,
235                                      legacy::PassManagerBase &PM) {
236   const PassManagerBuilderWrapper &BuilderWrapper =
237       static_cast<const PassManagerBuilderWrapper &>(Builder);
238   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
239   auto Opts = getSancovOptsFromCGOpts(CGOpts);
240   PM.add(createModuleSanitizerCoverageLegacyPassPass(
241       Opts, CGOpts.SanitizeCoverageAllowlistFiles,
242       CGOpts.SanitizeCoverageBlocklistFiles));
243 }
244 
245 // Check if ASan should use GC-friendly instrumentation for globals.
246 // First of all, there is no point if -fdata-sections is off (expect for MachO,
247 // where this is not a factor). Also, on ELF this feature requires an assembler
248 // extension that only works with -integrated-as at the moment.
249 static bool asanUseGlobalsGC(const Triple &T, const CodeGenOptions &CGOpts) {
250   if (!CGOpts.SanitizeAddressGlobalsDeadStripping)
251     return false;
252   switch (T.getObjectFormat()) {
253   case Triple::MachO:
254   case Triple::COFF:
255     return true;
256   case Triple::ELF:
257     return CGOpts.DataSections && !CGOpts.DisableIntegratedAS;
258   case Triple::XCOFF:
259     llvm::report_fatal_error("ASan not implemented for XCOFF.");
260   case Triple::Wasm:
261   case Triple::UnknownObjectFormat:
262     break;
263   }
264   return false;
265 }
266 
267 static void addAddressSanitizerPasses(const PassManagerBuilder &Builder,
268                                       legacy::PassManagerBase &PM) {
269   const PassManagerBuilderWrapper &BuilderWrapper =
270       static_cast<const PassManagerBuilderWrapper&>(Builder);
271   const Triple &T = BuilderWrapper.getTargetTriple();
272   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
273   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Address);
274   bool UseAfterScope = CGOpts.SanitizeAddressUseAfterScope;
275   bool UseOdrIndicator = CGOpts.SanitizeAddressUseOdrIndicator;
276   bool UseGlobalsGC = asanUseGlobalsGC(T, CGOpts);
277   PM.add(createAddressSanitizerFunctionPass(/*CompileKernel*/ false, Recover,
278                                             UseAfterScope));
279   PM.add(createModuleAddressSanitizerLegacyPassPass(
280       /*CompileKernel*/ false, Recover, UseGlobalsGC, UseOdrIndicator));
281 }
282 
283 static void addKernelAddressSanitizerPasses(const PassManagerBuilder &Builder,
284                                             legacy::PassManagerBase &PM) {
285   PM.add(createAddressSanitizerFunctionPass(
286       /*CompileKernel*/ true, /*Recover*/ true, /*UseAfterScope*/ false));
287   PM.add(createModuleAddressSanitizerLegacyPassPass(
288       /*CompileKernel*/ true, /*Recover*/ true, /*UseGlobalsGC*/ true,
289       /*UseOdrIndicator*/ false));
290 }
291 
292 static void addHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
293                                             legacy::PassManagerBase &PM) {
294   const PassManagerBuilderWrapper &BuilderWrapper =
295       static_cast<const PassManagerBuilderWrapper &>(Builder);
296   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
297   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
298   PM.add(
299       createHWAddressSanitizerLegacyPassPass(/*CompileKernel*/ false, Recover));
300 }
301 
302 static void addKernelHWAddressSanitizerPasses(const PassManagerBuilder &Builder,
303                                             legacy::PassManagerBase &PM) {
304   PM.add(createHWAddressSanitizerLegacyPassPass(
305       /*CompileKernel*/ true, /*Recover*/ true));
306 }
307 
308 static void addGeneralOptsForMemorySanitizer(const PassManagerBuilder &Builder,
309                                              legacy::PassManagerBase &PM,
310                                              bool CompileKernel) {
311   const PassManagerBuilderWrapper &BuilderWrapper =
312       static_cast<const PassManagerBuilderWrapper&>(Builder);
313   const CodeGenOptions &CGOpts = BuilderWrapper.getCGOpts();
314   int TrackOrigins = CGOpts.SanitizeMemoryTrackOrigins;
315   bool Recover = CGOpts.SanitizeRecover.has(SanitizerKind::Memory);
316   PM.add(createMemorySanitizerLegacyPassPass(
317       MemorySanitizerOptions{TrackOrigins, Recover, CompileKernel}));
318 
319   // MemorySanitizer inserts complex instrumentation that mostly follows
320   // the logic of the original code, but operates on "shadow" values.
321   // It can benefit from re-running some general purpose optimization passes.
322   if (Builder.OptLevel > 0) {
323     PM.add(createEarlyCSEPass());
324     PM.add(createReassociatePass());
325     PM.add(createLICMPass());
326     PM.add(createGVNPass());
327     PM.add(createInstructionCombiningPass());
328     PM.add(createDeadStoreEliminationPass());
329   }
330 }
331 
332 static void addMemorySanitizerPass(const PassManagerBuilder &Builder,
333                                    legacy::PassManagerBase &PM) {
334   addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ false);
335 }
336 
337 static void addKernelMemorySanitizerPass(const PassManagerBuilder &Builder,
338                                          legacy::PassManagerBase &PM) {
339   addGeneralOptsForMemorySanitizer(Builder, PM, /*CompileKernel*/ true);
340 }
341 
342 static void addThreadSanitizerPass(const PassManagerBuilder &Builder,
343                                    legacy::PassManagerBase &PM) {
344   PM.add(createThreadSanitizerLegacyPassPass());
345 }
346 
347 static void addDataFlowSanitizerPass(const PassManagerBuilder &Builder,
348                                      legacy::PassManagerBase &PM) {
349   const PassManagerBuilderWrapper &BuilderWrapper =
350       static_cast<const PassManagerBuilderWrapper&>(Builder);
351   const LangOptions &LangOpts = BuilderWrapper.getLangOpts();
352   PM.add(createDataFlowSanitizerPass(LangOpts.SanitizerBlacklistFiles));
353 }
354 
355 static TargetLibraryInfoImpl *createTLII(llvm::Triple &TargetTriple,
356                                          const CodeGenOptions &CodeGenOpts) {
357   TargetLibraryInfoImpl *TLII = new TargetLibraryInfoImpl(TargetTriple);
358 
359   switch (CodeGenOpts.getVecLib()) {
360   case CodeGenOptions::Accelerate:
361     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::Accelerate);
362     break;
363   case CodeGenOptions::MASSV:
364     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::MASSV);
365     break;
366   case CodeGenOptions::SVML:
367     TLII->addVectorizableFunctionsFromVecLib(TargetLibraryInfoImpl::SVML);
368     break;
369   default:
370     break;
371   }
372   return TLII;
373 }
374 
375 static void addSymbolRewriterPass(const CodeGenOptions &Opts,
376                                   legacy::PassManager *MPM) {
377   llvm::SymbolRewriter::RewriteDescriptorList DL;
378 
379   llvm::SymbolRewriter::RewriteMapParser MapParser;
380   for (const auto &MapFile : Opts.RewriteMapFiles)
381     MapParser.parse(MapFile, &DL);
382 
383   MPM->add(createRewriteSymbolsPass(DL));
384 }
385 
386 static CodeGenOpt::Level getCGOptLevel(const CodeGenOptions &CodeGenOpts) {
387   switch (CodeGenOpts.OptimizationLevel) {
388   default:
389     llvm_unreachable("Invalid optimization level!");
390   case 0:
391     return CodeGenOpt::None;
392   case 1:
393     return CodeGenOpt::Less;
394   case 2:
395     return CodeGenOpt::Default; // O2/Os/Oz
396   case 3:
397     return CodeGenOpt::Aggressive;
398   }
399 }
400 
401 static Optional<llvm::CodeModel::Model>
402 getCodeModel(const CodeGenOptions &CodeGenOpts) {
403   unsigned CodeModel = llvm::StringSwitch<unsigned>(CodeGenOpts.CodeModel)
404                            .Case("tiny", llvm::CodeModel::Tiny)
405                            .Case("small", llvm::CodeModel::Small)
406                            .Case("kernel", llvm::CodeModel::Kernel)
407                            .Case("medium", llvm::CodeModel::Medium)
408                            .Case("large", llvm::CodeModel::Large)
409                            .Case("default", ~1u)
410                            .Default(~0u);
411   assert(CodeModel != ~0u && "invalid code model!");
412   if (CodeModel == ~1u)
413     return None;
414   return static_cast<llvm::CodeModel::Model>(CodeModel);
415 }
416 
417 static CodeGenFileType getCodeGenFileType(BackendAction Action) {
418   if (Action == Backend_EmitObj)
419     return CGFT_ObjectFile;
420   else if (Action == Backend_EmitMCNull)
421     return CGFT_Null;
422   else {
423     assert(Action == Backend_EmitAssembly && "Invalid action!");
424     return CGFT_AssemblyFile;
425   }
426 }
427 
428 static void initTargetOptions(DiagnosticsEngine &Diags,
429                               llvm::TargetOptions &Options,
430                               const CodeGenOptions &CodeGenOpts,
431                               const clang::TargetOptions &TargetOpts,
432                               const LangOptions &LangOpts,
433                               const HeaderSearchOptions &HSOpts) {
434   Options.ThreadModel =
435       llvm::StringSwitch<llvm::ThreadModel::Model>(CodeGenOpts.ThreadModel)
436           .Case("posix", llvm::ThreadModel::POSIX)
437           .Case("single", llvm::ThreadModel::Single);
438 
439   // Set float ABI type.
440   assert((CodeGenOpts.FloatABI == "soft" || CodeGenOpts.FloatABI == "softfp" ||
441           CodeGenOpts.FloatABI == "hard" || CodeGenOpts.FloatABI.empty()) &&
442          "Invalid Floating Point ABI!");
443   Options.FloatABIType =
444       llvm::StringSwitch<llvm::FloatABI::ABIType>(CodeGenOpts.FloatABI)
445           .Case("soft", llvm::FloatABI::Soft)
446           .Case("softfp", llvm::FloatABI::Soft)
447           .Case("hard", llvm::FloatABI::Hard)
448           .Default(llvm::FloatABI::Default);
449 
450   // Set FP fusion mode.
451   switch (LangOpts.getDefaultFPContractMode()) {
452   case LangOptions::FPM_Off:
453     // Preserve any contraction performed by the front-end.  (Strict performs
454     // splitting of the muladd intrinsic in the backend.)
455     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
456     break;
457   case LangOptions::FPM_On:
458     Options.AllowFPOpFusion = llvm::FPOpFusion::Standard;
459     break;
460   case LangOptions::FPM_Fast:
461     Options.AllowFPOpFusion = llvm::FPOpFusion::Fast;
462     break;
463   }
464 
465   Options.UseInitArray = CodeGenOpts.UseInitArray;
466   Options.DisableIntegratedAS = CodeGenOpts.DisableIntegratedAS;
467   Options.CompressDebugSections = CodeGenOpts.getCompressDebugSections();
468   Options.RelaxELFRelocations = CodeGenOpts.RelaxELFRelocations;
469 
470   // Set EABI version.
471   Options.EABIVersion = TargetOpts.EABIVersion;
472 
473   if (LangOpts.SjLjExceptions)
474     Options.ExceptionModel = llvm::ExceptionHandling::SjLj;
475   if (LangOpts.SEHExceptions)
476     Options.ExceptionModel = llvm::ExceptionHandling::WinEH;
477   if (LangOpts.DWARFExceptions)
478     Options.ExceptionModel = llvm::ExceptionHandling::DwarfCFI;
479   if (LangOpts.WasmExceptions)
480     Options.ExceptionModel = llvm::ExceptionHandling::Wasm;
481 
482   Options.NoInfsFPMath = LangOpts.NoHonorInfs;
483   Options.NoNaNsFPMath = LangOpts.NoHonorNaNs;
484   Options.NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS;
485   Options.UnsafeFPMath = LangOpts.UnsafeFPMath;
486   Options.StackAlignmentOverride = CodeGenOpts.StackAlignment;
487 
488   Options.BBSections =
489       llvm::StringSwitch<llvm::BasicBlockSection>(CodeGenOpts.BBSections)
490           .Case("all", llvm::BasicBlockSection::All)
491           .Case("labels", llvm::BasicBlockSection::Labels)
492           .StartsWith("list=", llvm::BasicBlockSection::List)
493           .Case("none", llvm::BasicBlockSection::None)
494           .Default(llvm::BasicBlockSection::None);
495 
496   if (Options.BBSections == llvm::BasicBlockSection::List) {
497     ErrorOr<std::unique_ptr<MemoryBuffer>> MBOrErr =
498         MemoryBuffer::getFile(CodeGenOpts.BBSections.substr(5));
499     if (!MBOrErr)
500       Diags.Report(diag::err_fe_unable_to_load_basic_block_sections_file)
501           << MBOrErr.getError().message();
502     else
503       Options.BBSectionsFuncListBuf = std::move(*MBOrErr);
504   }
505 
506   Options.FunctionSections = CodeGenOpts.FunctionSections;
507   Options.DataSections = CodeGenOpts.DataSections;
508   Options.UniqueSectionNames = CodeGenOpts.UniqueSectionNames;
509   Options.UniqueBasicBlockSectionNames =
510       CodeGenOpts.UniqueBasicBlockSectionNames;
511   Options.TLSSize = CodeGenOpts.TLSSize;
512   Options.EmulatedTLS = CodeGenOpts.EmulatedTLS;
513   Options.ExplicitEmulatedTLS = CodeGenOpts.ExplicitEmulatedTLS;
514   Options.DebuggerTuning = CodeGenOpts.getDebuggerTuning();
515   Options.EmitStackSizeSection = CodeGenOpts.StackSizeSection;
516   Options.EmitAddrsig = CodeGenOpts.Addrsig;
517   Options.ForceDwarfFrameSection = CodeGenOpts.ForceDwarfFrameSection;
518   Options.EmitCallSiteInfo = CodeGenOpts.EmitCallSiteInfo;
519   Options.XRayOmitFunctionIndex = CodeGenOpts.XRayOmitFunctionIndex;
520 
521   Options.MCOptions.SplitDwarfFile = CodeGenOpts.SplitDwarfFile;
522   Options.MCOptions.MCRelaxAll = CodeGenOpts.RelaxAll;
523   Options.MCOptions.MCSaveTempLabels = CodeGenOpts.SaveTempLabels;
524   Options.MCOptions.MCUseDwarfDirectory = !CodeGenOpts.NoDwarfDirectoryAsm;
525   Options.MCOptions.MCNoExecStack = CodeGenOpts.NoExecStack;
526   Options.MCOptions.MCIncrementalLinkerCompatible =
527       CodeGenOpts.IncrementalLinkerCompatible;
528   Options.MCOptions.MCFatalWarnings = CodeGenOpts.FatalWarnings;
529   Options.MCOptions.MCNoWarn = CodeGenOpts.NoWarn;
530   Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
531   Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
532   Options.MCOptions.ABIName = TargetOpts.ABI;
533   for (const auto &Entry : HSOpts.UserEntries)
534     if (!Entry.IsFramework &&
535         (Entry.Group == frontend::IncludeDirGroup::Quoted ||
536          Entry.Group == frontend::IncludeDirGroup::Angled ||
537          Entry.Group == frontend::IncludeDirGroup::System))
538       Options.MCOptions.IASSearchPaths.push_back(
539           Entry.IgnoreSysRoot ? Entry.Path : HSOpts.Sysroot + Entry.Path);
540   Options.MCOptions.Argv0 = CodeGenOpts.Argv0;
541   Options.MCOptions.CommandLineArgs = CodeGenOpts.CommandLineArgs;
542 }
543 static Optional<GCOVOptions> getGCOVOptions(const CodeGenOptions &CodeGenOpts) {
544   if (CodeGenOpts.DisableGCov)
545     return None;
546   if (!CodeGenOpts.EmitGcovArcs && !CodeGenOpts.EmitGcovNotes)
547     return None;
548   // Not using 'GCOVOptions::getDefault' allows us to avoid exiting if
549   // LLVM's -default-gcov-version flag is set to something invalid.
550   GCOVOptions Options;
551   Options.EmitNotes = CodeGenOpts.EmitGcovNotes;
552   Options.EmitData = CodeGenOpts.EmitGcovArcs;
553   llvm::copy(CodeGenOpts.CoverageVersion, std::begin(Options.Version));
554   Options.NoRedZone = CodeGenOpts.DisableRedZone;
555   Options.Filter = CodeGenOpts.ProfileFilterFiles;
556   Options.Exclude = CodeGenOpts.ProfileExcludeFiles;
557   return Options;
558 }
559 
560 static Optional<InstrProfOptions>
561 getInstrProfOptions(const CodeGenOptions &CodeGenOpts,
562                     const LangOptions &LangOpts) {
563   if (!CodeGenOpts.hasProfileClangInstr())
564     return None;
565   InstrProfOptions Options;
566   Options.NoRedZone = CodeGenOpts.DisableRedZone;
567   Options.InstrProfileOutput = CodeGenOpts.InstrProfileOutput;
568 
569   // TODO: Surface the option to emit atomic profile counter increments at
570   // the driver level.
571   Options.Atomic = LangOpts.Sanitize.has(SanitizerKind::Thread);
572   return Options;
573 }
574 
575 void EmitAssemblyHelper::CreatePasses(legacy::PassManager &MPM,
576                                       legacy::FunctionPassManager &FPM) {
577   // Handle disabling of all LLVM passes, where we want to preserve the
578   // internal module before any optimization.
579   if (CodeGenOpts.DisableLLVMPasses)
580     return;
581 
582   // Figure out TargetLibraryInfo.  This needs to be added to MPM and FPM
583   // manually (and not via PMBuilder), since some passes (eg. InstrProfiling)
584   // are inserted before PMBuilder ones - they'd get the default-constructed
585   // TLI with an unknown target otherwise.
586   Triple TargetTriple(TheModule->getTargetTriple());
587   std::unique_ptr<TargetLibraryInfoImpl> TLII(
588       createTLII(TargetTriple, CodeGenOpts));
589 
590   // If we reached here with a non-empty index file name, then the index file
591   // was empty and we are not performing ThinLTO backend compilation (used in
592   // testing in a distributed build environment). Drop any the type test
593   // assume sequences inserted for whole program vtables so that codegen doesn't
594   // complain.
595   if (!CodeGenOpts.ThinLTOIndexFile.empty())
596     MPM.add(createLowerTypeTestsPass(/*ExportSummary=*/nullptr,
597                                      /*ImportSummary=*/nullptr,
598                                      /*DropTypeTests=*/true));
599 
600   PassManagerBuilderWrapper PMBuilder(TargetTriple, CodeGenOpts, LangOpts);
601 
602   // At O0 and O1 we only run the always inliner which is more efficient. At
603   // higher optimization levels we run the normal inliner.
604   if (CodeGenOpts.OptimizationLevel <= 1) {
605     bool InsertLifetimeIntrinsics = ((CodeGenOpts.OptimizationLevel != 0 &&
606                                       !CodeGenOpts.DisableLifetimeMarkers) ||
607                                      LangOpts.Coroutines);
608     PMBuilder.Inliner = createAlwaysInlinerLegacyPass(InsertLifetimeIntrinsics);
609   } else {
610     // We do not want to inline hot callsites for SamplePGO module-summary build
611     // because profile annotation will happen again in ThinLTO backend, and we
612     // want the IR of the hot path to match the profile.
613     PMBuilder.Inliner = createFunctionInliningPass(
614         CodeGenOpts.OptimizationLevel, CodeGenOpts.OptimizeSize,
615         (!CodeGenOpts.SampleProfileFile.empty() &&
616          CodeGenOpts.PrepareForThinLTO));
617   }
618 
619   PMBuilder.OptLevel = CodeGenOpts.OptimizationLevel;
620   PMBuilder.SizeLevel = CodeGenOpts.OptimizeSize;
621   PMBuilder.SLPVectorize = CodeGenOpts.VectorizeSLP;
622   PMBuilder.LoopVectorize = CodeGenOpts.VectorizeLoop;
623 
624   PMBuilder.DisableUnrollLoops = !CodeGenOpts.UnrollLoops;
625   // Loop interleaving in the loop vectorizer has historically been set to be
626   // enabled when loop unrolling is enabled.
627   PMBuilder.LoopsInterleaved = CodeGenOpts.UnrollLoops;
628   PMBuilder.MergeFunctions = CodeGenOpts.MergeFunctions;
629   PMBuilder.PrepareForThinLTO = CodeGenOpts.PrepareForThinLTO;
630   PMBuilder.PrepareForLTO = CodeGenOpts.PrepareForLTO;
631   PMBuilder.RerollLoops = CodeGenOpts.RerollLoops;
632 
633   MPM.add(new TargetLibraryInfoWrapperPass(*TLII));
634 
635   if (TM)
636     TM->adjustPassManager(PMBuilder);
637 
638   if (CodeGenOpts.DebugInfoForProfiling ||
639       !CodeGenOpts.SampleProfileFile.empty())
640     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
641                            addAddDiscriminatorsPass);
642 
643   // In ObjC ARC mode, add the main ARC optimization passes.
644   if (LangOpts.ObjCAutoRefCount) {
645     PMBuilder.addExtension(PassManagerBuilder::EP_EarlyAsPossible,
646                            addObjCARCExpandPass);
647     PMBuilder.addExtension(PassManagerBuilder::EP_ModuleOptimizerEarly,
648                            addObjCARCAPElimPass);
649     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
650                            addObjCARCOptPass);
651   }
652 
653   if (LangOpts.Coroutines)
654     addCoroutinePassesToExtensionPoints(PMBuilder);
655 
656   if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds)) {
657     PMBuilder.addExtension(PassManagerBuilder::EP_ScalarOptimizerLate,
658                            addBoundsCheckingPass);
659     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
660                            addBoundsCheckingPass);
661   }
662 
663   if (CodeGenOpts.SanitizeCoverageType ||
664       CodeGenOpts.SanitizeCoverageIndirectCalls ||
665       CodeGenOpts.SanitizeCoverageTraceCmp) {
666     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
667                            addSanitizerCoveragePass);
668     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
669                            addSanitizerCoveragePass);
670   }
671 
672   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
673     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
674                            addAddressSanitizerPasses);
675     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
676                            addAddressSanitizerPasses);
677   }
678 
679   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
680     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
681                            addKernelAddressSanitizerPasses);
682     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
683                            addKernelAddressSanitizerPasses);
684   }
685 
686   if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
687     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
688                            addHWAddressSanitizerPasses);
689     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
690                            addHWAddressSanitizerPasses);
691   }
692 
693   if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
694     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
695                            addKernelHWAddressSanitizerPasses);
696     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
697                            addKernelHWAddressSanitizerPasses);
698   }
699 
700   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
701     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
702                            addMemorySanitizerPass);
703     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
704                            addMemorySanitizerPass);
705   }
706 
707   if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
708     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
709                            addKernelMemorySanitizerPass);
710     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
711                            addKernelMemorySanitizerPass);
712   }
713 
714   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
715     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
716                            addThreadSanitizerPass);
717     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
718                            addThreadSanitizerPass);
719   }
720 
721   if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
722     PMBuilder.addExtension(PassManagerBuilder::EP_OptimizerLast,
723                            addDataFlowSanitizerPass);
724     PMBuilder.addExtension(PassManagerBuilder::EP_EnabledOnOptLevel0,
725                            addDataFlowSanitizerPass);
726   }
727 
728   // Set up the per-function pass manager.
729   FPM.add(new TargetLibraryInfoWrapperPass(*TLII));
730   if (CodeGenOpts.VerifyModule)
731     FPM.add(createVerifierPass());
732 
733   // Set up the per-module pass manager.
734   if (!CodeGenOpts.RewriteMapFiles.empty())
735     addSymbolRewriterPass(CodeGenOpts, &MPM);
736 
737   // Add UniqueInternalLinkageNames Pass which renames internal linkage symbols
738   // with unique names.
739   if (CodeGenOpts.UniqueInternalLinkageNames) {
740     MPM.add(createUniqueInternalLinkageNamesPass());
741   }
742 
743   if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts)) {
744     MPM.add(createGCOVProfilerPass(*Options));
745     if (CodeGenOpts.getDebugInfo() == codegenoptions::NoDebugInfo)
746       MPM.add(createStripSymbolsPass(true));
747   }
748 
749   if (Optional<InstrProfOptions> Options =
750           getInstrProfOptions(CodeGenOpts, LangOpts))
751     MPM.add(createInstrProfilingLegacyPass(*Options, false));
752 
753   bool hasIRInstr = false;
754   if (CodeGenOpts.hasProfileIRInstr()) {
755     PMBuilder.EnablePGOInstrGen = true;
756     hasIRInstr = true;
757   }
758   if (CodeGenOpts.hasProfileCSIRInstr()) {
759     assert(!CodeGenOpts.hasProfileCSIRUse() &&
760            "Cannot have both CSProfileUse pass and CSProfileGen pass at the "
761            "same time");
762     assert(!hasIRInstr &&
763            "Cannot have both ProfileGen pass and CSProfileGen pass at the "
764            "same time");
765     PMBuilder.EnablePGOCSInstrGen = true;
766     hasIRInstr = true;
767   }
768   if (hasIRInstr) {
769     if (!CodeGenOpts.InstrProfileOutput.empty())
770       PMBuilder.PGOInstrGen = CodeGenOpts.InstrProfileOutput;
771     else
772       PMBuilder.PGOInstrGen = std::string(DefaultProfileGenName);
773   }
774   if (CodeGenOpts.hasProfileIRUse()) {
775     PMBuilder.PGOInstrUse = CodeGenOpts.ProfileInstrumentUsePath;
776     PMBuilder.EnablePGOCSInstrUse = CodeGenOpts.hasProfileCSIRUse();
777   }
778 
779   if (!CodeGenOpts.SampleProfileFile.empty())
780     PMBuilder.PGOSampleUse = CodeGenOpts.SampleProfileFile;
781 
782   PMBuilder.populateFunctionPassManager(FPM);
783   PMBuilder.populateModulePassManager(MPM);
784 }
785 
786 static void setCommandLineOpts(const CodeGenOptions &CodeGenOpts) {
787   SmallVector<const char *, 16> BackendArgs;
788   BackendArgs.push_back("clang"); // Fake program name.
789   if (!CodeGenOpts.DebugPass.empty()) {
790     BackendArgs.push_back("-debug-pass");
791     BackendArgs.push_back(CodeGenOpts.DebugPass.c_str());
792   }
793   if (!CodeGenOpts.LimitFloatPrecision.empty()) {
794     BackendArgs.push_back("-limit-float-precision");
795     BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str());
796   }
797   BackendArgs.push_back(nullptr);
798   llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1,
799                                     BackendArgs.data());
800 }
801 
802 void EmitAssemblyHelper::CreateTargetMachine(bool MustCreateTM) {
803   // Create the TargetMachine for generating code.
804   std::string Error;
805   std::string Triple = TheModule->getTargetTriple();
806   const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error);
807   if (!TheTarget) {
808     if (MustCreateTM)
809       Diags.Report(diag::err_fe_unable_to_create_target) << Error;
810     return;
811   }
812 
813   Optional<llvm::CodeModel::Model> CM = getCodeModel(CodeGenOpts);
814   std::string FeaturesStr =
815       llvm::join(TargetOpts.Features.begin(), TargetOpts.Features.end(), ",");
816   llvm::Reloc::Model RM = CodeGenOpts.RelocationModel;
817   CodeGenOpt::Level OptLevel = getCGOptLevel(CodeGenOpts);
818 
819   llvm::TargetOptions Options;
820   initTargetOptions(Diags, Options, CodeGenOpts, TargetOpts, LangOpts, HSOpts);
821   TM.reset(TheTarget->createTargetMachine(Triple, TargetOpts.CPU, FeaturesStr,
822                                           Options, RM, CM, OptLevel));
823 }
824 
825 bool EmitAssemblyHelper::AddEmitPasses(legacy::PassManager &CodeGenPasses,
826                                        BackendAction Action,
827                                        raw_pwrite_stream &OS,
828                                        raw_pwrite_stream *DwoOS) {
829   // Add LibraryInfo.
830   llvm::Triple TargetTriple(TheModule->getTargetTriple());
831   std::unique_ptr<TargetLibraryInfoImpl> TLII(
832       createTLII(TargetTriple, CodeGenOpts));
833   CodeGenPasses.add(new TargetLibraryInfoWrapperPass(*TLII));
834 
835   // Normal mode, emit a .s or .o file by running the code generator. Note,
836   // this also adds codegenerator level optimization passes.
837   CodeGenFileType CGFT = getCodeGenFileType(Action);
838 
839   // Add ObjC ARC final-cleanup optimizations. This is done as part of the
840   // "codegen" passes so that it isn't run multiple times when there is
841   // inlining happening.
842   if (CodeGenOpts.OptimizationLevel > 0)
843     CodeGenPasses.add(createObjCARCContractPass());
844 
845   if (TM->addPassesToEmitFile(CodeGenPasses, OS, DwoOS, CGFT,
846                               /*DisableVerify=*/!CodeGenOpts.VerifyModule)) {
847     Diags.Report(diag::err_fe_unable_to_interface_with_target);
848     return false;
849   }
850 
851   return true;
852 }
853 
854 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
855                                       std::unique_ptr<raw_pwrite_stream> OS) {
856   TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
857 
858   setCommandLineOpts(CodeGenOpts);
859 
860   bool UsesCodeGen = (Action != Backend_EmitNothing &&
861                       Action != Backend_EmitBC &&
862                       Action != Backend_EmitLL);
863   CreateTargetMachine(UsesCodeGen);
864 
865   if (UsesCodeGen && !TM)
866     return;
867   if (TM)
868     TheModule->setDataLayout(TM->createDataLayout());
869 
870   legacy::PassManager PerModulePasses;
871   PerModulePasses.add(
872       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
873 
874   legacy::FunctionPassManager PerFunctionPasses(TheModule);
875   PerFunctionPasses.add(
876       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
877 
878   CreatePasses(PerModulePasses, PerFunctionPasses);
879 
880   legacy::PassManager CodeGenPasses;
881   CodeGenPasses.add(
882       createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
883 
884   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
885 
886   switch (Action) {
887   case Backend_EmitNothing:
888     break;
889 
890   case Backend_EmitBC:
891     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
892       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
893         ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
894         if (!ThinLinkOS)
895           return;
896       }
897       TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
898                                CodeGenOpts.EnableSplitLTOUnit);
899       PerModulePasses.add(createWriteThinLTOBitcodePass(
900           *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
901     } else {
902       // Emit a module summary by default for Regular LTO except for ld64
903       // targets
904       bool EmitLTOSummary =
905           (CodeGenOpts.PrepareForLTO &&
906            !CodeGenOpts.DisableLLVMPasses &&
907            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
908                llvm::Triple::Apple);
909       if (EmitLTOSummary) {
910         if (!TheModule->getModuleFlag("ThinLTO"))
911           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
912         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
913                                  uint32_t(1));
914       }
915 
916       PerModulePasses.add(createBitcodeWriterPass(
917           *OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
918     }
919     break;
920 
921   case Backend_EmitLL:
922     PerModulePasses.add(
923         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
924     break;
925 
926   default:
927     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
928       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
929       if (!DwoOS)
930         return;
931     }
932     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
933                        DwoOS ? &DwoOS->os() : nullptr))
934       return;
935   }
936 
937   // Before executing passes, print the final values of the LLVM options.
938   cl::PrintOptionValues();
939 
940   // Run passes. For now we do all passes at once, but eventually we
941   // would like to have the option of streaming code generation.
942 
943   {
944     PrettyStackTraceString CrashInfo("Per-function optimization");
945     llvm::TimeTraceScope TimeScope("PerFunctionPasses");
946 
947     PerFunctionPasses.doInitialization();
948     for (Function &F : *TheModule)
949       if (!F.isDeclaration())
950         PerFunctionPasses.run(F);
951     PerFunctionPasses.doFinalization();
952   }
953 
954   {
955     PrettyStackTraceString CrashInfo("Per-module optimization passes");
956     llvm::TimeTraceScope TimeScope("PerModulePasses");
957     PerModulePasses.run(*TheModule);
958   }
959 
960   {
961     PrettyStackTraceString CrashInfo("Code generation");
962     llvm::TimeTraceScope TimeScope("CodeGenPasses");
963     CodeGenPasses.run(*TheModule);
964   }
965 
966   if (ThinLinkOS)
967     ThinLinkOS->keep();
968   if (DwoOS)
969     DwoOS->keep();
970 }
971 
972 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
973   switch (Opts.OptimizationLevel) {
974   default:
975     llvm_unreachable("Invalid optimization level!");
976 
977   case 1:
978     return PassBuilder::OptimizationLevel::O1;
979 
980   case 2:
981     switch (Opts.OptimizeSize) {
982     default:
983       llvm_unreachable("Invalid optimization level for size!");
984 
985     case 0:
986       return PassBuilder::OptimizationLevel::O2;
987 
988     case 1:
989       return PassBuilder::OptimizationLevel::Os;
990 
991     case 2:
992       return PassBuilder::OptimizationLevel::Oz;
993     }
994 
995   case 3:
996     return PassBuilder::OptimizationLevel::O3;
997   }
998 }
999 
1000 static void addCoroutinePassesAtO0(ModulePassManager &MPM,
1001                                    const LangOptions &LangOpts,
1002                                    const CodeGenOptions &CodeGenOpts) {
1003   if (!LangOpts.Coroutines)
1004     return;
1005 
1006   MPM.addPass(createModuleToFunctionPassAdaptor(CoroEarlyPass()));
1007 
1008   CGSCCPassManager CGPM(CodeGenOpts.DebugPassManager);
1009   CGPM.addPass(CoroSplitPass());
1010   CGPM.addPass(createCGSCCToFunctionPassAdaptor(CoroElidePass()));
1011   MPM.addPass(createModuleToPostOrderCGSCCPassAdaptor(std::move(CGPM)));
1012 
1013   MPM.addPass(createModuleToFunctionPassAdaptor(CoroCleanupPass()));
1014 }
1015 
1016 static void addSanitizersAtO0(ModulePassManager &MPM,
1017                               const Triple &TargetTriple,
1018                               const LangOptions &LangOpts,
1019                               const CodeGenOptions &CodeGenOpts) {
1020   if (CodeGenOpts.SanitizeCoverageType ||
1021       CodeGenOpts.SanitizeCoverageIndirectCalls ||
1022       CodeGenOpts.SanitizeCoverageTraceCmp) {
1023     auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
1024     MPM.addPass(ModuleSanitizerCoveragePass(
1025         SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
1026         CodeGenOpts.SanitizeCoverageBlocklistFiles));
1027   }
1028 
1029   auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1030     MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1031     bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1032     MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass(
1033         CompileKernel, Recover, CodeGenOpts.SanitizeAddressUseAfterScope)));
1034     bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1035     MPM.addPass(
1036         ModuleAddressSanitizerPass(CompileKernel, Recover, ModuleUseAfterScope,
1037                                    CodeGenOpts.SanitizeAddressUseOdrIndicator));
1038   };
1039 
1040   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
1041     ASanPass(SanitizerKind::Address, /*CompileKernel=*/false);
1042   }
1043 
1044   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
1045     ASanPass(SanitizerKind::KernelAddress, /*CompileKernel=*/true);
1046   }
1047 
1048   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
1049     bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
1050     int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
1051     MPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
1052     MPM.addPass(createModuleToFunctionPassAdaptor(
1053         MemorySanitizerPass({TrackOrigins, Recover, false})));
1054   }
1055 
1056   if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
1057     MPM.addPass(createModuleToFunctionPassAdaptor(
1058         MemorySanitizerPass({0, false, /*Kernel=*/true})));
1059   }
1060 
1061   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1062     MPM.addPass(ThreadSanitizerPass());
1063     MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
1064   }
1065 }
1066 
1067 /// A clean version of `EmitAssembly` that uses the new pass manager.
1068 ///
1069 /// Not all features are currently supported in this system, but where
1070 /// necessary it falls back to the legacy pass manager to at least provide
1071 /// basic functionality.
1072 ///
1073 /// This API is planned to have its functionality finished and then to replace
1074 /// `EmitAssembly` at some point in the future when the default switches.
1075 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
1076     BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
1077   TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
1078   setCommandLineOpts(CodeGenOpts);
1079 
1080   bool RequiresCodeGen = (Action != Backend_EmitNothing &&
1081                           Action != Backend_EmitBC &&
1082                           Action != Backend_EmitLL);
1083   CreateTargetMachine(RequiresCodeGen);
1084 
1085   if (RequiresCodeGen && !TM)
1086     return;
1087   if (TM)
1088     TheModule->setDataLayout(TM->createDataLayout());
1089 
1090   Optional<PGOOptions> PGOOpt;
1091 
1092   if (CodeGenOpts.hasProfileIRInstr())
1093     // -fprofile-generate.
1094     PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
1095                             ? std::string(DefaultProfileGenName)
1096                             : CodeGenOpts.InstrProfileOutput,
1097                         "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
1098                         CodeGenOpts.DebugInfoForProfiling);
1099   else if (CodeGenOpts.hasProfileIRUse()) {
1100     // -fprofile-use.
1101     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
1102                                                     : PGOOptions::NoCSAction;
1103     PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
1104                         CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
1105                         CSAction, CodeGenOpts.DebugInfoForProfiling);
1106   } else if (!CodeGenOpts.SampleProfileFile.empty())
1107     // -fprofile-sample-use
1108     PGOOpt =
1109         PGOOptions(CodeGenOpts.SampleProfileFile, "",
1110                    CodeGenOpts.ProfileRemappingFile, PGOOptions::SampleUse,
1111                    PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
1112   else if (CodeGenOpts.DebugInfoForProfiling)
1113     // -fdebug-info-for-profiling
1114     PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
1115                         PGOOptions::NoCSAction, true);
1116 
1117   // Check to see if we want to generate a CS profile.
1118   if (CodeGenOpts.hasProfileCSIRInstr()) {
1119     assert(!CodeGenOpts.hasProfileCSIRUse() &&
1120            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
1121            "the same time");
1122     if (PGOOpt.hasValue()) {
1123       assert(PGOOpt->Action != PGOOptions::IRInstr &&
1124              PGOOpt->Action != PGOOptions::SampleUse &&
1125              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
1126              " pass");
1127       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
1128                                      ? std::string(DefaultProfileGenName)
1129                                      : CodeGenOpts.InstrProfileOutput;
1130       PGOOpt->CSAction = PGOOptions::CSIRInstr;
1131     } else
1132       PGOOpt = PGOOptions("",
1133                           CodeGenOpts.InstrProfileOutput.empty()
1134                               ? std::string(DefaultProfileGenName)
1135                               : CodeGenOpts.InstrProfileOutput,
1136                           "", PGOOptions::NoAction, PGOOptions::CSIRInstr,
1137                           CodeGenOpts.DebugInfoForProfiling);
1138   }
1139 
1140   PipelineTuningOptions PTO;
1141   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
1142   // For historical reasons, loop interleaving is set to mirror setting for loop
1143   // unrolling.
1144   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
1145   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
1146   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
1147   PTO.CallGraphProfile = CodeGenOpts.CallGraphProfile;
1148   PTO.Coroutines = LangOpts.Coroutines;
1149 
1150   PassInstrumentationCallbacks PIC;
1151   StandardInstrumentations SI;
1152   SI.registerCallbacks(PIC);
1153   PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
1154 
1155   // Attempt to load pass plugins and register their callbacks with PB.
1156   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
1157     auto PassPlugin = PassPlugin::Load(PluginFN);
1158     if (PassPlugin) {
1159       PassPlugin->registerPassBuilderCallbacks(PB);
1160     } else {
1161       Diags.Report(diag::err_fe_unable_to_load_plugin)
1162           << PluginFN << toString(PassPlugin.takeError());
1163     }
1164   }
1165 #define HANDLE_EXTENSION(Ext)                                                  \
1166   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
1167 #include "llvm/Support/Extension.def"
1168 
1169   LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager);
1170   FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager);
1171   CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager);
1172   ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager);
1173 
1174   // Register the AA manager first so that our version is the one used.
1175   FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
1176 
1177   // Register the target library analysis directly and give it a customized
1178   // preset TLI.
1179   Triple TargetTriple(TheModule->getTargetTriple());
1180   std::unique_ptr<TargetLibraryInfoImpl> TLII(
1181       createTLII(TargetTriple, CodeGenOpts));
1182   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
1183 
1184   // Register all the basic analyses with the managers.
1185   PB.registerModuleAnalyses(MAM);
1186   PB.registerCGSCCAnalyses(CGAM);
1187   PB.registerFunctionAnalyses(FAM);
1188   PB.registerLoopAnalyses(LAM);
1189   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
1190 
1191   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
1192 
1193   if (!CodeGenOpts.DisableLLVMPasses) {
1194     bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
1195     bool IsLTO = CodeGenOpts.PrepareForLTO;
1196 
1197     if (CodeGenOpts.OptimizationLevel == 0) {
1198       // If we reached here with a non-empty index file name, then the index
1199       // file was empty and we are not performing ThinLTO backend compilation
1200       // (used in testing in a distributed build environment). Drop any the type
1201       // test assume sequences inserted for whole program vtables so that
1202       // codegen doesn't complain.
1203       if (!CodeGenOpts.ThinLTOIndexFile.empty())
1204         MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1205                                        /*ImportSummary=*/nullptr,
1206                                        /*DropTypeTests=*/true));
1207       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1208         MPM.addPass(GCOVProfilerPass(*Options));
1209       if (Optional<InstrProfOptions> Options =
1210               getInstrProfOptions(CodeGenOpts, LangOpts))
1211         MPM.addPass(InstrProfiling(*Options, false));
1212 
1213       // Build a minimal pipeline based on the semantics required by Clang,
1214       // which is just that always inlining occurs. Further, disable generating
1215       // lifetime intrinsics to avoid enabling further optimizations during
1216       // code generation.
1217       // However, we need to insert lifetime intrinsics to avoid invalid access
1218       // caused by multithreaded coroutines.
1219       MPM.addPass(
1220           AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/LangOpts.Coroutines));
1221 
1222       // At -O0, we can still do PGO. Add all the requested passes for
1223       // instrumentation PGO, if requested.
1224       if (PGOOpt && (PGOOpt->Action == PGOOptions::IRInstr ||
1225                      PGOOpt->Action == PGOOptions::IRUse))
1226         PB.addPGOInstrPassesForO0(
1227             MPM, CodeGenOpts.DebugPassManager,
1228             /* RunProfileGen */ (PGOOpt->Action == PGOOptions::IRInstr),
1229             /* IsCS */ false, PGOOpt->ProfileFile,
1230             PGOOpt->ProfileRemappingFile);
1231 
1232       // At -O0 we directly run necessary sanitizer passes.
1233       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1234         MPM.addPass(createModuleToFunctionPassAdaptor(BoundsCheckingPass()));
1235 
1236       // Add UniqueInternalLinkageNames Pass which renames internal linkage
1237       // symbols with unique names.
1238       if (CodeGenOpts.UniqueInternalLinkageNames) {
1239         MPM.addPass(UniqueInternalLinkageNamesPass());
1240       }
1241 
1242       // Lastly, add semantically necessary passes for LTO.
1243       if (IsLTO || IsThinLTO) {
1244         MPM.addPass(CanonicalizeAliasesPass());
1245         MPM.addPass(NameAnonGlobalPass());
1246       }
1247     } else {
1248       // Map our optimization levels into one of the distinct levels used to
1249       // configure the pipeline.
1250       PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
1251 
1252       // If we reached here with a non-empty index file name, then the index
1253       // file was empty and we are not performing ThinLTO backend compilation
1254       // (used in testing in a distributed build environment). Drop any the type
1255       // test assume sequences inserted for whole program vtables so that
1256       // codegen doesn't complain.
1257       if (!CodeGenOpts.ThinLTOIndexFile.empty())
1258         PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1259           MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1260                                          /*ImportSummary=*/nullptr,
1261                                          /*DropTypeTests=*/true));
1262         });
1263 
1264       PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1265         MPM.addPass(createModuleToFunctionPassAdaptor(
1266             EntryExitInstrumenterPass(/*PostInlining=*/false)));
1267       });
1268 
1269       // Register callbacks to schedule sanitizer passes at the appropriate part of
1270       // the pipeline.
1271       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1272         PB.registerScalarOptimizerLateEPCallback(
1273             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1274               FPM.addPass(BoundsCheckingPass());
1275             });
1276 
1277       if (CodeGenOpts.SanitizeCoverageType ||
1278           CodeGenOpts.SanitizeCoverageIndirectCalls ||
1279           CodeGenOpts.SanitizeCoverageTraceCmp) {
1280         PB.registerOptimizerLastEPCallback(
1281             [this](ModulePassManager &MPM,
1282                    PassBuilder::OptimizationLevel Level) {
1283               auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
1284               MPM.addPass(ModuleSanitizerCoveragePass(
1285                   SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
1286                   CodeGenOpts.SanitizeCoverageBlocklistFiles));
1287             });
1288       }
1289 
1290       if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
1291         int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
1292         bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Memory);
1293         PB.registerOptimizerLastEPCallback(
1294             [TrackOrigins, Recover](ModulePassManager &MPM,
1295                                     PassBuilder::OptimizationLevel Level) {
1296               MPM.addPass(MemorySanitizerPass({TrackOrigins, Recover, false}));
1297               MPM.addPass(createModuleToFunctionPassAdaptor(
1298                   MemorySanitizerPass({TrackOrigins, Recover, false})));
1299             });
1300       }
1301       if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1302         PB.registerOptimizerLastEPCallback(
1303             [](ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
1304               MPM.addPass(ThreadSanitizerPass());
1305               MPM.addPass(
1306                   createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
1307             });
1308       }
1309       if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
1310         bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Address);
1311         bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
1312         bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1313         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
1314         PB.registerOptimizerLastEPCallback(
1315             [Recover, UseAfterScope, ModuleUseAfterScope, UseOdrIndicator](
1316                 ModulePassManager &MPM, PassBuilder::OptimizationLevel Level) {
1317               MPM.addPass(
1318                   RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1319               MPM.addPass(ModuleAddressSanitizerPass(
1320                   /*CompileKernel=*/false, Recover, ModuleUseAfterScope,
1321                   UseOdrIndicator));
1322               MPM.addPass(
1323                   createModuleToFunctionPassAdaptor(AddressSanitizerPass(
1324                       /*CompileKernel=*/false, Recover, UseAfterScope)));
1325             });
1326       }
1327       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1328         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1329           MPM.addPass(GCOVProfilerPass(*Options));
1330         });
1331       if (Optional<InstrProfOptions> Options =
1332               getInstrProfOptions(CodeGenOpts, LangOpts))
1333         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1334           MPM.addPass(InstrProfiling(*Options, false));
1335         });
1336 
1337       // Add UniqueInternalLinkageNames Pass which renames internal linkage
1338       // symbols with unique names.
1339       if (CodeGenOpts.UniqueInternalLinkageNames) {
1340         MPM.addPass(UniqueInternalLinkageNamesPass());
1341       }
1342 
1343       if (IsThinLTO) {
1344         MPM = PB.buildThinLTOPreLinkDefaultPipeline(
1345             Level, CodeGenOpts.DebugPassManager);
1346         MPM.addPass(CanonicalizeAliasesPass());
1347         MPM.addPass(NameAnonGlobalPass());
1348       } else if (IsLTO) {
1349         MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
1350                                                 CodeGenOpts.DebugPassManager);
1351         MPM.addPass(CanonicalizeAliasesPass());
1352         MPM.addPass(NameAnonGlobalPass());
1353       } else {
1354         MPM = PB.buildPerModuleDefaultPipeline(Level,
1355                                                CodeGenOpts.DebugPassManager);
1356       }
1357     }
1358 
1359     if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1360       bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1361       MPM.addPass(HWAddressSanitizerPass(
1362           /*CompileKernel=*/false, Recover));
1363     }
1364     if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1365       MPM.addPass(HWAddressSanitizerPass(
1366           /*CompileKernel=*/true, /*Recover=*/true));
1367     }
1368 
1369     if (CodeGenOpts.OptimizationLevel == 0) {
1370       addCoroutinePassesAtO0(MPM, LangOpts, CodeGenOpts);
1371       addSanitizersAtO0(MPM, TargetTriple, LangOpts, CodeGenOpts);
1372     }
1373   }
1374 
1375   // FIXME: We still use the legacy pass manager to do code generation. We
1376   // create that pass manager here and use it as needed below.
1377   legacy::PassManager CodeGenPasses;
1378   bool NeedCodeGen = false;
1379   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1380 
1381   // Append any output we need to the pass manager.
1382   switch (Action) {
1383   case Backend_EmitNothing:
1384     break;
1385 
1386   case Backend_EmitBC:
1387     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1388       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1389         ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1390         if (!ThinLinkOS)
1391           return;
1392       }
1393       TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1394                                CodeGenOpts.EnableSplitLTOUnit);
1395       MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
1396                                                            : nullptr));
1397     } else {
1398       // Emit a module summary by default for Regular LTO except for ld64
1399       // targets
1400       bool EmitLTOSummary =
1401           (CodeGenOpts.PrepareForLTO &&
1402            !CodeGenOpts.DisableLLVMPasses &&
1403            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1404                llvm::Triple::Apple);
1405       if (EmitLTOSummary) {
1406         if (!TheModule->getModuleFlag("ThinLTO"))
1407           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1408         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1409                                  uint32_t(1));
1410       }
1411       MPM.addPass(
1412           BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1413     }
1414     break;
1415 
1416   case Backend_EmitLL:
1417     MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1418     break;
1419 
1420   case Backend_EmitAssembly:
1421   case Backend_EmitMCNull:
1422   case Backend_EmitObj:
1423     NeedCodeGen = true;
1424     CodeGenPasses.add(
1425         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1426     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1427       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1428       if (!DwoOS)
1429         return;
1430     }
1431     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1432                        DwoOS ? &DwoOS->os() : nullptr))
1433       // FIXME: Should we handle this error differently?
1434       return;
1435     break;
1436   }
1437 
1438   // Before executing passes, print the final values of the LLVM options.
1439   cl::PrintOptionValues();
1440 
1441   // Now that we have all of the passes ready, run them.
1442   {
1443     PrettyStackTraceString CrashInfo("Optimizer");
1444     MPM.run(*TheModule, MAM);
1445   }
1446 
1447   // Now if needed, run the legacy PM for codegen.
1448   if (NeedCodeGen) {
1449     PrettyStackTraceString CrashInfo("Code generation");
1450     CodeGenPasses.run(*TheModule);
1451   }
1452 
1453   if (ThinLinkOS)
1454     ThinLinkOS->keep();
1455   if (DwoOS)
1456     DwoOS->keep();
1457 }
1458 
1459 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
1460   Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1461   if (!BMsOrErr)
1462     return BMsOrErr.takeError();
1463 
1464   // The bitcode file may contain multiple modules, we want the one that is
1465   // marked as being the ThinLTO module.
1466   if (const BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr))
1467     return *Bm;
1468 
1469   return make_error<StringError>("Could not find module summary",
1470                                  inconvertibleErrorCode());
1471 }
1472 
1473 BitcodeModule *clang::FindThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
1474   for (BitcodeModule &BM : BMs) {
1475     Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
1476     if (LTOInfo && LTOInfo->IsThinLTO)
1477       return &BM;
1478   }
1479   return nullptr;
1480 }
1481 
1482 static void runThinLTOBackend(
1483     DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M,
1484     const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts,
1485     const clang::TargetOptions &TOpts, const LangOptions &LOpts,
1486     std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile,
1487     std::string ProfileRemapping, BackendAction Action) {
1488   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1489       ModuleToDefinedGVSummaries;
1490   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1491 
1492   setCommandLineOpts(CGOpts);
1493 
1494   // We can simply import the values mentioned in the combined index, since
1495   // we should only invoke this using the individual indexes written out
1496   // via a WriteIndexesThinBackend.
1497   FunctionImporter::ImportMapTy ImportList;
1498   for (auto &GlobalList : *CombinedIndex) {
1499     // Ignore entries for undefined references.
1500     if (GlobalList.second.SummaryList.empty())
1501       continue;
1502 
1503     auto GUID = GlobalList.first;
1504     for (auto &Summary : GlobalList.second.SummaryList) {
1505       // Skip the summaries for the importing module. These are included to
1506       // e.g. record required linkage changes.
1507       if (Summary->modulePath() == M->getModuleIdentifier())
1508         continue;
1509       // Add an entry to provoke importing by thinBackend.
1510       ImportList[Summary->modulePath()].insert(GUID);
1511     }
1512   }
1513 
1514   std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
1515   MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
1516 
1517   for (auto &I : ImportList) {
1518     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1519         llvm::MemoryBuffer::getFile(I.first());
1520     if (!MBOrErr) {
1521       errs() << "Error loading imported file '" << I.first()
1522              << "': " << MBOrErr.getError().message() << "\n";
1523       return;
1524     }
1525 
1526     Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1527     if (!BMOrErr) {
1528       handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
1529         errs() << "Error loading imported file '" << I.first()
1530                << "': " << EIB.message() << '\n';
1531       });
1532       return;
1533     }
1534     ModuleMap.insert({I.first(), *BMOrErr});
1535 
1536     OwnedImports.push_back(std::move(*MBOrErr));
1537   }
1538   auto AddStream = [&](size_t Task) {
1539     return std::make_unique<lto::NativeObjectStream>(std::move(OS));
1540   };
1541   lto::Config Conf;
1542   if (CGOpts.SaveTempsFilePrefix != "") {
1543     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1544                                     /* UseInputModulePath */ false)) {
1545       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1546         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1547                << '\n';
1548       });
1549     }
1550   }
1551   Conf.CPU = TOpts.CPU;
1552   Conf.CodeModel = getCodeModel(CGOpts);
1553   Conf.MAttrs = TOpts.Features;
1554   Conf.RelocModel = CGOpts.RelocationModel;
1555   Conf.CGOptLevel = getCGOptLevel(CGOpts);
1556   Conf.OptLevel = CGOpts.OptimizationLevel;
1557   initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1558   Conf.SampleProfile = std::move(SampleProfile);
1559   Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1560   // For historical reasons, loop interleaving is set to mirror setting for loop
1561   // unrolling.
1562   Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1563   Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1564   Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1565   Conf.PTO.CallGraphProfile = CGOpts.CallGraphProfile;
1566 
1567   // Context sensitive profile.
1568   if (CGOpts.hasProfileCSIRInstr()) {
1569     Conf.RunCSIRInstr = true;
1570     Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1571   } else if (CGOpts.hasProfileCSIRUse()) {
1572     Conf.RunCSIRInstr = false;
1573     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1574   }
1575 
1576   Conf.ProfileRemapping = std::move(ProfileRemapping);
1577   Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
1578   Conf.DebugPassManager = CGOpts.DebugPassManager;
1579   Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1580   Conf.RemarksFilename = CGOpts.OptRecordFile;
1581   Conf.RemarksPasses = CGOpts.OptRecordPasses;
1582   Conf.RemarksFormat = CGOpts.OptRecordFormat;
1583   Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1584   Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1585   switch (Action) {
1586   case Backend_EmitNothing:
1587     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1588       return false;
1589     };
1590     break;
1591   case Backend_EmitLL:
1592     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1593       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1594       return false;
1595     };
1596     break;
1597   case Backend_EmitBC:
1598     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1599       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1600       return false;
1601     };
1602     break;
1603   default:
1604     Conf.CGFileType = getCodeGenFileType(Action);
1605     break;
1606   }
1607   if (Error E = thinBackend(
1608           Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1609           ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1610     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1611       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1612     });
1613   }
1614 }
1615 
1616 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1617                               const HeaderSearchOptions &HeaderOpts,
1618                               const CodeGenOptions &CGOpts,
1619                               const clang::TargetOptions &TOpts,
1620                               const LangOptions &LOpts,
1621                               const llvm::DataLayout &TDesc, Module *M,
1622                               BackendAction Action,
1623                               std::unique_ptr<raw_pwrite_stream> OS) {
1624 
1625   llvm::TimeTraceScope TimeScope("Backend");
1626 
1627   std::unique_ptr<llvm::Module> EmptyModule;
1628   if (!CGOpts.ThinLTOIndexFile.empty()) {
1629     // If we are performing a ThinLTO importing compile, load the function index
1630     // into memory and pass it into runThinLTOBackend, which will run the
1631     // function importer and invoke LTO passes.
1632     Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1633         llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1634                                            /*IgnoreEmptyThinLTOIndexFile*/true);
1635     if (!IndexOrErr) {
1636       logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1637                             "Error loading index file '" +
1638                             CGOpts.ThinLTOIndexFile + "': ");
1639       return;
1640     }
1641     std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1642     // A null CombinedIndex means we should skip ThinLTO compilation
1643     // (LLVM will optionally ignore empty index files, returning null instead
1644     // of an error).
1645     if (CombinedIndex) {
1646       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1647         runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
1648                           TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
1649                           CGOpts.ProfileRemappingFile, Action);
1650         return;
1651       }
1652       // Distributed indexing detected that nothing from the module is needed
1653       // for the final linking. So we can skip the compilation. We sill need to
1654       // output an empty object file to make sure that a linker does not fail
1655       // trying to read it. Also for some features, like CFI, we must skip
1656       // the compilation as CombinedIndex does not contain all required
1657       // information.
1658       EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
1659       EmptyModule->setTargetTriple(M->getTargetTriple());
1660       M = EmptyModule.get();
1661     }
1662   }
1663 
1664   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1665 
1666   if (CGOpts.ExperimentalNewPassManager)
1667     AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1668   else
1669     AsmHelper.EmitAssembly(Action, std::move(OS));
1670 
1671   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1672   // DataLayout.
1673   if (AsmHelper.TM) {
1674     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1675     if (DLDesc != TDesc.getStringRepresentation()) {
1676       unsigned DiagID = Diags.getCustomDiagID(
1677           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1678                                     "expected target description '%1'");
1679       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1680     }
1681   }
1682 }
1683 
1684 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1685 // __LLVM,__bitcode section.
1686 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1687                          llvm::MemoryBufferRef Buf) {
1688   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1689     return;
1690   llvm::EmbedBitcodeInModule(
1691       *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1692       CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1693       &CGOpts.CmdArgs);
1694 }
1695