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