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