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