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