xref: /freebsd-src/contrib/llvm-project/clang/lib/CodeGen/BackendUtil.cpp (revision 4824e7fd18a1223177218d4aec1b3c6c5c4a444e)
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       if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1038         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1039                                  CodeGenOpts.EnableSplitLTOUnit);
1040       PerModulePasses.add(createWriteThinLTOBitcodePass(
1041           *OS, ThinLinkOS ? &ThinLinkOS->os() : nullptr));
1042     } else {
1043       // Emit a module summary by default for Regular LTO except for ld64
1044       // targets
1045       bool EmitLTOSummary =
1046           (CodeGenOpts.PrepareForLTO &&
1047            !CodeGenOpts.DisableLLVMPasses &&
1048            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1049                llvm::Triple::Apple);
1050       if (EmitLTOSummary) {
1051         if (!TheModule->getModuleFlag("ThinLTO"))
1052           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1053         if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1054           TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1055                                    uint32_t(1));
1056       }
1057 
1058       PerModulePasses.add(createBitcodeWriterPass(
1059           *OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1060     }
1061     break;
1062 
1063   case Backend_EmitLL:
1064     PerModulePasses.add(
1065         createPrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1066     break;
1067 
1068   default:
1069     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1070       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1071       if (!DwoOS)
1072         return;
1073     }
1074     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1075                        DwoOS ? &DwoOS->os() : nullptr))
1076       return;
1077   }
1078 
1079   // Before executing passes, print the final values of the LLVM options.
1080   cl::PrintOptionValues();
1081 
1082   // Run passes. For now we do all passes at once, but eventually we
1083   // would like to have the option of streaming code generation.
1084 
1085   {
1086     PrettyStackTraceString CrashInfo("Per-function optimization");
1087     llvm::TimeTraceScope TimeScope("PerFunctionPasses");
1088 
1089     PerFunctionPasses.doInitialization();
1090     for (Function &F : *TheModule)
1091       if (!F.isDeclaration())
1092         PerFunctionPasses.run(F);
1093     PerFunctionPasses.doFinalization();
1094   }
1095 
1096   {
1097     PrettyStackTraceString CrashInfo("Per-module optimization passes");
1098     llvm::TimeTraceScope TimeScope("PerModulePasses");
1099     PerModulePasses.run(*TheModule);
1100   }
1101 
1102   {
1103     PrettyStackTraceString CrashInfo("Code generation");
1104     llvm::TimeTraceScope TimeScope("CodeGenPasses");
1105     CodeGenPasses.run(*TheModule);
1106   }
1107 
1108   if (ThinLinkOS)
1109     ThinLinkOS->keep();
1110   if (DwoOS)
1111     DwoOS->keep();
1112 }
1113 
1114 static OptimizationLevel mapToLevel(const CodeGenOptions &Opts) {
1115   switch (Opts.OptimizationLevel) {
1116   default:
1117     llvm_unreachable("Invalid optimization level!");
1118 
1119   case 0:
1120     return OptimizationLevel::O0;
1121 
1122   case 1:
1123     return OptimizationLevel::O1;
1124 
1125   case 2:
1126     switch (Opts.OptimizeSize) {
1127     default:
1128       llvm_unreachable("Invalid optimization level for size!");
1129 
1130     case 0:
1131       return OptimizationLevel::O2;
1132 
1133     case 1:
1134       return OptimizationLevel::Os;
1135 
1136     case 2:
1137       return OptimizationLevel::Oz;
1138     }
1139 
1140   case 3:
1141     return OptimizationLevel::O3;
1142   }
1143 }
1144 
1145 static void addSanitizers(const Triple &TargetTriple,
1146                           const CodeGenOptions &CodeGenOpts,
1147                           const LangOptions &LangOpts, PassBuilder &PB) {
1148   PB.registerOptimizerLastEPCallback([&](ModulePassManager &MPM,
1149                                          OptimizationLevel Level) {
1150     if (CodeGenOpts.hasSanitizeCoverage()) {
1151       auto SancovOpts = getSancovOptsFromCGOpts(CodeGenOpts);
1152       MPM.addPass(ModuleSanitizerCoveragePass(
1153           SancovOpts, CodeGenOpts.SanitizeCoverageAllowlistFiles,
1154           CodeGenOpts.SanitizeCoverageIgnorelistFiles));
1155     }
1156 
1157     auto MSanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1158       if (LangOpts.Sanitize.has(Mask)) {
1159         int TrackOrigins = CodeGenOpts.SanitizeMemoryTrackOrigins;
1160         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1161 
1162         MPM.addPass(
1163             ModuleMemorySanitizerPass({TrackOrigins, Recover, CompileKernel}));
1164         FunctionPassManager FPM;
1165         FPM.addPass(
1166             MemorySanitizerPass({TrackOrigins, Recover, CompileKernel}));
1167         if (Level != OptimizationLevel::O0) {
1168           // MemorySanitizer inserts complex instrumentation that mostly
1169           // follows the logic of the original code, but operates on
1170           // "shadow" values. It can benefit from re-running some
1171           // general purpose optimization passes.
1172           FPM.addPass(EarlyCSEPass());
1173           // TODO: Consider add more passes like in
1174           // addGeneralOptsForMemorySanitizer. EarlyCSEPass makes visible
1175           // difference on size. It's not clear if the rest is still
1176           // usefull. InstCombinePass breakes
1177           // compiler-rt/test/msan/select_origin.cpp.
1178         }
1179         MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));
1180       }
1181     };
1182     MSanPass(SanitizerKind::Memory, false);
1183     MSanPass(SanitizerKind::KernelMemory, true);
1184 
1185     if (LangOpts.Sanitize.has(SanitizerKind::Thread)) {
1186       MPM.addPass(ModuleThreadSanitizerPass());
1187       MPM.addPass(createModuleToFunctionPassAdaptor(ThreadSanitizerPass()));
1188     }
1189 
1190     auto ASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1191       if (LangOpts.Sanitize.has(Mask)) {
1192         bool UseGlobalGC = asanUseGlobalsGC(TargetTriple, CodeGenOpts);
1193         bool UseOdrIndicator = CodeGenOpts.SanitizeAddressUseOdrIndicator;
1194         llvm::AsanDtorKind DestructorKind =
1195             CodeGenOpts.getSanitizeAddressDtor();
1196         AddressSanitizerOptions Opts;
1197         Opts.CompileKernel = CompileKernel;
1198         Opts.Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1199         Opts.UseAfterScope = CodeGenOpts.SanitizeAddressUseAfterScope;
1200         Opts.UseAfterReturn = CodeGenOpts.getSanitizeAddressUseAfterReturn();
1201         MPM.addPass(RequireAnalysisPass<ASanGlobalsMetadataAnalysis, Module>());
1202         MPM.addPass(ModuleAddressSanitizerPass(
1203             Opts, UseGlobalGC, UseOdrIndicator, DestructorKind));
1204       }
1205     };
1206     ASanPass(SanitizerKind::Address, false);
1207     ASanPass(SanitizerKind::KernelAddress, true);
1208 
1209     auto HWASanPass = [&](SanitizerMask Mask, bool CompileKernel) {
1210       if (LangOpts.Sanitize.has(Mask)) {
1211         bool Recover = CodeGenOpts.SanitizeRecover.has(Mask);
1212         MPM.addPass(HWAddressSanitizerPass(
1213             {CompileKernel, Recover,
1214              /*DisableOptimization=*/CodeGenOpts.OptimizationLevel == 0}));
1215       }
1216     };
1217     HWASanPass(SanitizerKind::HWAddress, false);
1218     HWASanPass(SanitizerKind::KernelHWAddress, true);
1219 
1220     if (LangOpts.Sanitize.has(SanitizerKind::DataFlow)) {
1221       MPM.addPass(DataFlowSanitizerPass(LangOpts.NoSanitizeFiles));
1222     }
1223   });
1224 }
1225 
1226 void EmitAssemblyHelper::RunOptimizationPipeline(
1227     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1228     std::unique_ptr<llvm::ToolOutputFile> &ThinLinkOS) {
1229   Optional<PGOOptions> PGOOpt;
1230 
1231   if (CodeGenOpts.hasProfileIRInstr())
1232     // -fprofile-generate.
1233     PGOOpt = PGOOptions(CodeGenOpts.InstrProfileOutput.empty()
1234                             ? std::string(DefaultProfileGenName)
1235                             : CodeGenOpts.InstrProfileOutput,
1236                         "", "", PGOOptions::IRInstr, PGOOptions::NoCSAction,
1237                         CodeGenOpts.DebugInfoForProfiling);
1238   else if (CodeGenOpts.hasProfileIRUse()) {
1239     // -fprofile-use.
1240     auto CSAction = CodeGenOpts.hasProfileCSIRUse() ? PGOOptions::CSIRUse
1241                                                     : PGOOptions::NoCSAction;
1242     PGOOpt = PGOOptions(CodeGenOpts.ProfileInstrumentUsePath, "",
1243                         CodeGenOpts.ProfileRemappingFile, PGOOptions::IRUse,
1244                         CSAction, CodeGenOpts.DebugInfoForProfiling);
1245   } else if (!CodeGenOpts.SampleProfileFile.empty())
1246     // -fprofile-sample-use
1247     PGOOpt = PGOOptions(
1248         CodeGenOpts.SampleProfileFile, "", CodeGenOpts.ProfileRemappingFile,
1249         PGOOptions::SampleUse, PGOOptions::NoCSAction,
1250         CodeGenOpts.DebugInfoForProfiling, CodeGenOpts.PseudoProbeForProfiling);
1251   else if (CodeGenOpts.PseudoProbeForProfiling)
1252     // -fpseudo-probe-for-profiling
1253     PGOOpt =
1254         PGOOptions("", "", "", PGOOptions::NoAction, PGOOptions::NoCSAction,
1255                    CodeGenOpts.DebugInfoForProfiling, true);
1256   else if (CodeGenOpts.DebugInfoForProfiling)
1257     // -fdebug-info-for-profiling
1258     PGOOpt = PGOOptions("", "", "", PGOOptions::NoAction,
1259                         PGOOptions::NoCSAction, true);
1260 
1261   // Check to see if we want to generate a CS profile.
1262   if (CodeGenOpts.hasProfileCSIRInstr()) {
1263     assert(!CodeGenOpts.hasProfileCSIRUse() &&
1264            "Cannot have both CSProfileUse pass and CSProfileGen pass at "
1265            "the same time");
1266     if (PGOOpt.hasValue()) {
1267       assert(PGOOpt->Action != PGOOptions::IRInstr &&
1268              PGOOpt->Action != PGOOptions::SampleUse &&
1269              "Cannot run CSProfileGen pass with ProfileGen or SampleUse "
1270              " pass");
1271       PGOOpt->CSProfileGenFile = CodeGenOpts.InstrProfileOutput.empty()
1272                                      ? std::string(DefaultProfileGenName)
1273                                      : CodeGenOpts.InstrProfileOutput;
1274       PGOOpt->CSAction = PGOOptions::CSIRInstr;
1275     } else
1276       PGOOpt = PGOOptions("",
1277                           CodeGenOpts.InstrProfileOutput.empty()
1278                               ? std::string(DefaultProfileGenName)
1279                               : CodeGenOpts.InstrProfileOutput,
1280                           "", PGOOptions::NoAction, PGOOptions::CSIRInstr,
1281                           CodeGenOpts.DebugInfoForProfiling);
1282   }
1283   if (TM)
1284     TM->setPGOOption(PGOOpt);
1285 
1286   PipelineTuningOptions PTO;
1287   PTO.LoopUnrolling = CodeGenOpts.UnrollLoops;
1288   // For historical reasons, loop interleaving is set to mirror setting for loop
1289   // unrolling.
1290   PTO.LoopInterleaving = CodeGenOpts.UnrollLoops;
1291   PTO.LoopVectorization = CodeGenOpts.VectorizeLoop;
1292   PTO.SLPVectorization = CodeGenOpts.VectorizeSLP;
1293   PTO.MergeFunctions = CodeGenOpts.MergeFunctions;
1294   // Only enable CGProfilePass when using integrated assembler, since
1295   // non-integrated assemblers don't recognize .cgprofile section.
1296   PTO.CallGraphProfile = !CodeGenOpts.DisableIntegratedAS;
1297 
1298   LoopAnalysisManager LAM;
1299   FunctionAnalysisManager FAM;
1300   CGSCCAnalysisManager CGAM;
1301   ModuleAnalysisManager MAM;
1302 
1303   bool DebugPassStructure = CodeGenOpts.DebugPass == "Structure";
1304   PassInstrumentationCallbacks PIC;
1305   PrintPassOptions PrintPassOpts;
1306   PrintPassOpts.Indent = DebugPassStructure;
1307   PrintPassOpts.SkipAnalyses = DebugPassStructure;
1308   StandardInstrumentations SI(CodeGenOpts.DebugPassManager ||
1309                                   DebugPassStructure,
1310                               /*VerifyEach*/ false, PrintPassOpts);
1311   SI.registerCallbacks(PIC, &FAM);
1312   PassBuilder PB(TM.get(), PTO, PGOOpt, &PIC);
1313 
1314   // Attempt to load pass plugins and register their callbacks with PB.
1315   for (auto &PluginFN : CodeGenOpts.PassPlugins) {
1316     auto PassPlugin = PassPlugin::Load(PluginFN);
1317     if (PassPlugin) {
1318       PassPlugin->registerPassBuilderCallbacks(PB);
1319     } else {
1320       Diags.Report(diag::err_fe_unable_to_load_plugin)
1321           << PluginFN << toString(PassPlugin.takeError());
1322     }
1323   }
1324 #define HANDLE_EXTENSION(Ext)                                                  \
1325   get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
1326 #include "llvm/Support/Extension.def"
1327 
1328   // Register the target library analysis directly and give it a customized
1329   // preset TLI.
1330   Triple TargetTriple(TheModule->getTargetTriple());
1331   std::unique_ptr<TargetLibraryInfoImpl> TLII(
1332       createTLII(TargetTriple, CodeGenOpts));
1333   FAM.registerPass([&] { return TargetLibraryAnalysis(*TLII); });
1334 
1335   // Register all the basic analyses with the managers.
1336   PB.registerModuleAnalyses(MAM);
1337   PB.registerCGSCCAnalyses(CGAM);
1338   PB.registerFunctionAnalyses(FAM);
1339   PB.registerLoopAnalyses(LAM);
1340   PB.crossRegisterProxies(LAM, FAM, CGAM, MAM);
1341 
1342   ModulePassManager MPM;
1343 
1344   if (!CodeGenOpts.DisableLLVMPasses) {
1345     // Map our optimization levels into one of the distinct levels used to
1346     // configure the pipeline.
1347     OptimizationLevel Level = mapToLevel(CodeGenOpts);
1348 
1349     bool IsThinLTO = CodeGenOpts.PrepareForThinLTO;
1350     bool IsLTO = CodeGenOpts.PrepareForLTO;
1351 
1352     if (LangOpts.ObjCAutoRefCount) {
1353       PB.registerPipelineStartEPCallback(
1354           [](ModulePassManager &MPM, OptimizationLevel Level) {
1355             if (Level != OptimizationLevel::O0)
1356               MPM.addPass(
1357                   createModuleToFunctionPassAdaptor(ObjCARCExpandPass()));
1358           });
1359       PB.registerPipelineEarlySimplificationEPCallback(
1360           [](ModulePassManager &MPM, OptimizationLevel Level) {
1361             if (Level != OptimizationLevel::O0)
1362               MPM.addPass(ObjCARCAPElimPass());
1363           });
1364       PB.registerScalarOptimizerLateEPCallback(
1365           [](FunctionPassManager &FPM, OptimizationLevel Level) {
1366             if (Level != OptimizationLevel::O0)
1367               FPM.addPass(ObjCARCOptPass());
1368           });
1369     }
1370 
1371     // If we reached here with a non-empty index file name, then the index
1372     // file was empty and we are not performing ThinLTO backend compilation
1373     // (used in testing in a distributed build environment).
1374     bool IsThinLTOPostLink = !CodeGenOpts.ThinLTOIndexFile.empty();
1375     // If so drop any the type test assume sequences inserted for whole program
1376     // vtables so that codegen doesn't complain.
1377     if (IsThinLTOPostLink)
1378       PB.registerPipelineStartEPCallback(
1379           [](ModulePassManager &MPM, OptimizationLevel Level) {
1380             MPM.addPass(LowerTypeTestsPass(/*ExportSummary=*/nullptr,
1381                                            /*ImportSummary=*/nullptr,
1382                                            /*DropTypeTests=*/true));
1383           });
1384 
1385     if (CodeGenOpts.InstrumentFunctions ||
1386         CodeGenOpts.InstrumentFunctionEntryBare ||
1387         CodeGenOpts.InstrumentFunctionsAfterInlining ||
1388         CodeGenOpts.InstrumentForProfiling) {
1389       PB.registerPipelineStartEPCallback(
1390           [](ModulePassManager &MPM, OptimizationLevel Level) {
1391             MPM.addPass(createModuleToFunctionPassAdaptor(
1392                 EntryExitInstrumenterPass(/*PostInlining=*/false)));
1393           });
1394       PB.registerOptimizerLastEPCallback(
1395           [](ModulePassManager &MPM, OptimizationLevel Level) {
1396             MPM.addPass(createModuleToFunctionPassAdaptor(
1397                 EntryExitInstrumenterPass(/*PostInlining=*/true)));
1398           });
1399     }
1400 
1401     // Register callbacks to schedule sanitizer passes at the appropriate part
1402     // of the pipeline.
1403     if (LangOpts.Sanitize.has(SanitizerKind::LocalBounds))
1404       PB.registerScalarOptimizerLateEPCallback(
1405           [](FunctionPassManager &FPM, OptimizationLevel Level) {
1406             FPM.addPass(BoundsCheckingPass());
1407           });
1408 
1409     // Don't add sanitizers if we are here from ThinLTO PostLink. That already
1410     // done on PreLink stage.
1411     if (!IsThinLTOPostLink)
1412       addSanitizers(TargetTriple, CodeGenOpts, LangOpts, PB);
1413 
1414     if (Optional<GCOVOptions> Options = getGCOVOptions(CodeGenOpts, LangOpts))
1415       PB.registerPipelineStartEPCallback(
1416           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1417             MPM.addPass(GCOVProfilerPass(*Options));
1418           });
1419     if (Optional<InstrProfOptions> Options =
1420             getInstrProfOptions(CodeGenOpts, LangOpts))
1421       PB.registerPipelineStartEPCallback(
1422           [Options](ModulePassManager &MPM, OptimizationLevel Level) {
1423             MPM.addPass(InstrProfiling(*Options, false));
1424           });
1425 
1426     if (CodeGenOpts.OptimizationLevel == 0) {
1427       MPM = PB.buildO0DefaultPipeline(Level, IsLTO || IsThinLTO);
1428     } else if (IsThinLTO) {
1429       MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level);
1430     } else if (IsLTO) {
1431       MPM = PB.buildLTOPreLinkDefaultPipeline(Level);
1432     } else {
1433       MPM = PB.buildPerModuleDefaultPipeline(Level);
1434     }
1435 
1436     if (!CodeGenOpts.MemoryProfileOutput.empty()) {
1437       MPM.addPass(createModuleToFunctionPassAdaptor(MemProfilerPass()));
1438       MPM.addPass(ModuleMemProfilerPass());
1439     }
1440   }
1441 
1442   // Add a verifier pass if requested. We don't have to do this if the action
1443   // requires code generation because there will already be a verifier pass in
1444   // the code-generation pipeline.
1445   if (!actionRequiresCodeGen(Action) && CodeGenOpts.VerifyModule)
1446     MPM.addPass(VerifierPass());
1447 
1448   switch (Action) {
1449   case Backend_EmitBC:
1450     if (CodeGenOpts.PrepareForThinLTO && !CodeGenOpts.DisableLLVMPasses) {
1451       if (!CodeGenOpts.ThinLinkBitcodeFile.empty()) {
1452         ThinLinkOS = openOutputFile(CodeGenOpts.ThinLinkBitcodeFile);
1453         if (!ThinLinkOS)
1454           return;
1455       }
1456       if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1457         TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1458                                  CodeGenOpts.EnableSplitLTOUnit);
1459       MPM.addPass(ThinLTOBitcodeWriterPass(*OS, ThinLinkOS ? &ThinLinkOS->os()
1460                                                            : nullptr));
1461     } else {
1462       // Emit a module summary by default for Regular LTO except for ld64
1463       // targets
1464       bool EmitLTOSummary =
1465           (CodeGenOpts.PrepareForLTO && !CodeGenOpts.DisableLLVMPasses &&
1466            llvm::Triple(TheModule->getTargetTriple()).getVendor() !=
1467                llvm::Triple::Apple);
1468       if (EmitLTOSummary) {
1469         if (!TheModule->getModuleFlag("ThinLTO"))
1470           TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
1471         if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
1472           TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
1473                                    uint32_t(1));
1474       }
1475       MPM.addPass(
1476           BitcodeWriterPass(*OS, CodeGenOpts.EmitLLVMUseLists, EmitLTOSummary));
1477     }
1478     break;
1479 
1480   case Backend_EmitLL:
1481     MPM.addPass(PrintModulePass(*OS, "", CodeGenOpts.EmitLLVMUseLists));
1482     break;
1483 
1484   default:
1485     break;
1486   }
1487 
1488   // Now that we have all of the passes ready, run them.
1489   PrettyStackTraceString CrashInfo("Optimizer");
1490   MPM.run(*TheModule, MAM);
1491 }
1492 
1493 void EmitAssemblyHelper::RunCodegenPipeline(
1494     BackendAction Action, std::unique_ptr<raw_pwrite_stream> &OS,
1495     std::unique_ptr<llvm::ToolOutputFile> &DwoOS) {
1496   // We still use the legacy PM to run the codegen pipeline since the new PM
1497   // does not work with the codegen pipeline.
1498   // FIXME: make the new PM work with the codegen pipeline.
1499   legacy::PassManager CodeGenPasses;
1500 
1501   // Append any output we need to the pass manager.
1502   switch (Action) {
1503   case Backend_EmitAssembly:
1504   case Backend_EmitMCNull:
1505   case Backend_EmitObj:
1506     CodeGenPasses.add(
1507         createTargetTransformInfoWrapperPass(getTargetIRAnalysis()));
1508     if (!CodeGenOpts.SplitDwarfOutput.empty()) {
1509       DwoOS = openOutputFile(CodeGenOpts.SplitDwarfOutput);
1510       if (!DwoOS)
1511         return;
1512     }
1513     if (!AddEmitPasses(CodeGenPasses, Action, *OS,
1514                        DwoOS ? &DwoOS->os() : nullptr))
1515       // FIXME: Should we handle this error differently?
1516       return;
1517     break;
1518   default:
1519     return;
1520   }
1521 
1522   PrettyStackTraceString CrashInfo("Code generation");
1523   CodeGenPasses.run(*TheModule);
1524 }
1525 
1526 /// A clean version of `EmitAssembly` that uses the new pass manager.
1527 ///
1528 /// Not all features are currently supported in this system, but where
1529 /// necessary it falls back to the legacy pass manager to at least provide
1530 /// basic functionality.
1531 ///
1532 /// This API is planned to have its functionality finished and then to replace
1533 /// `EmitAssembly` at some point in the future when the default switches.
1534 void EmitAssemblyHelper::EmitAssembly(BackendAction Action,
1535                                       std::unique_ptr<raw_pwrite_stream> OS) {
1536   TimeRegion Region(CodeGenOpts.TimePasses ? &CodeGenerationTime : nullptr);
1537   setCommandLineOpts(CodeGenOpts);
1538 
1539   bool RequiresCodeGen = actionRequiresCodeGen(Action);
1540   CreateTargetMachine(RequiresCodeGen);
1541 
1542   if (RequiresCodeGen && !TM)
1543     return;
1544   if (TM)
1545     TheModule->setDataLayout(TM->createDataLayout());
1546 
1547   // Before executing passes, print the final values of the LLVM options.
1548   cl::PrintOptionValues();
1549 
1550   std::unique_ptr<llvm::ToolOutputFile> ThinLinkOS, DwoOS;
1551   RunOptimizationPipeline(Action, OS, ThinLinkOS);
1552   RunCodegenPipeline(Action, OS, DwoOS);
1553 
1554   if (ThinLinkOS)
1555     ThinLinkOS->keep();
1556   if (DwoOS)
1557     DwoOS->keep();
1558 }
1559 
1560 static void runThinLTOBackend(
1561     DiagnosticsEngine &Diags, ModuleSummaryIndex *CombinedIndex, Module *M,
1562     const HeaderSearchOptions &HeaderOpts, const CodeGenOptions &CGOpts,
1563     const clang::TargetOptions &TOpts, const LangOptions &LOpts,
1564     std::unique_ptr<raw_pwrite_stream> OS, std::string SampleProfile,
1565     std::string ProfileRemapping, BackendAction Action) {
1566   StringMap<DenseMap<GlobalValue::GUID, GlobalValueSummary *>>
1567       ModuleToDefinedGVSummaries;
1568   CombinedIndex->collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
1569 
1570   setCommandLineOpts(CGOpts);
1571 
1572   // We can simply import the values mentioned in the combined index, since
1573   // we should only invoke this using the individual indexes written out
1574   // via a WriteIndexesThinBackend.
1575   FunctionImporter::ImportMapTy ImportList;
1576   if (!lto::initImportList(*M, *CombinedIndex, ImportList))
1577     return;
1578 
1579   auto AddStream = [&](size_t Task) {
1580     return std::make_unique<CachedFileStream>(std::move(OS));
1581   };
1582   lto::Config Conf;
1583   if (CGOpts.SaveTempsFilePrefix != "") {
1584     if (Error E = Conf.addSaveTemps(CGOpts.SaveTempsFilePrefix + ".",
1585                                     /* UseInputModulePath */ false)) {
1586       handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1587         errs() << "Error setting up ThinLTO save-temps: " << EIB.message()
1588                << '\n';
1589       });
1590     }
1591   }
1592   Conf.CPU = TOpts.CPU;
1593   Conf.CodeModel = getCodeModel(CGOpts);
1594   Conf.MAttrs = TOpts.Features;
1595   Conf.RelocModel = CGOpts.RelocationModel;
1596   Conf.CGOptLevel = getCGOptLevel(CGOpts);
1597   Conf.OptLevel = CGOpts.OptimizationLevel;
1598   initTargetOptions(Diags, Conf.Options, CGOpts, TOpts, LOpts, HeaderOpts);
1599   Conf.SampleProfile = std::move(SampleProfile);
1600   Conf.PTO.LoopUnrolling = CGOpts.UnrollLoops;
1601   // For historical reasons, loop interleaving is set to mirror setting for loop
1602   // unrolling.
1603   Conf.PTO.LoopInterleaving = CGOpts.UnrollLoops;
1604   Conf.PTO.LoopVectorization = CGOpts.VectorizeLoop;
1605   Conf.PTO.SLPVectorization = CGOpts.VectorizeSLP;
1606   // Only enable CGProfilePass when using integrated assembler, since
1607   // non-integrated assemblers don't recognize .cgprofile section.
1608   Conf.PTO.CallGraphProfile = !CGOpts.DisableIntegratedAS;
1609 
1610   // Context sensitive profile.
1611   if (CGOpts.hasProfileCSIRInstr()) {
1612     Conf.RunCSIRInstr = true;
1613     Conf.CSIRProfile = std::move(CGOpts.InstrProfileOutput);
1614   } else if (CGOpts.hasProfileCSIRUse()) {
1615     Conf.RunCSIRInstr = false;
1616     Conf.CSIRProfile = std::move(CGOpts.ProfileInstrumentUsePath);
1617   }
1618 
1619   Conf.ProfileRemapping = std::move(ProfileRemapping);
1620   Conf.UseNewPM = !CGOpts.LegacyPassManager;
1621   Conf.DebugPassManager = CGOpts.DebugPassManager;
1622   Conf.RemarksWithHotness = CGOpts.DiagnosticsWithHotness;
1623   Conf.RemarksFilename = CGOpts.OptRecordFile;
1624   Conf.RemarksPasses = CGOpts.OptRecordPasses;
1625   Conf.RemarksFormat = CGOpts.OptRecordFormat;
1626   Conf.SplitDwarfFile = CGOpts.SplitDwarfFile;
1627   Conf.SplitDwarfOutput = CGOpts.SplitDwarfOutput;
1628   switch (Action) {
1629   case Backend_EmitNothing:
1630     Conf.PreCodeGenModuleHook = [](size_t Task, const Module &Mod) {
1631       return false;
1632     };
1633     break;
1634   case Backend_EmitLL:
1635     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1636       M->print(*OS, nullptr, CGOpts.EmitLLVMUseLists);
1637       return false;
1638     };
1639     break;
1640   case Backend_EmitBC:
1641     Conf.PreCodeGenModuleHook = [&](size_t Task, const Module &Mod) {
1642       WriteBitcodeToFile(*M, *OS, CGOpts.EmitLLVMUseLists);
1643       return false;
1644     };
1645     break;
1646   default:
1647     Conf.CGFileType = getCodeGenFileType(Action);
1648     break;
1649   }
1650   if (Error E =
1651           thinBackend(Conf, -1, AddStream, *M, *CombinedIndex, ImportList,
1652                       ModuleToDefinedGVSummaries[M->getModuleIdentifier()],
1653                       /* ModuleMap */ nullptr, CGOpts.CmdArgs)) {
1654     handleAllErrors(std::move(E), [&](ErrorInfoBase &EIB) {
1655       errs() << "Error running ThinLTO backend: " << EIB.message() << '\n';
1656     });
1657   }
1658 }
1659 
1660 void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
1661                               const HeaderSearchOptions &HeaderOpts,
1662                               const CodeGenOptions &CGOpts,
1663                               const clang::TargetOptions &TOpts,
1664                               const LangOptions &LOpts,
1665                               StringRef TDesc, Module *M,
1666                               BackendAction Action,
1667                               std::unique_ptr<raw_pwrite_stream> OS) {
1668 
1669   llvm::TimeTraceScope TimeScope("Backend");
1670 
1671   std::unique_ptr<llvm::Module> EmptyModule;
1672   if (!CGOpts.ThinLTOIndexFile.empty()) {
1673     // If we are performing a ThinLTO importing compile, load the function index
1674     // into memory and pass it into runThinLTOBackend, which will run the
1675     // function importer and invoke LTO passes.
1676     std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
1677     if (Error E = llvm::getModuleSummaryIndexForFile(
1678                       CGOpts.ThinLTOIndexFile,
1679                       /*IgnoreEmptyThinLTOIndexFile*/ true)
1680                       .moveInto(CombinedIndex)) {
1681       logAllUnhandledErrors(std::move(E), errs(),
1682                             "Error loading index file '" +
1683                             CGOpts.ThinLTOIndexFile + "': ");
1684       return;
1685     }
1686 
1687     // A null CombinedIndex means we should skip ThinLTO compilation
1688     // (LLVM will optionally ignore empty index files, returning null instead
1689     // of an error).
1690     if (CombinedIndex) {
1691       if (!CombinedIndex->skipModuleByDistributedBackend()) {
1692         runThinLTOBackend(Diags, CombinedIndex.get(), M, HeaderOpts, CGOpts,
1693                           TOpts, LOpts, std::move(OS), CGOpts.SampleProfileFile,
1694                           CGOpts.ProfileRemappingFile, Action);
1695         return;
1696       }
1697       // Distributed indexing detected that nothing from the module is needed
1698       // for the final linking. So we can skip the compilation. We sill need to
1699       // output an empty object file to make sure that a linker does not fail
1700       // trying to read it. Also for some features, like CFI, we must skip
1701       // the compilation as CombinedIndex does not contain all required
1702       // information.
1703       EmptyModule = std::make_unique<llvm::Module>("empty", M->getContext());
1704       EmptyModule->setTargetTriple(M->getTargetTriple());
1705       M = EmptyModule.get();
1706     }
1707   }
1708 
1709   EmitAssemblyHelper AsmHelper(Diags, HeaderOpts, CGOpts, TOpts, LOpts, M);
1710 
1711   if (CGOpts.LegacyPassManager)
1712     AsmHelper.EmitAssemblyWithLegacyPassManager(Action, std::move(OS));
1713   else
1714     AsmHelper.EmitAssembly(Action, std::move(OS));
1715 
1716   // Verify clang's TargetInfo DataLayout against the LLVM TargetMachine's
1717   // DataLayout.
1718   if (AsmHelper.TM) {
1719     std::string DLDesc = M->getDataLayout().getStringRepresentation();
1720     if (DLDesc != TDesc) {
1721       unsigned DiagID = Diags.getCustomDiagID(
1722           DiagnosticsEngine::Error, "backend data layout '%0' does not match "
1723                                     "expected target description '%1'");
1724       Diags.Report(DiagID) << DLDesc << TDesc;
1725     }
1726   }
1727 }
1728 
1729 // With -fembed-bitcode, save a copy of the llvm IR as data in the
1730 // __LLVM,__bitcode section.
1731 void clang::EmbedBitcode(llvm::Module *M, const CodeGenOptions &CGOpts,
1732                          llvm::MemoryBufferRef Buf) {
1733   if (CGOpts.getEmbedBitcode() == CodeGenOptions::Embed_Off)
1734     return;
1735   llvm::EmbedBitcodeInModule(
1736       *M, Buf, CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Marker,
1737       CGOpts.getEmbedBitcode() != CodeGenOptions::Embed_Bitcode,
1738       CGOpts.CmdArgs);
1739 }
1740