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