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