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