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