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