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   Options.EnableDebugEntryValues = CodeGenOpts.EnableDebugEntryValues;
475 
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       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
868       if (!DwoOS)
869         return;
870     }
871     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
872                        DwoOS ? &DwoOS->os() : nullptr))
873       return;
874   }
875 
876   // Before executing passes, print the final values of the LLVM options.
877   cl::PrintOptionValues();
878 
879   // Run passes. For now we do all passes at once, but eventually we
880   // would like to have the option of streaming code generation.
881 
882   {
883     PrettyStackTraceString CrashInfo("Per-function optimization");
884 
885     PerFunctionPasses.doInitialization();
886     for (Function &F : *TheModule)
887       if (!F.isDeclaration())
888         PerFunctionPasses.run(F);
889     PerFunctionPasses.doFinalization();
890   }
891 
892   {
893     PrettyStackTraceString CrashInfo("Per-module optimization passes");
894     PerModulePasses.run(*TheModule);
895   }
896 
897   {
898     PrettyStackTraceString CrashInfo("Code generation");
899     CodeGenPasses.run(*TheModule);
900   }
901 
902   if (ThinLinkOS)
903     ThinLinkOS->keep();
904   if (DwoOS)
905     DwoOS->keep();
906 }
907 
908 static PassBuilder::OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
909   switch (Opts.OptimizationLevel) {
910   default:
911     llvm_unreachable("Invalid optimization level!");
912 
913   case 1:
914     return PassBuilder::O1;
915 
916   case 2:
917     switch (Opts.OptimizeSize) {
918     default:
919       llvm_unreachable("Invalid optimization level for size!");
920 
921     case 0:
922       return PassBuilder::O2;
923 
924     case 1:
925       return PassBuilder::Os;
926 
927     case 2:
928       return PassBuilder::Oz;
929     }
930 
931   case 3:
932     return PassBuilder::O3;
933   }
934 }
935 
936 static void addSanitizersAtO0(ModulePassManager &MPM,
937                               const Triple &TargetTriple,
938                               const LangOptions &LangOpts,
939                               const CodeGenOptions &CodeGenOpts) {
940   auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
941     MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
942     bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
943     MPM.addPass(createModuleToFunctionPassAdaptor(AddressSanitizerPass(
944         CompileKernel, Recover, CodeGenOpts.SanitizeAddressUseAfterScope)));
945     bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
946     MPM.addPass(
947         ModuleAddressSanitizerPass(CompileKernel, Recover, ModuleUseAfterScope,
948                                    CodeGenOpts.SanitizeAddressUseOdrIndicator));
949   };
950 
951   if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
952     ASanPass(SanitizerKind::Address, /*CompileKernel=*/false);
953   }
954 
955   if (LangOpts.Sanitize.has(SanitizerKind::KernelAddress)) {
956     ASanPass(SanitizerKind::KernelAddress, /*CompileKernel=*/true);
957   }
958 
959   if (LangOpts.Sanitize.has(SanitizerKind::Memory)) {
960     MPM.addPass(createModuleToFunctionPassAdaptor(MemorySanitizerPass({})));
961   }
962 
963   if (LangOpts.Sanitize.has(SanitizerKind::KernelMemory)) {
964     MPM.addPass(createModuleToFunctionPassAdaptor(
965         MemorySanitizerPass({0, false, /*Kernel=*/true})));
966   }
967 
968   if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
969     MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
970   }
971 
972   if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
973     bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
974     MPM.addPass(createModuleToFunctionPassAdaptor(
975         HWAddressSanitizerPass(/*CompileKernel=*/false, Recover)));
976   }
977 
978   if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
979     MPM.addPass(createModuleToFunctionPassAdaptor(
980         HWAddressSanitizerPass(/*CompileKernel=*/true, /*Recover=*/true)));
981   }
982 }
983 
984 /// A clean version of `EmitAssembly` that uses the new pass manager.
985 ///
986 /// Not all features are currently supported in this system, but where
987 /// necessary it falls back to the legacy pass manager to at least provide
988 /// basic functionality.
989 ///
990 /// This API is planned to have its functionality finished and then to replace
991 /// `EmitAssembly` at some point in the future when the default switches.
992 void EmitAssemblyHelper::EmitAssemblyWithNewPassManager(
993     BackendAction Action, std::unique_ptr<raw_pwrite_stream> OS) {
994   TimeRegion Region(FrontendTimesIsEnabled ? &CodeGenerationTime : nullptr);
995   setCommandLineOpts(CodeGenOpts);
996 
997   bool RequiresCodeGen = (Action != Backend_EmitNothing &&
998                           Action != Backend_EmitBC &&
999                           Action != Backend_EmitLL);
1000   CreateTargetMachine(RequiresCodeGen);
1001 
1002   if (RequiresCodeGen && !TM)
1003     return;
1004   if (TM)
1005     TheModule->setDataLayout(TM->createDataLayout());
1006 
1007   Optional<PGOOptions> PGOOpt;
1008 
1009   if (CodeGenOpts.hasProfileIRInstr())
1010     // -fprofile-generate.
1011     PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
1012                             ? DefaultProfileGenName
1013                             : CodeGenOpts.InstrProfileOutput,
1014                         "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
1015                         CodeGenOpts.DebugInfoForProfiling);
1016   else if (CodeGenOpts.hasProfileIRUse()) {
1017     // -fprofile-use.
1018     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
1019                                                     : PGOOptions::NoCSAction;
1020     PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
1021                         CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
1022                         CSAction, CodeGenOpts.DebugInfoForProfiling);
1023   } else if (!CodeGenOpts.SampleProfileFile.empty())
1024     // -fprofile-sample-use
1025     PGOOpt =
1026         PGOOptions(CodeGenOpts.SampleProfileFile, "",
1027                    CodeGenOpts.ProfileRemappingFile, PGOOptions::SampleUse,
1028                    PGOOptions::NoCSAction, CodeGenOpts.DebugInfoForProfiling);
1029   else if (CodeGenOpts.DebugInfoForProfiling)
1030     // -fdebug-info-for-profiling
1031     PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
1032                         PGOOptions::NoCSAction, true);
1033 
1034   // Check to see if we want to generate a CS profile.
1035   if (CodeGenOpts.hasProfileCSIRInstr()) {
1036     assert(!CodeGenOpts.hasProfileCSIRUse() &&
1037            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
1038            "the same time");
1039     if (PGOOpt.hasValue()) {
1040       assert(PGOOpt->Action != PGOOptions::IRInstr &&
1041              PGOOpt->Action != PGOOptions::SampleUse &&
1042              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
1043              " pass");
1044       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
1045                                      ? DefaultProfileGenName
1046                                      : CodeGenOpts.InstrProfileOutput;
1047       PGOOpt->CSAction = PGOOptions::CSIRInstr;
1048     } else
1049       PGOOpt = PGOOptions("",
1050                           CodeGenOpts.InstrProfileOutput.empty()
1051                               ? DefaultProfileGenName
1052                               : CodeGenOpts.InstrProfileOutput,
1053                           "", PGOOptions::NoAction, PGOOptions::CSIRInstr,
1054                           CodeGenOpts.DebugInfoForProfiling);
1055   }
1056 
1057   PipelineTuningOptions PTO;
1058   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
1059   // For historical reasons, loop interleaving is set to mirror setting for loop
1060   // unrolling.
1061   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
1062   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
1063   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
1064 
1065   PassBuilder PB(TM.get(), PTO, PGOOpt);
1066 
1067   // Attempt to load pass plugins and register their callbacks with PB.
1068   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
1069     auto PassPlugin = PassPlugin::Load(PluginFN);
1070     if (PassPlugin) {
1071       PassPlugin->registerPassBuilderCallbacks(PB);
1072     } else {
1073       Diags.Report(diag::err_fe_unable_to_load_plugin)
1074           << PluginFN << toString(PassPlugin.takeError());
1075     }
1076   }
1077 
1078   LoopAnalysisManager LAM(CodeGenOpts.DebugPassManager);
1079   FunctionAnalysisManager FAM(CodeGenOpts.DebugPassManager);
1080   CGSCCAnalysisManager CGAM(CodeGenOpts.DebugPassManager);
1081   ModuleAnalysisManager MAM(CodeGenOpts.DebugPassManager);
1082 
1083   // Register the AA manager first so that our version is the one used.
1084   FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); });
1085 
1086   // Register the target library analysis directly and give it a customized
1087   // preset TLI.
1088   Triple TargetTriple(TheModule->getTargetTriple());
1089   std::unique_ptr<TargetLibraryInfoImpl> TLII(
1090       createTLII(TargetTriple, CodeGenOpts));
1091   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
1092   MAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
1093 
1094   // Register all the basic analyses with the managers.
1095   PB.registerModuleAnalyses(MAM);
1096   PB.registerCGSCCAnalyses(CGAM);
1097   PB.registerFunctionAnalyses(FAM);
1098   PB.registerLoopAnalyses(LAM);
1099   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
1100 
1101   ModulePassManager MPM(CodeGenOpts.DebugPassManager);
1102 
1103   if (!CodeGenOpts.DisableLLVMPasses) {
1104     bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
1105     bool IsLTO = CodeGenOpts.PrepareForLTO;
1106 
1107     if (CodeGenOpts.OptimizationLevel == 0) {
1108       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1109         MPM.addPass(GCOVProfilerPass(*Options));
1110       if (Optional<InstrProfOptions> Options =
1111               getInstrProfOptions(CodeGenOpts, LangOpts))
1112         MPM.addPass(InstrProfiling(*Options, false));
1113 
1114       // Build a minimal pipeline based on the semantics required by Clang,
1115       // which is just that always inlining occurs. Further, disable generating
1116       // lifetime intrinsics to avoid enabling further optimizations during
1117       // code generation.
1118       MPM.addPass(AlwaysInlinerPass(/*InsertLifetimeIntrinsics=*/false));
1119 
1120       // At -O0 we directly run necessary sanitizer passes.
1121       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1122         MPM.addPass(createModuleToFunctionPassAdaptor(BoundsCheckingPass()));
1123 
1124       // Lastly, add semantically necessary passes for LTO.
1125       if (IsLTO || IsThinLTO) {
1126         MPM.addPass(CanonicalizeAliasesPass());
1127         MPM.addPass(NameAnonGlobalPass());
1128       }
1129     } else {
1130       // Map our optimization levels into one of the distinct levels used to
1131       // configure the pipeline.
1132       PassBuilder::OptimizationLevel Level = mapToLevel(CodeGenOpts);
1133 
1134       PB.registerPipelineStartEPCallback([](ModulePassManager &MPM) {
1135         MPM.addPass(createModuleToFunctionPassAdaptor(
1136             EntryExitInstrumenterPass(/*PostInlining=*/false)));
1137       });
1138 
1139       // Register callbacks to schedule sanitizer passes at the appropriate part of
1140       // the pipeline.
1141       // FIXME: either handle asan/the remaining sanitizers or error out
1142       if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1143         PB.registerScalarOptimizerLateEPCallback(
1144             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1145               FPM.addPass(BoundsCheckingPass());
1146             });
1147       if (LangOpts.Sanitize.has(SanitizerKind::Memory))
1148         PB.registerOptimizerLastEPCallback(
1149             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1150               FPM.addPass(MemorySanitizerPass({}));
1151             });
1152       if (LangOpts.Sanitize.has(SanitizerKind::Thread))
1153         PB.registerOptimizerLastEPCallback(
1154             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1155               FPM.addPass(ThreadSanitizerPass());
1156             });
1157       if (LangOpts.Sanitize.has(SanitizerKind::Address)) {
1158         PB.registerPipelineStartEPCallback([&](ModulePassManager &MPM) {
1159           MPM.addPass(
1160               RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1161         });
1162         bool Recover = CodeGenOpts.SanitizeRecover.has(SanitizerKind::Address);
1163         bool UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
1164         PB.registerOptimizerLastEPCallback(
1165             [Recover, UseAfterScope](FunctionPassManager &FPM,
1166                                      PassBuilder::OptimizationLevel Level) {
1167               FPM.addPass(AddressSanitizerPass(
1168                   /*CompileKernel=*/false, Recover, UseAfterScope));
1169             });
1170         bool ModuleUseAfterScope = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1171         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
1172         PB.registerPipelineStartEPCallback(
1173             [Recover, ModuleUseAfterScope,
1174              UseOdrIndicator](ModulePassManager &MPM) {
1175               MPM.addPass(ModuleAddressSanitizerPass(
1176                   /*CompileKernel=*/false, Recover, ModuleUseAfterScope,
1177                   UseOdrIndicator));
1178             });
1179       }
1180       if (LangOpts.Sanitize.has(SanitizerKind::HWAddress)) {
1181         bool Recover =
1182             CodeGenOpts.SanitizeRecover.has(SanitizerKind::HWAddress);
1183         PB.registerOptimizerLastEPCallback(
1184             [Recover](FunctionPassManager &FPM,
1185                       PassBuilder::OptimizationLevel Level) {
1186               FPM.addPass(HWAddressSanitizerPass(
1187                   /*CompileKernel=*/false, Recover));
1188             });
1189       }
1190       if (LangOpts.Sanitize.has(SanitizerKind::KernelHWAddress)) {
1191         PB.registerOptimizerLastEPCallback(
1192             [](FunctionPassManager &FPM, PassBuilder::OptimizationLevel Level) {
1193               FPM.addPass(HWAddressSanitizerPass(
1194                   /*CompileKernel=*/true, /*Recover=*/true));
1195             });
1196       }
1197       if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts))
1198         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1199           MPM.addPass(GCOVProfilerPass(*Options));
1200         });
1201       if (Optional<InstrProfOptions> Options =
1202               getInstrProfOptions(CodeGenOpts, LangOpts))
1203         PB.registerPipelineStartEPCallback([Options](ModulePassManager &MPM) {
1204           MPM.addPass(InstrProfiling(*Options, false));
1205         });
1206 
1207       if (IsThinLTO) {
1208         MPM = PB.buildThinLTOPreLinkDefaultPipeline(
1209             Level, CodeGenOpts.DebugPassManager);
1210         MPM.addPass(CanonicalizeAliasesPass());
1211         MPM.addPass(NameAnonGlobalPass());
1212       } else if (IsLTO) {
1213         MPM = PB.buildLTOPreLinkDefaultPipeline(Level,
1214                                                 CodeGenOpts.DebugPassManager);
1215         MPM.addPass(CanonicalizeAliasesPass());
1216         MPM.addPass(NameAnonGlobalPass());
1217       } else {
1218         MPM = PB.buildPerModuleDefaultPipeline(Level,
1219                                                CodeGenOpts.DebugPassManager);
1220       }
1221     }
1222 
1223     if (CodeGenOpts.OptimizationLevel == 0)
1224       addSanitizersAtO0(MPM, TargetTriple, LangOpts, CodeGenOpts);
1225 
1226     if (CodeGenOpts.hasProfileIRInstr()) {
1227       // This file is stored as the ProfileFile.
1228       MPM.addPass(PGOInstrumentationGenCreateVar(PGOOpt->ProfileFile));
1229     }
1230   }
1231 
1232   // FIXME: We still use the legacy pass manager to do code generation. We
1233   // create that pass manager here and use it as needed below.
1234   legacy::PassManager CodeGenPasses;
1235   bool NeedCodeGen = false;
1236   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1237 
1238   // Append any output we need to the pass manager.
1239   switch (Action) {
1240   case Backend_EmitNothing:
1241     break;
1242 
1243   case Backend_EmitBC:
1244     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1245       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1246         ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1247         if (!ThinLinkOS)
1248           return;
1249       }
1250       TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1251                                CodeGenOpts.EnableSplitLTOUnit);
1252       MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
1253                                                            : nullptr));
1254     } else {
1255       // Emit a module summary by default for Regular LTO except for ld64
1256       // targets
1257       bool EmitLTOSummary =
1258           (CodeGenOpts.PrepareForLTO &&
1259            !CodeGenOpts.DisableLLVMPasses &&
1260            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1261                llvm::Triple::Apple);
1262       if (EmitLTOSummary) {
1263         if (!TheModule->getModuleFlag("ThinLTO"))
1264           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1265         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1266                                  CodeGenOpts.EnableSplitLTOUnit);
1267       }
1268       MPM.addPass(
1269           BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1270     }
1271     break;
1272 
1273   case Backend_EmitLL:
1274     MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1275     break;
1276 
1277   case Backend_EmitAssembly:
1278   case Backend_EmitMCNull:
1279   case Backend_EmitObj:
1280     NeedCodeGen = true;
1281     CodeGenPasses.add(
1282         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1283     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1284       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1285       if (!DwoOS)
1286         return;
1287     }
1288     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1289                        DwoOS ? &DwoOS->os() : nullptr))
1290       // FIXME: Should we handle this error differently?
1291       return;
1292     break;
1293   }
1294 
1295   // Before executing passes, print the final values of the LLVM options.
1296   cl::PrintOptionValues();
1297 
1298   // Now that we have all of the passes ready, run them.
1299   {
1300     PrettyStackTraceString CrashInfo("Optimizer");
1301     MPM.run(*TheModule, MAM);
1302   }
1303 
1304   // Now if needed, run the legacy PM for codegen.
1305   if (NeedCodeGen) {
1306     PrettyStackTraceString CrashInfo("Code generation");
1307     CodeGenPasses.run(*TheModule);
1308   }
1309 
1310   if (ThinLinkOS)
1311     ThinLinkOS->keep();
1312   if (DwoOS)
1313     DwoOS->keep();
1314 }
1315 
1316 Expected<BitcodeModule> clang::FindThinLTOModule(MemoryBufferRef MBRef) {
1317   Expected<std::vector<BitcodeModule>> BMsOrErr = getBitcodeModuleList(MBRef);
1318   if (!BMsOrErr)
1319     return BMsOrErr.takeError();
1320 
1321   // The bitcode file may contain multiple modules, we want the one that is
1322   // marked as being the ThinLTO module.
1323   if (const BitcodeModule *Bm = FindThinLTOModule(*BMsOrErr))
1324     return *Bm;
1325 
1326   return make_error<StringError>("Could not find module summary",
1327                                  inconvertibleErrorCode());
1328 }
1329 
1330 BitcodeModule *clang::FindThinLTOModule(MutableArrayRef<BitcodeModule> BMs) {
1331   for (BitcodeModule &BM : BMs) {
1332     Expected<BitcodeLTOInfo> LTOInfo = BM.getLTOInfo();
1333     if (LTOInfo && LTOInfo->IsThinLTO)
1334       return &BM;
1335   }
1336   return nullptr;
1337 }
1338 
1339 static void runThinLTOBackend(ModuleSummaryIndex *CombinedIndex, Module *M,
1340                               const HeaderSearchOptions &HeaderOpts,
1341                               const CodeGenOptions &CGOpts,
1342                               const clang::TargetOptions &TOpts,
1343                               const LangOptions &LOpts,
1344                               std::unique_ptr<raw_pwrite_stream> OS,
1345                               std::string SampleProfile,
1346                               std::string ProfileRemapping,
1347                               BackendAction Action) {
1348   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1349       ModuleToDefinedGVSummaries;
1350   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1351 
1352   setCommandLineOpts(CGOpts);
1353 
1354   // We can simply import the values mentioned in the combined index, since
1355   // we should only invoke this using the individual indexes written out
1356   // via a WriteIndexesThinBackend.
1357   FunctionImporter::ImportMapTy ImportList;
1358   for (auto &GlobalList : *CombinedIndex) {
1359     // Ignore entries for undefined references.
1360     if (GlobalList.second.SummaryList.empty())
1361       continue;
1362 
1363     auto GUID = GlobalList.first;
1364     for (auto &Summary : GlobalList.second.SummaryList) {
1365       // Skip the summaries for the importing module. These are included to
1366       // e.g. record required linkage changes.
1367       if (Summary->modulePath() == M->getModuleIdentifier())
1368         continue;
1369       // Add an entry to provoke importing by thinBackend.
1370       ImportList[Summary->modulePath()].insert(GUID);
1371     }
1372   }
1373 
1374   std::vector<std::unique_ptr<llvm::MemoryBuffer>> OwnedImports;
1375   MapVector<llvm::StringRef, llvm::BitcodeModule> ModuleMap;
1376 
1377   for (auto &I : ImportList) {
1378     ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> MBOrErr =
1379         llvm::MemoryBuffer::getFile(I.first());
1380     if (!MBOrErr) {
1381       errs() << "Error loading imported file '" << I.first()
1382              << "': " << MBOrErr.getError().message() << "\n";
1383       return;
1384     }
1385 
1386     Expected<BitcodeModule> BMOrErr = FindThinLTOModule(**MBOrErr);
1387     if (!BMOrErr) {
1388       handleAllErrors(BMOrErr.takeError(), [&](ErrorInfoBase &EIB) {
1389         errs() << "Error loading imported file '" << I.first()
1390                << "': " << EIB.message() << '\n';
1391       });
1392       return;
1393     }
1394     ModuleMap.insert({I.first(), *BMOrErr});
1395 
1396     OwnedImports.push_back(std::move(*MBOrErr));
1397   }
1398   auto AddStream = [&](size_t Task) {
1399     return llvm::make_unique<lto::NativeObjectStream>(std::move(OS));
1400   };
1401   lto::Config Conf;
1402   if (CGOpts.SaveTempsFilePrefix != "") {
1403     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1404                                     /* UseInputModulePath */ false)) {
1405       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1406         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1407                << '\n';
1408       });
1409     }
1410   }
1411   Conf.CPU = TOpts.CPU;
1412   Conf.CodeModel = getCodeModel(CGOpts);
1413   Conf.MAttrs = TOpts.Features;
1414   Conf.RelocModel = CGOpts.RelocationModel;
1415   Conf.CGOptLevel = getCGOptLevel(CGOpts);
1416   Conf.OptLevel = CGOpts.OptimizationLevel;
1417   initTargetOptions(Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1418   Conf.SampleProfile = std::move(SampleProfile);
1419 
1420   // Context sensitive profile.
1421   if (CGOpts.hasProfileCSIRInstr()) {
1422     Conf.RunCSIRInstr = true;
1423     Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1424   } else if (CGOpts.hasProfileCSIRUse()) {
1425     Conf.RunCSIRInstr = false;
1426     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1427   }
1428 
1429   Conf.ProfileRemapping = std::move(ProfileRemapping);
1430   Conf.UseNewPM = CGOpts.ExperimentalNewPassManager;
1431   Conf.DebugPassManager = CGOpts.DebugPassManager;
1432   Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1433   Conf.RemarksFilename = CGOpts.OptRecordFile;
1434   Conf.RemarksPasses = CGOpts.OptRecordPasses;
1435   Conf.RemarksFormat = CGOpts.OptRecordFormat;
1436   Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1437   Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1438   switch (Action) {
1439   case Backend_EmitNothing:
1440     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1441       return false;
1442     };
1443     break;
1444   case Backend_EmitLL:
1445     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1446       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1447       return false;
1448     };
1449     break;
1450   case Backend_EmitBC:
1451     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1452       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1453       return false;
1454     };
1455     break;
1456   default:
1457     Conf.CGFileType = getCodeGenFileType(Action);
1458     break;
1459   }
1460   if (Error E = thinBackend(
1461           Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1462           ModuleToDefinedGVSummaries[M->getModuleIdentifier()], ModuleMap)) {
1463     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1464       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1465     });
1466   }
1467 }
1468 
1469 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1470                               const HeaderSearchOptions &HeaderOpts,
1471                               const CodeGenOptions &CGOpts,
1472                               const clang::TargetOptions &TOpts,
1473                               const LangOptions &LOpts,
1474                               const llvm::DataLayout &TDesc, Module *M,
1475                               BackendAction Action,
1476                               std::unique_ptr<raw_pwrite_stream> OS) {
1477 
1478   llvm::TimeTraceScope TimeScope("Backend", StringRef(""));
1479 
1480   std::unique_ptr<llvm::Module> EmptyModule;
1481   if (!CGOpts.ThinLTOIndexFile.empty()) {
1482     // If we are performing a ThinLTO importing compile, load the function index
1483     // into memory and pass it into runThinLTOBackend, which will run the
1484     // function importer and invoke LTO passes.
1485     Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1486         llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1487                                            /*IgnoreEmptyThinLTOIndexFile*/true);
1488     if (!IndexOrErr) {
1489       logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1490                             "Error loading index file '" +
1491                             CGOpts.ThinLTOIndexFile + "': ");
1492       return;
1493     }
1494     std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1495     // A null CombinedIndex means we should skip ThinLTO compilation
1496     // (LLVM will optionally ignore empty index files, returning null instead
1497     // of an error).
1498     if (CombinedIndex) {
1499       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1500         runThinLTOBackend(CombinedIndex.get(), M, HeaderOpts, CGOpts, TOpts,
1501                           LOpts, std::move(OS), CGOpts.SampleProfileFile,
1502                           CGOpts.ProfileRemappingFile, Action);
1503         return;
1504       }
1505       // Distributed indexing detected that nothing from the module is needed
1506       // for the final linking. So we can skip the compilation. We sill need to
1507       // output an empty object file to make sure that a linker does not fail
1508       // trying to read it. Also for some features, like CFI, we must skip
1509       // the compilation as CombinedIndex does not contain all required
1510       // information.
1511       EmptyModule = llvm::make_unique<llvm::Module>("empty", M->getContext());
1512       EmptyModule->setTargetTriple(M->getTargetTriple());
1513       M = EmptyModule.get();
1514     }
1515   }
1516 
1517   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1518 
1519   if (CGOpts.ExperimentalNewPassManager)
1520     AsmHelper.EmitAssemblyWithNewPassManager(Action, std::move(OS));
1521   else
1522     AsmHelper.EmitAssembly(Action, std::move(OS));
1523 
1524   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1525   // DataLayout.
1526   if (AsmHelper.TM) {
1527     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1528     if (DLDesc != TDesc.getStringRepresentation()) {
1529       unsigned DiagID = Diags.getCustomDiagID(
1530           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1531                                     "expected target description '%1'");
1532       Diags.Report(DiagID) << DLDesc << TDesc.getStringRepresentation();
1533     }
1534   }
1535 }
1536 
1537 static const char* getSectionNameForBitcode(const Triple &T) {
1538   switch (T.getObjectFormat()) {
1539   case Triple::MachO:
1540     return "__LLVM,__bitcode";
1541   case Triple::COFF:
1542   case Triple::ELF:
1543   case Triple::Wasm:
1544   case Triple::UnknownObjectFormat:
1545     return ".llvmbc";
1546   case Triple::XCOFF:
1547     llvm_unreachable("XCOFF is not yet implemented");
1548     break;
1549   }
1550   llvm_unreachable("Unimplemented ObjectFormatType");
1551 }
1552 
1553 static const char* getSectionNameForCommandline(const Triple &T) {
1554   switch (T.getObjectFormat()) {
1555   case Triple::MachO:
1556     return "__LLVM,__cmdline";
1557   case Triple::COFF:
1558   case Triple::ELF:
1559   case Triple::Wasm:
1560   case Triple::UnknownObjectFormat:
1561     return ".llvmcmd";
1562   case Triple::XCOFF:
1563     llvm_unreachable("XCOFF is not yet implemented");
1564     break;
1565   }
1566   llvm_unreachable("Unimplemented ObjectFormatType");
1567 }
1568 
1569 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1570 // __LLVM,__bitcode section.
1571 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1572                          llvm::MemoryBufferRef Buf) {
1573   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1574     return;
1575 
1576   // Save llvm.compiler.used and remote it.
1577   SmallVector<Constant*, 2> UsedArray;
1578   SmallPtrSet<GlobalValue*, 4> UsedGlobals;
1579   Type *UsedElementType = Type::getInt8Ty(M->getContext())->getPointerTo(0);
1580   GlobalVariable *Used = collectUsedGlobalVariables(*M, UsedGlobals, true);
1581   for (auto *GV : UsedGlobals) {
1582     if (GV->getName() != "llvm.embedded.module" &&
1583         GV->getName() != "llvm.cmdline")
1584       UsedArray.push_back(
1585           ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1586   }
1587   if (Used)
1588     Used->eraseFromParent();
1589 
1590   // Embed the bitcode for the llvm module.
1591   std::string Data;
1592   ArrayRef<uint8_t> ModuleData;
1593   Triple T(M->getTargetTriple());
1594   // Create a constant that contains the bitcode.
1595   // In case of embedding a marker, ignore the input Buf and use the empty
1596   // ArrayRef. It is also legal to create a bitcode marker even Buf is empty.
1597   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker) {
1598     if (!isBitcode((const unsigned char *)Buf.getBufferStart(),
1599                    (const unsigned char *)Buf.getBufferEnd())) {
1600       // If the input is LLVM Assembly, bitcode is produced by serializing
1601       // the module. Use-lists order need to be perserved in this case.
1602       llvm::raw_string_ostream OS(Data);
1603       llvm::WriteBitcodeToFile(*M, OS, /* ShouldPreserveUseListOrder */ true);
1604       ModuleData =
1605           ArrayRef<uint8_t>((const uint8_t *)OS.str().data(), OS.str().size());
1606     } else
1607       // If the input is LLVM bitcode, write the input byte stream directly.
1608       ModuleData = ArrayRef<uint8_t>((const uint8_t *)Buf.getBufferStart(),
1609                                      Buf.getBufferSize());
1610   }
1611   llvm::Constant *ModuleConstant =
1612       llvm::ConstantDataArray::get(M->getContext(), ModuleData);
1613   llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1614       *M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
1615       ModuleConstant);
1616   GV->setSection(getSectionNameForBitcode(T));
1617   UsedArray.push_back(
1618       ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1619   if (llvm::GlobalVariable *Old =
1620           M->getGlobalVariable("llvm.embedded.module", true)) {
1621     assert(Old->hasOneUse() &&
1622            "llvm.embedded.module can only be used once in llvm.compiler.used");
1623     GV->takeName(Old);
1624     Old->eraseFromParent();
1625   } else {
1626     GV->setName("llvm.embedded.module");
1627   }
1628 
1629   // Skip if only bitcode needs to be embedded.
1630   if (CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode) {
1631     // Embed command-line options.
1632     ArrayRef<uint8_t> CmdData(const_cast<uint8_t *>(CGOpts.CmdArgs.data()),
1633                               CGOpts.CmdArgs.size());
1634     llvm::Constant *CmdConstant =
1635       llvm::ConstantDataArray::get(M->getContext(), CmdData);
1636     GV = new llvm::GlobalVariable(*M, CmdConstant->getType(), true,
1637                                   llvm::GlobalValue::PrivateLinkage,
1638                                   CmdConstant);
1639     GV->setSection(getSectionNameForCommandline(T));
1640     UsedArray.push_back(
1641         ConstantExpr::getPointerBitCastOrAddrSpaceCast(GV, UsedElementType));
1642     if (llvm::GlobalVariable *Old =
1643             M->getGlobalVariable("llvm.cmdline", true)) {
1644       assert(Old->hasOneUse() &&
1645              "llvm.cmdline can only be used once in llvm.compiler.used");
1646       GV->takeName(Old);
1647       Old->eraseFromParent();
1648     } else {
1649       GV->setName("llvm.cmdline");
1650     }
1651   }
1652 
1653   if (UsedArray.empty())
1654     return;
1655 
1656   // Recreate llvm.compiler.used.
1657   ArrayType *ATy = ArrayType::get(UsedElementType, UsedArray.size());
1658   auto *NewUsed = new GlobalVariable(
1659       *M, ATy, false, llvm::GlobalValue::AppendingLinkage,
1660       llvm::ConstantArray::get(ATy, UsedArray), "llvm.compiler.used");
1661   NewUsed->setSection("llvm.metadata");
1662 }
1663