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