clang  10.0.0svn
CodeGenModule.cpp
Go to the documentation of this file.
1 //===--- CodeGenModule.cpp - Emit LLVM Code from ASTs for a Module --------===//
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 // This coordinates the per-module state used while generating code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CodeGenModule.h"
14 #include "CGBlocks.h"
15 #include "CGCUDARuntime.h"
16 #include "CGCXXABI.h"
17 #include "CGCall.h"
18 #include "CGDebugInfo.h"
19 #include "CGObjCRuntime.h"
20 #include "CGOpenCLRuntime.h"
21 #include "CGOpenMPRuntime.h"
22 #include "CGOpenMPRuntimeNVPTX.h"
23 #include "CodeGenFunction.h"
24 #include "CodeGenPGO.h"
25 #include "ConstantEmitter.h"
26 #include "CoverageMappingGen.h"
27 #include "TargetInfo.h"
28 #include "clang/AST/ASTContext.h"
29 #include "clang/AST/CharUnits.h"
30 #include "clang/AST/DeclCXX.h"
31 #include "clang/AST/DeclObjC.h"
32 #include "clang/AST/DeclTemplate.h"
33 #include "clang/AST/Mangle.h"
34 #include "clang/AST/RecordLayout.h"
36 #include "clang/AST/StmtVisitor.h"
37 #include "clang/Basic/Builtins.h"
38 #include "clang/Basic/CharInfo.h"
40 #include "clang/Basic/Diagnostic.h"
41 #include "clang/Basic/Module.h"
43 #include "clang/Basic/TargetInfo.h"
44 #include "clang/Basic/Version.h"
47 #include "llvm/ADT/StringSwitch.h"
48 #include "llvm/ADT/Triple.h"
49 #include "llvm/Analysis/TargetLibraryInfo.h"
50 #include "llvm/IR/CallingConv.h"
51 #include "llvm/IR/DataLayout.h"
52 #include "llvm/IR/Intrinsics.h"
53 #include "llvm/IR/LLVMContext.h"
54 #include "llvm/IR/Module.h"
55 #include "llvm/IR/ProfileSummary.h"
56 #include "llvm/ProfileData/InstrProfReader.h"
57 #include "llvm/Support/CodeGen.h"
58 #include "llvm/Support/ConvertUTF.h"
59 #include "llvm/Support/ErrorHandling.h"
60 #include "llvm/Support/MD5.h"
61 #include "llvm/Support/TimeProfiler.h"
62 
63 using namespace clang;
64 using namespace CodeGen;
65 
66 static llvm::cl::opt<bool> LimitedCoverage(
67  "limited-coverage-experimental", llvm::cl::ZeroOrMore, llvm::cl::Hidden,
68  llvm::cl::desc("Emit limited coverage mapping information (experimental)"),
69  llvm::cl::init(false));
70 
71 static const char AnnotationSection[] = "llvm.metadata";
72 
74  switch (CGM.getTarget().getCXXABI().getKind()) {
77  case TargetCXXABI::iOS:
83  return CreateItaniumCXXABI(CGM);
85  return CreateMicrosoftCXXABI(CGM);
86  }
87 
88  llvm_unreachable("invalid C++ ABI kind");
89 }
90 
91 CodeGenModule::CodeGenModule(ASTContext &C, const HeaderSearchOptions &HSO,
92  const PreprocessorOptions &PPO,
93  const CodeGenOptions &CGO, llvm::Module &M,
94  DiagnosticsEngine &diags,
95  CoverageSourceInfo *CoverageInfo)
96  : Context(C), LangOpts(C.getLangOpts()), HeaderSearchOpts(HSO),
97  PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
98  Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
99  VMContext(M.getContext()), Types(*this), VTables(*this),
100  SanitizerMD(new SanitizerMetadata(*this)) {
101 
102  // Initialize the type cache.
103  llvm::LLVMContext &LLVMContext = M.getContext();
104  VoidTy = llvm::Type::getVoidTy(LLVMContext);
105  Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
106  Int16Ty = llvm::Type::getInt16Ty(LLVMContext);
107  Int32Ty = llvm::Type::getInt32Ty(LLVMContext);
108  Int64Ty = llvm::Type::getInt64Ty(LLVMContext);
109  HalfTy = llvm::Type::getHalfTy(LLVMContext);
110  FloatTy = llvm::Type::getFloatTy(LLVMContext);
111  DoubleTy = llvm::Type::getDoubleTy(LLVMContext);
114  C.toCharUnitsFromBits(C.getTargetInfo().getPointerAlign(0)).getQuantity();
118  C.toCharUnitsFromBits(C.getTargetInfo().getIntAlign()).getQuantity();
119  IntTy = llvm::IntegerType::get(LLVMContext, C.getTargetInfo().getIntWidth());
120  IntPtrTy = llvm::IntegerType::get(LLVMContext,
122  Int8PtrTy = Int8Ty->getPointerTo(0);
123  Int8PtrPtrTy = Int8PtrTy->getPointerTo(0);
124  AllocaInt8PtrTy = Int8Ty->getPointerTo(
125  M.getDataLayout().getAllocaAddrSpace());
127 
129 
130  if (LangOpts.ObjC)
131  createObjCRuntime();
132  if (LangOpts.OpenCL)
133  createOpenCLRuntime();
134  if (LangOpts.OpenMP)
135  createOpenMPRuntime();
136  if (LangOpts.CUDA)
137  createCUDARuntime();
138 
139  // Enable TBAA unless it's suppressed. ThreadSanitizer needs TBAA even at O0.
140  if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
141  (!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
142  TBAA.reset(new CodeGenTBAA(Context, TheModule, CodeGenOpts, getLangOpts(),
143  getCXXABI().getMangleContext()));
144 
145  // If debug info or coverage generation is enabled, create the CGDebugInfo
146  // object.
147  if (CodeGenOpts.getDebugInfo() != codegenoptions::NoDebugInfo ||
148  CodeGenOpts.EmitGcovArcs || CodeGenOpts.EmitGcovNotes)
149  DebugInfo.reset(new CGDebugInfo(*this));
150 
151  Block.GlobalUniqueCount = 0;
152 
153  if (C.getLangOpts().ObjC)
154  ObjCData.reset(new ObjCEntrypoints());
155 
156  if (CodeGenOpts.hasProfileClangUse()) {
157  auto ReaderOrErr = llvm::IndexedInstrProfReader::create(
158  CodeGenOpts.ProfileInstrumentUsePath, CodeGenOpts.ProfileRemappingFile);
159  if (auto E = ReaderOrErr.takeError()) {
160  unsigned DiagID = Diags.getCustomDiagID(DiagnosticsEngine::Error,
161  "Could not read profile %0: %1");
162  llvm::handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EI) {
163  getDiags().Report(DiagID) << CodeGenOpts.ProfileInstrumentUsePath
164  << EI.message();
165  });
166  } else
167  PGOReader = std::move(ReaderOrErr.get());
168  }
169 
170  // If coverage mapping generation is enabled, create the
171  // CoverageMappingModuleGen object.
172  if (CodeGenOpts.CoverageMapping)
173  CoverageMapping.reset(new CoverageMappingModuleGen(*this, *CoverageInfo));
174 }
175 
177 
178 void CodeGenModule::createObjCRuntime() {
179  // This is just isGNUFamily(), but we want to force implementors of
180  // new ABIs to decide how best to do this.
181  switch (LangOpts.ObjCRuntime.getKind()) {
183  case ObjCRuntime::GCC:
184  case ObjCRuntime::ObjFW:
185  ObjCRuntime.reset(CreateGNUObjCRuntime(*this));
186  return;
187 
189  case ObjCRuntime::MacOSX:
190  case ObjCRuntime::iOS:
192  ObjCRuntime.reset(CreateMacObjCRuntime(*this));
193  return;
194  }
195  llvm_unreachable("bad runtime kind");
196 }
197 
198 void CodeGenModule::createOpenCLRuntime() {
199  OpenCLRuntime.reset(new CGOpenCLRuntime(*this));
200 }
201 
202 void CodeGenModule::createOpenMPRuntime() {
203  // Select a specialized code generation class based on the target, if any.
204  // If it does not exist use the default implementation.
205  switch (getTriple().getArch()) {
206  case llvm::Triple::nvptx:
207  case llvm::Triple::nvptx64:
208  assert(getLangOpts().OpenMPIsDevice &&
209  "OpenMP NVPTX is only prepared to deal with device code.");
210  OpenMPRuntime.reset(new CGOpenMPRuntimeNVPTX(*this));
211  break;
212  default:
213  if (LangOpts.OpenMPSimd)
214  OpenMPRuntime.reset(new CGOpenMPSIMDRuntime(*this));
215  else
216  OpenMPRuntime.reset(new CGOpenMPRuntime(*this));
217  break;
218  }
219 }
220 
221 void CodeGenModule::createCUDARuntime() {
222  CUDARuntime.reset(CreateNVCUDARuntime(*this));
223 }
224 
225 void CodeGenModule::addReplacement(StringRef Name, llvm::Constant *C) {
226  Replacements[Name] = C;
227 }
228 
229 void CodeGenModule::applyReplacements() {
230  for (auto &I : Replacements) {
231  StringRef MangledName = I.first();
232  llvm::Constant *Replacement = I.second;
233  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
234  if (!Entry)
235  continue;
236  auto *OldF = cast<llvm::Function>(Entry);
237  auto *NewF = dyn_cast<llvm::Function>(Replacement);
238  if (!NewF) {
239  if (auto *Alias = dyn_cast<llvm::GlobalAlias>(Replacement)) {
240  NewF = dyn_cast<llvm::Function>(Alias->getAliasee());
241  } else {
242  auto *CE = cast<llvm::ConstantExpr>(Replacement);
243  assert(CE->getOpcode() == llvm::Instruction::BitCast ||
244  CE->getOpcode() == llvm::Instruction::GetElementPtr);
245  NewF = dyn_cast<llvm::Function>(CE->getOperand(0));
246  }
247  }
248 
249  // Replace old with new, but keep the old order.
250  OldF->replaceAllUsesWith(Replacement);
251  if (NewF) {
252  NewF->removeFromParent();
253  OldF->getParent()->getFunctionList().insertAfter(OldF->getIterator(),
254  NewF);
255  }
256  OldF->eraseFromParent();
257  }
258 }
259 
260 void CodeGenModule::addGlobalValReplacement(llvm::GlobalValue *GV, llvm::Constant *C) {
261  GlobalValReplacements.push_back(std::make_pair(GV, C));
262 }
263 
264 void CodeGenModule::applyGlobalValReplacements() {
265  for (auto &I : GlobalValReplacements) {
266  llvm::GlobalValue *GV = I.first;
267  llvm::Constant *C = I.second;
268 
269  GV->replaceAllUsesWith(C);
270  GV->eraseFromParent();
271  }
272 }
273 
274 // This is only used in aliases that we created and we know they have a
275 // linear structure.
276 static const llvm::GlobalObject *getAliasedGlobal(
277  const llvm::GlobalIndirectSymbol &GIS) {
278  llvm::SmallPtrSet<const llvm::GlobalIndirectSymbol*, 4> Visited;
279  const llvm::Constant *C = &GIS;
280  for (;;) {
281  C = C->stripPointerCasts();
282  if (auto *GO = dyn_cast<llvm::GlobalObject>(C))
283  return GO;
284  // stripPointerCasts will not walk over weak aliases.
285  auto *GIS2 = dyn_cast<llvm::GlobalIndirectSymbol>(C);
286  if (!GIS2)
287  return nullptr;
288  if (!Visited.insert(GIS2).second)
289  return nullptr;
290  C = GIS2->getIndirectSymbol();
291  }
292 }
293 
294 void CodeGenModule::checkAliases() {
295  // Check if the constructed aliases are well formed. It is really unfortunate
296  // that we have to do this in CodeGen, but we only construct mangled names
297  // and aliases during codegen.
298  bool Error = false;
299  DiagnosticsEngine &Diags = getDiags();
300  for (const GlobalDecl &GD : Aliases) {
301  const auto *D = cast<ValueDecl>(GD.getDecl());
302  SourceLocation Location;
303  bool IsIFunc = D->hasAttr<IFuncAttr>();
304  if (const Attr *A = D->getDefiningAttr())
305  Location = A->getLocation();
306  else
307  llvm_unreachable("Not an alias or ifunc?");
308  StringRef MangledName = getMangledName(GD);
309  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
310  auto *Alias = cast<llvm::GlobalIndirectSymbol>(Entry);
311  const llvm::GlobalValue *GV = getAliasedGlobal(*Alias);
312  if (!GV) {
313  Error = true;
314  Diags.Report(Location, diag::err_cyclic_alias) << IsIFunc;
315  } else if (GV->isDeclaration()) {
316  Error = true;
317  Diags.Report(Location, diag::err_alias_to_undefined)
318  << IsIFunc << IsIFunc;
319  } else if (IsIFunc) {
320  // Check resolver function type.
321  llvm::FunctionType *FTy = dyn_cast<llvm::FunctionType>(
322  GV->getType()->getPointerElementType());
323  assert(FTy);
324  if (!FTy->getReturnType()->isPointerTy())
325  Diags.Report(Location, diag::err_ifunc_resolver_return);
326  }
327 
328  llvm::Constant *Aliasee = Alias->getIndirectSymbol();
329  llvm::GlobalValue *AliaseeGV;
330  if (auto CE = dyn_cast<llvm::ConstantExpr>(Aliasee))
331  AliaseeGV = cast<llvm::GlobalValue>(CE->getOperand(0));
332  else
333  AliaseeGV = cast<llvm::GlobalValue>(Aliasee);
334 
335  if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
336  StringRef AliasSection = SA->getName();
337  if (AliasSection != AliaseeGV->getSection())
338  Diags.Report(SA->getLocation(), diag::warn_alias_with_section)
339  << AliasSection << IsIFunc << IsIFunc;
340  }
341 
342  // We have to handle alias to weak aliases in here. LLVM itself disallows
343  // this since the object semantics would not match the IL one. For
344  // compatibility with gcc we implement it by just pointing the alias
345  // to its aliasee's aliasee. We also warn, since the user is probably
346  // expecting the link to be weak.
347  if (auto GA = dyn_cast<llvm::GlobalIndirectSymbol>(AliaseeGV)) {
348  if (GA->isInterposable()) {
349  Diags.Report(Location, diag::warn_alias_to_weak_alias)
350  << GV->getName() << GA->getName() << IsIFunc;
351  Aliasee = llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
352  GA->getIndirectSymbol(), Alias->getType());
353  Alias->setIndirectSymbol(Aliasee);
354  }
355  }
356  }
357  if (!Error)
358  return;
359 
360  for (const GlobalDecl &GD : Aliases) {
361  StringRef MangledName = getMangledName(GD);
362  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
363  auto *Alias = dyn_cast<llvm::GlobalIndirectSymbol>(Entry);
364  Alias->replaceAllUsesWith(llvm::UndefValue::get(Alias->getType()));
365  Alias->eraseFromParent();
366  }
367 }
368 
370  DeferredDeclsToEmit.clear();
371  if (OpenMPRuntime)
372  OpenMPRuntime->clear();
373 }
374 
376  StringRef MainFile) {
377  if (!hasDiagnostics())
378  return;
379  if (VisitedInMainFile > 0 && VisitedInMainFile == MissingInMainFile) {
380  if (MainFile.empty())
381  MainFile = "<stdin>";
382  Diags.Report(diag::warn_profile_data_unprofiled) << MainFile;
383  } else {
384  if (Mismatched > 0)
385  Diags.Report(diag::warn_profile_data_out_of_date) << Visited << Mismatched;
386 
387  if (Missing > 0)
388  Diags.Report(diag::warn_profile_data_missing) << Visited << Missing;
389  }
390 }
391 
393  EmitDeferred();
394  EmitVTablesOpportunistically();
395  applyGlobalValReplacements();
396  applyReplacements();
397  checkAliases();
398  emitMultiVersionFunctions();
399  EmitCXXGlobalInitFunc();
400  EmitCXXGlobalDtorFunc();
401  registerGlobalDtorsWithAtExit();
402  EmitCXXThreadLocalInitFunc();
403  if (ObjCRuntime)
404  if (llvm::Function *ObjCInitFunction = ObjCRuntime->ModuleInitFunction())
405  AddGlobalCtor(ObjCInitFunction);
406  if (Context.getLangOpts().CUDA && !Context.getLangOpts().CUDAIsDevice &&
407  CUDARuntime) {
408  if (llvm::Function *CudaCtorFunction =
409  CUDARuntime->makeModuleCtorFunction())
410  AddGlobalCtor(CudaCtorFunction);
411  }
412  if (OpenMPRuntime) {
413  if (llvm::Function *OpenMPRequiresDirectiveRegFun =
414  OpenMPRuntime->emitRequiresDirectiveRegFun()) {
415  AddGlobalCtor(OpenMPRequiresDirectiveRegFun, 0);
416  }
417  if (llvm::Function *OpenMPRegistrationFunction =
418  OpenMPRuntime->emitRegistrationFunction()) {
419  auto ComdatKey = OpenMPRegistrationFunction->hasComdat() ?
420  OpenMPRegistrationFunction : nullptr;
421  AddGlobalCtor(OpenMPRegistrationFunction, 0, ComdatKey);
422  }
423  OpenMPRuntime->clear();
424  }
425  if (PGOReader) {
426  getModule().setProfileSummary(
427  PGOReader->getSummary(/* UseCS */ false).getMD(VMContext),
428  llvm::ProfileSummary::PSK_Instr);
429  if (PGOStats.hasDiagnostics())
430  PGOStats.reportDiagnostics(getDiags(), getCodeGenOpts().MainFileName);
431  }
432  EmitCtorList(GlobalCtors, "llvm.global_ctors");
433  EmitCtorList(GlobalDtors, "llvm.global_dtors");
435  EmitStaticExternCAliases();
437  if (CoverageMapping)
438  CoverageMapping->emit();
439  if (CodeGenOpts.SanitizeCfiCrossDso) {
442  }
443  emitAtAvailableLinkGuard();
444  emitLLVMUsed();
445  if (SanStats)
446  SanStats->finish();
447 
448  if (CodeGenOpts.Autolink &&
449  (Context.getLangOpts().Modules || !LinkerOptionsMetadata.empty())) {
450  EmitModuleLinkOptions();
451  }
452 
453  // On ELF we pass the dependent library specifiers directly to the linker
454  // without manipulating them. This is in contrast to other platforms where
455  // they are mapped to a specific linker option by the compiler. This
456  // difference is a result of the greater variety of ELF linkers and the fact
457  // that ELF linkers tend to handle libraries in a more complicated fashion
458  // than on other platforms. This forces us to defer handling the dependent
459  // libs to the linker.
460  //
461  // CUDA/HIP device and host libraries are different. Currently there is no
462  // way to differentiate dependent libraries for host or device. Existing
463  // usage of #pragma comment(lib, *) is intended for host libraries on
464  // Windows. Therefore emit llvm.dependent-libraries only for host.
465  if (!ELFDependentLibraries.empty() && !Context.getLangOpts().CUDAIsDevice) {
466  auto *NMD = getModule().getOrInsertNamedMetadata("llvm.dependent-libraries");
467  for (auto *MD : ELFDependentLibraries)
468  NMD->addOperand(MD);
469  }
470 
471  // Record mregparm value now so it is visible through rest of codegen.
472  if (Context.getTargetInfo().getTriple().getArch() == llvm::Triple::x86)
473  getModule().addModuleFlag(llvm::Module::Error, "NumRegisterParameters",
474  CodeGenOpts.NumRegisterParameters);
475 
476  if (CodeGenOpts.DwarfVersion) {
477  // We actually want the latest version when there are conflicts.
478  // We can change from Warning to Latest if such mode is supported.
479  getModule().addModuleFlag(llvm::Module::Warning, "Dwarf Version",
480  CodeGenOpts.DwarfVersion);
481  }
482  if (CodeGenOpts.EmitCodeView) {
483  // Indicate that we want CodeView in the metadata.
484  getModule().addModuleFlag(llvm::Module::Warning, "CodeView", 1);
485  }
486  if (CodeGenOpts.CodeViewGHash) {
487  getModule().addModuleFlag(llvm::Module::Warning, "CodeViewGHash", 1);
488  }
489  if (CodeGenOpts.ControlFlowGuard) {
490  // We want function ID tables for Control Flow Guard.
491  getModule().addModuleFlag(llvm::Module::Warning, "cfguardtable", 1);
492  }
493  if (CodeGenOpts.OptimizationLevel > 0 && CodeGenOpts.StrictVTablePointers) {
494  // We don't support LTO with 2 with different StrictVTablePointers
495  // FIXME: we could support it by stripping all the information introduced
496  // by StrictVTablePointers.
497 
498  getModule().addModuleFlag(llvm::Module::Error, "StrictVTablePointers",1);
499 
500  llvm::Metadata *Ops[2] = {
501  llvm::MDString::get(VMContext, "StrictVTablePointers"),
502  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
503  llvm::Type::getInt32Ty(VMContext), 1))};
504 
505  getModule().addModuleFlag(llvm::Module::Require,
506  "StrictVTablePointersRequirement",
507  llvm::MDNode::get(VMContext, Ops));
508  }
509  if (DebugInfo)
510  // We support a single version in the linked module. The LLVM
511  // parser will drop debug info with a different version number
512  // (and warn about it, too).
513  getModule().addModuleFlag(llvm::Module::Warning, "Debug Info Version",
514  llvm::DEBUG_METADATA_VERSION);
515 
516  // We need to record the widths of enums and wchar_t, so that we can generate
517  // the correct build attributes in the ARM backend. wchar_size is also used by
518  // TargetLibraryInfo.
519  uint64_t WCharWidth =
520  Context.getTypeSizeInChars(Context.getWideCharType()).getQuantity();
521  getModule().addModuleFlag(llvm::Module::Error, "wchar_size", WCharWidth);
522 
523  llvm::Triple::ArchType Arch = Context.getTargetInfo().getTriple().getArch();
524  if ( Arch == llvm::Triple::arm
525  || Arch == llvm::Triple::armeb
526  || Arch == llvm::Triple::thumb
527  || Arch == llvm::Triple::thumbeb) {
528  // The minimum width of an enum in bytes
529  uint64_t EnumWidth = Context.getLangOpts().ShortEnums ? 1 : 4;
530  getModule().addModuleFlag(llvm::Module::Error, "min_enum_size", EnumWidth);
531  }
532 
533  if (CodeGenOpts.SanitizeCfiCrossDso) {
534  // Indicate that we want cross-DSO control flow integrity checks.
535  getModule().addModuleFlag(llvm::Module::Override, "Cross-DSO CFI", 1);
536  }
537 
538  if (LangOpts.Sanitize.has(SanitizerKind::CFIICall)) {
539  getModule().addModuleFlag(llvm::Module::Override,
540  "CFI Canonical Jump Tables",
541  CodeGenOpts.SanitizeCfiCanonicalJumpTables);
542  }
543 
544  if (CodeGenOpts.CFProtectionReturn &&
546  // Indicate that we want to instrument return control flow protection.
547  getModule().addModuleFlag(llvm::Module::Override, "cf-protection-return",
548  1);
549  }
550 
551  if (CodeGenOpts.CFProtectionBranch &&
553  // Indicate that we want to instrument branch control flow protection.
554  getModule().addModuleFlag(llvm::Module::Override, "cf-protection-branch",
555  1);
556  }
557 
558  if (LangOpts.CUDAIsDevice && getTriple().isNVPTX()) {
559  // Indicate whether __nvvm_reflect should be configured to flush denormal
560  // floating point values to 0. (This corresponds to its "__CUDA_FTZ"
561  // property.)
562  getModule().addModuleFlag(llvm::Module::Override, "nvvm-reflect-ftz",
563  CodeGenOpts.FlushDenorm ? 1 : 0);
564  }
565 
566  // Emit OpenCL specific module metadata: OpenCL/SPIR version.
567  if (LangOpts.OpenCL) {
568  EmitOpenCLMetadata();
569  // Emit SPIR version.
570  if (getTriple().isSPIR()) {
571  // SPIR v2.0 s2.12 - The SPIR version used by the module is stored in the
572  // opencl.spir.version named metadata.
573  // C++ is backwards compatible with OpenCL v2.0.
574  auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
575  llvm::Metadata *SPIRVerElts[] = {
576  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
577  Int32Ty, Version / 100)),
578  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
579  Int32Ty, (Version / 100 > 1) ? 0 : 2))};
580  llvm::NamedMDNode *SPIRVerMD =
581  TheModule.getOrInsertNamedMetadata("opencl.spir.version");
582  llvm::LLVMContext &Ctx = TheModule.getContext();
583  SPIRVerMD->addOperand(llvm::MDNode::get(Ctx, SPIRVerElts));
584  }
585  }
586 
587  if (uint32_t PLevel = Context.getLangOpts().PICLevel) {
588  assert(PLevel < 3 && "Invalid PIC Level");
589  getModule().setPICLevel(static_cast<llvm::PICLevel::Level>(PLevel));
590  if (Context.getLangOpts().PIE)
591  getModule().setPIELevel(static_cast<llvm::PIELevel::Level>(PLevel));
592  }
593 
594  if (getCodeGenOpts().CodeModel.size() > 0) {
595  unsigned CM = llvm::StringSwitch<unsigned>(getCodeGenOpts().CodeModel)
596  .Case("tiny", llvm::CodeModel::Tiny)
597  .Case("small", llvm::CodeModel::Small)
598  .Case("kernel", llvm::CodeModel::Kernel)
599  .Case("medium", llvm::CodeModel::Medium)
600  .Case("large", llvm::CodeModel::Large)
601  .Default(~0u);
602  if (CM != ~0u) {
603  llvm::CodeModel::Model codeModel = static_cast<llvm::CodeModel::Model>(CM);
604  getModule().setCodeModel(codeModel);
605  }
606  }
607 
608  if (CodeGenOpts.NoPLT)
609  getModule().setRtLibUseGOT();
610 
611  SimplifyPersonality();
612 
613  if (getCodeGenOpts().EmitDeclMetadata)
614  EmitDeclMetadata();
615 
616  if (getCodeGenOpts().EmitGcovArcs || getCodeGenOpts().EmitGcovNotes)
617  EmitCoverageFile();
618 
619  if (DebugInfo)
620  DebugInfo->finalize();
621 
622  if (getCodeGenOpts().EmitVersionIdentMetadata)
623  EmitVersionIdentMetadata();
624 
625  if (!getCodeGenOpts().RecordCommandLine.empty())
626  EmitCommandLineMetadata();
627 
628  EmitTargetMetadata();
629 }
630 
631 void CodeGenModule::EmitOpenCLMetadata() {
632  // SPIR v2.0 s2.13 - The OpenCL version used by the module is stored in the
633  // opencl.ocl.version named metadata node.
634  // C++ is backwards compatible with OpenCL v2.0.
635  // FIXME: We might need to add CXX version at some point too?
636  auto Version = LangOpts.OpenCLCPlusPlus ? 200 : LangOpts.OpenCLVersion;
637  llvm::Metadata *OCLVerElts[] = {
638  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
639  Int32Ty, Version / 100)),
640  llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
641  Int32Ty, (Version % 100) / 10))};
642  llvm::NamedMDNode *OCLVerMD =
643  TheModule.getOrInsertNamedMetadata("opencl.ocl.version");
644  llvm::LLVMContext &Ctx = TheModule.getContext();
645  OCLVerMD->addOperand(llvm::MDNode::get(Ctx, OCLVerElts));
646 }
647 
649  // Make sure that this type is translated.
650  Types.UpdateCompletedType(TD);
651 }
652 
654  // Make sure that this type is translated.
655  Types.RefreshTypeCacheForClass(RD);
656 }
657 
659  if (!TBAA)
660  return nullptr;
661  return TBAA->getTypeInfo(QTy);
662 }
663 
665  if (!TBAA)
666  return TBAAAccessInfo();
667  return TBAA->getAccessInfo(AccessType);
668 }
669 
672  if (!TBAA)
673  return TBAAAccessInfo();
674  return TBAA->getVTablePtrAccessInfo(VTablePtrType);
675 }
676 
678  if (!TBAA)
679  return nullptr;
680  return TBAA->getTBAAStructInfo(QTy);
681 }
682 
684  if (!TBAA)
685  return nullptr;
686  return TBAA->getBaseTypeInfo(QTy);
687 }
688 
690  if (!TBAA)
691  return nullptr;
692  return TBAA->getAccessTagInfo(Info);
693 }
694 
697  if (!TBAA)
698  return TBAAAccessInfo();
699  return TBAA->mergeTBAAInfoForCast(SourceInfo, TargetInfo);
700 }
701 
704  TBAAAccessInfo InfoB) {
705  if (!TBAA)
706  return TBAAAccessInfo();
707  return TBAA->mergeTBAAInfoForConditionalOperator(InfoA, InfoB);
708 }
709 
712  TBAAAccessInfo SrcInfo) {
713  if (!TBAA)
714  return TBAAAccessInfo();
715  return TBAA->mergeTBAAInfoForConditionalOperator(DestInfo, SrcInfo);
716 }
717 
718 void CodeGenModule::DecorateInstructionWithTBAA(llvm::Instruction *Inst,
719  TBAAAccessInfo TBAAInfo) {
720  if (llvm::MDNode *Tag = getTBAAAccessTagInfo(TBAAInfo))
721  Inst->setMetadata(llvm::LLVMContext::MD_tbaa, Tag);
722 }
723 
725  llvm::Instruction *I, const CXXRecordDecl *RD) {
726  I->setMetadata(llvm::LLVMContext::MD_invariant_group,
727  llvm::MDNode::get(getLLVMContext(), {}));
728 }
729 
730 void CodeGenModule::Error(SourceLocation loc, StringRef message) {
731  unsigned diagID = getDiags().getCustomDiagID(DiagnosticsEngine::Error, "%0");
732  getDiags().Report(Context.getFullLoc(loc), diagID) << message;
733 }
734 
735 /// ErrorUnsupported - Print out an error that codegen doesn't support the
736 /// specified stmt yet.
737 void CodeGenModule::ErrorUnsupported(const Stmt *S, const char *Type) {
739  "cannot compile this %0 yet");
740  std::string Msg = Type;
741  getDiags().Report(Context.getFullLoc(S->getBeginLoc()), DiagID)
742  << Msg << S->getSourceRange();
743 }
744 
745 /// ErrorUnsupported - Print out an error that codegen doesn't support the
746 /// specified decl yet.
747 void CodeGenModule::ErrorUnsupported(const Decl *D, const char *Type) {
749  "cannot compile this %0 yet");
750  std::string Msg = Type;
751  getDiags().Report(Context.getFullLoc(D->getLocation()), DiagID) << Msg;
752 }
753 
754 llvm::ConstantInt *CodeGenModule::getSize(CharUnits size) {
755  return llvm::ConstantInt::get(SizeTy, size.getQuantity());
756 }
757 
758 void CodeGenModule::setGlobalVisibility(llvm::GlobalValue *GV,
759  const NamedDecl *D) const {
760  if (GV->hasDLLImportStorageClass())
761  return;
762  // Internal definitions always have default visibility.
763  if (GV->hasLocalLinkage()) {
764  GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
765  return;
766  }
767  if (!D)
768  return;
769  // Set visibility for definitions, and for declarations if requested globally
770  // or set explicitly.
772  if (LV.isVisibilityExplicit() || getLangOpts().SetVisibilityForExternDecls ||
773  !GV->isDeclarationForLinker())
774  GV->setVisibility(GetLLVMVisibility(LV.getVisibility()));
775 }
776 
777 static bool shouldAssumeDSOLocal(const CodeGenModule &CGM,
778  llvm::GlobalValue *GV) {
779  if (GV->hasLocalLinkage())
780  return true;
781 
782  if (!GV->hasDefaultVisibility() && !GV->hasExternalWeakLinkage())
783  return true;
784 
785  // DLLImport explicitly marks the GV as external.
786  if (GV->hasDLLImportStorageClass())
787  return false;
788 
789  const llvm::Triple &TT = CGM.getTriple();
790  if (TT.isWindowsGNUEnvironment()) {
791  // In MinGW, variables without DLLImport can still be automatically
792  // imported from a DLL by the linker; don't mark variables that
793  // potentially could come from another DLL as DSO local.
794  if (GV->isDeclarationForLinker() && isa<llvm::GlobalVariable>(GV) &&
795  !GV->isThreadLocal())
796  return false;
797  }
798 
799  // On COFF, don't mark 'extern_weak' symbols as DSO local. If these symbols
800  // remain unresolved in the link, they can be resolved to zero, which is
801  // outside the current DSO.
802  if (TT.isOSBinFormatCOFF() && GV->hasExternalWeakLinkage())
803  return false;
804 
805  // Every other GV is local on COFF.
806  // Make an exception for windows OS in the triple: Some firmware builds use
807  // *-win32-macho triples. This (accidentally?) produced windows relocations
808  // without GOT tables in older clang versions; Keep this behaviour.
809  // FIXME: even thread local variables?
810  if (TT.isOSBinFormatCOFF() || (TT.isOSWindows() && TT.isOSBinFormatMachO()))
811  return true;
812 
813  // Only handle COFF and ELF for now.
814  if (!TT.isOSBinFormatELF())
815  return false;
816 
817  // If this is not an executable, don't assume anything is local.
818  const auto &CGOpts = CGM.getCodeGenOpts();
819  llvm::Reloc::Model RM = CGOpts.RelocationModel;
820  const auto &LOpts = CGM.getLangOpts();
821  if (RM != llvm::Reloc::Static && !LOpts.PIE && !LOpts.OpenMPIsDevice)
822  return false;
823 
824  // A definition cannot be preempted from an executable.
825  if (!GV->isDeclarationForLinker())
826  return true;
827 
828  // Most PIC code sequences that assume that a symbol is local cannot produce a
829  // 0 if it turns out the symbol is undefined. While this is ABI and relocation
830  // depended, it seems worth it to handle it here.
831  if (RM == llvm::Reloc::PIC_ && GV->hasExternalWeakLinkage())
832  return false;
833 
834  // PPC has no copy relocations and cannot use a plt entry as a symbol address.
835  llvm::Triple::ArchType Arch = TT.getArch();
836  if (Arch == llvm::Triple::ppc || Arch == llvm::Triple::ppc64 ||
837  Arch == llvm::Triple::ppc64le)
838  return false;
839 
840  // If we can use copy relocations we can assume it is local.
841  if (auto *Var = dyn_cast<llvm::GlobalVariable>(GV))
842  if (!Var->isThreadLocal() &&
843  (RM == llvm::Reloc::Static || CGOpts.PIECopyRelocations))
844  return true;
845 
846  // If we can use a plt entry as the symbol address we can assume it
847  // is local.
848  // FIXME: This should work for PIE, but the gold linker doesn't support it.
849  if (isa<llvm::Function>(GV) && !CGOpts.NoPLT && RM == llvm::Reloc::Static)
850  return true;
851 
852  // Otherwise don't assue it is local.
853  return false;
854 }
855 
856 void CodeGenModule::setDSOLocal(llvm::GlobalValue *GV) const {
857  GV->setDSOLocal(shouldAssumeDSOLocal(*this, GV));
858 }
859 
860 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
861  GlobalDecl GD) const {
862  const auto *D = dyn_cast<NamedDecl>(GD.getDecl());
863  // C++ destructors have a few C++ ABI specific special cases.
864  if (const auto *Dtor = dyn_cast_or_null<CXXDestructorDecl>(D)) {
866  return;
867  }
868  setDLLImportDLLExport(GV, D);
869 }
870 
871 void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
872  const NamedDecl *D) const {
873  if (D && D->isExternallyVisible()) {
874  if (D->hasAttr<DLLImportAttr>())
875  GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
876  else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker())
877  GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
878  }
879 }
880 
881 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
882  GlobalDecl GD) const {
883  setDLLImportDLLExport(GV, GD);
884  setGVPropertiesAux(GV, dyn_cast<NamedDecl>(GD.getDecl()));
885 }
886 
887 void CodeGenModule::setGVProperties(llvm::GlobalValue *GV,
888  const NamedDecl *D) const {
889  setDLLImportDLLExport(GV, D);
890  setGVPropertiesAux(GV, D);
891 }
892 
893 void CodeGenModule::setGVPropertiesAux(llvm::GlobalValue *GV,
894  const NamedDecl *D) const {
895  setGlobalVisibility(GV, D);
896  setDSOLocal(GV);
897  GV->setPartition(CodeGenOpts.SymbolPartition);
898 }
899 
900 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(StringRef S) {
901  return llvm::StringSwitch<llvm::GlobalVariable::ThreadLocalMode>(S)
902  .Case("global-dynamic", llvm::GlobalVariable::GeneralDynamicTLSModel)
903  .Case("local-dynamic", llvm::GlobalVariable::LocalDynamicTLSModel)
904  .Case("initial-exec", llvm::GlobalVariable::InitialExecTLSModel)
905  .Case("local-exec", llvm::GlobalVariable::LocalExecTLSModel);
906 }
907 
908 static llvm::GlobalVariable::ThreadLocalMode GetLLVMTLSModel(
910  switch (M) {
912  return llvm::GlobalVariable::GeneralDynamicTLSModel;
914  return llvm::GlobalVariable::LocalDynamicTLSModel;
916  return llvm::GlobalVariable::InitialExecTLSModel;
918  return llvm::GlobalVariable::LocalExecTLSModel;
919  }
920  llvm_unreachable("Invalid TLS model!");
921 }
922 
923 void CodeGenModule::setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const {
924  assert(D.getTLSKind() && "setting TLS mode on non-TLS var!");
925 
926  llvm::GlobalValue::ThreadLocalMode TLM;
927  TLM = GetLLVMTLSModel(CodeGenOpts.getDefaultTLSModel());
928 
929  // Override the TLS model if it is explicitly specified.
930  if (const TLSModelAttr *Attr = D.getAttr<TLSModelAttr>()) {
931  TLM = GetLLVMTLSModel(Attr->getModel());
932  }
933 
934  GV->setThreadLocalMode(TLM);
935 }
936 
937 static std::string getCPUSpecificMangling(const CodeGenModule &CGM,
938  StringRef Name) {
939  const TargetInfo &Target = CGM.getTarget();
940  return (Twine('.') + Twine(Target.CPUSpecificManglingCharacter(Name))).str();
941 }
942 
944  const CPUSpecificAttr *Attr,
945  unsigned CPUIndex,
946  raw_ostream &Out) {
947  // cpu_specific gets the current name, dispatch gets the resolver if IFunc is
948  // supported.
949  if (Attr)
950  Out << getCPUSpecificMangling(CGM, Attr->getCPUName(CPUIndex)->getName());
951  else if (CGM.getTarget().supportsIFunc())
952  Out << ".resolver";
953 }
954 
955 static void AppendTargetMangling(const CodeGenModule &CGM,
956  const TargetAttr *Attr, raw_ostream &Out) {
957  if (Attr->isDefaultVersion())
958  return;
959 
960  Out << '.';
961  const TargetInfo &Target = CGM.getTarget();
962  TargetAttr::ParsedTargetAttr Info =
963  Attr->parse([&Target](StringRef LHS, StringRef RHS) {
964  // Multiversioning doesn't allow "no-${feature}", so we can
965  // only have "+" prefixes here.
966  assert(LHS.startswith("+") && RHS.startswith("+") &&
967  "Features should always have a prefix.");
968  return Target.multiVersionSortPriority(LHS.substr(1)) >
969  Target.multiVersionSortPriority(RHS.substr(1));
970  });
971 
972  bool IsFirst = true;
973 
974  if (!Info.Architecture.empty()) {
975  IsFirst = false;
976  Out << "arch_" << Info.Architecture;
977  }
978 
979  for (StringRef Feat : Info.Features) {
980  if (!IsFirst)
981  Out << '_';
982  IsFirst = false;
983  Out << Feat.substr(1);
984  }
985 }
986 
987 static std::string getMangledNameImpl(const CodeGenModule &CGM, GlobalDecl GD,
988  const NamedDecl *ND,
989  bool OmitMultiVersionMangling = false) {
990  SmallString<256> Buffer;
991  llvm::raw_svector_ostream Out(Buffer);
993  if (MC.shouldMangleDeclName(ND)) {
994  llvm::raw_svector_ostream Out(Buffer);
995  if (const auto *D = dyn_cast<CXXConstructorDecl>(ND))
996  MC.mangleCXXCtor(D, GD.getCtorType(), Out);
997  else if (const auto *D = dyn_cast<CXXDestructorDecl>(ND))
998  MC.mangleCXXDtor(D, GD.getDtorType(), Out);
999  else
1000  MC.mangleName(ND, Out);
1001  } else {
1002  IdentifierInfo *II = ND->getIdentifier();
1003  assert(II && "Attempt to mangle unnamed decl.");
1004  const auto *FD = dyn_cast<FunctionDecl>(ND);
1005 
1006  if (FD &&
1007  FD->getType()->castAs<FunctionType>()->getCallConv() == CC_X86RegCall) {
1008  llvm::raw_svector_ostream Out(Buffer);
1009  Out << "__regcall3__" << II->getName();
1010  } else {
1011  Out << II->getName();
1012  }
1013  }
1014 
1015  if (const auto *FD = dyn_cast<FunctionDecl>(ND))
1016  if (FD->isMultiVersion() && !OmitMultiVersionMangling) {
1017  switch (FD->getMultiVersionKind()) {
1021  FD->getAttr<CPUSpecificAttr>(),
1022  GD.getMultiVersionIndex(), Out);
1023  break;
1025  AppendTargetMangling(CGM, FD->getAttr<TargetAttr>(), Out);
1026  break;
1028  llvm_unreachable("None multiversion type isn't valid here");
1029  }
1030  }
1031 
1032  return Out.str();
1033 }
1034 
1035 void CodeGenModule::UpdateMultiVersionNames(GlobalDecl GD,
1036  const FunctionDecl *FD) {
1037  if (!FD->isMultiVersion())
1038  return;
1039 
1040  // Get the name of what this would be without the 'target' attribute. This
1041  // allows us to lookup the version that was emitted when this wasn't a
1042  // multiversion function.
1043  std::string NonTargetName =
1044  getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
1045  GlobalDecl OtherGD;
1046  if (lookupRepresentativeDecl(NonTargetName, OtherGD)) {
1047  assert(OtherGD.getCanonicalDecl()
1048  .getDecl()
1049  ->getAsFunction()
1050  ->isMultiVersion() &&
1051  "Other GD should now be a multiversioned function");
1052  // OtherFD is the version of this function that was mangled BEFORE
1053  // becoming a MultiVersion function. It potentially needs to be updated.
1054  const FunctionDecl *OtherFD = OtherGD.getCanonicalDecl()
1055  .getDecl()
1056  ->getAsFunction()
1057  ->getMostRecentDecl();
1058  std::string OtherName = getMangledNameImpl(*this, OtherGD, OtherFD);
1059  // This is so that if the initial version was already the 'default'
1060  // version, we don't try to update it.
1061  if (OtherName != NonTargetName) {
1062  // Remove instead of erase, since others may have stored the StringRef
1063  // to this.
1064  const auto ExistingRecord = Manglings.find(NonTargetName);
1065  if (ExistingRecord != std::end(Manglings))
1066  Manglings.remove(&(*ExistingRecord));
1067  auto Result = Manglings.insert(std::make_pair(OtherName, OtherGD));
1068  MangledDeclNames[OtherGD.getCanonicalDecl()] = Result.first->first();
1069  if (llvm::GlobalValue *Entry = GetGlobalValue(NonTargetName))
1070  Entry->setName(OtherName);
1071  }
1072  }
1073 }
1074 
1076  GlobalDecl CanonicalGD = GD.getCanonicalDecl();
1077 
1078  // Some ABIs don't have constructor variants. Make sure that base and
1079  // complete constructors get mangled the same.
1080  if (const auto *CD = dyn_cast<CXXConstructorDecl>(CanonicalGD.getDecl())) {
1082  CXXCtorType OrigCtorType = GD.getCtorType();
1083  assert(OrigCtorType == Ctor_Base || OrigCtorType == Ctor_Complete);
1084  if (OrigCtorType == Ctor_Base)
1085  CanonicalGD = GlobalDecl(CD, Ctor_Complete);
1086  }
1087  }
1088 
1089  auto FoundName = MangledDeclNames.find(CanonicalGD);
1090  if (FoundName != MangledDeclNames.end())
1091  return FoundName->second;
1092 
1093  // Keep the first result in the case of a mangling collision.
1094  const auto *ND = cast<NamedDecl>(GD.getDecl());
1095  std::string MangledName = getMangledNameImpl(*this, GD, ND);
1096 
1097  // Adjust kernel stub mangling as we may need to be able to differentiate
1098  // them from the kernel itself (e.g., for HIP).
1099  if (auto *FD = dyn_cast<FunctionDecl>(GD.getDecl()))
1100  if (!getLangOpts().CUDAIsDevice && FD->hasAttr<CUDAGlobalAttr>())
1101  MangledName = getCUDARuntime().getDeviceStubName(MangledName);
1102 
1103  auto Result = Manglings.insert(std::make_pair(MangledName, GD));
1104  return MangledDeclNames[CanonicalGD] = Result.first->first();
1105 }
1106 
1108  const BlockDecl *BD) {
1109  MangleContext &MangleCtx = getCXXABI().getMangleContext();
1110  const Decl *D = GD.getDecl();
1111 
1112  SmallString<256> Buffer;
1113  llvm::raw_svector_ostream Out(Buffer);
1114  if (!D)
1115  MangleCtx.mangleGlobalBlock(BD,
1116  dyn_cast_or_null<VarDecl>(initializedGlobalDecl.getDecl()), Out);
1117  else if (const auto *CD = dyn_cast<CXXConstructorDecl>(D))
1118  MangleCtx.mangleCtorBlock(CD, GD.getCtorType(), BD, Out);
1119  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(D))
1120  MangleCtx.mangleDtorBlock(DD, GD.getDtorType(), BD, Out);
1121  else
1122  MangleCtx.mangleBlock(cast<DeclContext>(D), BD, Out);
1123 
1124  auto Result = Manglings.insert(std::make_pair(Out.str(), BD));
1125  return Result.first->first();
1126 }
1127 
1128 llvm::GlobalValue *CodeGenModule::GetGlobalValue(StringRef Name) {
1129  return getModule().getNamedValue(Name);
1130 }
1131 
1132 /// AddGlobalCtor - Add a function to the list that will be called before
1133 /// main() runs.
1134 void CodeGenModule::AddGlobalCtor(llvm::Function *Ctor, int Priority,
1135  llvm::Constant *AssociatedData) {
1136  // FIXME: Type coercion of void()* types.
1137  GlobalCtors.push_back(Structor(Priority, Ctor, AssociatedData));
1138 }
1139 
1140 /// AddGlobalDtor - Add a function to the list that will be called
1141 /// when the module is unloaded.
1142 void CodeGenModule::AddGlobalDtor(llvm::Function *Dtor, int Priority) {
1143  if (CodeGenOpts.RegisterGlobalDtorsWithAtExit) {
1144  DtorsUsingAtExit[Priority].push_back(Dtor);
1145  return;
1146  }
1147 
1148  // FIXME: Type coercion of void()* types.
1149  GlobalDtors.push_back(Structor(Priority, Dtor, nullptr));
1150 }
1151 
1152 void CodeGenModule::EmitCtorList(CtorList &Fns, const char *GlobalName) {
1153  if (Fns.empty()) return;
1154 
1155  // Ctor function type is void()*.
1156  llvm::FunctionType* CtorFTy = llvm::FunctionType::get(VoidTy, false);
1157  llvm::Type *CtorPFTy = llvm::PointerType::get(CtorFTy,
1158  TheModule.getDataLayout().getProgramAddressSpace());
1159 
1160  // Get the type of a ctor entry, { i32, void ()*, i8* }.
1161  llvm::StructType *CtorStructTy = llvm::StructType::get(
1162  Int32Ty, CtorPFTy, VoidPtrTy);
1163 
1164  // Construct the constructor and destructor arrays.
1165  ConstantInitBuilder builder(*this);
1166  auto ctors = builder.beginArray(CtorStructTy);
1167  for (const auto &I : Fns) {
1168  auto ctor = ctors.beginStruct(CtorStructTy);
1169  ctor.addInt(Int32Ty, I.Priority);
1170  ctor.add(llvm::ConstantExpr::getBitCast(I.Initializer, CtorPFTy));
1171  if (I.AssociatedData)
1172  ctor.add(llvm::ConstantExpr::getBitCast(I.AssociatedData, VoidPtrTy));
1173  else
1174  ctor.addNullPointer(VoidPtrTy);
1175  ctor.finishAndAddTo(ctors);
1176  }
1177 
1178  auto list =
1179  ctors.finishAndCreateGlobal(GlobalName, getPointerAlign(),
1180  /*constant*/ false,
1181  llvm::GlobalValue::AppendingLinkage);
1182 
1183  // The LTO linker doesn't seem to like it when we set an alignment
1184  // on appending variables. Take it off as a workaround.
1185  list->setAlignment(0);
1186 
1187  Fns.clear();
1188 }
1189 
1190 llvm::GlobalValue::LinkageTypes
1192  const auto *D = cast<FunctionDecl>(GD.getDecl());
1193 
1195 
1196  if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(D))
1197  return getCXXABI().getCXXDestructorLinkage(Linkage, Dtor, GD.getDtorType());
1198 
1199  if (isa<CXXConstructorDecl>(D) &&
1200  cast<CXXConstructorDecl>(D)->isInheritingConstructor() &&
1201  Context.getTargetInfo().getCXXABI().isMicrosoft()) {
1202  // Our approach to inheriting constructors is fundamentally different from
1203  // that used by the MS ABI, so keep our inheriting constructor thunks
1204  // internal rather than trying to pick an unambiguous mangling for them.
1206  }
1207 
1208  return getLLVMLinkageForDeclarator(D, Linkage, /*IsConstantVariable=*/false);
1209 }
1210 
1211 llvm::ConstantInt *CodeGenModule::CreateCrossDsoCfiTypeId(llvm::Metadata *MD) {
1212  llvm::MDString *MDS = dyn_cast<llvm::MDString>(MD);
1213  if (!MDS) return nullptr;
1214 
1215  return llvm::ConstantInt::get(Int64Ty, llvm::MD5Hash(MDS->getString()));
1216 }
1217 
1219  const CGFunctionInfo &Info,
1220  llvm::Function *F) {
1221  unsigned CallingConv;
1222  llvm::AttributeList PAL;
1223  ConstructAttributeList(F->getName(), Info, GD, PAL, CallingConv, false);
1224  F->setAttributes(PAL);
1225  F->setCallingConv(static_cast<llvm::CallingConv::ID>(CallingConv));
1226 }
1227 
1228 static void removeImageAccessQualifier(std::string& TyName) {
1229  std::string ReadOnlyQual("__read_only");
1230  std::string::size_type ReadOnlyPos = TyName.find(ReadOnlyQual);
1231  if (ReadOnlyPos != std::string::npos)
1232  // "+ 1" for the space after access qualifier.
1233  TyName.erase(ReadOnlyPos, ReadOnlyQual.size() + 1);
1234  else {
1235  std::string WriteOnlyQual("__write_only");
1236  std::string::size_type WriteOnlyPos = TyName.find(WriteOnlyQual);
1237  if (WriteOnlyPos != std::string::npos)
1238  TyName.erase(WriteOnlyPos, WriteOnlyQual.size() + 1);
1239  else {
1240  std::string ReadWriteQual("__read_write");
1241  std::string::size_type ReadWritePos = TyName.find(ReadWriteQual);
1242  if (ReadWritePos != std::string::npos)
1243  TyName.erase(ReadWritePos, ReadWriteQual.size() + 1);
1244  }
1245  }
1246 }
1247 
1248 // Returns the address space id that should be produced to the
1249 // kernel_arg_addr_space metadata. This is always fixed to the ids
1250 // as specified in the SPIR 2.0 specification in order to differentiate
1251 // for example in clGetKernelArgInfo() implementation between the address
1252 // spaces with targets without unique mapping to the OpenCL address spaces
1253 // (basically all single AS CPUs).
1254 static unsigned ArgInfoAddressSpace(LangAS AS) {
1255  switch (AS) {
1256  case LangAS::opencl_global: return 1;
1257  case LangAS::opencl_constant: return 2;
1258  case LangAS::opencl_local: return 3;
1259  case LangAS::opencl_generic: return 4; // Not in SPIR 2.0 specs.
1260  default:
1261  return 0; // Assume private.
1262  }
1263 }
1264 
1265 void CodeGenModule::GenOpenCLArgMetadata(llvm::Function *Fn,
1266  const FunctionDecl *FD,
1267  CodeGenFunction *CGF) {
1268  assert(((FD && CGF) || (!FD && !CGF)) &&
1269  "Incorrect use - FD and CGF should either be both null or not!");
1270  // Create MDNodes that represent the kernel arg metadata.
1271  // Each MDNode is a list in the form of "key", N number of values which is
1272  // the same number of values as their are kernel arguments.
1273 
1274  const PrintingPolicy &Policy = Context.getPrintingPolicy();
1275 
1276  // MDNode for the kernel argument address space qualifiers.
1277  SmallVector<llvm::Metadata *, 8> addressQuals;
1278 
1279  // MDNode for the kernel argument access qualifiers (images only).
1281 
1282  // MDNode for the kernel argument type names.
1283  SmallVector<llvm::Metadata *, 8> argTypeNames;
1284 
1285  // MDNode for the kernel argument base type names.
1286  SmallVector<llvm::Metadata *, 8> argBaseTypeNames;
1287 
1288  // MDNode for the kernel argument type qualifiers.
1289  SmallVector<llvm::Metadata *, 8> argTypeQuals;
1290 
1291  // MDNode for the kernel argument names.
1293 
1294  if (FD && CGF)
1295  for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) {
1296  const ParmVarDecl *parm = FD->getParamDecl(i);
1297  QualType ty = parm->getType();
1298  std::string typeQuals;
1299 
1300  if (ty->isPointerType()) {
1301  QualType pointeeTy = ty->getPointeeType();
1302 
1303  // Get address qualifier.
1304  addressQuals.push_back(
1305  llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(
1306  ArgInfoAddressSpace(pointeeTy.getAddressSpace()))));
1307 
1308  // Get argument type name.
1309  std::string typeName =
1310  pointeeTy.getUnqualifiedType().getAsString(Policy) + "*";
1311 
1312  // Turn "unsigned type" to "utype"
1313  std::string::size_type pos = typeName.find("unsigned");
1314  if (pointeeTy.isCanonical() && pos != std::string::npos)
1315  typeName.erase(pos + 1, 8);
1316 
1317  argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1318 
1319  std::string baseTypeName =
1321  Policy) +
1322  "*";
1323 
1324  // Turn "unsigned type" to "utype"
1325  pos = baseTypeName.find("unsigned");
1326  if (pos != std::string::npos)
1327  baseTypeName.erase(pos + 1, 8);
1328 
1329  argBaseTypeNames.push_back(
1330  llvm::MDString::get(VMContext, baseTypeName));
1331 
1332  // Get argument type qualifiers:
1333  if (ty.isRestrictQualified())
1334  typeQuals = "restrict";
1335  if (pointeeTy.isConstQualified() ||
1336  (pointeeTy.getAddressSpace() == LangAS::opencl_constant))
1337  typeQuals += typeQuals.empty() ? "const" : " const";
1338  if (pointeeTy.isVolatileQualified())
1339  typeQuals += typeQuals.empty() ? "volatile" : " volatile";
1340  } else {
1341  uint32_t AddrSpc = 0;
1342  bool isPipe = ty->isPipeType();
1343  if (ty->isImageType() || isPipe)
1345 
1346  addressQuals.push_back(
1347  llvm::ConstantAsMetadata::get(CGF->Builder.getInt32(AddrSpc)));
1348 
1349  // Get argument type name.
1350  std::string typeName;
1351  if (isPipe)
1352  typeName = ty.getCanonicalType()
1353  ->getAs<PipeType>()
1354  ->getElementType()
1355  .getAsString(Policy);
1356  else
1357  typeName = ty.getUnqualifiedType().getAsString(Policy);
1358 
1359  // Turn "unsigned type" to "utype"
1360  std::string::size_type pos = typeName.find("unsigned");
1361  if (ty.isCanonical() && pos != std::string::npos)
1362  typeName.erase(pos + 1, 8);
1363 
1364  std::string baseTypeName;
1365  if (isPipe)
1366  baseTypeName = ty.getCanonicalType()
1367  ->getAs<PipeType>()
1368  ->getElementType()
1369  .getCanonicalType()
1370  .getAsString(Policy);
1371  else
1372  baseTypeName =
1374 
1375  // Remove access qualifiers on images
1376  // (as they are inseparable from type in clang implementation,
1377  // but OpenCL spec provides a special query to get access qualifier
1378  // via clGetKernelArgInfo with CL_KERNEL_ARG_ACCESS_QUALIFIER):
1379  if (ty->isImageType()) {
1380  removeImageAccessQualifier(typeName);
1381  removeImageAccessQualifier(baseTypeName);
1382  }
1383 
1384  argTypeNames.push_back(llvm::MDString::get(VMContext, typeName));
1385 
1386  // Turn "unsigned type" to "utype"
1387  pos = baseTypeName.find("unsigned");
1388  if (pos != std::string::npos)
1389  baseTypeName.erase(pos + 1, 8);
1390 
1391  argBaseTypeNames.push_back(
1392  llvm::MDString::get(VMContext, baseTypeName));
1393 
1394  if (isPipe)
1395  typeQuals = "pipe";
1396  }
1397 
1398  argTypeQuals.push_back(llvm::MDString::get(VMContext, typeQuals));
1399 
1400  // Get image and pipe access qualifier:
1401  if (ty->isImageType() || ty->isPipeType()) {
1402  const Decl *PDecl = parm;
1403  if (auto *TD = dyn_cast<TypedefType>(ty))
1404  PDecl = TD->getDecl();
1405  const OpenCLAccessAttr *A = PDecl->getAttr<OpenCLAccessAttr>();
1406  if (A && A->isWriteOnly())
1407  accessQuals.push_back(llvm::MDString::get(VMContext, "write_only"));
1408  else if (A && A->isReadWrite())
1409  accessQuals.push_back(llvm::MDString::get(VMContext, "read_write"));
1410  else
1411  accessQuals.push_back(llvm::MDString::get(VMContext, "read_only"));
1412  } else
1413  accessQuals.push_back(llvm::MDString::get(VMContext, "none"));
1414 
1415  // Get argument name.
1416  argNames.push_back(llvm::MDString::get(VMContext, parm->getName()));
1417  }
1418 
1419  Fn->setMetadata("kernel_arg_addr_space",
1420  llvm::MDNode::get(VMContext, addressQuals));
1421  Fn->setMetadata("kernel_arg_access_qual",
1422  llvm::MDNode::get(VMContext, accessQuals));
1423  Fn->setMetadata("kernel_arg_type",
1424  llvm::MDNode::get(VMContext, argTypeNames));
1425  Fn->setMetadata("kernel_arg_base_type",
1426  llvm::MDNode::get(VMContext, argBaseTypeNames));
1427  Fn->setMetadata("kernel_arg_type_qual",
1428  llvm::MDNode::get(VMContext, argTypeQuals));
1429  if (getCodeGenOpts().EmitOpenCLArgMetadata)
1430  Fn->setMetadata("kernel_arg_name",
1431  llvm::MDNode::get(VMContext, argNames));
1432 }
1433 
1434 /// Determines whether the language options require us to model
1435 /// unwind exceptions. We treat -fexceptions as mandating this
1436 /// except under the fragile ObjC ABI with only ObjC exceptions
1437 /// enabled. This means, for example, that C with -fexceptions
1438 /// enables this.
1439 static bool hasUnwindExceptions(const LangOptions &LangOpts) {
1440  // If exceptions are completely disabled, obviously this is false.
1441  if (!LangOpts.Exceptions) return false;
1442 
1443  // If C++ exceptions are enabled, this is true.
1444  if (LangOpts.CXXExceptions) return true;
1445 
1446  // If ObjC exceptions are enabled, this depends on the ABI.
1447  if (LangOpts.ObjCExceptions) {
1448  return LangOpts.ObjCRuntime.hasUnwindExceptions();
1449  }
1450 
1451  return true;
1452 }
1453 
1455  const CXXMethodDecl *MD) {
1456  // Check that the type metadata can ever actually be used by a call.
1457  if (!CGM.getCodeGenOpts().LTOUnit ||
1458  !CGM.HasHiddenLTOVisibility(MD->getParent()))
1459  return false;
1460 
1461  // Only functions whose address can be taken with a member function pointer
1462  // need this sort of type metadata.
1463  return !MD->isStatic() && !MD->isVirtual() && !isa<CXXConstructorDecl>(MD) &&
1464  !isa<CXXDestructorDecl>(MD);
1465 }
1466 
1467 std::vector<const CXXRecordDecl *>
1469  llvm::SetVector<const CXXRecordDecl *> MostBases;
1470 
1471  std::function<void (const CXXRecordDecl *)> CollectMostBases;
1472  CollectMostBases = [&](const CXXRecordDecl *RD) {
1473  if (RD->getNumBases() == 0)
1474  MostBases.insert(RD);
1475  for (const CXXBaseSpecifier &B : RD->bases())
1476  CollectMostBases(B.getType()->getAsCXXRecordDecl());
1477  };
1478  CollectMostBases(RD);
1479  return MostBases.takeVector();
1480 }
1481 
1483  llvm::Function *F) {
1484  llvm::AttrBuilder B;
1485 
1486  if (CodeGenOpts.UnwindTables)
1487  B.addAttribute(llvm::Attribute::UWTable);
1488 
1489  if (!hasUnwindExceptions(LangOpts))
1490  B.addAttribute(llvm::Attribute::NoUnwind);
1491 
1492  if (!D || !D->hasAttr<NoStackProtectorAttr>()) {
1493  if (LangOpts.getStackProtector() == LangOptions::SSPOn)
1494  B.addAttribute(llvm::Attribute::StackProtect);
1495  else if (LangOpts.getStackProtector() == LangOptions::SSPStrong)
1496  B.addAttribute(llvm::Attribute::StackProtectStrong);
1497  else if (LangOpts.getStackProtector() == LangOptions::SSPReq)
1498  B.addAttribute(llvm::Attribute::StackProtectReq);
1499  }
1500 
1501  if (!D) {
1502  // If we don't have a declaration to control inlining, the function isn't
1503  // explicitly marked as alwaysinline for semantic reasons, and inlining is
1504  // disabled, mark the function as noinline.
1505  if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
1506  CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining)
1507  B.addAttribute(llvm::Attribute::NoInline);
1508 
1509  F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1510  return;
1511  }
1512 
1513  // Track whether we need to add the optnone LLVM attribute,
1514  // starting with the default for this optimization level.
1515  bool ShouldAddOptNone =
1516  !CodeGenOpts.DisableO0ImplyOptNone && CodeGenOpts.OptimizationLevel == 0;
1517  // We can't add optnone in the following cases, it won't pass the verifier.
1518  ShouldAddOptNone &= !D->hasAttr<MinSizeAttr>();
1519  ShouldAddOptNone &= !F->hasFnAttribute(llvm::Attribute::AlwaysInline);
1520  ShouldAddOptNone &= !D->hasAttr<AlwaysInlineAttr>();
1521 
1522  if (ShouldAddOptNone || D->hasAttr<OptimizeNoneAttr>()) {
1523  B.addAttribute(llvm::Attribute::OptimizeNone);
1524 
1525  // OptimizeNone implies noinline; we should not be inlining such functions.
1526  B.addAttribute(llvm::Attribute::NoInline);
1527  assert(!F->hasFnAttribute(llvm::Attribute::AlwaysInline) &&
1528  "OptimizeNone and AlwaysInline on same function!");
1529 
1530  // We still need to handle naked functions even though optnone subsumes
1531  // much of their semantics.
1532  if (D->hasAttr<NakedAttr>())
1533  B.addAttribute(llvm::Attribute::Naked);
1534 
1535  // OptimizeNone wins over OptimizeForSize and MinSize.
1536  F->removeFnAttr(llvm::Attribute::OptimizeForSize);
1537  F->removeFnAttr(llvm::Attribute::MinSize);
1538  } else if (D->hasAttr<NakedAttr>()) {
1539  // Naked implies noinline: we should not be inlining such functions.
1540  B.addAttribute(llvm::Attribute::Naked);
1541  B.addAttribute(llvm::Attribute::NoInline);
1542  } else if (D->hasAttr<NoDuplicateAttr>()) {
1543  B.addAttribute(llvm::Attribute::NoDuplicate);
1544  } else if (D->hasAttr<NoInlineAttr>()) {
1545  B.addAttribute(llvm::Attribute::NoInline);
1546  } else if (D->hasAttr<AlwaysInlineAttr>() &&
1547  !F->hasFnAttribute(llvm::Attribute::NoInline)) {
1548  // (noinline wins over always_inline, and we can't specify both in IR)
1549  B.addAttribute(llvm::Attribute::AlwaysInline);
1550  } else if (CodeGenOpts.getInlining() == CodeGenOptions::OnlyAlwaysInlining) {
1551  // If we're not inlining, then force everything that isn't always_inline to
1552  // carry an explicit noinline attribute.
1553  if (!F->hasFnAttribute(llvm::Attribute::AlwaysInline))
1554  B.addAttribute(llvm::Attribute::NoInline);
1555  } else {
1556  // Otherwise, propagate the inline hint attribute and potentially use its
1557  // absence to mark things as noinline.
1558  if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1559  // Search function and template pattern redeclarations for inline.
1560  auto CheckForInline = [](const FunctionDecl *FD) {
1561  auto CheckRedeclForInline = [](const FunctionDecl *Redecl) {
1562  return Redecl->isInlineSpecified();
1563  };
1564  if (any_of(FD->redecls(), CheckRedeclForInline))
1565  return true;
1566  const FunctionDecl *Pattern = FD->getTemplateInstantiationPattern();
1567  if (!Pattern)
1568  return false;
1569  return any_of(Pattern->redecls(), CheckRedeclForInline);
1570  };
1571  if (CheckForInline(FD)) {
1572  B.addAttribute(llvm::Attribute::InlineHint);
1573  } else if (CodeGenOpts.getInlining() ==
1575  !FD->isInlined() &&
1576  !F->hasFnAttribute(llvm::Attribute::AlwaysInline)) {
1577  B.addAttribute(llvm::Attribute::NoInline);
1578  }
1579  }
1580  }
1581 
1582  // Add other optimization related attributes if we are optimizing this
1583  // function.
1584  if (!D->hasAttr<OptimizeNoneAttr>()) {
1585  if (D->hasAttr<ColdAttr>()) {
1586  if (!ShouldAddOptNone)
1587  B.addAttribute(llvm::Attribute::OptimizeForSize);
1588  B.addAttribute(llvm::Attribute::Cold);
1589  }
1590 
1591  if (D->hasAttr<MinSizeAttr>())
1592  B.addAttribute(llvm::Attribute::MinSize);
1593  }
1594 
1595  F->addAttributes(llvm::AttributeList::FunctionIndex, B);
1596 
1597  unsigned alignment = D->getMaxAlignment() / Context.getCharWidth();
1598  if (alignment)
1599  F->setAlignment(alignment);
1600 
1601  if (!D->hasAttr<AlignedAttr>())
1602  if (LangOpts.FunctionAlignment)
1603  F->setAlignment(1 << LangOpts.FunctionAlignment);
1604 
1605  // Some C++ ABIs require 2-byte alignment for member functions, in order to
1606  // reserve a bit for differentiating between virtual and non-virtual member
1607  // functions. If the current target's C++ ABI requires this and this is a
1608  // member function, set its alignment accordingly.
1609  if (getTarget().getCXXABI().areMemberFunctionsAligned()) {
1610  if (F->getAlignment() < 2 && isa<CXXMethodDecl>(D))
1611  F->setAlignment(2);
1612  }
1613 
1614  // In the cross-dso CFI mode with canonical jump tables, we want !type
1615  // attributes on definitions only.
1616  if (CodeGenOpts.SanitizeCfiCrossDso &&
1617  CodeGenOpts.SanitizeCfiCanonicalJumpTables) {
1618  if (auto *FD = dyn_cast<FunctionDecl>(D)) {
1619  // Skip available_externally functions. They won't be codegen'ed in the
1620  // current module anyway.
1621  if (getContext().GetGVALinkageForFunction(FD) != GVA_AvailableExternally)
1623  }
1624  }
1625 
1626  // Emit type metadata on member functions for member function pointer checks.
1627  // These are only ever necessary on definitions; we're guaranteed that the
1628  // definition will be present in the LTO unit as a result of LTO visibility.
1629  auto *MD = dyn_cast<CXXMethodDecl>(D);
1630  if (MD && requiresMemberFunctionPointerTypeMetadata(*this, MD)) {
1631  for (const CXXRecordDecl *Base : getMostBaseClasses(MD->getParent())) {
1632  llvm::Metadata *Id =
1634  MD->getType(), Context.getRecordType(Base).getTypePtr()));
1635  F->addTypeMetadata(0, Id);
1636  }
1637  }
1638 }
1639 
1640 void CodeGenModule::SetCommonAttributes(GlobalDecl GD, llvm::GlobalValue *GV) {
1641  const Decl *D = GD.getDecl();
1642  if (dyn_cast_or_null<NamedDecl>(D))
1643  setGVProperties(GV, GD);
1644  else
1645  GV->setVisibility(llvm::GlobalValue::DefaultVisibility);
1646 
1647  if (D && D->hasAttr<UsedAttr>())
1648  addUsedGlobal(GV);
1649 
1650  if (CodeGenOpts.KeepStaticConsts && D && isa<VarDecl>(D)) {
1651  const auto *VD = cast<VarDecl>(D);
1652  if (VD->getType().isConstQualified() &&
1653  VD->getStorageDuration() == SD_Static)
1654  addUsedGlobal(GV);
1655  }
1656 }
1657 
1658 bool CodeGenModule::GetCPUAndFeaturesAttributes(GlobalDecl GD,
1659  llvm::AttrBuilder &Attrs) {
1660  // Add target-cpu and target-features attributes to functions. If
1661  // we have a decl for the function and it has a target attribute then
1662  // parse that and add it to the feature set.
1663  StringRef TargetCPU = getTarget().getTargetOpts().CPU;
1664  std::vector<std::string> Features;
1665  const auto *FD = dyn_cast_or_null<FunctionDecl>(GD.getDecl());
1666  FD = FD ? FD->getMostRecentDecl() : FD;
1667  const auto *TD = FD ? FD->getAttr<TargetAttr>() : nullptr;
1668  const auto *SD = FD ? FD->getAttr<CPUSpecificAttr>() : nullptr;
1669  bool AddedAttr = false;
1670  if (TD || SD) {
1671  llvm::StringMap<bool> FeatureMap;
1672  getFunctionFeatureMap(FeatureMap, GD);
1673 
1674  // Produce the canonical string for this set of features.
1675  for (const llvm::StringMap<bool>::value_type &Entry : FeatureMap)
1676  Features.push_back((Entry.getValue() ? "+" : "-") + Entry.getKey().str());
1677 
1678  // Now add the target-cpu and target-features to the function.
1679  // While we populated the feature map above, we still need to
1680  // get and parse the target attribute so we can get the cpu for
1681  // the function.
1682  if (TD) {
1683  TargetAttr::ParsedTargetAttr ParsedAttr = TD->parse();
1684  if (ParsedAttr.Architecture != "" &&
1685  getTarget().isValidCPUName(ParsedAttr.Architecture))
1686  TargetCPU = ParsedAttr.Architecture;
1687  }
1688  } else {
1689  // Otherwise just add the existing target cpu and target features to the
1690  // function.
1691  Features = getTarget().getTargetOpts().Features;
1692  }
1693 
1694  if (TargetCPU != "") {
1695  Attrs.addAttribute("target-cpu", TargetCPU);
1696  AddedAttr = true;
1697  }
1698  if (!Features.empty()) {
1699  llvm::sort(Features);
1700  Attrs.addAttribute("target-features", llvm::join(Features, ","));
1701  AddedAttr = true;
1702  }
1703 
1704  return AddedAttr;
1705 }
1706 
1707 void CodeGenModule::setNonAliasAttributes(GlobalDecl GD,
1708  llvm::GlobalObject *GO) {
1709  const Decl *D = GD.getDecl();
1710  SetCommonAttributes(GD, GO);
1711 
1712  if (D) {
1713  if (auto *GV = dyn_cast<llvm::GlobalVariable>(GO)) {
1714  if (auto *SA = D->getAttr<PragmaClangBSSSectionAttr>())
1715  GV->addAttribute("bss-section", SA->getName());
1716  if (auto *SA = D->getAttr<PragmaClangDataSectionAttr>())
1717  GV->addAttribute("data-section", SA->getName());
1718  if (auto *SA = D->getAttr<PragmaClangRodataSectionAttr>())
1719  GV->addAttribute("rodata-section", SA->getName());
1720  }
1721 
1722  if (auto *F = dyn_cast<llvm::Function>(GO)) {
1723  if (auto *SA = D->getAttr<PragmaClangTextSectionAttr>())
1724  if (!D->getAttr<SectionAttr>())
1725  F->addFnAttr("implicit-section-name", SA->getName());
1726 
1727  llvm::AttrBuilder Attrs;
1728  if (GetCPUAndFeaturesAttributes(GD, Attrs)) {
1729  // We know that GetCPUAndFeaturesAttributes will always have the
1730  // newest set, since it has the newest possible FunctionDecl, so the
1731  // new ones should replace the old.
1732  F->removeFnAttr("target-cpu");
1733  F->removeFnAttr("target-features");
1734  F->addAttributes(llvm::AttributeList::FunctionIndex, Attrs);
1735  }
1736  }
1737 
1738  if (const auto *CSA = D->getAttr<CodeSegAttr>())
1739  GO->setSection(CSA->getName());
1740  else if (const auto *SA = D->getAttr<SectionAttr>())
1741  GO->setSection(SA->getName());
1742  }
1743 
1744  getTargetCodeGenInfo().setTargetAttributes(D, GO, *this);
1745 }
1746 
1748  llvm::Function *F,
1749  const CGFunctionInfo &FI) {
1750  const Decl *D = GD.getDecl();
1751  SetLLVMFunctionAttributes(GD, FI, F);
1753 
1754  F->setLinkage(llvm::Function::InternalLinkage);
1755 
1756  setNonAliasAttributes(GD, F);
1757 }
1758 
1759 static void setLinkageForGV(llvm::GlobalValue *GV, const NamedDecl *ND) {
1760  // Set linkage and visibility in case we never see a definition.
1762  // Don't set internal linkage on declarations.
1763  // "extern_weak" is overloaded in LLVM; we probably should have
1764  // separate linkage types for this.
1765  if (isExternallyVisible(LV.getLinkage()) &&
1766  (ND->hasAttr<WeakAttr>() || ND->isWeakImported()))
1767  GV->setLinkage(llvm::GlobalValue::ExternalWeakLinkage);
1768 }
1769 
1771  llvm::Function *F) {
1772  // Only if we are checking indirect calls.
1773  if (!LangOpts.Sanitize.has(SanitizerKind::CFIICall))
1774  return;
1775 
1776  // Non-static class methods are handled via vtable or member function pointer
1777  // checks elsewhere.
1778  if (isa<CXXMethodDecl>(FD) && !cast<CXXMethodDecl>(FD)->isStatic())
1779  return;
1780 
1781  llvm::Metadata *MD = CreateMetadataIdentifierForType(FD->getType());
1782  F->addTypeMetadata(0, MD);
1783  F->addTypeMetadata(0, CreateMetadataIdentifierGeneralized(FD->getType()));
1784 
1785  // Emit a hash-based bit set entry for cross-DSO calls.
1786  if (CodeGenOpts.SanitizeCfiCrossDso)
1787  if (auto CrossDsoTypeId = CreateCrossDsoCfiTypeId(MD))
1788  F->addTypeMetadata(0, llvm::ConstantAsMetadata::get(CrossDsoTypeId));
1789 }
1790 
1791 void CodeGenModule::SetFunctionAttributes(GlobalDecl GD, llvm::Function *F,
1792  bool IsIncompleteFunction,
1793  bool IsThunk) {
1794 
1795  if (llvm::Intrinsic::ID IID = F->getIntrinsicID()) {
1796  // If this is an intrinsic function, set the function's attributes
1797  // to the intrinsic's attributes.
1798  F->setAttributes(llvm::Intrinsic::getAttributes(getLLVMContext(), IID));
1799  return;
1800  }
1801 
1802  const auto *FD = cast<FunctionDecl>(GD.getDecl());
1803 
1804  if (!IsIncompleteFunction)
1805  SetLLVMFunctionAttributes(GD, getTypes().arrangeGlobalDeclaration(GD), F);
1806 
1807  // Add the Returned attribute for "this", except for iOS 5 and earlier
1808  // where substantial code, including the libstdc++ dylib, was compiled with
1809  // GCC and does not actually return "this".
1810  if (!IsThunk && getCXXABI().HasThisReturn(GD) &&
1811  !(getTriple().isiOS() && getTriple().isOSVersionLT(6))) {
1812  assert(!F->arg_empty() &&
1813  F->arg_begin()->getType()
1814  ->canLosslesslyBitCastTo(F->getReturnType()) &&
1815  "unexpected this return");
1816  F->addAttribute(1, llvm::Attribute::Returned);
1817  }
1818 
1819  // Only a few attributes are set on declarations; these may later be
1820  // overridden by a definition.
1821 
1822  setLinkageForGV(F, FD);
1823  setGVProperties(F, FD);
1824 
1825  // Setup target-specific attributes.
1826  if (!IsIncompleteFunction && F->isDeclaration())
1827  getTargetCodeGenInfo().setTargetAttributes(FD, F, *this);
1828 
1829  if (const auto *CSA = FD->getAttr<CodeSegAttr>())
1830  F->setSection(CSA->getName());
1831  else if (const auto *SA = FD->getAttr<SectionAttr>())
1832  F->setSection(SA->getName());
1833 
1835  // A replaceable global allocation function does not act like a builtin by
1836  // default, only if it is invoked by a new-expression or delete-expression.
1837  F->addAttribute(llvm::AttributeList::FunctionIndex,
1838  llvm::Attribute::NoBuiltin);
1839 
1840  // A sane operator new returns a non-aliasing pointer.
1841  // FIXME: Also add NonNull attribute to the return value
1842  // for the non-nothrow forms?
1843  auto Kind = FD->getDeclName().getCXXOverloadedOperator();
1844  if (getCodeGenOpts().AssumeSaneOperatorNew &&
1845  (Kind == OO_New || Kind == OO_Array_New))
1846  F->addAttribute(llvm::AttributeList::ReturnIndex,
1847  llvm::Attribute::NoAlias);
1848  }
1849 
1850  if (isa<CXXConstructorDecl>(FD) || isa<CXXDestructorDecl>(FD))
1851  F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1852  else if (const auto *MD = dyn_cast<CXXMethodDecl>(FD))
1853  if (MD->isVirtual())
1854  F->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1855 
1856  // Don't emit entries for function declarations in the cross-DSO mode. This
1857  // is handled with better precision by the receiving DSO. But if jump tables
1858  // are non-canonical then we need type metadata in order to produce the local
1859  // jump table.
1860  if (!CodeGenOpts.SanitizeCfiCrossDso ||
1861  !CodeGenOpts.SanitizeCfiCanonicalJumpTables)
1863 
1864  if (getLangOpts().OpenMP && FD->hasAttr<OMPDeclareSimdDeclAttr>())
1865  getOpenMPRuntime().emitDeclareSimdFunction(FD, F);
1866 
1867  if (const auto *CB = FD->getAttr<CallbackAttr>()) {
1868  // Annotate the callback behavior as metadata:
1869  // - The callback callee (as argument number).
1870  // - The callback payloads (as argument numbers).
1871  llvm::LLVMContext &Ctx = F->getContext();
1872  llvm::MDBuilder MDB(Ctx);
1873 
1874  // The payload indices are all but the first one in the encoding. The first
1875  // identifies the callback callee.
1876  int CalleeIdx = *CB->encoding_begin();
1877  ArrayRef<int> PayloadIndices(CB->encoding_begin() + 1, CB->encoding_end());
1878  F->addMetadata(llvm::LLVMContext::MD_callback,
1879  *llvm::MDNode::get(Ctx, {MDB.createCallbackEncoding(
1880  CalleeIdx, PayloadIndices,
1881  /* VarArgsArePassed */ false)}));
1882  }
1883 }
1884 
1885 void CodeGenModule::addUsedGlobal(llvm::GlobalValue *GV) {
1886  assert(!GV->isDeclaration() &&
1887  "Only globals with definition can force usage.");
1888  LLVMUsed.emplace_back(GV);
1889 }
1890 
1891 void CodeGenModule::addCompilerUsedGlobal(llvm::GlobalValue *GV) {
1892  assert(!GV->isDeclaration() &&
1893  "Only globals with definition can force usage.");
1894  LLVMCompilerUsed.emplace_back(GV);
1895 }
1896 
1897 static void emitUsed(CodeGenModule &CGM, StringRef Name,
1898  std::vector<llvm::WeakTrackingVH> &List) {
1899  // Don't create llvm.used if there is no need.
1900  if (List.empty())
1901  return;
1902 
1903  // Convert List to what ConstantArray needs.
1905  UsedArray.resize(List.size());
1906  for (unsigned i = 0, e = List.size(); i != e; ++i) {
1907  UsedArray[i] =
1908  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
1909  cast<llvm::Constant>(&*List[i]), CGM.Int8PtrTy);
1910  }
1911 
1912  if (UsedArray.empty())
1913  return;
1914  llvm::ArrayType *ATy = llvm::ArrayType::get(CGM.Int8PtrTy, UsedArray.size());
1915 
1916  auto *GV = new llvm::GlobalVariable(
1917  CGM.getModule(), ATy, false, llvm::GlobalValue::AppendingLinkage,
1918  llvm::ConstantArray::get(ATy, UsedArray), Name);
1919 
1920  GV->setSection("llvm.metadata");
1921 }
1922 
1923 void CodeGenModule::emitLLVMUsed() {
1924  emitUsed(*this, "llvm.used", LLVMUsed);
1925  emitUsed(*this, "llvm.compiler.used", LLVMCompilerUsed);
1926 }
1927 
1929  auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opts);
1930  LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
1931 }
1932 
1933 void CodeGenModule::AddDetectMismatch(StringRef Name, StringRef Value) {
1935  getTargetCodeGenInfo().getDetectMismatchOption(Name, Value, Opt);
1936  auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
1937  LinkerOptionsMetadata.push_back(llvm::MDNode::get(getLLVMContext(), MDOpts));
1938 }
1939 
1940 void CodeGenModule::AddDependentLib(StringRef Lib) {
1941  auto &C = getLLVMContext();
1942  if (getTarget().getTriple().isOSBinFormatELF()) {
1943  ELFDependentLibraries.push_back(
1944  llvm::MDNode::get(C, llvm::MDString::get(C, Lib)));
1945  return;
1946  }
1947 
1950  auto *MDOpts = llvm::MDString::get(getLLVMContext(), Opt);
1951  LinkerOptionsMetadata.push_back(llvm::MDNode::get(C, MDOpts));
1952 }
1953 
1954 /// Add link options implied by the given module, including modules
1955 /// it depends on, using a postorder walk.
1958  llvm::SmallPtrSet<Module *, 16> &Visited) {
1959  // Import this module's parent.
1960  if (Mod->Parent && Visited.insert(Mod->Parent).second) {
1961  addLinkOptionsPostorder(CGM, Mod->Parent, Metadata, Visited);
1962  }
1963 
1964  // Import this module's dependencies.
1965  for (unsigned I = Mod->Imports.size(); I > 0; --I) {
1966  if (Visited.insert(Mod->Imports[I - 1]).second)
1967  addLinkOptionsPostorder(CGM, Mod->Imports[I-1], Metadata, Visited);
1968  }
1969 
1970  // Add linker options to link against the libraries/frameworks
1971  // described by this module.
1972  llvm::LLVMContext &Context = CGM.getLLVMContext();
1973  bool IsELF = CGM.getTarget().getTriple().isOSBinFormatELF();
1974 
1975  // For modules that use export_as for linking, use that module
1976  // name instead.
1977  if (Mod->UseExportAsModuleLinkName)
1978  return;
1979 
1980  for (unsigned I = Mod->LinkLibraries.size(); I > 0; --I) {
1981  // Link against a framework. Frameworks are currently Darwin only, so we
1982  // don't to ask TargetCodeGenInfo for the spelling of the linker option.
1983  if (Mod->LinkLibraries[I-1].IsFramework) {
1984  llvm::Metadata *Args[2] = {
1985  llvm::MDString::get(Context, "-framework"),
1986  llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library)};
1987 
1988  Metadata.push_back(llvm::MDNode::get(Context, Args));
1989  continue;
1990  }
1991 
1992  // Link against a library.
1993  if (IsELF) {
1994  llvm::Metadata *Args[2] = {
1995  llvm::MDString::get(Context, "lib"),
1996  llvm::MDString::get(Context, Mod->LinkLibraries[I - 1].Library),
1997  };
1998  Metadata.push_back(llvm::MDNode::get(Context, Args));
1999  } else {
2002  Mod->LinkLibraries[I - 1].Library, Opt);
2003  auto *OptString = llvm::MDString::get(Context, Opt);
2004  Metadata.push_back(llvm::MDNode::get(Context, OptString));
2005  }
2006  }
2007 }
2008 
2009 void CodeGenModule::EmitModuleLinkOptions() {
2010  // Collect the set of all of the modules we want to visit to emit link
2011  // options, which is essentially the imported modules and all of their
2012  // non-explicit child modules.
2013  llvm::SetVector<clang::Module *> LinkModules;
2014  llvm::SmallPtrSet<clang::Module *, 16> Visited;
2016 
2017  // Seed the stack with imported modules.
2018  for (Module *M : ImportedModules) {
2019  // Do not add any link flags when an implementation TU of a module imports
2020  // a header of that same module.
2021  if (M->getTopLevelModuleName() == getLangOpts().CurrentModule &&
2023  continue;
2024  if (Visited.insert(M).second)
2025  Stack.push_back(M);
2026  }
2027 
2028  // Find all of the modules to import, making a little effort to prune
2029  // non-leaf modules.
2030  while (!Stack.empty()) {
2031  clang::Module *Mod = Stack.pop_back_val();
2032 
2033  bool AnyChildren = false;
2034 
2035  // Visit the submodules of this module.
2036  for (const auto &SM : Mod->submodules()) {
2037  // Skip explicit children; they need to be explicitly imported to be
2038  // linked against.
2039  if (SM->IsExplicit)
2040  continue;
2041 
2042  if (Visited.insert(SM).second) {
2043  Stack.push_back(SM);
2044  AnyChildren = true;
2045  }
2046  }
2047 
2048  // We didn't find any children, so add this module to the list of
2049  // modules to link against.
2050  if (!AnyChildren) {
2051  LinkModules.insert(Mod);
2052  }
2053  }
2054 
2055  // Add link options for all of the imported modules in reverse topological
2056  // order. We don't do anything to try to order import link flags with respect
2057  // to linker options inserted by things like #pragma comment().
2058  SmallVector<llvm::MDNode *, 16> MetadataArgs;
2059  Visited.clear();
2060  for (Module *M : LinkModules)
2061  if (Visited.insert(M).second)
2062  addLinkOptionsPostorder(*this, M, MetadataArgs, Visited);
2063  std::reverse(MetadataArgs.begin(), MetadataArgs.end());
2064  LinkerOptionsMetadata.append(MetadataArgs.begin(), MetadataArgs.end());
2065 
2066  // Add the linker options metadata flag.
2067  auto *NMD = getModule().getOrInsertNamedMetadata("llvm.linker.options");
2068  for (auto *MD : LinkerOptionsMetadata)
2069  NMD->addOperand(MD);
2070 }
2071 
2072 void CodeGenModule::EmitDeferred() {
2073  // Emit deferred declare target declarations.
2074  if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd)
2075  getOpenMPRuntime().emitDeferredTargetDecls();
2076 
2077  // Emit code for any potentially referenced deferred decls. Since a
2078  // previously unused static decl may become used during the generation of code
2079  // for a static function, iterate until no changes are made.
2080 
2081  if (!DeferredVTables.empty()) {
2082  EmitDeferredVTables();
2083 
2084  // Emitting a vtable doesn't directly cause more vtables to
2085  // become deferred, although it can cause functions to be
2086  // emitted that then need those vtables.
2087  assert(DeferredVTables.empty());
2088  }
2089 
2090  // Stop if we're out of both deferred vtables and deferred declarations.
2091  if (DeferredDeclsToEmit.empty())
2092  return;
2093 
2094  // Grab the list of decls to emit. If EmitGlobalDefinition schedules more
2095  // work, it will not interfere with this.
2096  std::vector<GlobalDecl> CurDeclsToEmit;
2097  CurDeclsToEmit.swap(DeferredDeclsToEmit);
2098 
2099  for (GlobalDecl &D : CurDeclsToEmit) {
2100  // We should call GetAddrOfGlobal with IsForDefinition set to true in order
2101  // to get GlobalValue with exactly the type we need, not something that
2102  // might had been created for another decl with the same mangled name but
2103  // different type.
2104  llvm::GlobalValue *GV = dyn_cast<llvm::GlobalValue>(
2106 
2107  // In case of different address spaces, we may still get a cast, even with
2108  // IsForDefinition equal to true. Query mangled names table to get
2109  // GlobalValue.
2110  if (!GV)
2111  GV = GetGlobalValue(getMangledName(D));
2112 
2113  // Make sure GetGlobalValue returned non-null.
2114  assert(GV);
2115 
2116  // Check to see if we've already emitted this. This is necessary
2117  // for a couple of reasons: first, decls can end up in the
2118  // deferred-decls queue multiple times, and second, decls can end
2119  // up with definitions in unusual ways (e.g. by an extern inline
2120  // function acquiring a strong function redefinition). Just
2121  // ignore these cases.
2122  if (!GV->isDeclaration())
2123  continue;
2124 
2125  // If this is OpenMP, check if it is legal to emit this global normally.
2126  if (LangOpts.OpenMP && OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(D))
2127  continue;
2128 
2129  // Otherwise, emit the definition and move on to the next one.
2130  EmitGlobalDefinition(D, GV);
2131 
2132  // If we found out that we need to emit more decls, do that recursively.
2133  // This has the advantage that the decls are emitted in a DFS and related
2134  // ones are close together, which is convenient for testing.
2135  if (!DeferredVTables.empty() || !DeferredDeclsToEmit.empty()) {
2136  EmitDeferred();
2137  assert(DeferredVTables.empty() && DeferredDeclsToEmit.empty());
2138  }
2139  }
2140 }
2141 
2142 void CodeGenModule::EmitVTablesOpportunistically() {
2143  // Try to emit external vtables as available_externally if they have emitted
2144  // all inlined virtual functions. It runs after EmitDeferred() and therefore
2145  // is not allowed to create new references to things that need to be emitted
2146  // lazily. Note that it also uses fact that we eagerly emitting RTTI.
2147 
2148  assert((OpportunisticVTables.empty() || shouldOpportunisticallyEmitVTables())
2149  && "Only emit opportunistic vtables with optimizations");
2150 
2151  for (const CXXRecordDecl *RD : OpportunisticVTables) {
2152  assert(getVTables().isVTableExternal(RD) &&
2153  "This queue should only contain external vtables");
2154  if (getCXXABI().canSpeculativelyEmitVTable(RD))
2155  VTables.GenerateClassData(RD);
2156  }
2157  OpportunisticVTables.clear();
2158 }
2159 
2161  if (Annotations.empty())
2162  return;
2163 
2164  // Create a new global variable for the ConstantStruct in the Module.
2165  llvm::Constant *Array = llvm::ConstantArray::get(llvm::ArrayType::get(
2166  Annotations[0]->getType(), Annotations.size()), Annotations);
2167  auto *gv = new llvm::GlobalVariable(getModule(), Array->getType(), false,
2168  llvm::GlobalValue::AppendingLinkage,
2169  Array, "llvm.global.annotations");
2170  gv->setSection(AnnotationSection);
2171 }
2172 
2173 llvm::Constant *CodeGenModule::EmitAnnotationString(StringRef Str) {
2174  llvm::Constant *&AStr = AnnotationStrings[Str];
2175  if (AStr)
2176  return AStr;
2177 
2178  // Not found yet, create a new global.
2179  llvm::Constant *s = llvm::ConstantDataArray::getString(getLLVMContext(), Str);
2180  auto *gv =
2181  new llvm::GlobalVariable(getModule(), s->getType(), true,
2182  llvm::GlobalValue::PrivateLinkage, s, ".str");
2183  gv->setSection(AnnotationSection);
2184  gv->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
2185  AStr = gv;
2186  return gv;
2187 }
2188 
2191  PresumedLoc PLoc = SM.getPresumedLoc(Loc);
2192  if (PLoc.isValid())
2193  return EmitAnnotationString(PLoc.getFilename());
2194  return EmitAnnotationString(SM.getBufferName(Loc));
2195 }
2196 
2199  PresumedLoc PLoc = SM.getPresumedLoc(L);
2200  unsigned LineNo = PLoc.isValid() ? PLoc.getLine() :
2201  SM.getExpansionLineNumber(L);
2202  return llvm::ConstantInt::get(Int32Ty, LineNo);
2203 }
2204 
2205 llvm::Constant *CodeGenModule::EmitAnnotateAttr(llvm::GlobalValue *GV,
2206  const AnnotateAttr *AA,
2207  SourceLocation L) {
2208  // Get the globals for file name, annotation, and the line number.
2209  llvm::Constant *AnnoGV = EmitAnnotationString(AA->getAnnotation()),
2210  *UnitGV = EmitAnnotationUnit(L),
2211  *LineNoCst = EmitAnnotationLineNo(L);
2212 
2213  // Create the ConstantStruct for the global annotation.
2214  llvm::Constant *Fields[4] = {
2215  llvm::ConstantExpr::getBitCast(GV, Int8PtrTy),
2216  llvm::ConstantExpr::getBitCast(AnnoGV, Int8PtrTy),
2217  llvm::ConstantExpr::getBitCast(UnitGV, Int8PtrTy),
2218  LineNoCst
2219  };
2220  return llvm::ConstantStruct::getAnon(Fields);
2221 }
2222 
2224  llvm::GlobalValue *GV) {
2225  assert(D->hasAttr<AnnotateAttr>() && "no annotate attribute");
2226  // Get the struct elements for these annotations.
2227  for (const auto *I : D->specific_attrs<AnnotateAttr>())
2228  Annotations.push_back(EmitAnnotateAttr(GV, I, D->getLocation()));
2229 }
2230 
2232  llvm::Function *Fn,
2233  SourceLocation Loc) const {
2234  const auto &SanitizerBL = getContext().getSanitizerBlacklist();
2235  // Blacklist by function name.
2236  if (SanitizerBL.isBlacklistedFunction(Kind, Fn->getName()))
2237  return true;
2238  // Blacklist by location.
2239  if (Loc.isValid())
2240  return SanitizerBL.isBlacklistedLocation(Kind, Loc);
2241  // If location is unknown, this may be a compiler-generated function. Assume
2242  // it's located in the main file.
2243  auto &SM = Context.getSourceManager();
2244  if (const auto *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
2245  return SanitizerBL.isBlacklistedFile(Kind, MainFile->getName());
2246  }
2247  return false;
2248 }
2249 
2250 bool CodeGenModule::isInSanitizerBlacklist(llvm::GlobalVariable *GV,
2251  SourceLocation Loc, QualType Ty,
2252  StringRef Category) const {
2253  // For now globals can be blacklisted only in ASan and KASan.
2254  const SanitizerMask EnabledAsanMask =
2255  LangOpts.Sanitize.Mask &
2256  (SanitizerKind::Address | SanitizerKind::KernelAddress |
2257  SanitizerKind::HWAddress | SanitizerKind::KernelHWAddress |
2258  SanitizerKind::MemTag);
2259  if (!EnabledAsanMask)
2260  return false;
2261  const auto &SanitizerBL = getContext().getSanitizerBlacklist();
2262  if (SanitizerBL.isBlacklistedGlobal(EnabledAsanMask, GV->getName(), Category))
2263  return true;
2264  if (SanitizerBL.isBlacklistedLocation(EnabledAsanMask, Loc, Category))
2265  return true;
2266  // Check global type.
2267  if (!Ty.isNull()) {
2268  // Drill down the array types: if global variable of a fixed type is
2269  // blacklisted, we also don't instrument arrays of them.
2270  while (auto AT = dyn_cast<ArrayType>(Ty.getTypePtr()))
2271  Ty = AT->getElementType();
2273  // We allow to blacklist only record types (classes, structs etc.)
2274  if (Ty->isRecordType()) {
2275  std::string TypeStr = Ty.getAsString(getContext().getPrintingPolicy());
2276  if (SanitizerBL.isBlacklistedType(EnabledAsanMask, TypeStr, Category))
2277  return true;
2278  }
2279  }
2280  return false;
2281 }
2282 
2283 bool CodeGenModule::imbueXRayAttrs(llvm::Function *Fn, SourceLocation Loc,
2284  StringRef Category) const {
2285  const auto &XRayFilter = getContext().getXRayFilter();
2286  using ImbueAttr = XRayFunctionFilter::ImbueAttribute;
2287  auto Attr = ImbueAttr::NONE;
2288  if (Loc.isValid())
2289  Attr = XRayFilter.shouldImbueLocation(Loc, Category);
2290  if (Attr == ImbueAttr::NONE)
2291  Attr = XRayFilter.shouldImbueFunction(Fn->getName());
2292  switch (Attr) {
2293  case ImbueAttr::NONE:
2294  return false;
2295  case ImbueAttr::ALWAYS:
2296  Fn->addFnAttr("function-instrument", "xray-always");
2297  break;
2298  case ImbueAttr::ALWAYS_ARG1:
2299  Fn->addFnAttr("function-instrument", "xray-always");
2300  Fn->addFnAttr("xray-log-args", "1");
2301  break;
2302  case ImbueAttr::NEVER:
2303  Fn->addFnAttr("function-instrument", "xray-never");
2304  break;
2305  }
2306  return true;
2307 }
2308 
2309 bool CodeGenModule::MustBeEmitted(const ValueDecl *Global) {
2310  // Never defer when EmitAllDecls is specified.
2311  if (LangOpts.EmitAllDecls)
2312  return true;
2313 
2314  if (CodeGenOpts.KeepStaticConsts) {
2315  const auto *VD = dyn_cast<VarDecl>(Global);
2316  if (VD && VD->getType().isConstQualified() &&
2317  VD->getStorageDuration() == SD_Static)
2318  return true;
2319  }
2320 
2321  return getContext().DeclMustBeEmitted(Global);
2322 }
2323 
2324 bool CodeGenModule::MayBeEmittedEagerly(const ValueDecl *Global) {
2325  if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2327  // Implicit template instantiations may change linkage if they are later
2328  // explicitly instantiated, so they should not be emitted eagerly.
2329  return false;
2330  // In OpenMP 5.0 function may be marked as device_type(nohost) and we should
2331  // not emit them eagerly unless we sure that the function must be emitted on
2332  // the host.
2333  if (LangOpts.OpenMP >= 50 && !LangOpts.OpenMPSimd &&
2334  !LangOpts.OpenMPIsDevice &&
2335  !OMPDeclareTargetDeclAttr::getDeviceType(FD) &&
2336  !FD->isUsed(/*CheckUsedAttr=*/false) && !FD->isReferenced())
2337  return false;
2338  }
2339  if (const auto *VD = dyn_cast<VarDecl>(Global))
2340  if (Context.getInlineVariableDefinitionKind(VD) ==
2342  // A definition of an inline constexpr static data member may change
2343  // linkage later if it's redeclared outside the class.
2344  return false;
2345  // If OpenMP is enabled and threadprivates must be generated like TLS, delay
2346  // codegen for global variables, because they may be marked as threadprivate.
2347  if (LangOpts.OpenMP && LangOpts.OpenMPUseTLS &&
2348  getContext().getTargetInfo().isTLSSupported() && isa<VarDecl>(Global) &&
2349  !isTypeConstant(Global->getType(), false) &&
2350  !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(Global))
2351  return false;
2352 
2353  return true;
2354 }
2355 
2357  const CXXUuidofExpr* E) {
2358  // Sema has verified that IIDSource has a __declspec(uuid()), and that its
2359  // well-formed.
2360  StringRef Uuid = E->getUuidStr();
2361  std::string Name = "_GUID_" + Uuid.lower();
2362  std::replace(Name.begin(), Name.end(), '-', '_');
2363 
2364  // The UUID descriptor should be pointer aligned.
2366 
2367  // Look for an existing global.
2368  if (llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name))
2369  return ConstantAddress(GV, Alignment);
2370 
2371  llvm::Constant *Init = EmitUuidofInitializer(Uuid);
2372  assert(Init && "failed to initialize as constant");
2373 
2374  auto *GV = new llvm::GlobalVariable(
2375  getModule(), Init->getType(),
2376  /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
2377  if (supportsCOMDAT())
2378  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
2379  setDSOLocal(GV);
2380  return ConstantAddress(GV, Alignment);
2381 }
2382 
2384  const AliasAttr *AA = VD->getAttr<AliasAttr>();
2385  assert(AA && "No alias?");
2386 
2387  CharUnits Alignment = getContext().getDeclAlign(VD);
2389 
2390  // See if there is already something with the target's name in the module.
2391  llvm::GlobalValue *Entry = GetGlobalValue(AA->getAliasee());
2392  if (Entry) {
2393  unsigned AS = getContext().getTargetAddressSpace(VD->getType());
2394  auto Ptr = llvm::ConstantExpr::getBitCast(Entry, DeclTy->getPointerTo(AS));
2395  return ConstantAddress(Ptr, Alignment);
2396  }
2397 
2398  llvm::Constant *Aliasee;
2399  if (isa<llvm::FunctionType>(DeclTy))
2400  Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy,
2401  GlobalDecl(cast<FunctionDecl>(VD)),
2402  /*ForVTable=*/false);
2403  else
2404  Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
2405  llvm::PointerType::getUnqual(DeclTy),
2406  nullptr);
2407 
2408  auto *F = cast<llvm::GlobalValue>(Aliasee);
2409  F->setLinkage(llvm::Function::ExternalWeakLinkage);
2410  WeakRefReferences.insert(F);
2411 
2412  return ConstantAddress(Aliasee, Alignment);
2413 }
2414 
2416  const auto *Global = cast<ValueDecl>(GD.getDecl());
2417 
2418  // Weak references don't produce any output by themselves.
2419  if (Global->hasAttr<WeakRefAttr>())
2420  return;
2421 
2422  // If this is an alias definition (which otherwise looks like a declaration)
2423  // emit it now.
2424  if (Global->hasAttr<AliasAttr>())
2425  return EmitAliasDefinition(GD);
2426 
2427  // IFunc like an alias whose value is resolved at runtime by calling resolver.
2428  if (Global->hasAttr<IFuncAttr>())
2429  return emitIFuncDefinition(GD);
2430 
2431  // If this is a cpu_dispatch multiversion function, emit the resolver.
2432  if (Global->hasAttr<CPUDispatchAttr>())
2433  return emitCPUDispatchDefinition(GD);
2434 
2435  // If this is CUDA, be selective about which declarations we emit.
2436  if (LangOpts.CUDA) {
2437  if (LangOpts.CUDAIsDevice) {
2438  if (!Global->hasAttr<CUDADeviceAttr>() &&
2439  !Global->hasAttr<CUDAGlobalAttr>() &&
2440  !Global->hasAttr<CUDAConstantAttr>() &&
2441  !Global->hasAttr<CUDASharedAttr>() &&
2442  !(LangOpts.HIP && Global->hasAttr<HIPPinnedShadowAttr>()))
2443  return;
2444  } else {
2445  // We need to emit host-side 'shadows' for all global
2446  // device-side variables because the CUDA runtime needs their
2447  // size and host-side address in order to provide access to
2448  // their device-side incarnations.
2449 
2450  // So device-only functions are the only things we skip.
2451  if (isa<FunctionDecl>(Global) && !Global->hasAttr<CUDAHostAttr>() &&
2452  Global->hasAttr<CUDADeviceAttr>())
2453  return;
2454 
2455  assert((isa<FunctionDecl>(Global) || isa<VarDecl>(Global)) &&
2456  "Expected Variable or Function");
2457  }
2458  }
2459 
2460  if (LangOpts.OpenMP) {
2461  // If this is OpenMP, check if it is legal to emit this global normally.
2462  if (OpenMPRuntime && OpenMPRuntime->emitTargetGlobal(GD))
2463  return;
2464  if (auto *DRD = dyn_cast<OMPDeclareReductionDecl>(Global)) {
2465  if (MustBeEmitted(Global))
2467  return;
2468  } else if (auto *DMD = dyn_cast<OMPDeclareMapperDecl>(Global)) {
2469  if (MustBeEmitted(Global))
2470  EmitOMPDeclareMapper(DMD);
2471  return;
2472  }
2473  }
2474 
2475  // Ignore declarations, they will be emitted on their first use.
2476  if (const auto *FD = dyn_cast<FunctionDecl>(Global)) {
2477  // Forward declarations are emitted lazily on first use.
2478  if (!FD->doesThisDeclarationHaveABody()) {
2480  return;
2481 
2482  StringRef MangledName = getMangledName(GD);
2483 
2484  // Compute the function info and LLVM type.
2486  llvm::Type *Ty = getTypes().GetFunctionType(FI);
2487 
2488  GetOrCreateLLVMFunction(MangledName, Ty, GD, /*ForVTable=*/false,
2489  /*DontDefer=*/false);
2490  return;
2491  }
2492  } else {
2493  const auto *VD = cast<VarDecl>(Global);
2494  assert(VD->isFileVarDecl() && "Cannot emit local var decl as global.");
2495  if (VD->isThisDeclarationADefinition() != VarDecl::Definition &&
2496  !Context.isMSStaticDataMemberInlineDefinition(VD)) {
2497  if (LangOpts.OpenMP) {
2498  // Emit declaration of the must-be-emitted declare target variable.
2500  OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD)) {
2501  bool UnifiedMemoryEnabled =
2502  getOpenMPRuntime().hasRequiresUnifiedSharedMemory();
2503  if (*Res == OMPDeclareTargetDeclAttr::MT_To &&
2504  !UnifiedMemoryEnabled) {
2505  (void)GetAddrOfGlobalVar(VD);
2506  } else {
2507  assert(((*Res == OMPDeclareTargetDeclAttr::MT_Link) ||
2508  (*Res == OMPDeclareTargetDeclAttr::MT_To &&
2509  UnifiedMemoryEnabled)) &&
2510  "Link clause or to clause with unified memory expected.");
2511  (void)getOpenMPRuntime().getAddrOfDeclareTargetVar(VD);
2512  }
2513 
2514  return;
2515  }
2516  }
2517  // If this declaration may have caused an inline variable definition to
2518  // change linkage, make sure that it's emitted.
2519  if (Context.getInlineVariableDefinitionKind(VD) ==
2521  GetAddrOfGlobalVar(VD);
2522  return;
2523  }
2524  }
2525 
2526  // Defer code generation to first use when possible, e.g. if this is an inline
2527  // function. If the global must always be emitted, do it eagerly if possible
2528  // to benefit from cache locality.
2529  if (MustBeEmitted(Global) && MayBeEmittedEagerly(Global)) {
2530  // Emit the definition if it can't be deferred.
2531  EmitGlobalDefinition(GD);
2532  return;
2533  }
2534 
2535  // If we're deferring emission of a C++ variable with an
2536  // initializer, remember the order in which it appeared in the file.
2537  if (getLangOpts().CPlusPlus && isa<VarDecl>(Global) &&
2538  cast<VarDecl>(Global)->hasInit()) {
2539  DelayedCXXInitPosition[Global] = CXXGlobalInits.size();
2540  CXXGlobalInits.push_back(nullptr);
2541  }
2542 
2543  StringRef MangledName = getMangledName(GD);
2544  if (GetGlobalValue(MangledName) != nullptr) {
2545  // The value has already been used and should therefore be emitted.
2546  addDeferredDeclToEmit(GD);
2547  } else if (MustBeEmitted(Global)) {
2548  // The value must be emitted, but cannot be emitted eagerly.
2549  assert(!MayBeEmittedEagerly(Global));
2550  addDeferredDeclToEmit(GD);
2551  } else {
2552  // Otherwise, remember that we saw a deferred decl with this name. The
2553  // first use of the mangled name will cause it to move into
2554  // DeferredDeclsToEmit.
2555  DeferredDecls[MangledName] = GD;
2556  }
2557 }
2558 
2559 // Check if T is a class type with a destructor that's not dllimport.
2561  if (const auto *RT = T->getBaseElementTypeUnsafe()->getAs<RecordType>())
2562  if (CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl()))
2563  if (RD->getDestructor() && !RD->getDestructor()->hasAttr<DLLImportAttr>())
2564  return true;
2565 
2566  return false;
2567 }
2568 
2569 namespace {
2570  struct FunctionIsDirectlyRecursive
2571  : public ConstStmtVisitor<FunctionIsDirectlyRecursive, bool> {
2572  const StringRef Name;
2573  const Builtin::Context &BI;
2574  FunctionIsDirectlyRecursive(StringRef N, const Builtin::Context &C)
2575  : Name(N), BI(C) {}
2576 
2577  bool VisitCallExpr(const CallExpr *E) {
2578  const FunctionDecl *FD = E->getDirectCallee();
2579  if (!FD)
2580  return false;
2581  AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
2582  if (Attr && Name == Attr->getLabel())
2583  return true;
2584  unsigned BuiltinID = FD->getBuiltinID();
2585  if (!BuiltinID || !BI.isLibFunction(BuiltinID))
2586  return false;
2587  StringRef BuiltinName = BI.getName(BuiltinID);
2588  if (BuiltinName.startswith("__builtin_") &&
2589  Name == BuiltinName.slice(strlen("__builtin_"), StringRef::npos)) {
2590  return true;
2591  }
2592  return false;
2593  }
2594 
2595  bool VisitStmt(const Stmt *S) {
2596  for (const Stmt *Child : S->children())
2597  if (Child && this->Visit(Child))
2598  return true;
2599  return false;
2600  }
2601  };
2602 
2603  // Make sure we're not referencing non-imported vars or functions.
2604  struct DLLImportFunctionVisitor
2605  : public RecursiveASTVisitor<DLLImportFunctionVisitor> {
2606  bool SafeToInline = true;
2607 
2608  bool shouldVisitImplicitCode() const { return true; }
2609 
2610  bool VisitVarDecl(VarDecl *VD) {
2611  if (VD->getTLSKind()) {
2612  // A thread-local variable cannot be imported.
2613  SafeToInline = false;
2614  return SafeToInline;
2615  }
2616 
2617  // A variable definition might imply a destructor call.
2618  if (VD->isThisDeclarationADefinition())
2619  SafeToInline = !HasNonDllImportDtor(VD->getType());
2620 
2621  return SafeToInline;
2622  }
2623 
2624  bool VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) {
2625  if (const auto *D = E->getTemporary()->getDestructor())
2626  SafeToInline = D->hasAttr<DLLImportAttr>();
2627  return SafeToInline;
2628  }
2629 
2630  bool VisitDeclRefExpr(DeclRefExpr *E) {
2631  ValueDecl *VD = E->getDecl();
2632  if (isa<FunctionDecl>(VD))
2633  SafeToInline = VD->hasAttr<DLLImportAttr>();
2634  else if (VarDecl *V = dyn_cast<VarDecl>(VD))
2635  SafeToInline = !V->hasGlobalStorage() || V->hasAttr<DLLImportAttr>();
2636  return SafeToInline;
2637  }
2638 
2639  bool VisitCXXConstructExpr(CXXConstructExpr *E) {
2640  SafeToInline = E->getConstructor()->hasAttr<DLLImportAttr>();
2641  return SafeToInline;
2642  }
2643 
2644  bool VisitCXXMemberCallExpr(CXXMemberCallExpr *E) {
2645  CXXMethodDecl *M = E->getMethodDecl();
2646  if (!M) {
2647  // Call through a pointer to member function. This is safe to inline.
2648  SafeToInline = true;
2649  } else {
2650  SafeToInline = M->hasAttr<DLLImportAttr>();
2651  }
2652  return SafeToInline;
2653  }
2654 
2655  bool VisitCXXDeleteExpr(CXXDeleteExpr *E) {
2656  SafeToInline = E->getOperatorDelete()->hasAttr<DLLImportAttr>();
2657  return SafeToInline;
2658  }
2659 
2660  bool VisitCXXNewExpr(CXXNewExpr *E) {
2661  SafeToInline = E->getOperatorNew()->hasAttr<DLLImportAttr>();
2662  return SafeToInline;
2663  }
2664  };
2665 }
2666 
2667 // isTriviallyRecursive - Check if this function calls another
2668 // decl that, because of the asm attribute or the other decl being a builtin,
2669 // ends up pointing to itself.
2670 bool
2671 CodeGenModule::isTriviallyRecursive(const FunctionDecl *FD) {
2672  StringRef Name;
2673  if (getCXXABI().getMangleContext().shouldMangleDeclName(FD)) {
2674  // asm labels are a special kind of mangling we have to support.
2675  AsmLabelAttr *Attr = FD->getAttr<AsmLabelAttr>();
2676  if (!Attr)
2677  return false;
2678  Name = Attr->getLabel();
2679  } else {
2680  Name = FD->getName();
2681  }
2682 
2683  FunctionIsDirectlyRecursive Walker(Name, Context.BuiltinInfo);
2684  const Stmt *Body = FD->getBody();
2685  return Body ? Walker.Visit(Body) : false;
2686 }
2687 
2688 bool CodeGenModule::shouldEmitFunction(GlobalDecl GD) {
2689  if (getFunctionLinkage(GD) != llvm::Function::AvailableExternallyLinkage)
2690  return true;
2691  const auto *F = cast<FunctionDecl>(GD.getDecl());
2692  if (CodeGenOpts.OptimizationLevel == 0 && !F->hasAttr<AlwaysInlineAttr>())
2693  return false;
2694 
2695  if (F->hasAttr<DLLImportAttr>()) {
2696  // Check whether it would be safe to inline this dllimport function.
2697  DLLImportFunctionVisitor Visitor;
2698  Visitor.TraverseFunctionDecl(const_cast<FunctionDecl*>(F));
2699  if (!Visitor.SafeToInline)
2700  return false;
2701 
2702  if (const CXXDestructorDecl *Dtor = dyn_cast<CXXDestructorDecl>(F)) {
2703  // Implicit destructor invocations aren't captured in the AST, so the
2704  // check above can't see them. Check for them manually here.
2705  for (const Decl *Member : Dtor->getParent()->decls())
2706  if (isa<FieldDecl>(Member))
2707  if (HasNonDllImportDtor(cast<FieldDecl>(Member)->getType()))
2708  return false;
2709  for (const CXXBaseSpecifier &B : Dtor->getParent()->bases())
2710  if (HasNonDllImportDtor(B.getType()))
2711  return false;
2712  }
2713  }
2714 
2715  // PR9614. Avoid cases where the source code is lying to us. An available
2716  // externally function should have an equivalent function somewhere else,
2717  // but a function that calls itself is clearly not equivalent to the real
2718  // implementation.
2719  // This happens in glibc's btowc and in some configure checks.
2720  return !isTriviallyRecursive(F);
2721 }
2722 
2723 bool CodeGenModule::shouldOpportunisticallyEmitVTables() {
2724  return CodeGenOpts.OptimizationLevel > 0;
2725 }
2726 
2727 void CodeGenModule::EmitMultiVersionFunctionDefinition(GlobalDecl GD,
2728  llvm::GlobalValue *GV) {
2729  const auto *FD = cast<FunctionDecl>(GD.getDecl());
2730 
2731  if (FD->isCPUSpecificMultiVersion()) {
2732  auto *Spec = FD->getAttr<CPUSpecificAttr>();
2733  for (unsigned I = 0; I < Spec->cpus_size(); ++I)
2734  EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
2735  // Requires multiple emits.
2736  } else
2737  EmitGlobalFunctionDefinition(GD, GV);
2738 }
2739 
2740 void CodeGenModule::EmitGlobalDefinition(GlobalDecl GD, llvm::GlobalValue *GV) {
2741  const auto *D = cast<ValueDecl>(GD.getDecl());
2742 
2743  PrettyStackTraceDecl CrashInfo(const_cast<ValueDecl *>(D), D->getLocation(),
2744  Context.getSourceManager(),
2745  "Generating code for declaration");
2746 
2747  if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
2748  // At -O0, don't generate IR for functions with available_externally
2749  // linkage.
2750  if (!shouldEmitFunction(GD))
2751  return;
2752 
2753  llvm::TimeTraceScope TimeScope("CodeGen Function", [&]() {
2754  std::string Name;
2755  llvm::raw_string_ostream OS(Name);
2756  FD->getNameForDiagnostic(OS, getContext().getPrintingPolicy(),
2757  /*Qualified=*/true);
2758  return Name;
2759  });
2760 
2761  if (const auto *Method = dyn_cast<CXXMethodDecl>(D)) {
2762  // Make sure to emit the definition(s) before we emit the thunks.
2763  // This is necessary for the generation of certain thunks.
2764  if (isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method))
2765  ABI->emitCXXStructor(GD);
2766  else if (FD->isMultiVersion())
2767  EmitMultiVersionFunctionDefinition(GD, GV);
2768  else
2769  EmitGlobalFunctionDefinition(GD, GV);
2770 
2771  if (Method->isVirtual())
2772  getVTables().EmitThunks(GD);
2773 
2774  return;
2775  }
2776 
2777  if (FD->isMultiVersion())
2778  return EmitMultiVersionFunctionDefinition(GD, GV);
2779  return EmitGlobalFunctionDefinition(GD, GV);
2780  }
2781 
2782  if (const auto *VD = dyn_cast<VarDecl>(D))
2783  return EmitGlobalVarDefinition(VD, !VD->hasDefinition());
2784 
2785  llvm_unreachable("Invalid argument to EmitGlobalDefinition()");
2786 }
2787 
2788 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
2789  llvm::Function *NewFn);
2790 
2791 static unsigned
2794  unsigned Priority = 0;
2795  for (StringRef Feat : RO.Conditions.Features)
2796  Priority = std::max(Priority, TI.multiVersionSortPriority(Feat));
2797 
2798  if (!RO.Conditions.Architecture.empty())
2799  Priority = std::max(
2801  return Priority;
2802 }
2803 
2804 void CodeGenModule::emitMultiVersionFunctions() {
2805  for (GlobalDecl GD : MultiVersionFuncs) {
2807  const FunctionDecl *FD = cast<FunctionDecl>(GD.getDecl());
2809  FD, [this, &GD, &Options](const FunctionDecl *CurFD) {
2810  GlobalDecl CurGD{
2811  (CurFD->isDefined() ? CurFD->getDefinition() : CurFD)};
2812  StringRef MangledName = getMangledName(CurGD);
2813  llvm::Constant *Func = GetGlobalValue(MangledName);
2814  if (!Func) {
2815  if (CurFD->isDefined()) {
2816  EmitGlobalFunctionDefinition(CurGD, nullptr);
2817  Func = GetGlobalValue(MangledName);
2818  } else {
2819  const CGFunctionInfo &FI =
2821  llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
2822  Func = GetAddrOfFunction(CurGD, Ty, /*ForVTable=*/false,
2823  /*DontDefer=*/false, ForDefinition);
2824  }
2825  assert(Func && "This should have just been created");
2826  }
2827 
2828  const auto *TA = CurFD->getAttr<TargetAttr>();
2830  TA->getAddedFeatures(Feats);
2831 
2832  Options.emplace_back(cast<llvm::Function>(Func),
2833  TA->getArchitecture(), Feats);
2834  });
2835 
2836  llvm::Function *ResolverFunc;
2837  const TargetInfo &TI = getTarget();
2838 
2839  if (TI.supportsIFunc() || FD->isTargetMultiVersion()) {
2840  ResolverFunc = cast<llvm::Function>(
2841  GetGlobalValue((getMangledName(GD) + ".resolver").str()));
2842  ResolverFunc->setLinkage(llvm::Function::WeakODRLinkage);
2843  } else {
2844  ResolverFunc = cast<llvm::Function>(GetGlobalValue(getMangledName(GD)));
2845  }
2846 
2847  if (supportsCOMDAT())
2848  ResolverFunc->setComdat(
2849  getModule().getOrInsertComdat(ResolverFunc->getName()));
2850 
2851  llvm::stable_sort(
2852  Options, [&TI](const CodeGenFunction::MultiVersionResolverOption &LHS,
2854  return TargetMVPriority(TI, LHS) > TargetMVPriority(TI, RHS);
2855  });
2856  CodeGenFunction CGF(*this);
2857  CGF.EmitMultiVersionResolver(ResolverFunc, Options);
2858  }
2859 }
2860 
2861 void CodeGenModule::emitCPUDispatchDefinition(GlobalDecl GD) {
2862  const auto *FD = cast<FunctionDecl>(GD.getDecl());
2863  assert(FD && "Not a FunctionDecl?");
2864  const auto *DD = FD->getAttr<CPUDispatchAttr>();
2865  assert(DD && "Not a cpu_dispatch Function?");
2867 
2868  if (const auto *CXXFD = dyn_cast<CXXMethodDecl>(FD)) {
2869  const CGFunctionInfo &FInfo = getTypes().arrangeCXXMethodDeclaration(CXXFD);
2870  DeclTy = getTypes().GetFunctionType(FInfo);
2871  }
2872 
2873  StringRef ResolverName = getMangledName(GD);
2874 
2875  llvm::Type *ResolverType;
2876  GlobalDecl ResolverGD;
2877  if (getTarget().supportsIFunc())
2878  ResolverType = llvm::FunctionType::get(
2879  llvm::PointerType::get(DeclTy,
2880  Context.getTargetAddressSpace(FD->getType())),
2881  false);
2882  else {
2883  ResolverType = DeclTy;
2884  ResolverGD = GD;
2885  }
2886 
2887  auto *ResolverFunc = cast<llvm::Function>(GetOrCreateLLVMFunction(
2888  ResolverName, ResolverType, ResolverGD, /*ForVTable=*/false));
2889  ResolverFunc->setLinkage(llvm::Function::WeakODRLinkage);
2890  if (supportsCOMDAT())
2891  ResolverFunc->setComdat(
2892  getModule().getOrInsertComdat(ResolverFunc->getName()));
2893 
2895  const TargetInfo &Target = getTarget();
2896  unsigned Index = 0;
2897  for (const IdentifierInfo *II : DD->cpus()) {
2898  // Get the name of the target function so we can look it up/create it.
2899  std::string MangledName = getMangledNameImpl(*this, GD, FD, true) +
2900  getCPUSpecificMangling(*this, II->getName());
2901 
2902  llvm::Constant *Func = GetGlobalValue(MangledName);
2903 
2904  if (!Func) {
2905  GlobalDecl ExistingDecl = Manglings.lookup(MangledName);
2906  if (ExistingDecl.getDecl() &&
2907  ExistingDecl.getDecl()->getAsFunction()->isDefined()) {
2908  EmitGlobalFunctionDefinition(ExistingDecl, nullptr);
2909  Func = GetGlobalValue(MangledName);
2910  } else {
2911  if (!ExistingDecl.getDecl())
2912  ExistingDecl = GD.getWithMultiVersionIndex(Index);
2913 
2914  Func = GetOrCreateLLVMFunction(
2915  MangledName, DeclTy, ExistingDecl,
2916  /*ForVTable=*/false, /*DontDefer=*/true,
2917  /*IsThunk=*/false, llvm::AttributeList(), ForDefinition);
2918  }
2919  }
2920 
2922  Target.getCPUSpecificCPUDispatchFeatures(II->getName(), Features);
2923  llvm::transform(Features, Features.begin(),
2924  [](StringRef Str) { return Str.substr(1); });
2925  Features.erase(std::remove_if(
2926  Features.begin(), Features.end(), [&Target](StringRef Feat) {
2927  return !Target.validateCpuSupports(Feat);
2928  }), Features.end());
2929  Options.emplace_back(cast<llvm::Function>(Func), StringRef{}, Features);
2930  ++Index;
2931  }
2932 
2933  llvm::sort(
2934  Options, [](const CodeGenFunction::MultiVersionResolverOption &LHS,
2938  });
2939 
2940  // If the list contains multiple 'default' versions, such as when it contains
2941  // 'pentium' and 'generic', don't emit the call to the generic one (since we
2942  // always run on at least a 'pentium'). We do this by deleting the 'least
2943  // advanced' (read, lowest mangling letter).
2944  while (Options.size() > 1 &&
2946  (Options.end() - 2)->Conditions.Features) == 0) {
2947  StringRef LHSName = (Options.end() - 2)->Function->getName();
2948  StringRef RHSName = (Options.end() - 1)->Function->getName();
2949  if (LHSName.compare(RHSName) < 0)
2950  Options.erase(Options.end() - 2);
2951  else
2952  Options.erase(Options.end() - 1);
2953  }
2954 
2955  CodeGenFunction CGF(*this);
2956  CGF.EmitMultiVersionResolver(ResolverFunc, Options);
2957 
2958  if (getTarget().supportsIFunc()) {
2959  std::string AliasName = getMangledNameImpl(
2960  *this, GD, FD, /*OmitMultiVersionMangling=*/true);
2961  llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
2962  if (!AliasFunc) {
2963  auto *IFunc = cast<llvm::GlobalIFunc>(GetOrCreateLLVMFunction(
2964  AliasName, DeclTy, GD, /*ForVTable=*/false, /*DontDefer=*/true,
2965  /*IsThunk=*/false, llvm::AttributeList(), NotForDefinition));
2966  auto *GA = llvm::GlobalAlias::create(
2967  DeclTy, 0, getFunctionLinkage(GD), AliasName, IFunc, &getModule());
2968  GA->setLinkage(llvm::Function::WeakODRLinkage);
2969  SetCommonAttributes(GD, GA);
2970  }
2971  }
2972 }
2973 
2974 /// If a dispatcher for the specified mangled name is not in the module, create
2975 /// and return an llvm Function with the specified type.
2976 llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(
2977  GlobalDecl GD, llvm::Type *DeclTy, const FunctionDecl *FD) {
2978  std::string MangledName =
2979  getMangledNameImpl(*this, GD, FD, /*OmitMultiVersionMangling=*/true);
2980 
2981  // Holds the name of the resolver, in ifunc mode this is the ifunc (which has
2982  // a separate resolver).
2983  std::string ResolverName = MangledName;
2984  if (getTarget().supportsIFunc())
2985  ResolverName += ".ifunc";
2986  else if (FD->isTargetMultiVersion())
2987  ResolverName += ".resolver";
2988 
2989  // If this already exists, just return that one.
2990  if (llvm::GlobalValue *ResolverGV = GetGlobalValue(ResolverName))
2991  return ResolverGV;
2992 
2993  // Since this is the first time we've created this IFunc, make sure
2994  // that we put this multiversioned function into the list to be
2995  // replaced later if necessary (target multiversioning only).
2997  MultiVersionFuncs.push_back(GD);
2998 
2999  if (getTarget().supportsIFunc()) {
3000  llvm::Type *ResolverType = llvm::FunctionType::get(
3001  llvm::PointerType::get(
3002  DeclTy, getContext().getTargetAddressSpace(FD->getType())),
3003  false);
3004  llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3005  MangledName + ".resolver", ResolverType, GlobalDecl{},
3006  /*ForVTable=*/false);
3007  llvm::GlobalIFunc *GIF = llvm::GlobalIFunc::create(
3008  DeclTy, 0, llvm::Function::WeakODRLinkage, "", Resolver, &getModule());
3009  GIF->setName(ResolverName);
3010  SetCommonAttributes(FD, GIF);
3011 
3012  return GIF;
3013  }
3014 
3015  llvm::Constant *Resolver = GetOrCreateLLVMFunction(
3016  ResolverName, DeclTy, GlobalDecl{}, /*ForVTable=*/false);
3017  assert(isa<llvm::GlobalValue>(Resolver) &&
3018  "Resolver should be created for the first time");
3019  SetCommonAttributes(FD, cast<llvm::GlobalValue>(Resolver));
3020  return Resolver;
3021 }
3022 
3023 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
3024 /// module, create and return an llvm Function with the specified type. If there
3025 /// is something in the module with the specified name, return it potentially
3026 /// bitcasted to the right type.
3027 ///
3028 /// If D is non-null, it specifies a decl that correspond to this. This is used
3029 /// to set the attributes on the function when it is first created.
3030 llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
3031  StringRef MangledName, llvm::Type *Ty, GlobalDecl GD, bool ForVTable,
3032  bool DontDefer, bool IsThunk, llvm::AttributeList ExtraAttrs,
3033  ForDefinition_t IsForDefinition) {
3034  const Decl *D = GD.getDecl();
3035 
3036  // Any attempts to use a MultiVersion function should result in retrieving
3037  // the iFunc instead. Name Mangling will handle the rest of the changes.
3038  if (const FunctionDecl *FD = cast_or_null<FunctionDecl>(D)) {
3039  // For the device mark the function as one that should be emitted.
3040  if (getLangOpts().OpenMPIsDevice && OpenMPRuntime &&
3041  !OpenMPRuntime->markAsGlobalTarget(GD) && FD->isDefined() &&
3042  !DontDefer && !IsForDefinition) {
3043  if (const FunctionDecl *FDDef = FD->getDefinition()) {
3044  GlobalDecl GDDef;
3045  if (const auto *CD = dyn_cast<CXXConstructorDecl>(FDDef))
3046  GDDef = GlobalDecl(CD, GD.getCtorType());
3047  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(FDDef))
3048  GDDef = GlobalDecl(DD, GD.getDtorType());
3049  else
3050  GDDef = GlobalDecl(FDDef);
3051  EmitGlobal(GDDef);
3052  }
3053  }
3054 
3055  if (FD->isMultiVersion()) {
3056  const auto *TA = FD->getAttr<TargetAttr>();
3057  if (TA && TA->isDefaultVersion())
3058  UpdateMultiVersionNames(GD, FD);
3059  if (!IsForDefinition)
3060  return GetOrCreateMultiVersionResolver(GD, Ty, FD);
3061  }
3062  }
3063 
3064  // Lookup the entry, lazily creating it if necessary.
3065  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3066  if (Entry) {
3067  if (WeakRefReferences.erase(Entry)) {
3068  const FunctionDecl *FD = cast_or_null<FunctionDecl>(D);
3069  if (FD && !FD->hasAttr<WeakAttr>())
3070  Entry->setLinkage(llvm::Function::ExternalLinkage);
3071  }
3072 
3073  // Handle dropped DLL attributes.
3074  if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) {
3075  Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3076  setDSOLocal(Entry);
3077  }
3078 
3079  // If there are two attempts to define the same mangled name, issue an
3080  // error.
3081  if (IsForDefinition && !Entry->isDeclaration()) {
3082  GlobalDecl OtherGD;
3083  // Check that GD is not yet in DiagnosedConflictingDefinitions is required
3084  // to make sure that we issue an error only once.
3085  if (lookupRepresentativeDecl(MangledName, OtherGD) &&
3086  (GD.getCanonicalDecl().getDecl() !=
3087  OtherGD.getCanonicalDecl().getDecl()) &&
3088  DiagnosedConflictingDefinitions.insert(GD).second) {
3089  getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3090  << MangledName;
3091  getDiags().Report(OtherGD.getDecl()->getLocation(),
3092  diag::note_previous_definition);
3093  }
3094  }
3095 
3096  if ((isa<llvm::Function>(Entry) || isa<llvm::GlobalAlias>(Entry)) &&
3097  (Entry->getType()->getElementType() == Ty)) {
3098  return Entry;
3099  }
3100 
3101  // Make sure the result is of the correct type.
3102  // (If function is requested for a definition, we always need to create a new
3103  // function, not just return a bitcast.)
3104  if (!IsForDefinition)
3105  return llvm::ConstantExpr::getBitCast(Entry, Ty->getPointerTo());
3106  }
3107 
3108  // This function doesn't have a complete type (for example, the return
3109  // type is an incomplete struct). Use a fake type instead, and make
3110  // sure not to try to set attributes.
3111  bool IsIncompleteFunction = false;
3112 
3113  llvm::FunctionType *FTy;
3114  if (isa<llvm::FunctionType>(Ty)) {
3115  FTy = cast<llvm::FunctionType>(Ty);
3116  } else {
3117  FTy = llvm::FunctionType::get(VoidTy, false);
3118  IsIncompleteFunction = true;
3119  }
3120 
3121  llvm::Function *F =
3123  Entry ? StringRef() : MangledName, &getModule());
3124 
3125  // If we already created a function with the same mangled name (but different
3126  // type) before, take its name and add it to the list of functions to be
3127  // replaced with F at the end of CodeGen.
3128  //
3129  // This happens if there is a prototype for a function (e.g. "int f()") and
3130  // then a definition of a different type (e.g. "int f(int x)").
3131  if (Entry) {
3132  F->takeName(Entry);
3133 
3134  // This might be an implementation of a function without a prototype, in
3135  // which case, try to do special replacement of calls which match the new
3136  // prototype. The really key thing here is that we also potentially drop
3137  // arguments from the call site so as to make a direct call, which makes the
3138  // inliner happier and suppresses a number of optimizer warnings (!) about
3139  // dropping arguments.
3140  if (!Entry->use_empty()) {
3142  Entry->removeDeadConstantUsers();
3143  }
3144 
3145  llvm::Constant *BC = llvm::ConstantExpr::getBitCast(
3146  F, Entry->getType()->getElementType()->getPointerTo());
3147  addGlobalValReplacement(Entry, BC);
3148  }
3149 
3150  assert(F->getName() == MangledName && "name was uniqued!");
3151  if (D)
3152  SetFunctionAttributes(GD, F, IsIncompleteFunction, IsThunk);
3153  if (ExtraAttrs.hasAttributes(llvm::AttributeList::FunctionIndex)) {
3154  llvm::AttrBuilder B(ExtraAttrs, llvm::AttributeList::FunctionIndex);
3155  F->addAttributes(llvm::AttributeList::FunctionIndex, B);
3156  }
3157 
3158  if (!DontDefer) {
3159  // All MSVC dtors other than the base dtor are linkonce_odr and delegate to
3160  // each other bottoming out with the base dtor. Therefore we emit non-base
3161  // dtors on usage, even if there is no dtor definition in the TU.
3162  if (D && isa<CXXDestructorDecl>(D) &&
3163  getCXXABI().useThunkForDtorVariant(cast<CXXDestructorDecl>(D),
3164  GD.getDtorType()))
3165  addDeferredDeclToEmit(GD);
3166 
3167  // This is the first use or definition of a mangled name. If there is a
3168  // deferred decl with this name, remember that we need to emit it at the end
3169  // of the file.
3170  auto DDI = DeferredDecls.find(MangledName);
3171  if (DDI != DeferredDecls.end()) {
3172  // Move the potentially referenced deferred decl to the
3173  // DeferredDeclsToEmit list, and remove it from DeferredDecls (since we
3174  // don't need it anymore).
3175  addDeferredDeclToEmit(DDI->second);
3176  DeferredDecls.erase(DDI);
3177 
3178  // Otherwise, there are cases we have to worry about where we're
3179  // using a declaration for which we must emit a definition but where
3180  // we might not find a top-level definition:
3181  // - member functions defined inline in their classes
3182  // - friend functions defined inline in some class
3183  // - special member functions with implicit definitions
3184  // If we ever change our AST traversal to walk into class methods,
3185  // this will be unnecessary.
3186  //
3187  // We also don't emit a definition for a function if it's going to be an
3188  // entry in a vtable, unless it's already marked as used.
3189  } else if (getLangOpts().CPlusPlus && D) {
3190  // Look for a declaration that's lexically in a record.
3191  for (const auto *FD = cast<FunctionDecl>(D)->getMostRecentDecl(); FD;
3192  FD = FD->getPreviousDecl()) {
3193  if (isa<CXXRecordDecl>(FD->getLexicalDeclContext())) {
3194  if (FD->doesThisDeclarationHaveABody()) {
3195  addDeferredDeclToEmit(GD.getWithDecl(FD));
3196  break;
3197  }
3198  }
3199  }
3200  }
3201  }
3202 
3203  // Make sure the result is of the requested type.
3204  if (!IsIncompleteFunction) {
3205  assert(F->getType()->getElementType() == Ty);
3206  return F;
3207  }
3208 
3209  llvm::Type *PTy = llvm::PointerType::getUnqual(Ty);
3210  return llvm::ConstantExpr::getBitCast(F, PTy);
3211 }
3212 
3213 /// GetAddrOfFunction - Return the address of the given function. If Ty is
3214 /// non-null, then this function will use the specified type if it has to
3215 /// create it (this occurs when we see a definition of the function).
3217  llvm::Type *Ty,
3218  bool ForVTable,
3219  bool DontDefer,
3220  ForDefinition_t IsForDefinition) {
3221  // If there was no specific requested type, just convert it now.
3222  if (!Ty) {
3223  const auto *FD = cast<FunctionDecl>(GD.getDecl());
3224  Ty = getTypes().ConvertType(FD->getType());
3225  }
3226 
3227  // Devirtualized destructor calls may come through here instead of via
3228  // getAddrOfCXXStructor. Make sure we use the MS ABI base destructor instead
3229  // of the complete destructor when necessary.
3230  if (const auto *DD = dyn_cast<CXXDestructorDecl>(GD.getDecl())) {
3231  if (getTarget().getCXXABI().isMicrosoft() &&
3232  GD.getDtorType() == Dtor_Complete &&
3233  DD->getParent()->getNumVBases() == 0)
3234  GD = GlobalDecl(DD, Dtor_Base);
3235  }
3236 
3237  StringRef MangledName = getMangledName(GD);
3238  return GetOrCreateLLVMFunction(MangledName, Ty, GD, ForVTable, DontDefer,
3239  /*IsThunk=*/false, llvm::AttributeList(),
3240  IsForDefinition);
3241 }
3242 
3243 static const FunctionDecl *
3244 GetRuntimeFunctionDecl(ASTContext &C, StringRef Name) {
3247 
3248  IdentifierInfo &CII = C.Idents.get(Name);
3249  for (const auto &Result : DC->lookup(&CII))
3250  if (const auto FD = dyn_cast<FunctionDecl>(Result))
3251  return FD;
3252 
3253  if (!C.getLangOpts().CPlusPlus)
3254  return nullptr;
3255 
3256  // Demangle the premangled name from getTerminateFn()
3257  IdentifierInfo &CXXII =
3258  (Name == "_ZSt9terminatev" || Name == "?terminate@@YAXXZ")
3259  ? C.Idents.get("terminate")
3260  : C.Idents.get(Name);
3261 
3262  for (const auto &N : {"__cxxabiv1", "std"}) {
3263  IdentifierInfo &NS = C.Idents.get(N);
3264  for (const auto &Result : DC->lookup(&NS)) {
3265  NamespaceDecl *ND = dyn_cast<NamespaceDecl>(Result);
3266  if (auto LSD = dyn_cast<LinkageSpecDecl>(Result))
3267  for (const auto &Result : LSD->lookup(&NS))
3268  if ((ND = dyn_cast<NamespaceDecl>(Result)))
3269  break;
3270 
3271  if (ND)
3272  for (const auto &Result : ND->lookup(&CXXII))
3273  if (const auto *FD = dyn_cast<FunctionDecl>(Result))
3274  return FD;
3275  }
3276  }
3277 
3278  return nullptr;
3279 }
3280 
3281 /// CreateRuntimeFunction - Create a new runtime function with the specified
3282 /// type and name.
3283 llvm::FunctionCallee
3284 CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name,
3285  llvm::AttributeList ExtraAttrs,
3286  bool Local) {
3287  llvm::Constant *C =
3288  GetOrCreateLLVMFunction(Name, FTy, GlobalDecl(), /*ForVTable=*/false,
3289  /*DontDefer=*/false, /*IsThunk=*/false,
3290  ExtraAttrs);
3291 
3292  if (auto *F = dyn_cast<llvm::Function>(C)) {
3293  if (F->empty()) {
3294  F->setCallingConv(getRuntimeCC());
3295 
3296  // In Windows Itanium environments, try to mark runtime functions
3297  // dllimport. For Mingw and MSVC, don't. We don't really know if the user
3298  // will link their standard library statically or dynamically. Marking
3299  // functions imported when they are not imported can cause linker errors
3300  // and warnings.
3301  if (!Local && getTriple().isWindowsItaniumEnvironment() &&
3302  !getCodeGenOpts().LTOVisibilityPublicStd) {
3303  const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name);
3304  if (!FD || FD->hasAttr<DLLImportAttr>()) {
3305  F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
3306  F->setLinkage(llvm::GlobalValue::ExternalLinkage);
3307  }
3308  }
3309  setDSOLocal(F);
3310  }
3311  }
3312 
3313  return {FTy, C};
3314 }
3315 
3316 /// isTypeConstant - Determine whether an object of this type can be emitted
3317 /// as a constant.
3318 ///
3319 /// If ExcludeCtor is true, the duration when the object's constructor runs
3320 /// will not be considered. The caller will need to verify that the object is
3321 /// not written to during its construction.
3322 bool CodeGenModule::isTypeConstant(QualType Ty, bool ExcludeCtor) {
3323  if (!Ty.isConstant(Context) && !Ty->isReferenceType())
3324  return false;
3325 
3326  if (Context.getLangOpts().CPlusPlus) {
3327  if (const CXXRecordDecl *Record
3328  = Context.getBaseElementType(Ty)->getAsCXXRecordDecl())
3329  return ExcludeCtor && !Record->hasMutableFields() &&
3330  Record->hasTrivialDestructor();
3331  }
3332 
3333  return true;
3334 }
3335 
3336 /// GetOrCreateLLVMGlobal - If the specified mangled name is not in the module,
3337 /// create and return an llvm GlobalVariable with the specified type. If there
3338 /// is something in the module with the specified name, return it potentially
3339 /// bitcasted to the right type.
3340 ///
3341 /// If D is non-null, it specifies a decl that correspond to this. This is used
3342 /// to set the attributes on the global when it is first created.
3343 ///
3344 /// If IsForDefinition is true, it is guaranteed that an actual global with
3345 /// type Ty will be returned, not conversion of a variable with the same
3346 /// mangled name but some other type.
3347 llvm::Constant *
3348 CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName,
3349  llvm::PointerType *Ty,
3350  const VarDecl *D,
3351  ForDefinition_t IsForDefinition) {
3352  // Lookup the entry, lazily creating it if necessary.
3353  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
3354  if (Entry) {
3355  if (WeakRefReferences.erase(Entry)) {
3356  if (D && !D->hasAttr<WeakAttr>())
3357  Entry->setLinkage(llvm::Function::ExternalLinkage);
3358  }
3359 
3360  // Handle dropped DLL attributes.
3361  if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
3362  Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
3363 
3364  if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
3365  getOpenMPRuntime().registerTargetGlobalVariable(D, Entry);
3366 
3367  if (Entry->getType() == Ty)
3368  return Entry;
3369 
3370  // If there are two attempts to define the same mangled name, issue an
3371  // error.
3372  if (IsForDefinition && !Entry->isDeclaration()) {
3373  GlobalDecl OtherGD;
3374  const VarDecl *OtherD;
3375 
3376  // Check that D is not yet in DiagnosedConflictingDefinitions is required
3377  // to make sure that we issue an error only once.
3378  if (D && lookupRepresentativeDecl(MangledName, OtherGD) &&
3379  (D->getCanonicalDecl() != OtherGD.getCanonicalDecl().getDecl()) &&
3380  (OtherD = dyn_cast<VarDecl>(OtherGD.getDecl())) &&
3381  OtherD->hasInit() &&
3382  DiagnosedConflictingDefinitions.insert(D).second) {
3383  getDiags().Report(D->getLocation(), diag::err_duplicate_mangled_name)
3384  << MangledName;
3385  getDiags().Report(OtherGD.getDecl()->getLocation(),
3386  diag::note_previous_definition);
3387  }
3388  }
3389 
3390  // Make sure the result is of the correct type.
3391  if (Entry->getType()->getAddressSpace() != Ty->getAddressSpace())
3392  return llvm::ConstantExpr::getAddrSpaceCast(Entry, Ty);
3393 
3394  // (If global is requested for a definition, we always need to create a new
3395  // global, not just return a bitcast.)
3396  if (!IsForDefinition)
3397  return llvm::ConstantExpr::getBitCast(Entry, Ty);
3398  }
3399 
3400  auto AddrSpace = GetGlobalVarAddressSpace(D);
3401  auto TargetAddrSpace = getContext().getTargetAddressSpace(AddrSpace);
3402 
3403  auto *GV = new llvm::GlobalVariable(
3404  getModule(), Ty->getElementType(), false,
3405  llvm::GlobalValue::ExternalLinkage, nullptr, MangledName, nullptr,
3406  llvm::GlobalVariable::NotThreadLocal, TargetAddrSpace);
3407 
3408  // If we already created a global with the same mangled name (but different
3409  // type) before, take its name and remove it from its parent.
3410  if (Entry) {
3411  GV->takeName(Entry);
3412 
3413  if (!Entry->use_empty()) {
3414  llvm::Constant *NewPtrForOldDecl =
3415  llvm::ConstantExpr::getBitCast(GV, Entry->getType());
3416  Entry->replaceAllUsesWith(NewPtrForOldDecl);
3417  }
3418 
3419  Entry->eraseFromParent();
3420  }
3421 
3422  // This is the first use or definition of a mangled name. If there is a
3423  // deferred decl with this name, remember that we need to emit it at the end
3424  // of the file.
3425  auto DDI = DeferredDecls.find(MangledName);
3426  if (DDI != DeferredDecls.end()) {
3427  // Move the potentially referenced deferred decl to the DeferredDeclsToEmit
3428  // list, and remove it from DeferredDecls (since we don't need it anymore).
3429  addDeferredDeclToEmit(DDI->second);
3430  DeferredDecls.erase(DDI);
3431  }
3432 
3433  // Handle things which are present even on external declarations.
3434  if (D) {
3435  if (LangOpts.OpenMP && !LangOpts.OpenMPSimd)
3436  getOpenMPRuntime().registerTargetGlobalVariable(D, GV);
3437 
3438  // FIXME: This code is overly simple and should be merged with other global
3439  // handling.
3440  GV->setConstant(isTypeConstant(D->getType(), false));
3441 
3442  GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
3443 
3444  setLinkageForGV(GV, D);
3445 
3446  if (D->getTLSKind()) {
3447  if (D->getTLSKind() == VarDecl::TLS_Dynamic)
3448  CXXThreadLocals.push_back(D);
3449  setTLSMode(GV, *D);
3450  }
3451 
3452  setGVProperties(GV, D);
3453 
3454  // If required by the ABI, treat declarations of static data members with
3455  // inline initializers as definitions.
3456  if (getContext().isMSStaticDataMemberInlineDefinition(D)) {
3457  EmitGlobalVarDefinition(D);
3458  }
3459 
3460  // Emit section information for extern variables.
3461  if (D->hasExternalStorage()) {
3462  if (const SectionAttr *SA = D->getAttr<SectionAttr>())
3463  GV->setSection(SA->getName());
3464  }
3465 
3466  // Handle XCore specific ABI requirements.
3467  if (getTriple().getArch() == llvm::Triple::xcore &&
3469  D->getType().isConstant(Context) &&
3471  GV->setSection(".cp.rodata");
3472 
3473  // Check if we a have a const declaration with an initializer, we may be
3474  // able to emit it as available_externally to expose it's value to the
3475  // optimizer.
3476  if (Context.getLangOpts().CPlusPlus && GV->hasExternalLinkage() &&
3477  D->getType().isConstQualified() && !GV->hasInitializer() &&
3478  !D->hasDefinition() && D->hasInit() && !D->hasAttr<DLLImportAttr>()) {
3479  const auto *Record =
3480  Context.getBaseElementType(D->getType())->getAsCXXRecordDecl();
3481  bool HasMutableFields = Record && Record->hasMutableFields();
3482  if (!HasMutableFields) {
3483  const VarDecl *InitDecl;
3484  const Expr *InitExpr = D->getAnyInitializer(InitDecl);
3485  if (InitExpr) {
3486  ConstantEmitter emitter(*this);
3487  llvm::Constant *Init = emitter.tryEmitForInitializer(*InitDecl);
3488  if (Init) {
3489  auto *InitType = Init->getType();
3490  if (GV->getType()->getElementType() != InitType) {
3491  // The type of the initializer does not match the definition.
3492  // This happens when an initializer has a different type from
3493  // the type of the global (because of padding at the end of a
3494  // structure for instance).
3495  GV->setName(StringRef());
3496  // Make a new global with the correct type, this is now guaranteed
3497  // to work.
3498  auto *NewGV = cast<llvm::GlobalVariable>(
3499  GetAddrOfGlobalVar(D, InitType, IsForDefinition));
3500 
3501  // Erase the old global, since it is no longer used.
3502  GV->eraseFromParent();
3503  GV = NewGV;
3504  } else {
3505  GV->setInitializer(Init);
3506  GV->setConstant(true);
3507  GV->setLinkage(llvm::GlobalValue::AvailableExternallyLinkage);
3508  }
3509  emitter.finalize(GV);
3510  }
3511  }
3512  }
3513  }
3514  }
3515 
3516  LangAS ExpectedAS =
3517  D ? D->getType().getAddressSpace()
3518  : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default);
3519  assert(getContext().getTargetAddressSpace(ExpectedAS) ==
3520  Ty->getPointerAddressSpace());
3521  if (AddrSpace != ExpectedAS)
3522  return getTargetCodeGenInfo().performAddrSpaceCast(*this, GV, AddrSpace,
3523  ExpectedAS, Ty);
3524 
3525  if (GV->isDeclaration())
3526  getTargetCodeGenInfo().setTargetAttributes(D, GV, *this);
3527 
3528  return GV;
3529 }
3530 
3531 llvm::Constant *
3533  ForDefinition_t IsForDefinition) {
3534  const Decl *D = GD.getDecl();
3535  if (isa<CXXConstructorDecl>(D) || isa<CXXDestructorDecl>(D))
3536  return getAddrOfCXXStructor(GD, /*FnInfo=*/nullptr, /*FnType=*/nullptr,
3537  /*DontDefer=*/false, IsForDefinition);
3538  else if (isa<CXXMethodDecl>(D)) {
3539  auto FInfo = &getTypes().arrangeCXXMethodDeclaration(
3540  cast<CXXMethodDecl>(D));
3541  auto Ty = getTypes().GetFunctionType(*FInfo);
3542  return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
3543  IsForDefinition);
3544  } else if (isa<FunctionDecl>(D)) {
3546  llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
3547  return GetAddrOfFunction(GD, Ty, /*ForVTable=*/false, /*DontDefer=*/false,
3548  IsForDefinition);
3549  } else
3550  return GetAddrOfGlobalVar(cast<VarDecl>(D), /*Ty=*/nullptr,
3551  IsForDefinition);
3552 }
3553 
3555  StringRef Name, llvm::Type *Ty, llvm::GlobalValue::LinkageTypes Linkage,
3556  unsigned Alignment) {
3557  llvm::GlobalVariable *GV = getModule().getNamedGlobal(Name);
3558  llvm::GlobalVariable *OldGV = nullptr;
3559 
3560  if (GV) {
3561  // Check if the variable has the right type.
3562  if (GV->getType()->getElementType() == Ty)
3563  return GV;
3564 
3565  // Because C++ name mangling, the only way we can end up with an already
3566  // existing global with the same name is if it has been declared extern "C".
3567  assert(GV->isDeclaration() && "Declaration has wrong type!");
3568  OldGV = GV;
3569  }
3570 
3571  // Create a new variable.
3572  GV = new llvm::GlobalVariable(getModule(), Ty, /*isConstant=*/true,
3573  Linkage, nullptr, Name);
3574 
3575  if (OldGV) {
3576  // Replace occurrences of the old variable if needed.
3577  GV->takeName(OldGV);
3578 
3579  if (!OldGV->use_empty()) {
3580  llvm::Constant *NewPtrForOldDecl =
3581  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
3582  OldGV->replaceAllUsesWith(NewPtrForOldDecl);
3583  }
3584 
3585  OldGV->eraseFromParent();
3586  }
3587 
3588  if (supportsCOMDAT() && GV->isWeakForLinker() &&
3589  !GV->hasAvailableExternallyLinkage())
3590  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
3591 
3592  GV->setAlignment(Alignment);
3593 
3594  return GV;
3595 }
3596 
3597 /// GetAddrOfGlobalVar - Return the llvm::Constant for the address of the
3598 /// given global variable. If Ty is non-null and if the global doesn't exist,
3599 /// then it will be created with the specified type instead of whatever the
3600 /// normal requested type would be. If IsForDefinition is true, it is guaranteed
3601 /// that an actual global with type Ty will be returned, not conversion of a
3602 /// variable with the same mangled name but some other type.
3604  llvm::Type *Ty,
3605  ForDefinition_t IsForDefinition) {
3606  assert(D->hasGlobalStorage() && "Not a global variable");
3607  QualType ASTTy = D->getType();
3608  if (!Ty)
3609  Ty = getTypes().ConvertTypeForMem(ASTTy);
3610 
3611  llvm::PointerType *PTy =
3612  llvm::PointerType::get(Ty, getContext().getTargetAddressSpace(ASTTy));
3613 
3614  StringRef MangledName = getMangledName(D);
3615  return GetOrCreateLLVMGlobal(MangledName, PTy, D, IsForDefinition);
3616 }
3617 
3618 /// CreateRuntimeVariable - Create a new runtime global variable with the
3619 /// specified type and name.
3620 llvm::Constant *
3622  StringRef Name) {
3623  auto PtrTy =
3624  getContext().getLangOpts().OpenCL
3625  ? llvm::PointerType::get(
3626  Ty, getContext().getTargetAddressSpace(LangAS::opencl_global))
3627  : llvm::PointerType::getUnqual(Ty);
3628  auto *Ret = GetOrCreateLLVMGlobal(Name, PtrTy, nullptr);
3629  setDSOLocal(cast<llvm::GlobalValue>(Ret->stripPointerCasts()));
3630  return Ret;
3631 }
3632 
3634  assert(!D->getInit() && "Cannot emit definite definitions here!");
3635 
3636  StringRef MangledName = getMangledName(D);
3637  llvm::GlobalValue *GV = GetGlobalValue(MangledName);
3638 
3639  // We already have a definition, not declaration, with the same mangled name.
3640  // Emitting of declaration is not required (and actually overwrites emitted
3641  // definition).
3642  if (GV && !GV->isDeclaration())
3643  return;
3644 
3645  // If we have not seen a reference to this variable yet, place it into the
3646  // deferred declarations table to be emitted if needed later.
3647  if (!MustBeEmitted(D) && !GV) {
3648  DeferredDecls[MangledName] = D;
3649  return;
3650  }
3651 
3652  // The tentative definition is the only definition.
3653  EmitGlobalVarDefinition(D);
3654 }
3655 
3657  return Context.toCharUnitsFromBits(
3658  getDataLayout().getTypeStoreSizeInBits(Ty));
3659 }
3660 
3662  LangAS AddrSpace = LangAS::Default;
3663  if (LangOpts.OpenCL) {
3664  AddrSpace = D ? D->getType().getAddressSpace() : LangAS::opencl_global;
3665  assert(AddrSpace == LangAS::opencl_global ||
3666  AddrSpace == LangAS::opencl_constant ||
3667  AddrSpace == LangAS::opencl_local ||
3668  AddrSpace >= LangAS::FirstTargetAddressSpace);
3669  return AddrSpace;
3670  }
3671 
3672  if (LangOpts.CUDA && LangOpts.CUDAIsDevice) {
3673  if (D && D->hasAttr<CUDAConstantAttr>())
3674  return LangAS::cuda_constant;
3675  else if (D && D->hasAttr<CUDASharedAttr>())
3676  return LangAS::cuda_shared;
3677  else if (D && D->hasAttr<CUDADeviceAttr>())
3678  return LangAS::cuda_device;
3679  else if (D && D->getType().isConstQualified())
3680  return LangAS::cuda_constant;
3681  else
3682  return LangAS::cuda_device;
3683  }
3684 
3685  if (LangOpts.OpenMP) {
3686  LangAS AS;
3687  if (OpenMPRuntime->hasAllocateAttributeForGlobalVar(D, AS))
3688  return AS;
3689  }
3691 }
3692 
3694  // OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
3695  if (LangOpts.OpenCL)
3696  return LangAS::opencl_constant;
3697  if (auto AS = getTarget().getConstantAddressSpace())
3698  return AS.getValue();
3699  return LangAS::Default;
3700 }
3701 
3702 // In address space agnostic languages, string literals are in default address
3703 // space in AST. However, certain targets (e.g. amdgcn) request them to be
3704 // emitted in constant address space in LLVM IR. To be consistent with other
3705 // parts of AST, string literal global variables in constant address space
3706 // need to be casted to default address space before being put into address
3707 // map and referenced by other part of CodeGen.
3708 // In OpenCL, string literals are in constant address space in AST, therefore
3709 // they should not be casted to default address space.
3710 static llvm::Constant *
3712  llvm::GlobalVariable *GV) {
3713  llvm::Constant *Cast = GV;
3714  if (!CGM.getLangOpts().OpenCL) {
3715  if (auto AS = CGM.getTarget().getConstantAddressSpace()) {
3716  if (AS != LangAS::Default)
3718  CGM, GV, AS.getValue(), LangAS::Default,
3719  GV->getValueType()->getPointerTo(
3721  }
3722  }
3723  return Cast;
3724 }
3725 
3726 template<typename SomeDecl>
3728  llvm::GlobalValue *GV) {
3729  if (!getLangOpts().CPlusPlus)
3730  return;
3731 
3732  // Must have 'used' attribute, or else inline assembly can't rely on
3733  // the name existing.
3734  if (!D->template hasAttr<UsedAttr>())
3735  return;
3736 
3737  // Must have internal linkage and an ordinary name.
3738  if (!D->getIdentifier() || D->getFormalLinkage() != InternalLinkage)
3739  return;
3740 
3741  // Must be in an extern "C" context. Entities declared directly within
3742  // a record are not extern "C" even if the record is in such a context.
3743  const SomeDecl *First = D->getFirstDecl();
3744  if (First->getDeclContext()->isRecord() || !First->isInExternCContext())
3745  return;
3746 
3747  // OK, this is an internal linkage entity inside an extern "C" linkage
3748  // specification. Make a note of that so we can give it the "expected"
3749  // mangled name if nothing else is using that name.
3750  std::pair<StaticExternCMap::iterator, bool> R =
3751  StaticExternCValues.insert(std::make_pair(D->getIdentifier(), GV));
3752 
3753  // If we have multiple internal linkage entities with the same name
3754  // in extern "C" regions, none of them gets that name.
3755  if (!R.second)
3756  R.first->second = nullptr;
3757 }
3758 
3759 static bool shouldBeInCOMDAT(CodeGenModule &CGM, const Decl &D) {
3760  if (!CGM.supportsCOMDAT())
3761  return false;
3762 
3763  // Do not set COMDAT attribute for CUDA/HIP stub functions to prevent
3764  // them being "merged" by the COMDAT Folding linker optimization.
3765  if (D.hasAttr<CUDAGlobalAttr>())
3766  return false;
3767 
3768  if (D.hasAttr<SelectAnyAttr>())
3769  return true;
3770 
3772  if (auto *VD = dyn_cast<VarDecl>(&D))
3773  Linkage = CGM.getContext().GetGVALinkageForVariable(VD);
3774  else
3775  Linkage = CGM.getContext().GetGVALinkageForFunction(cast<FunctionDecl>(&D));
3776 
3777  switch (Linkage) {
3778  case GVA_Internal:
3780  case GVA_StrongExternal:
3781  return false;
3782  case GVA_DiscardableODR:
3783  case GVA_StrongODR:
3784  return true;
3785  }
3786  llvm_unreachable("No such linkage");
3787 }
3788 
3790  llvm::GlobalObject &GO) {
3791  if (!shouldBeInCOMDAT(*this, D))
3792  return;
3793  GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
3794 }
3795 
3796 /// Pass IsTentative as true if you want to create a tentative definition.
3797 void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
3798  bool IsTentative) {
3799  // OpenCL global variables of sampler type are translated to function calls,
3800  // therefore no need to be translated.
3801  QualType ASTTy = D->getType();
3802  if (getLangOpts().OpenCL && ASTTy->isSamplerT())
3803  return;
3804 
3805  // If this is OpenMP device, check if it is legal to emit this global
3806  // normally.
3807  if (LangOpts.OpenMPIsDevice && OpenMPRuntime &&
3808  OpenMPRuntime->emitTargetGlobalVariable(D))
3809  return;
3810 
3811  llvm::Constant *Init = nullptr;
3813  bool NeedsGlobalCtor = false;
3814  bool NeedsGlobalDtor = RD && !RD->hasTrivialDestructor();
3815 
3816  const VarDecl *InitDecl;
3817  const Expr *InitExpr = D->getAnyInitializer(InitDecl);
3818 
3819  Optional<ConstantEmitter> emitter;
3820 
3821  // CUDA E.2.4.1 "__shared__ variables cannot have an initialization
3822  // as part of their declaration." Sema has already checked for
3823  // error cases, so we just need to set Init to UndefValue.
3824  bool IsCUDASharedVar =
3825  getLangOpts().CUDAIsDevice && D->hasAttr<CUDASharedAttr>();
3826  // Shadows of initialized device-side global variables are also left
3827  // undefined.
3828  bool IsCUDAShadowVar =
3829  !getLangOpts().CUDAIsDevice &&
3830  (D->hasAttr<CUDAConstantAttr>() || D->hasAttr<CUDADeviceAttr>() ||
3831  D->hasAttr<CUDASharedAttr>());
3832  // HIP pinned shadow of initialized host-side global variables are also
3833  // left undefined.
3834  bool IsHIPPinnedShadowVar =
3835  getLangOpts().CUDAIsDevice && D->hasAttr<HIPPinnedShadowAttr>();
3836  if (getLangOpts().CUDA &&
3837  (IsCUDASharedVar || IsCUDAShadowVar || IsHIPPinnedShadowVar))
3838  Init = llvm::UndefValue::get(getTypes().ConvertType(ASTTy));
3839  else if (!InitExpr) {
3840  // This is a tentative definition; tentative definitions are
3841  // implicitly initialized with { 0 }.
3842  //
3843  // Note that tentative definitions are only emitted at the end of
3844  // a translation unit, so they should never have incomplete
3845  // type. In addition, EmitTentativeDefinition makes sure that we
3846  // never attempt to emit a tentative definition if a real one
3847  // exists. A use may still exists, however, so we still may need
3848  // to do a RAUW.
3849  assert(!ASTTy->isIncompleteType() && "Unexpected incomplete type");
3850  Init = EmitNullConstant(D->getType());
3851  } else {
3852  initializedGlobalDecl = GlobalDecl(D);
3853  emitter.emplace(*this);
3854  Init = emitter->tryEmitForInitializer(*InitDecl);
3855 
3856  if (!Init) {
3857  QualType T = InitExpr->getType();
3858  if (D->getType()->isReferenceType())
3859  T = D->getType();
3860 
3861  if (getLangOpts().CPlusPlus) {
3862  Init = EmitNullConstant(T);
3863  NeedsGlobalCtor = true;
3864  } else {
3865  ErrorUnsupported(D, "static initializer");
3866  Init = llvm::UndefValue::get(getTypes().ConvertType(T));
3867  }
3868  } else {
3869  // We don't need an initializer, so remove the entry for the delayed
3870  // initializer position (just in case this entry was delayed) if we
3871  // also don't need to register a destructor.
3872  if (getLangOpts().CPlusPlus && !NeedsGlobalDtor)
3873  DelayedCXXInitPosition.erase(D);
3874  }
3875  }
3876 
3877  llvm::Type* InitType = Init->getType();
3878  llvm::Constant *Entry =
3879  GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative));
3880 
3881  // Strip off a bitcast if we got one back.
3882  if (auto *CE = dyn_cast<llvm::ConstantExpr>(Entry)) {
3883  assert(CE->getOpcode() == llvm::Instruction::BitCast ||
3884  CE->getOpcode() == llvm::Instruction::AddrSpaceCast ||
3885  // All zero index gep.
3886  CE->getOpcode() == llvm::Instruction::GetElementPtr);
3887  Entry = CE->getOperand(0);
3888  }
3889 
3890  // Entry is now either a Function or GlobalVariable.
3891  auto *GV = dyn_cast<llvm::GlobalVariable>(Entry);
3892 
3893  // We have a definition after a declaration with the wrong type.
3894  // We must make a new GlobalVariable* and update everything that used OldGV
3895  // (a declaration or tentative definition) with the new GlobalVariable*
3896  // (which will be a definition).
3897  //
3898  // This happens if there is a prototype for a global (e.g.
3899  // "extern int x[];") and then a definition of a different type (e.g.
3900  // "int x[10];"). This also happens when an initializer has a different type
3901  // from the type of the global (this happens with unions).
3902  if (!GV || GV->getType()->getElementType() != InitType ||
3903  GV->getType()->getAddressSpace() !=
3905 
3906  // Move the old entry aside so that we'll create a new one.
3907  Entry->setName(StringRef());
3908 
3909  // Make a new global with the correct type, this is now guaranteed to work.
3910  GV = cast<llvm::GlobalVariable>(
3911  GetAddrOfGlobalVar(D, InitType, ForDefinition_t(!IsTentative)));
3912 
3913  // Replace all uses of the old global with the new global
3914  llvm::Constant *NewPtrForOldDecl =
3915  llvm::ConstantExpr::getBitCast(GV, Entry->getType());
3916  Entry->replaceAllUsesWith(NewPtrForOldDecl);
3917 
3918  // Erase the old global, since it is no longer used.
3919  cast<llvm::GlobalValue>(Entry)->eraseFromParent();
3920  }
3921 
3923 
3924  if (D->hasAttr<AnnotateAttr>())
3925  AddGlobalAnnotations(D, GV);
3926 
3927  // Set the llvm linkage type as appropriate.
3928  llvm::GlobalValue::LinkageTypes Linkage =
3929  getLLVMLinkageVarDefinition(D, GV->isConstant());
3930 
3931  // CUDA B.2.1 "The __device__ qualifier declares a variable that resides on
3932  // the device. [...]"
3933  // CUDA B.2.2 "The __constant__ qualifier, optionally used together with
3934  // __device__, declares a variable that: [...]
3935  // Is accessible from all the threads within the grid and from the host
3936  // through the runtime library (cudaGetSymbolAddress() / cudaGetSymbolSize()
3937  // / cudaMemcpyToSymbol() / cudaMemcpyFromSymbol())."
3938  if (GV && LangOpts.CUDA) {
3939  if (LangOpts.CUDAIsDevice) {
3940  if (Linkage != llvm::GlobalValue::InternalLinkage &&
3941  (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>()))
3942  GV->setExternallyInitialized(true);
3943  } else {
3944  // Host-side shadows of external declarations of device-side
3945  // global variables become internal definitions. These have to
3946  // be internal in order to prevent name conflicts with global
3947  // host variables with the same name in a different TUs.
3948  if (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
3949  D->hasAttr<HIPPinnedShadowAttr>()) {
3951 
3952  // Shadow variables and their properties must be registered
3953  // with CUDA runtime.
3954  unsigned Flags = 0;
3955  if (!D->hasDefinition())
3957  if (D->hasAttr<CUDAConstantAttr>())
3959  // Extern global variables will be registered in the TU where they are
3960  // defined.
3961  if (!D->hasExternalStorage())
3962  getCUDARuntime().registerDeviceVar(D, *GV, Flags);
3963  } else if (D->hasAttr<CUDASharedAttr>())
3964  // __shared__ variables are odd. Shadows do get created, but
3965  // they are not registered with the CUDA runtime, so they
3966  // can't really be used to access their device-side
3967  // counterparts. It's not clear yet whether it's nvcc's bug or
3968  // a feature, but we've got to do the same for compatibility.
3970  }
3971  }
3972 
3973  if (!IsHIPPinnedShadowVar)
3974  GV->setInitializer(Init);
3975  if (emitter) emitter->finalize(GV);
3976 
3977  // If it is safe to mark the global 'constant', do so now.
3978  GV->setConstant(!NeedsGlobalCtor && !NeedsGlobalDtor &&
3979  isTypeConstant(D->getType(), true));
3980 
3981  // If it is in a read-only section, mark it 'constant'.
3982  if (const SectionAttr *SA = D->getAttr<SectionAttr>()) {
3983  const ASTContext::SectionInfo &SI = Context.SectionInfos[SA->getName()];
3984  if ((SI.SectionFlags & ASTContext::PSF_Write) == 0)
3985  GV->setConstant(true);
3986  }
3987 
3988  GV->setAlignment(getContext().getDeclAlign(D).getQuantity());
3989 
3990 
3991  // On Darwin, if the normal linkage of a C++ thread_local variable is
3992  // LinkOnce or Weak, we keep the normal linkage to prevent multiple
3993  // copies within a linkage unit; otherwise, the backing variable has
3994  // internal linkage and all accesses should just be calls to the
3995  // Itanium-specified entry point, which has the normal linkage of the
3996  // variable. This is to preserve the ability to change the implementation
3997  // behind the scenes.
3998  if (!D->isStaticLocal() && D->getTLSKind() == VarDecl::TLS_Dynamic &&
3999  Context.getTargetInfo().getTriple().isOSDarwin() &&
4000  !llvm::GlobalVariable::isLinkOnceLinkage(Linkage) &&
4001  !llvm::GlobalVariable::isWeakLinkage(Linkage))
4003 
4004  GV->setLinkage(Linkage);
4005  if (D->hasAttr<DLLImportAttr>())
4006  GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
4007  else if (D->hasAttr<DLLExportAttr>())
4008  GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
4009  else
4010  GV->setDLLStorageClass(llvm::GlobalVariable::DefaultStorageClass);
4011 
4012  if (Linkage == llvm::GlobalVariable::CommonLinkage) {
4013  // common vars aren't constant even if declared const.
4014  GV->setConstant(false);
4015  // Tentative definition of global variables may be initialized with
4016  // non-zero null pointers. In this case they should have weak linkage
4017  // since common linkage must have zero initializer and must not have
4018  // explicit section therefore cannot have non-zero initial value.
4019  if (!GV->getInitializer()->isNullValue())
4020  GV->setLinkage(llvm::GlobalVariable::WeakAnyLinkage);
4021  }
4022 
4023  setNonAliasAttributes(D, GV);
4024 
4025  if (D->getTLSKind() && !GV->isThreadLocal()) {
4026  if (D->getTLSKind() == VarDecl::TLS_Dynamic)
4027  CXXThreadLocals.push_back(D);
4028  setTLSMode(GV, *D);
4029  }
4030 
4031  maybeSetTrivialComdat(*D, *GV);
4032 
4033  // Emit the initializer function if necessary.
4034  if (NeedsGlobalCtor || NeedsGlobalDtor)
4035  EmitCXXGlobalVarDeclInitFunc(D, GV, NeedsGlobalCtor);
4036 
4037  SanitizerMD->reportGlobalToASan(GV, *D, NeedsGlobalCtor);
4038 
4039  // Emit global variable debug information.
4040  if (CGDebugInfo *DI = getModuleDebugInfo())
4041  if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo)
4042  DI->EmitGlobalVariable(GV, D);
4043 }
4044 
4045 static bool isVarDeclStrongDefinition(const ASTContext &Context,
4046  CodeGenModule &CGM, const VarDecl *D,
4047  bool NoCommon) {
4048  // Don't give variables common linkage if -fno-common was specified unless it
4049  // was overridden by a NoCommon attribute.
4050  if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
4051  return true;
4052 
4053  // C11 6.9.2/2:
4054  // A declaration of an identifier for an object that has file scope without
4055  // an initializer, and without a storage-class specifier or with the
4056  // storage-class specifier static, constitutes a tentative definition.
4057  if (D->getInit() || D->hasExternalStorage())
4058  return true;
4059 
4060  // A variable cannot be both common and exist in a section.
4061  if (D->hasAttr<SectionAttr>())
4062  return true;
4063 
4064  // A variable cannot be both common and exist in a section.
4065  // We don't try to determine which is the right section in the front-end.
4066  // If no specialized section name is applicable, it will resort to default.
4067  if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
4068  D->hasAttr<PragmaClangDataSectionAttr>() ||
4069  D->hasAttr<PragmaClangRodataSectionAttr>())
4070  return true;
4071 
4072  // Thread local vars aren't considered common linkage.
4073  if (D->getTLSKind())
4074  return true;
4075 
4076  // Tentative definitions marked with WeakImportAttr are true definitions.
4077  if (D->hasAttr<WeakImportAttr>())
4078  return true;
4079 
4080  // A variable cannot be both common and exist in a comdat.
4081  if (shouldBeInCOMDAT(CGM, *D))
4082  return true;
4083 
4084  // Declarations with a required alignment do not have common linkage in MSVC
4085  // mode.
4086  if (Context.getTargetInfo().getCXXABI().isMicrosoft()) {
4087  if (D->hasAttr<AlignedAttr>())
4088  return true;
4089  QualType VarType = D->getType();
4090  if (Context.isAlignmentRequired(VarType))
4091  return true;
4092 
4093  if (const auto *RT = VarType->getAs<RecordType>()) {
4094  const RecordDecl *RD = RT->getDecl();
4095  for (const FieldDecl *FD : RD->fields()) {
4096  if (FD->isBitField())
4097  continue;
4098  if (FD->hasAttr<AlignedAttr>())
4099  return true;
4100  if (Context.isAlignmentRequired(FD->getType()))
4101  return true;
4102  }
4103  }
4104  }
4105 
4106  // Microsoft's link.exe doesn't support alignments greater than 32 bytes for
4107  // common symbols, so symbols with greater alignment requirements cannot be
4108  // common.
4109  // Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
4110  // alignments for common symbols via the aligncomm directive, so this
4111  // restriction only applies to MSVC environments.
4112  if (Context.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
4113  Context.getTypeAlignIfKnown(D->getType()) >
4114  Context.toBits(CharUnits::fromQuantity(32)))
4115  return true;
4116 
4117  return false;
4118 }
4119 
4120 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageForDeclarator(
4121  const DeclaratorDecl *D, GVALinkage Linkage, bool IsConstantVariable) {
4122  if (Linkage == GVA_Internal)
4124 
4125  if (D->hasAttr<WeakAttr>()) {
4126  if (IsConstantVariable)
4127  return llvm::GlobalVariable::WeakODRLinkage;
4128  else
4129  return llvm::GlobalVariable::WeakAnyLinkage;
4130  }
4131 
4132  if (const auto *FD = D->getAsFunction())
4133  if (FD->isMultiVersion() && Linkage == GVA_AvailableExternally)
4134  return llvm::GlobalVariable::LinkOnceAnyLinkage;
4135 
4136  // We are guaranteed to have a strong definition somewhere else,
4137  // so we can use available_externally linkage.
4138  if (Linkage == GVA_AvailableExternally)
4139  return llvm::GlobalValue::AvailableExternallyLinkage;
4140 
4141  // Note that Apple's kernel linker doesn't support symbol
4142  // coalescing, so we need to avoid linkonce and weak linkages there.
4143  // Normally, this means we just map to internal, but for explicit
4144  // instantiations we'll map to external.
4145 
4146  // In C++, the compiler has to emit a definition in every translation unit
4147  // that references the function. We should use linkonce_odr because
4148  // a) if all references in this translation unit are optimized away, we
4149  // don't need to codegen it. b) if the function persists, it needs to be
4150  // merged with other definitions. c) C++ has the ODR, so we know the
4151  // definition is dependable.
4152  if (Linkage == GVA_DiscardableODR)
4153  return !Context.getLangOpts().AppleKext ? llvm::Function::LinkOnceODRLinkage
4155 
4156  // An explicit instantiation of a template has weak linkage, since
4157  // explicit instantiations can occur in multiple translation units
4158  // and must all be equivalent. However, we are not allowed to
4159  // throw away these explicit instantiations.
4160  //
4161  // We don't currently support CUDA device code spread out across multiple TUs,
4162  // so say that CUDA templates are either external (for kernels) or internal.
4163  // This lets llvm perform aggressive inter-procedural optimizations.
4164  if (Linkage == GVA_StrongODR) {
4165  if (Context.getLangOpts().AppleKext)
4167  if (Context.getLangOpts().CUDA && Context.getLangOpts().CUDAIsDevice)
4168  return D->hasAttr<CUDAGlobalAttr>() ? llvm::Function::ExternalLinkage
4170  return llvm::Function::WeakODRLinkage;
4171  }
4172 
4173  // C++ doesn't have tentative definitions and thus cannot have common
4174  // linkage.
4175  if (!getLangOpts().CPlusPlus && isa<VarDecl>(D) &&
4176  !isVarDeclStrongDefinition(Context, *this, cast<VarDecl>(D),
4177  CodeGenOpts.NoCommon))
4178  return llvm::GlobalVariable::CommonLinkage;
4179 
4180  // selectany symbols are externally visible, so use weak instead of
4181  // linkonce. MSVC optimizes away references to const selectany globals, so
4182  // all definitions should be the same and ODR linkage should be used.
4183  // http://msdn.microsoft.com/en-us/library/5tkz6s71.aspx
4184  if (D->hasAttr<SelectAnyAttr>())
4185  return llvm::GlobalVariable::WeakODRLinkage;
4186 
4187  // Otherwise, we have strong external linkage.
4188  assert(Linkage == GVA_StrongExternal);
4190 }
4191 
4192 llvm::GlobalValue::LinkageTypes CodeGenModule::getLLVMLinkageVarDefinition(
4193  const VarDecl *VD, bool IsConstant) {
4195  return getLLVMLinkageForDeclarator(VD, Linkage, IsConstant);
4196 }
4197 
4198 /// Replace the uses of a function that was declared with a non-proto type.
4199 /// We want to silently drop extra arguments from call sites
4200 static void replaceUsesOfNonProtoConstant(llvm::Constant *old,
4201  llvm::Function *newFn) {
4202  // Fast path.
4203  if (old->use_empty()) return;
4204 
4205  llvm::Type *newRetTy = newFn->getReturnType();
4208 
4209  for (llvm::Value::use_iterator ui = old->use_begin(), ue = old->use_end();
4210  ui != ue; ) {
4211  llvm::Value::use_iterator use = ui++; // Increment before the use is erased.
4212  llvm::User *user = use->getUser();
4213 
4214  // Recognize and replace uses of bitcasts. Most calls to
4215  // unprototyped functions will use bitcasts.
4216  if (auto *bitcast = dyn_cast<llvm::ConstantExpr>(user)) {
4217  if (bitcast->getOpcode() == llvm::Instruction::BitCast)
4218  replaceUsesOfNonProtoConstant(bitcast, newFn);
4219  continue;
4220  }
4221 
4222  // Recognize calls to the function.
4223  llvm::CallBase *callSite = dyn_cast<llvm::CallBase>(user);
4224  if (!callSite) continue;
4225  if (!callSite->isCallee(&*use))
4226  continue;
4227 
4228  // If the return types don't match exactly, then we can't
4229  // transform this call unless it's dead.
4230  if (callSite->getType() != newRetTy && !callSite->use_empty())
4231  continue;
4232 
4233  // Get the call site's attribute list.
4235  llvm::AttributeList oldAttrs = callSite->getAttributes();
4236 
4237  // If the function was passed too few arguments, don't transform.
4238  unsigned newNumArgs = newFn->arg_size();
4239  if (callSite->arg_size() < newNumArgs)
4240  continue;
4241 
4242  // If extra arguments were passed, we silently drop them.
4243  // If any of the types mismatch, we don't transform.
4244  unsigned argNo = 0;
4245  bool dontTransform = false;
4246  for (llvm::Argument &A : newFn->args()) {
4247  if (callSite->getArgOperand(argNo)->getType() != A.getType()) {
4248  dontTransform = true;
4249  break;
4250  }
4251 
4252  // Add any parameter attributes.
4253  newArgAttrs.push_back(oldAttrs.getParamAttributes(argNo));
4254  argNo++;
4255  }
4256  if (dontTransform)
4257  continue;
4258 
4259  // Okay, we can transform this. Create the new call instruction and copy
4260  // over the required information.
4261  newArgs.append(callSite->arg_begin(), callSite->arg_begin() + argNo);
4262 
4263  // Copy over any operand bundles.
4264  callSite->getOperandBundlesAsDefs(newBundles);
4265 
4266  llvm::CallBase *newCall;
4267  if (dyn_cast<llvm::CallInst>(callSite)) {
4268  newCall =
4269  llvm::CallInst::Create(newFn, newArgs, newBundles, "", callSite);
4270  } else {
4271  auto *oldInvoke = cast<llvm::InvokeInst>(callSite);
4272  newCall = llvm::InvokeInst::Create(newFn, oldInvoke->getNormalDest(),
4273  oldInvoke->getUnwindDest(), newArgs,
4274  newBundles, "", callSite);
4275  }
4276  newArgs.clear(); // for the next iteration
4277 
4278  if (!newCall->getType()->isVoidTy())
4279  newCall->takeName(callSite);
4280  newCall->setAttributes(llvm::AttributeList::get(
4281  newFn->getContext(), oldAttrs.getFnAttributes(),
4282  oldAttrs.getRetAttributes(), newArgAttrs));
4283  newCall->setCallingConv(callSite->getCallingConv());
4284 
4285  // Finally, remove the old call, replacing any uses with the new one.
4286  if (!callSite->use_empty())
4287  callSite->replaceAllUsesWith(newCall);
4288 
4289  // Copy debug location attached to CI.
4290  if (callSite->getDebugLoc())
4291  newCall->setDebugLoc(callSite->getDebugLoc());
4292 
4293  callSite->eraseFromParent();
4294  }
4295 }
4296 
4297 /// ReplaceUsesOfNonProtoTypeWithRealFunction - This function is called when we
4298 /// implement a function with no prototype, e.g. "int foo() {}". If there are
4299 /// existing call uses of the old function in the module, this adjusts them to
4300 /// call the new function directly.
4301 ///
4302 /// This is not just a cleanup: the always_inline pass requires direct calls to
4303 /// functions to be able to inline them. If there is a bitcast in the way, it
4304 /// won't inline them. Instcombine normally deletes these calls, but it isn't
4305 /// run at -O0.
4306 static void ReplaceUsesOfNonProtoTypeWithRealFunction(llvm::GlobalValue *Old,
4307  llvm::Function *NewFn) {
4308  // If we're redefining a global as a function, don't transform it.
4309  if (!isa<llvm::Function>(Old)) return;
4310 
4311  replaceUsesOfNonProtoConstant(Old, NewFn);
4312 }
4313 
4315  auto DK = VD->isThisDeclarationADefinition();
4316  if (DK == VarDecl::Definition && VD->hasAttr<DLLImportAttr>())
4317  return;
4318 
4320  // If we have a definition, this might be a deferred decl. If the
4321  // instantiation is explicit, make sure we emit it at the end.
4323  GetAddrOfGlobalVar(VD);
4324 
4325  EmitTopLevelDecl(VD);
4326 }
4327 
4328 void CodeGenModule::EmitGlobalFunctionDefinition(GlobalDecl GD,
4329  llvm::GlobalValue *GV) {
4330  const auto *D = cast<FunctionDecl>(GD.getDecl());
4331 
4332  // Compute the function info and LLVM type.
4334  llvm::FunctionType *Ty = getTypes().GetFunctionType(FI);
4335 
4336  // Get or create the prototype for the function.
4337  if (!GV || (GV->getType()->getElementType() != Ty))
4338  GV = cast<llvm::GlobalValue>(GetAddrOfFunction(GD, Ty, /*ForVTable=*/false,
4339  /*DontDefer=*/true,
4340  ForDefinition));
4341 
4342  // Already emitted.
4343  if (!GV->isDeclaration())
4344  return;
4345 
4346  // We need to set linkage and visibility on the function before
4347  // generating code for it because various parts of IR generation
4348  // want to propagate this information down (e.g. to local static
4349  // declarations).
4350  auto *Fn = cast<llvm::Function>(GV);
4351  setFunctionLinkage(GD, Fn);
4352 
4353  // FIXME: this is redundant with part of setFunctionDefinitionAttributes
4354  setGVProperties(Fn, GD);
4355 
4357 
4358 
4359  maybeSetTrivialComdat(*D, *Fn);
4360 
4361  CodeGenFunction(*this).GenerateCode(D, Fn, FI);
4362 
4363  setNonAliasAttributes(GD, Fn);
4365 
4366  if (const ConstructorAttr *CA = D->getAttr<ConstructorAttr>())
4367  AddGlobalCtor(Fn, CA->getPriority());
4368  if (const DestructorAttr *DA = D->getAttr<DestructorAttr>())
4369  AddGlobalDtor(Fn, DA->getPriority());
4370  if (D->hasAttr<AnnotateAttr>())
4371  AddGlobalAnnotations(D, Fn);
4372 }
4373 
4374 void CodeGenModule::EmitAliasDefinition(GlobalDecl GD) {
4375  const auto *D = cast<ValueDecl>(GD.getDecl());
4376  const AliasAttr *AA = D->getAttr<AliasAttr>();
4377  assert(AA && "Not an alias?");
4378 
4379  StringRef MangledName = getMangledName(GD);
4380 
4381  if (AA->getAliasee() == MangledName) {
4382  Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
4383  return;
4384  }
4385 
4386  // If there is a definition in the module, then it wins over the alias.
4387  // This is dubious, but allow it to be safe. Just ignore the alias.
4388  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4389  if (Entry && !Entry->isDeclaration())
4390  return;
4391 
4392  Aliases.push_back(GD);
4393 
4394  llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
4395 
4396  // Create a reference to the named value. This ensures that it is emitted
4397  // if a deferred decl.
4398  llvm::Constant *Aliasee;
4399  llvm::GlobalValue::LinkageTypes LT;
4400  if (isa<llvm::FunctionType>(DeclTy)) {
4401  Aliasee = GetOrCreateLLVMFunction(AA->getAliasee(), DeclTy, GD,
4402  /*ForVTable=*/false);
4403  LT = getFunctionLinkage(GD);
4404  } else {
4405  Aliasee = GetOrCreateLLVMGlobal(AA->getAliasee(),
4406  llvm::PointerType::getUnqual(DeclTy),
4407  /*D=*/nullptr);
4408  LT = getLLVMLinkageVarDefinition(cast<VarDecl>(GD.getDecl()),
4409  D->getType().isConstQualified());
4410  }
4411 
4412  // Create the new alias itself, but don't set a name yet.
4413  auto *GA =
4414  llvm::GlobalAlias::create(DeclTy, 0, LT, "", Aliasee, &getModule());
4415 
4416  if (Entry) {
4417  if (GA->getAliasee() == Entry) {
4418  Diags.Report(AA->getLocation(), diag::err_cyclic_alias) << 0;
4419  return;
4420  }
4421 
4422  assert(Entry->isDeclaration());
4423 
4424  // If there is a declaration in the module, then we had an extern followed
4425  // by the alias, as in:
4426  // extern int test6();
4427  // ...
4428  // int test6() __attribute__((alias("test7")));
4429  //
4430  // Remove it and replace uses of it with the alias.
4431  GA->takeName(Entry);
4432 
4433  Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GA,
4434  Entry->getType()));
4435  Entry->eraseFromParent();
4436  } else {
4437  GA->setName(MangledName);
4438  }
4439 
4440  // Set attributes which are particular to an alias; this is a
4441  // specialization of the attributes which may be set on a global
4442  // variable/function.
4443  if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
4444  D->isWeakImported()) {
4445  GA->setLinkage(llvm::Function::WeakAnyLinkage);
4446  }
4447 
4448  if (const auto *VD = dyn_cast<VarDecl>(D))
4449  if (VD->getTLSKind())
4450  setTLSMode(GA, *VD);
4451 
4452  SetCommonAttributes(GD, GA);
4453 }
4454 
4455 void CodeGenModule::emitIFuncDefinition(GlobalDecl GD) {
4456  const auto *D = cast<ValueDecl>(GD.getDecl());
4457  const IFuncAttr *IFA = D->getAttr<IFuncAttr>();
4458  assert(IFA && "Not an ifunc?");
4459 
4460  StringRef MangledName = getMangledName(GD);
4461 
4462  if (IFA->getResolver() == MangledName) {
4463  Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
4464  return;
4465  }
4466 
4467  // Report an error if some definition overrides ifunc.
4468  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
4469  if (Entry && !Entry->isDeclaration()) {
4470  GlobalDecl OtherGD;
4471  if (lookupRepresentativeDecl(MangledName, OtherGD) &&
4472  DiagnosedConflictingDefinitions.insert(GD).second) {
4473  Diags.Report(D->getLocation(), diag::err_duplicate_mangled_name)
4474  << MangledName;
4475  Diags.Report(OtherGD.getDecl()->getLocation(),
4476  diag::note_previous_definition);
4477  }
4478  return;
4479  }
4480 
4481  Aliases.push_back(GD);
4482 
4483  llvm::Type *DeclTy = getTypes().ConvertTypeForMem(D->getType());
4484  llvm::Constant *Resolver =
4485  GetOrCreateLLVMFunction(IFA->getResolver(), DeclTy, GD,
4486  /*ForVTable=*/false);
4487  llvm::GlobalIFunc *GIF =
4489  "", Resolver, &getModule());
4490  if (Entry) {
4491  if (GIF->getResolver() == Entry) {
4492  Diags.Report(IFA->getLocation(), diag::err_cyclic_alias) << 1;
4493  return;
4494  }
4495  assert(Entry->isDeclaration());
4496 
4497  // If there is a declaration in the module, then we had an extern followed
4498  // by the ifunc, as in:
4499  // extern int test();
4500  // ...
4501  // int test() __attribute__((ifunc("resolver")));
4502  //
4503  // Remove it and replace uses of it with the ifunc.
4504  GIF->takeName(Entry);
4505 
4506  Entry->replaceAllUsesWith(llvm::ConstantExpr::getBitCast(GIF,
4507  Entry->getType()));
4508  Entry->eraseFromParent();
4509  } else
4510  GIF->setName(MangledName);
4511 
4512  SetCommonAttributes(GD, GIF);
4513 }
4514 
4515 llvm::Function *CodeGenModule::getIntrinsic(unsigned IID,
4516  ArrayRef<llvm::Type*> Tys) {
4517  return llvm::Intrinsic::getDeclaration(&getModule(), (llvm::Intrinsic::ID)IID,
4518  Tys);
4519 }
4520 
4521 static llvm::StringMapEntry<llvm::GlobalVariable *> &
4522 GetConstantCFStringEntry(llvm::StringMap<llvm::GlobalVariable *> &Map,
4523  const StringLiteral *Literal, bool TargetIsLSB,
4524  bool &IsUTF16, unsigned &StringLength) {
4525  StringRef String = Literal->getString();
4526  unsigned NumBytes = String.size();
4527 
4528  // Check for simple case.
4529  if (!Literal->containsNonAsciiOrNull()) {
4530  StringLength = NumBytes;
4531  return *Map.insert(std::make_pair(String, nullptr)).first;
4532  }
4533 
4534  // Otherwise, convert the UTF8 literals into a string of shorts.
4535  IsUTF16 = true;
4536 
4537  SmallVector<llvm::UTF16, 128> ToBuf(NumBytes + 1); // +1 for ending nulls.
4538  const llvm::UTF8 *FromPtr = (const llvm::UTF8 *)String.data();
4539  llvm::UTF16 *ToPtr = &ToBuf[0];
4540 
4541  (void)llvm::ConvertUTF8toUTF16(&FromPtr, FromPtr + NumBytes, &ToPtr,
4542  ToPtr + NumBytes, llvm::strictConversion);
4543 
4544  // ConvertUTF8toUTF16 returns the length in ToPtr.
4545  StringLength = ToPtr - &ToBuf[0];
4546 
4547  // Add an explicit null.
4548  *ToPtr = 0;
4549  return *Map.insert(std::make_pair(
4550  StringRef(reinterpret_cast<const char *>(ToBuf.data()),
4551  (StringLength + 1) * 2),
4552  nullptr)).first;
4553 }
4554 
4557  unsigned StringLength = 0;
4558  bool isUTF16 = false;
4559  llvm::StringMapEntry<llvm::GlobalVariable *> &Entry =
4560  GetConstantCFStringEntry(CFConstantStringMap, Literal,
4561  getDataLayout().isLittleEndian(), isUTF16,
4562  StringLength);
4563 
4564  if (auto *C = Entry.second)
4565  return ConstantAddress(C, CharUnits::fromQuantity(C->getAlignment()));
4566 
4567  llvm::Constant *Zero = llvm::Constant::getNullValue(Int32Ty);
4568  llvm::Constant *Zeros[] = { Zero, Zero };
4569 
4570  const ASTContext &Context = getContext();
4571  const llvm::Triple &Triple = getTriple();
4572 
4573  const auto CFRuntime = getLangOpts().CFRuntime;
4574  const bool IsSwiftABI =
4575  static_cast<unsigned>(CFRuntime) >=
4576  static_cast<unsigned>(LangOptions::CoreFoundationABI::Swift);
4577  const bool IsSwift4_1 = CFRuntime == LangOptions::CoreFoundationABI::Swift4_1;
4578 
4579  // If we don't already have it, get __CFConstantStringClassReference.
4580  if (!CFConstantStringClassRef) {
4581  const char *CFConstantStringClassName = "__CFConstantStringClassReference";
4583  Ty = llvm::ArrayType::get(Ty, 0);
4584 
4585  switch (CFRuntime) {
4586  default: break;
4587  case LangOptions::CoreFoundationABI::Swift: LLVM_FALLTHROUGH;
4589  CFConstantStringClassName =
4590  Triple.isOSDarwin() ? "$s15SwiftFoundation19_NSCFConstantStringCN"
4591  : "$s10Foundation19_NSCFConstantStringCN";
4592  Ty = IntPtrTy;
4593  break;
4595  CFConstantStringClassName =
4596  Triple.isOSDarwin() ? "$S15SwiftFoundation19_NSCFConstantStringCN"
4597  : "$S10Foundation19_NSCFConstantStringCN";
4598  Ty = IntPtrTy;
4599  break;
4601  CFConstantStringClassName =
4602  Triple.isOSDarwin() ? "__T015SwiftFoundation19_NSCFConstantStringCN"
4603  : "__T010Foundation19_NSCFConstantStringCN";
4604  Ty = IntPtrTy;
4605  break;
4606  }
4607 
4608  llvm::Constant *C = CreateRuntimeVariable(Ty, CFConstantStringClassName);
4609 
4610  if (Triple.isOSBinFormatELF() || Triple.isOSBinFormatCOFF()) {
4611  llvm::GlobalValue *GV = nullptr;
4612 
4613  if ((GV = dyn_cast<llvm::GlobalValue>(C))) {
4614  IdentifierInfo &II = Context.Idents.get(GV->getName());
4615  TranslationUnitDecl *TUDecl = Context.getTranslationUnitDecl();
4617 
4618  const VarDecl *VD = nullptr;
4619  for (const auto &Result : DC->lookup(&II))
4620  if ((VD = dyn_cast<VarDecl>(Result)))
4621  break;
4622 
4623  if (Triple.isOSBinFormatELF()) {
4624  if (!VD)
4625  GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4626  } else {
4627  GV->setLinkage(llvm::GlobalValue::ExternalLinkage);
4628  if (!VD || !VD->hasAttr<DLLExportAttr>())
4629  GV->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass);
4630  else
4631  GV->setDLLStorageClass(llvm::GlobalValue::DLLExportStorageClass);
4632  }
4633 
4634  setDSOLocal(GV);
4635  }
4636  }
4637 
4638  // Decay array -> ptr
4639  CFConstantStringClassRef =
4640  IsSwiftABI ? llvm::ConstantExpr::getPtrToInt(C, Ty)
4641  : llvm::ConstantExpr::getGetElementPtr(Ty, C, Zeros);
4642  }
4643 
4644  QualType CFTy = Context.getCFConstantStringType();
4645 
4646  auto *STy = cast<llvm::StructType>(getTypes().ConvertType(CFTy));
4647 
4648  ConstantInitBuilder Builder(*this);
4649  auto Fields = Builder.beginStruct(STy);
4650 
4651  // Class pointer.
4652  Fields.add(cast<llvm::ConstantExpr>(CFConstantStringClassRef));
4653 
4654  // Flags.
4655  if (IsSwiftABI) {
4656  Fields.addInt(IntPtrTy, IsSwift4_1 ? 0x05 : 0x01);
4657  Fields.addInt(Int64Ty, isUTF16 ? 0x07d0 : 0x07c8);
4658  } else {
4659  Fields.addInt(IntTy, isUTF16 ? 0x07d0 : 0x07C8);
4660  }
4661 
4662  // String pointer.
4663  llvm::Constant *C = nullptr;
4664  if (isUTF16) {
4665  auto Arr = llvm::makeArrayRef(
4666  reinterpret_cast<uint16_t *>(const_cast<char *>(Entry.first().data())),
4667  Entry.first().size() / 2);
4668  C = llvm::ConstantDataArray::get(VMContext, Arr);
4669  } else {
4670  C = llvm::ConstantDataArray::getString(VMContext, Entry.first());
4671  }
4672 
4673  // Note: -fwritable-strings doesn't make the backing store strings of
4674  // CFStrings writable. (See <rdar://problem/10657500>)
4675  auto *GV =
4676  new llvm::GlobalVariable(getModule(), C->getType(), /*isConstant=*/true,
4677  llvm::GlobalValue::PrivateLinkage, C, ".str");
4678  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4679  // Don't enforce the target's minimum global alignment, since the only use
4680  // of the string is via this class initializer.
4681  CharUnits Align = isUTF16 ? Context.getTypeAlignInChars(Context.ShortTy)
4682  : Context.getTypeAlignInChars(Context.CharTy);
4683  GV->setAlignment(Align.getQuantity());
4684 
4685  // FIXME: We set the section explicitly to avoid a bug in ld64 224.1.
4686  // Without it LLVM can merge the string with a non unnamed_addr one during
4687  // LTO. Doing that changes the section it ends in, which surprises ld64.
4688  if (Triple.isOSBinFormatMachO())
4689  GV->setSection(isUTF16 ? "__TEXT,__ustring"
4690  : "__TEXT,__cstring,cstring_literals");
4691  // Make sure the literal ends up in .rodata to allow for safe ICF and for
4692  // the static linker to adjust permissions to read-only later on.
4693  else if (Triple.isOSBinFormatELF())
4694  GV->setSection(".rodata");
4695 
4696  // String.
4697  llvm::Constant *Str =
4698  llvm::ConstantExpr::getGetElementPtr(GV->getValueType(), GV, Zeros);
4699 
4700  if (isUTF16)
4701  // Cast the UTF16 string to the correct type.
4702  Str = llvm::ConstantExpr::getBitCast(Str, Int8PtrTy);
4703  Fields.add(Str);
4704 
4705  // String length.
4706  llvm::IntegerType *LengthTy =
4707  llvm::IntegerType::get(getModule().getContext(),
4708  Context.getTargetInfo().getLongWidth());
4709  if (IsSwiftABI) {
4710  if (CFRuntime == LangOptions::CoreFoundationABI::Swift4_1 ||
4712  LengthTy = Int32Ty;
4713  else
4714  LengthTy = IntPtrTy;
4715  }
4716  Fields.addInt(LengthTy, StringLength);
4717 
4718  // Swift ABI requires 8-byte alignment to ensure that the _Atomic(uint64_t) is
4719  // properly aligned on 32-bit platforms.
4720  CharUnits Alignment =
4721  IsSwiftABI ? Context.toCharUnitsFromBits(64) : getPointerAlign();
4722 
4723  // The struct.
4724  GV = Fields.finishAndCreateGlobal("_unnamed_cfstring_", Alignment,
4725  /*isConstant=*/false,
4726  llvm::GlobalVariable::PrivateLinkage);
4727  GV->addAttribute("objc_arc_inert");
4728  switch (Triple.getObjectFormat()) {
4729  case llvm::Triple::UnknownObjectFormat:
4730  llvm_unreachable("unknown file format");
4731  case llvm::Triple::XCOFF:
4732  llvm_unreachable("XCOFF is not yet implemented");
4733  case llvm::Triple::COFF:
4734  case llvm::Triple::ELF:
4735  case llvm::Triple::Wasm:
4736  GV->setSection("cfstring");
4737  break;
4738  case llvm::Triple::MachO:
4739  GV->setSection("__DATA,__cfstring");
4740  break;
4741  }
4742  Entry.second = GV;
4743 
4744  return ConstantAddress(GV, Alignment);
4745 }
4746 
4748  return !CodeGenOpts.EmitCodeView || CodeGenOpts.DebugColumnInfo;
4749 }
4750 
4752  if (ObjCFastEnumerationStateType.isNull()) {
4753  RecordDecl *D = Context.buildImplicitRecord("__objcFastEnumerationState");
4754  D->startDefinition();
4755 
4756  QualType FieldTypes[] = {
4757  Context.UnsignedLongTy,
4758  Context.getPointerType(Context.getObjCIdType()),
4759  Context.getPointerType(Context.UnsignedLongTy),
4760  Context.getConstantArrayType(Context.UnsignedLongTy,
4761  llvm::APInt(32, 5), ArrayType::Normal, 0)
4762  };
4763 
4764  for (size_t i = 0; i < 4; ++i) {
4765  FieldDecl *Field = FieldDecl::Create(Context,
4766  D,
4767  SourceLocation(),
4768  SourceLocation(), nullptr,
4769  FieldTypes[i], /*TInfo=*/nullptr,
4770  /*BitWidth=*/nullptr,
4771  /*Mutable=*/false,
4772  ICIS_NoInit);
4773  Field->setAccess(AS_public);
4774  D->addDecl(Field);
4775  }
4776 
4777  D->completeDefinition();
4778  ObjCFastEnumerationStateType = Context.getTagDeclType(D);
4779  }
4780 
4781  return ObjCFastEnumerationStateType;
4782 }
4783 
4784 llvm::Constant *
4786  assert(!E->getType()->isPointerType() && "Strings are always arrays");
4787 
4788  // Don't emit it as the address of the string, emit the string data itself
4789  // as an inline array.
4790  if (E->getCharByteWidth() == 1) {
4791  SmallString<64> Str(E->getString());
4792 
4793  // Resize the string to the right size, which is indicated by its type.
4794  const ConstantArrayType *CAT = Context.getAsConstantArrayType(E->getType());
4795  Str.resize(CAT->getSize().getZExtValue());
4796  return llvm::ConstantDataArray::getString(VMContext, Str, false);
4797  }
4798 
4799  auto *AType = cast<llvm::ArrayType>(getTypes().ConvertType(E->getType()));
4800  llvm::Type *ElemTy = AType->getElementType();
4801  unsigned NumElements = AType->getNumElements();
4802 
4803  // Wide strings have either 2-byte or 4-byte elements.
4804  if (ElemTy->getPrimitiveSizeInBits() == 16) {
4805  SmallVector<uint16_t, 32> Elements;
4806  Elements.reserve(NumElements);
4807 
4808  for(unsigned i = 0, e = E->getLength(); i != e; ++i)
4809  Elements.push_back(E->getCodeUnit(i));
4810  Elements.resize(NumElements);
4811  return llvm::ConstantDataArray::get(VMContext, Elements);
4812  }
4813 
4814  assert(ElemTy->getPrimitiveSizeInBits() == 32);
4815  SmallVector<uint32_t, 32> Elements;
4816  Elements.reserve(NumElements);
4817 
4818  for(unsigned i = 0, e = E->getLength(); i != e; ++i)
4819  Elements.push_back(E->getCodeUnit(i));
4820  Elements.resize(NumElements);
4821  return llvm::ConstantDataArray::get(VMContext, Elements);
4822 }
4823 
4824 static llvm::GlobalVariable *
4825 GenerateStringLiteral(llvm::Constant *C, llvm::GlobalValue::LinkageTypes LT,
4826  CodeGenModule &CGM, StringRef GlobalName,
4827  CharUnits Alignment) {
4828  unsigned AddrSpace = CGM.getContext().getTargetAddressSpace(
4830 
4831  llvm::Module &M = CGM.getModule();
4832  // Create a global variable for this string
4833  auto *GV = new llvm::GlobalVariable(
4834  M, C->getType(), !CGM.getLangOpts().WritableStrings, LT, C, GlobalName,
4835  nullptr, llvm::GlobalVariable::NotThreadLocal, AddrSpace);
4836  GV->setAlignment(Alignment.getQuantity());
4837  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
4838  if (GV->isWeakForLinker()) {
4839  assert(CGM.supportsCOMDAT() && "Only COFF uses weak string literals");
4840  GV->setComdat(M.getOrInsertComdat(GV->getName()));
4841  }
4842  CGM.setDSOLocal(GV);
4843 
4844  return GV;
4845 }
4846 
4847 /// GetAddrOfConstantStringFromLiteral - Return a pointer to a
4848 /// constant array for the given string literal.
4851  StringRef Name) {
4853 
4854  llvm::Constant *C = GetConstantArrayFromStringLiteral(S);
4855  llvm::GlobalVariable **Entry = nullptr;
4856  if (!LangOpts.WritableStrings) {
4857  Entry = &ConstantStringMap[C];
4858  if (auto GV = *Entry) {
4859  if (Alignment.getQuantity() > GV->getAlignment())
4860  GV->setAlignment(Alignment.getQuantity());
4862  Alignment);
4863  }
4864  }
4865 
4866  SmallString<256> MangledNameBuffer;
4867  StringRef GlobalVariableName;
4868  llvm::GlobalValue::LinkageTypes LT;
4869 
4870  // Mangle the string literal if that's how the ABI merges duplicate strings.
4871  // Don't do it if they are writable, since we don't want writes in one TU to
4872  // affect strings in another.
4873  if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
4874  !LangOpts.WritableStrings) {
4875  llvm::raw_svector_ostream Out(MangledNameBuffer);
4877  LT = llvm::GlobalValue::LinkOnceODRLinkage;
4878  GlobalVariableName = MangledNameBuffer;
4879  } else {
4880  LT = llvm::GlobalValue::PrivateLinkage;
4881  GlobalVariableName = Name;
4882  }
4883 
4884  auto GV = GenerateStringLiteral(C, LT, *this, GlobalVariableName, Alignment);
4885  if (Entry)
4886  *Entry = GV;
4887 
4888  SanitizerMD->reportGlobalToASan(GV, S->getStrTokenLoc(0), "<string literal>",
4889  QualType());
4890 
4892  Alignment);
4893 }
4894 
4895 /// GetAddrOfConstantStringFromObjCEncode - Return a pointer to a constant
4896 /// array for the given ObjCEncodeExpr node.
4899  std::string Str;
4901 
4902  return GetAddrOfConstantCString(Str);
4903 }
4904 
4905 /// GetAddrOfConstantCString - Returns a pointer to a character array containing
4906 /// the literal and a terminating '\0' character.
4907 /// The result has pointer to array type.
4909  const std::string &Str, const char *GlobalName) {
4910  StringRef StrWithNull(Str.c_str(), Str.size() + 1);
4911  CharUnits Alignment =
4913 
4914  llvm::Constant *C =
4915  llvm::ConstantDataArray::getString(getLLVMContext(), StrWithNull, false);
4916 
4917  // Don't share any string literals if strings aren't constant.
4918  llvm::GlobalVariable **Entry = nullptr;
4919  if (!LangOpts.WritableStrings) {
4920  Entry = &ConstantStringMap[C];
4921  if (auto GV = *Entry) {
4922  if (Alignment.getQuantity() > GV->getAlignment())
4923  GV->setAlignment(Alignment.getQuantity());
4925  Alignment);
4926  }
4927  }
4928 
4929  // Get the default prefix if a name wasn't specified.
4930  if (!GlobalName)
4931  GlobalName = ".str";
4932  // Create a global variable for this.
4933  auto GV = GenerateStringLiteral(C, llvm::GlobalValue::PrivateLinkage, *this,
4934  GlobalName, Alignment);
4935  if (Entry)
4936  *Entry = GV;
4937 
4939  Alignment);
4940 }
4941 
4943  const MaterializeTemporaryExpr *E, const Expr *Init) {
4944  assert((E->getStorageDuration() == SD_Static ||
4945  E->getStorageDuration() == SD_Thread) && "not a global temporary");
4946  const auto *VD = cast<VarDecl>(E->getExtendingDecl());
4947 
4948  // If we're not materializing a subobject of the temporary, keep the
4949  // cv-qualifiers from the type of the MaterializeTemporaryExpr.
4950  QualType MaterializedType = Init->getType();
4951  if (Init == E->GetTemporaryExpr())
4952  MaterializedType = E->getType();
4953 
4954  CharUnits Align = getContext().getTypeAlignInChars(MaterializedType);
4955 
4956  if (llvm::Constant *Slot = MaterializedGlobalTemporaryMap[E])
4957  return ConstantAddress(Slot, Align);
4958 
4959  // FIXME: If an externally-visible declaration extends multiple temporaries,
4960  // we need to give each temporary the same name in every translation unit (and
4961  // we also need to make the temporaries externally-visible).
4962  SmallString<256> Name;
4963  llvm::raw_svector_ostream Out(Name);
4965  VD, E->getManglingNumber(), Out);
4966 
4967  APValue *Value = nullptr;
4968  if (E->getStorageDuration() == SD_Static) {
4969  // We might have a cached constant initializer for this temporary. Note
4970  // that this might have a different value from the value computed by
4971  // evaluating the initializer if the surrounding constant expression
4972  // modifies the temporary.
4973  Value = getContext().getMaterializedTemporaryValue(E, false);
4974  if (Value && Value->isAbsent())
4975  Value = nullptr;
4976  }
4977 
4978  // Try evaluating it now, it might have a constant initializer.
4979  Expr::EvalResult EvalResult;
4980  if (!Value && Init->EvaluateAsRValue(EvalResult, getContext()) &&
4981  !EvalResult.hasSideEffects())
4982  Value = &EvalResult.Val;
4983 
4984  LangAS AddrSpace =
4985  VD ? GetGlobalVarAddressSpace(VD) : MaterializedType.getAddressSpace();
4986 
4987  Optional<ConstantEmitter> emitter;
4988  llvm::Constant *InitialValue = nullptr;
4989  bool Constant = false;
4990  llvm::Type *Type;
4991  if (Value) {
4992  // The temporary has a constant initializer, use it.
4993  emitter.emplace(*this);
4994  InitialValue = emitter->emitForInitializer(*Value, AddrSpace,
4995  MaterializedType);
4996  Constant = isTypeConstant(MaterializedType, /*ExcludeCtor*/Value);
4997  Type = InitialValue->getType();
4998  } else {
4999  // No initializer, the initialization will be provided when we
5000  // initialize the declaration which performed lifetime extension.
5001  Type = getTypes().ConvertTypeForMem(MaterializedType);
5002  }
5003 
5004  // Create a global variable for this lifetime-extended temporary.
5005  llvm::GlobalValue::LinkageTypes Linkage =
5006  getLLVMLinkageVarDefinition(VD, Constant);
5007  if (Linkage == llvm::GlobalVariable::ExternalLinkage) {
5008  const VarDecl *InitVD;
5009  if (VD->isStaticDataMember() && VD->getAnyInitializer(InitVD) &&
5010  isa<CXXRecordDecl>(InitVD->getLexicalDeclContext())) {
5011  // Temporaries defined inside a class get linkonce_odr linkage because the
5012  // class can be defined in multiple translation units.
5013  Linkage = llvm::GlobalVariable::LinkOnceODRLinkage;
5014  } else {
5015  // There is no need for this temporary to have external linkage if the
5016  // VarDecl has external linkage.
5018  }
5019  }
5020  auto TargetAS = getContext().getTargetAddressSpace(AddrSpace);
5021  auto *GV = new llvm::GlobalVariable(
5022  getModule(), Type, Constant, Linkage, InitialValue, Name.c_str(),
5023  /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
5024  if (emitter) emitter->finalize(GV);
5025  setGVProperties(GV, VD);
5026  GV->setAlignment(Align.getQuantity());
5027  if (supportsCOMDAT() && GV->isWeakForLinker())
5028  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
5029  if (VD->getTLSKind())
5030  setTLSMode(GV, *VD);
5031  llvm::Constant *CV = GV;
5032  if (AddrSpace != LangAS::Default)
5034  *this, GV, AddrSpace, LangAS::Default,
5035  Type->getPointerTo(
5036  getContext().getTargetAddressSpace(LangAS::Default)));
5037  MaterializedGlobalTemporaryMap[E] = CV;
5038  return ConstantAddress(CV, Align);
5039 }
5040 
5041 /// EmitObjCPropertyImplementations - Emit information for synthesized
5042 /// properties for an implementation.
5043 void CodeGenModule::EmitObjCPropertyImplementations(const
5045  for (const auto *PID : D->property_impls()) {
5046  // Dynamic is just for type-checking.
5047  if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Synthesize) {
5048  ObjCPropertyDecl *PD = PID->getPropertyDecl();
5049 
5050  // Determine which methods need to be implemented, some may have
5051  // been overridden. Note that ::isPropertyAccessor is not the method
5052  // we want, that just indicates if the decl came from a
5053  // property. What we want to know is if the method is defined in
5054  // this implementation.
5055  if (!D->getInstanceMethod(PD->getGetterName()))
5057  const_cast<ObjCImplementationDecl *>(D), PID);
5058  if (!PD->isReadOnly() &&
5059  !D->getInstanceMethod(PD->getSetterName()))
5061  const_cast<ObjCImplementationDecl *>(D), PID);
5062  }
5063  }
5064 }
5065 
5067  const ObjCInterfaceDecl *iface = impl->getClassInterface();
5068  for (const ObjCIvarDecl *ivar = iface->all_declared_ivar_begin();
5069  ivar; ivar = ivar->getNextIvar())
5070  if (ivar->getType().isDestructedType())
5071  return true;
5072 
5073  return false;
5074 }
5075 
5078  CodeGenFunction CGF(CGM);
5080  E = D->init_end(); B != E; ++B) {
5081  CXXCtorInitializer *CtorInitExp = *B;
5082  Expr *Init = CtorInitExp->getInit();
5083  if (!CGF.isTrivialInitializer(Init))
5084  return false;
5085  }
5086  return true;
5087 }
5088 
5089 /// EmitObjCIvarInitializations - Emit information for ivar initialization
5090 /// for an implementation.
5091 void CodeGenModule::EmitObjCIvarInitializations(ObjCImplementationDecl *D) {
5092  // We might need a .cxx_destruct even if we don't have any ivar initializers.
5093  if (needsDestructMethod(D)) {
5094  IdentifierInfo *II = &getContext().Idents.get(".cxx_destruct");
5095  Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
5096  ObjCMethodDecl *DTORMethod =
5098  cxxSelector, getContext().VoidTy, nullptr, D,
5099  /*isInstance=*/true, /*isVariadic=*/false,
5100  /*isPropertyAccessor=*/true, /*isImplicitlyDeclared=*/true,
5101  /*isDefined=*/false, ObjCMethodDecl::Required);
5102  D->addInstanceMethod(DTORMethod);
5103  CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, DTORMethod, false);
5104  D->setHasDestructors(true);
5105  }
5106 
5107  // If the implementation doesn't have any ivar initializers, we don't need
5108  // a .cxx_construct.
5109  if (D->getNumIvarInitializers() == 0 ||
5110  AllTrivialInitializers(*this, D))
5111  return;
5112 
5113  IdentifierInfo *II = &getContext().Idents.get(".cxx_construct");
5114  Selector cxxSelector = getContext().Selectors.getSelector(0, &II);
5115  // The constructor returns 'self'.
5117  D->getLocation(),
5118  D->getLocation(),
5119  cxxSelector,
5121  nullptr, D, /*isInstance=*/true,
5122  /*isVariadic=*/false,
5123  /*isPropertyAccessor=*/true,
5124  /*isImplicitlyDeclared=*/true,
5125  /*isDefined=*/false,
5127  D->addInstanceMethod(CTORMethod);
5128  CodeGenFunction(*this).GenerateObjCCtorDtorMethod(D, CTORMethod, true);
5129  D->setHasNonZeroConstructors(true);
5130 }
5131 
5132 // EmitLinkageSpec - Emit all declarations in a linkage spec.
5133 void CodeGenModule::EmitLinkageSpec(const LinkageSpecDecl *LSD) {
5134  if (LSD->getLanguage() != LinkageSpecDecl::lang_c &&
5136  ErrorUnsupported(LSD, "linkage spec");
5137  return;
5138  }
5139 
5140  EmitDeclContext(LSD);
5141 }
5142 
5143 void CodeGenModule::EmitDeclContext(const DeclContext *DC) {
5144  for (auto *I : DC->decls()) {
5145  // Unlike other DeclContexts, the contents of an ObjCImplDecl at TU scope
5146  // are themselves considered "top-level", so EmitTopLevelDecl on an
5147  // ObjCImplDecl does not recursively visit them. We need to do that in
5148  // case they're nested inside another construct (LinkageSpecDecl /
5149  // ExportDecl) that does stop them from being considered "top-level".
5150  if (auto *OID = dyn_cast<ObjCImplDecl>(I)) {
5151  for (auto *M : OID->methods())
5152  EmitTopLevelDecl(M);
5153  }
5154 
5155  EmitTopLevelDecl(I);
5156  }
5157 }
5158 
5159 /// EmitTopLevelDecl - Emit code for a single top level declaration.
5161  // Ignore dependent declarations.
5162  if (D->isTemplated())
5163  return;
5164 
5165  switch (D->getKind()) {
5166  case Decl::CXXConversion:
5167  case Decl::CXXMethod:
5168  case Decl::Function:
5169  EmitGlobal(cast<FunctionDecl>(D));
5170  // Always provide some coverage mapping
5171  // even for the functions that aren't emitted.
5173  break;
5174 
5175  case Decl::CXXDeductionGuide:
5176  // Function-like, but does not result in code emission.
5177  break;
5178 
5179  case Decl::Var:
5180  case Decl::Decomposition:
5181  case Decl::VarTemplateSpecialization:
5182  EmitGlobal(cast<VarDecl>(D));
5183  if (auto *DD = dyn_cast<DecompositionDecl>(D))
5184  for (auto *B : DD->bindings())
5185  if (auto *HD = B->getHoldingVar())
5186  EmitGlobal(HD);
5187  break;
5188 
5189  // Indirect fields from global anonymous structs and unions can be
5190  // ignored; only the actual variable requires IR gen support.
5191  case Decl::IndirectField:
5192  break;
5193 
5194  // C++ Decls
5195  case Decl::Namespace:
5196  EmitDeclContext(cast<NamespaceDecl>(D));
5197  break;
5198  case Decl::ClassTemplateSpecialization: {
5199  const auto *Spec = cast<ClassTemplateSpecializationDecl>(D);
5200  if (DebugInfo &&
5201  Spec->getSpecializationKind() == TSK_ExplicitInstantiationDefinition &&
5202  Spec->hasDefinition())
5203  DebugInfo->completeTemplateDefinition(*Spec);
5204  } LLVM_FALLTHROUGH;
5205  case Decl::CXXRecord:
5206  if (DebugInfo) {
5207  if (auto *ES = D->getASTContext().getExternalSource())
5208  if (ES->hasExternalDefinitions(D) == ExternalASTSource::EK_Never)
5209  DebugInfo->completeUnusedClass(cast<CXXRecordDecl>(*D));
5210  }
5211  // Emit any static data members, they may be definitions.
5212  for (auto *I : cast<CXXRecordDecl>(D)->decls())
5213  if (isa<VarDecl>(I) || isa<CXXRecordDecl>(I))
5214  EmitTopLevelDecl(I);
5215  break;
5216  // No code generation needed.
5217  case Decl::UsingShadow:
5218  case Decl::ClassTemplate:
5219  case Decl::VarTemplate:
5220  case Decl::Concept:
5221  case Decl::VarTemplatePartialSpecialization:
5222  case Decl::FunctionTemplate:
5223  case Decl::TypeAliasTemplate:
5224  case Decl::Block:
5225  case Decl::Empty:
5226  case Decl::Binding:
5227  break;
5228  case Decl::Using: // using X; [C++]
5229  if (CGDebugInfo *DI = getModuleDebugInfo())
5230  DI->EmitUsingDecl(cast<UsingDecl>(*D));
5231  return;
5232  case Decl::NamespaceAlias:
5233  if (CGDebugInfo *DI = getModuleDebugInfo())
5234  DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(*D));
5235  return;
5236  case Decl::UsingDirective: // using namespace X; [C++]
5237  if (CGDebugInfo *DI = getModuleDebugInfo())
5238  DI->EmitUsingDirective(cast<UsingDirectiveDecl>(*D));
5239  return;
5240  case Decl::CXXConstructor:
5241  getCXXABI().EmitCXXConstructors(cast<CXXConstructorDecl>(D));
5242  break;
5243  case Decl::CXXDestructor:
5244  getCXXABI().EmitCXXDestructors(cast<CXXDestructorDecl>(D));
5245  break;
5246 
5247  case Decl::StaticAssert:
5248  // Nothing to do.
5249  break;
5250 
5251  // Objective-C Decls
5252 
5253  // Forward declarations, no (immediate) code generation.
5254  case Decl::ObjCInterface:
5255  case Decl::ObjCCategory:
5256  break;
5257 
5258  case Decl::ObjCProtocol: {
5259  auto *Proto = cast<ObjCProtocolDecl>(D);
5260  if (Proto->isThisDeclarationADefinition())
5261  ObjCRuntime->GenerateProtocol(Proto);
5262  break;
5263  }
5264 
5265  case Decl::ObjCCategoryImpl:
5266  // Categories have properties but don't support synthesize so we
5267  // can ignore them here.
5268  ObjCRuntime->GenerateCategory(cast<ObjCCategoryImplDecl>(D));
5269  break;
5270 
5271  case Decl::ObjCImplementation: {
5272  auto *OMD = cast<ObjCImplementationDecl>(D);
5273  EmitObjCPropertyImplementations(OMD);
5274  EmitObjCIvarInitializations(OMD);
5275  ObjCRuntime->GenerateClass(OMD);
5276  // Emit global variable debug information.
5277  if (CGDebugInfo *DI = getModuleDebugInfo())
5278  if (getCodeGenOpts().getDebugInfo() >= codegenoptions::LimitedDebugInfo)
5279  DI->getOrCreateInterfaceType(getContext().getObjCInterfaceType(
5280  OMD->getClassInterface()), OMD->getLocation());
5281  break;
5282  }
5283  case Decl::ObjCMethod: {
5284  auto *OMD = cast<ObjCMethodDecl>(D);
5285  // If this is not a prototype, emit the body.
5286  if (OMD->getBody())
5287  CodeGenFunction(*this).GenerateObjCMethod(OMD);
5288  break;
5289  }
5290  case Decl::ObjCCompatibleAlias:
5291  ObjCRuntime->RegisterAlias(cast<ObjCCompatibleAliasDecl>(D));
5292  break;
5293 
5294  case Decl::PragmaComment: {
5295  const auto *PCD = cast<PragmaCommentDecl>(D);
5296  switch (PCD->getCommentKind()) {
5297  case PCK_Unknown:
5298  llvm_unreachable("unexpected pragma comment kind");
5299  case PCK_Linker:
5300  AppendLinkerOptions(PCD->getArg());
5301  break;
5302  case PCK_Lib:
5303  AddDependentLib(PCD->getArg());
5304  break;
5305  case PCK_Compiler:
5306  case PCK_ExeStr:
5307  case PCK_User:
5308  break; // We ignore all of these.
5309  }
5310  break;
5311  }
5312 
5313  case Decl::PragmaDetectMismatch: {
5314  const auto *PDMD = cast<PragmaDetectMismatchDecl>(D);
5315  AddDetectMismatch(PDMD->getName(), PDMD->getValue());
5316  break;
5317  }
5318 
5319  case Decl::LinkageSpec:
5320  EmitLinkageSpec(cast<LinkageSpecDecl>(D));
5321  break;
5322 
5323  case Decl::FileScopeAsm: {
5324  // File-scope asm is ignored during device-side CUDA compilation.
5325  if (LangOpts.CUDA && LangOpts.CUDAIsDevice)
5326  break;
5327  // File-scope asm is ignored during device-side OpenMP compilation.
5328  if (LangOpts.OpenMPIsDevice)
5329  break;
5330  auto *AD = cast<FileScopeAsmDecl>(D);
5331  getModule().appendModuleInlineAsm(AD->getAsmString()->getString());
5332  break;
5333  }
5334 
5335  case Decl::Import: {
5336  auto *Import = cast<ImportDecl>(D);
5337 
5338  // If we've already imported this module, we're done.
5339  if (!ImportedModules.insert(Import->getImportedModule()))
5340  break;
5341 
5342  // Emit debug information for direct imports.
5343  if (!Import->getImportedOwningModule()) {
5344  if (CGDebugInfo *DI = getModuleDebugInfo())
5345  DI->EmitImportDecl(*Import);
5346  }
5347 
5348  // Find all of the submodules and emit the module initializers.
5349  llvm::SmallPtrSet<clang::Module *, 16> Visited;
5351  Visited.insert(Import->getImportedModule());
5352  Stack.push_back(Import->getImportedModule());
5353 
5354  while (!Stack.empty()) {
5355  clang::Module *Mod = Stack.pop_back_val();
5356  if (!EmittedModuleInitializers.insert(Mod).second)
5357  continue;
5358 
5359  for (auto *D : Context.getModuleInitializers(Mod))
5360  EmitTopLevelDecl(D);
5361 
5362  // Visit the submodules of this module.
5364  SubEnd = Mod->submodule_end();
5365  Sub != SubEnd; ++Sub) {
5366  // Skip explicit children; they need to be explicitly imported to emit
5367  // the initializers.
5368  if ((*Sub)->IsExplicit)
5369  continue;
5370 
5371  if (Visited.insert(*Sub).second)
5372  Stack.push_back(*Sub);
5373  }
5374  }
5375  break;
5376  }
5377 
5378  case Decl::Export:
5379  EmitDeclContext(cast<ExportDecl>(D));
5380  break;
5381 
5382  case Decl::OMPThreadPrivate:
5383  EmitOMPThreadPrivateDecl(cast<OMPThreadPrivateDecl>(D));
5384  break;
5385 
5386  case Decl::OMPAllocate:
5387  break;
5388 
5389  case Decl::OMPDeclareReduction:
5390  EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(D));
5391  break;
5392 
5393  case Decl::OMPDeclareMapper:
5394  EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(D));
5395  break;
5396 
5397  case Decl::OMPRequires:
5398  EmitOMPRequiresDecl(cast<OMPRequiresDecl>(D));
5399  break;
5400 
5401  default:
5402  // Make sure we handled everything we should, every other kind is a
5403  // non-top-level decl. FIXME: Would be nice to have an isTopLevelDeclKind
5404  // function. Need to recode Decl::Kind to do that easily.
5405  assert(isa<TypeDecl>(D) && "Unsupported decl kind");
5406  break;
5407  }
5408 }
5409 
5411  // Do we need to generate coverage mapping?
5412  if (!CodeGenOpts.CoverageMapping)
5413  return;
5414  switch (D->getKind()) {
5415  case Decl::CXXConversion:
5416  case Decl::CXXMethod:
5417  case Decl::Function:
5418  case Decl::ObjCMethod:
5419  case Decl::CXXConstructor:
5420  case Decl::CXXDestructor: {
5421  if (!cast<FunctionDecl>(D)->doesThisDeclarationHaveABody())
5422  return;
5424  if (LimitedCoverage && SM.getMainFileID() != SM.getFileID(D->getBeginLoc()))
5425  return;
5426  auto I = DeferredEmptyCoverageMappingDecls.find(D);
5427  if (I == DeferredEmptyCoverageMappingDecls.end())
5428  DeferredEmptyCoverageMappingDecls[D] = true;
5429  break;
5430  }
5431  default:
5432  break;
5433  };
5434 }
5435 
5437  // Do we need to generate coverage mapping?
5438  if (!CodeGenOpts.CoverageMapping)
5439  return;
5440  if (const auto *Fn = dyn_cast<FunctionDecl>(D)) {
5441  if (Fn->isTemplateInstantiation())
5442  ClearUnusedCoverageMapping(Fn->getTemplateInstantiationPattern());
5443  }
5444  auto I = DeferredEmptyCoverageMappingDecls.find(D);
5445  if (I == DeferredEmptyCoverageMappingDecls.end())
5446  DeferredEmptyCoverageMappingDecls[D] = false;
5447  else
5448  I->second = false;
5449 }
5450 
5452  // We call takeVector() here to avoid use-after-free.
5453  // FIXME: DeferredEmptyCoverageMappingDecls is getting mutated because
5454  // we deserialize function bodies to emit coverage info for them, and that
5455  // deserializes more declarations. How should we handle that case?
5456  for (const auto &Entry : DeferredEmptyCoverageMappingDecls.takeVector()) {
5457  if (!Entry.second)
5458  continue;
5459  const Decl *D = Entry.first;
5460  switch (D->getKind()) {
5461  case Decl::CXXConversion:
5462  case Decl::CXXMethod:
5463  case Decl::Function:
5464  case Decl::ObjCMethod: {
5465  CodeGenPGO PGO(*this);
5466  GlobalDecl GD(cast<FunctionDecl>(D));
5468  getFunctionLinkage(GD));
5469  break;
5470  }
5471  case Decl::CXXConstructor: {
5472  CodeGenPGO PGO(*this);
5473  GlobalDecl GD(cast<CXXConstructorDecl>(D), Ctor_Base);
5475  getFunctionLinkage(GD));
5476  break;
5477  }
5478  case Decl::CXXDestructor: {
5479  CodeGenPGO PGO(*this);
5480  GlobalDecl GD(cast<CXXDestructorDecl>(D), Dtor_Base);
5482  getFunctionLinkage(GD));
5483  break;
5484  }
5485  default:
5486  break;
5487  };
5488  }
5489 }
5490 
5491 /// Turns the given pointer into a constant.
5492 static llvm::Constant *GetPointerConstant(llvm::LLVMContext &Context,
5493  const void *Ptr) {
5494  uintptr_t PtrInt = reinterpret_cast<uintptr_t>(Ptr);
5495  llvm::Type *i64 = llvm::Type::getInt64Ty(Context);
5496  return llvm::ConstantInt::get(i64, PtrInt);
5497 }
5498 
5500  llvm::NamedMDNode *&GlobalMetadata,
5501  GlobalDecl D,
5502  llvm::GlobalValue *Addr) {
5503  if (!GlobalMetadata)
5504  GlobalMetadata =
5505  CGM.getModule().getOrInsertNamedMetadata("clang.global.decl.ptrs");
5506 
5507  // TODO: should we report variant information for ctors/dtors?
5508  llvm::Metadata *Ops[] = {llvm::ConstantAsMetadata::get(Addr),
5509  llvm::ConstantAsMetadata::get(GetPointerConstant(
5510  CGM.getLLVMContext(), D.getDecl()))};
5511  GlobalMetadata->addOperand(llvm::MDNode::get(CGM.getLLVMContext(), Ops));
5512 }
5513 
5514 /// For each function which is declared within an extern "C" region and marked
5515 /// as 'used', but has internal linkage, create an alias from the unmangled
5516 /// name to the mangled name if possible. People expect to be able to refer
5517 /// to such functions with an unmangled name from inline assembly within the
5518 /// same translation unit.
5519 void CodeGenModule::EmitStaticExternCAliases() {
5520  if (!getTargetCodeGenInfo().shouldEmitStaticExternCAliases())
5521  return;
5522  for (auto &I : StaticExternCValues) {
5523  IdentifierInfo *Name = I.first;
5524  llvm::GlobalValue *Val = I.second;
5525  if (Val && !getModule().getNamedValue(Name->getName()))
5527  }
5528 }
5529 
5530 bool CodeGenModule::lookupRepresentativeDecl(StringRef MangledName,
5531  GlobalDecl &Result) const {
5532  auto Res = Manglings.find(MangledName);
5533  if (Res == Manglings.end())
5534  return false;
5535  Result = Res->getValue();
5536  return true;
5537 }
5538 
5539 /// Emits metadata nodes associating all the global values in the
5540 /// current module with the Decls they came from. This is useful for
5541 /// projects using IR gen as a subroutine.
5542 ///
5543 /// Since there's currently no way to associate an MDNode directly
5544 /// with an llvm::GlobalValue, we create a global named metadata
5545 /// with the name 'clang.global.decl.ptrs'.
5546 void CodeGenModule::EmitDeclMetadata() {
5547  llvm::NamedMDNode *GlobalMetadata = nullptr;
5548 
5549  for (auto &I : MangledDeclNames) {
5550  llvm::GlobalValue *Addr = getModule().getNamedValue(I.second);
5551  // Some mangled names don't necessarily have an associated GlobalValue
5552  // in this module, e.g. if we mangled it for DebugInfo.
5553  if (Addr)
5554  EmitGlobalDeclMetadata(*this, GlobalMetadata, I.first, Addr);
5555  }
5556 }
5557 
5558 /// Emits metadata nodes for all the local variables in the current
5559 /// function.
5560 void CodeGenFunction::EmitDeclMetadata() {
5561  if (LocalDeclMap.empty()) return;
5562 
5563  llvm::LLVMContext &Context = getLLVMContext();
5564 
5565  // Find the unique metadata ID for this name.
5566  unsigned DeclPtrKind = Context.getMDKindID("clang.decl.ptr");
5567 
5568  llvm::NamedMDNode *GlobalMetadata = nullptr;
5569 
5570  for (auto &I : LocalDeclMap) {
5571  const Decl *D = I.first;
5572  llvm::Value *Addr = I.second.getPointer();
5573  if (auto *Alloca = dyn_cast<llvm::AllocaInst>(Addr)) {
5575  Alloca->setMetadata(
5576  DeclPtrKind, llvm::MDNode::get(
5577  Context, llvm::ValueAsMetadata::getConstant(DAddr)));
5578  } else if (auto *GV = dyn_cast<llvm::GlobalValue>(Addr)) {
5579  GlobalDecl GD = GlobalDecl(cast<VarDecl>(D));
5580  EmitGlobalDeclMetadata(CGM, GlobalMetadata, GD, GV);
5581  }
5582  }
5583 }
5584 
5585 void CodeGenModule::EmitVersionIdentMetadata() {
5586  llvm::NamedMDNode *IdentMetadata =
5587  TheModule.getOrInsertNamedMetadata("llvm.ident");
5588  std::string Version = getClangFullVersion();
5589  llvm::LLVMContext &Ctx = TheModule.getContext();
5590 
5591  llvm::Metadata *IdentNode[] = {llvm::MDString::get(Ctx, Version)};
5592  IdentMetadata->addOperand(llvm::MDNode::get(Ctx, IdentNode));
5593 }
5594 
5595 void CodeGenModule::EmitCommandLineMetadata() {
5596  llvm::NamedMDNode *CommandLineMetadata =
5597  TheModule.getOrInsertNamedMetadata("llvm.commandline");
5598  std::string CommandLine = getCodeGenOpts().RecordCommandLine;
5599  llvm::LLVMContext &Ctx = TheModule.getContext();
5600 
5601  llvm::Metadata *CommandLineNode[] = {llvm::MDString::get(Ctx, CommandLine)};
5602  CommandLineMetadata->addOperand(llvm::MDNode::get(Ctx, CommandLineNode));
5603 }
5604 
5605 void CodeGenModule::EmitTargetMetadata() {
5606  // Warning, new MangledDeclNames may be appended within this loop.
5607  // We rely on MapVector insertions adding new elements to the end
5608  // of the container.
5609  // FIXME: Move this loop into the one target that needs it, and only
5610  // loop over those declarations for which we couldn't emit the target
5611  // metadata when we emitted the declaration.
5612  for (unsigned I = 0; I != MangledDeclNames.size(); ++I) {
5613  auto Val = *(MangledDeclNames.begin() + I);
5614  const Decl *D = Val.first.getDecl()->getMostRecentDecl();
5615  llvm::GlobalValue *GV = GetGlobalValue(Val.second);
5616  getTargetCodeGenInfo().emitTargetMD(D, GV, *this);
5617  }
5618 }
5619 
5620 void CodeGenModule::EmitCoverageFile() {
5621  if (getCodeGenOpts().CoverageDataFile.empty() &&
5623  return;
5624 
5625  llvm::NamedMDNode *CUNode = TheModule.getNamedMetadata("llvm.dbg.cu");
5626  if (!CUNode)
5627  return;
5628 
5629  llvm::NamedMDNode *GCov = TheModule.getOrInsertNamedMetadata("llvm.gcov");
5630  llvm::LLVMContext &Ctx = TheModule.getContext();
5631  auto *CoverageDataFile =
5632  llvm::MDString::get(Ctx, getCodeGenOpts().CoverageDataFile);
5633  auto *CoverageNotesFile =
5634  llvm::MDString::get(Ctx, getCodeGenOpts().CoverageNotesFile);
5635  for (int i = 0, e = CUNode->getNumOperands(); i != e; ++i) {
5636  llvm::MDNode *CU = CUNode->getOperand(i);
5637  llvm::Metadata *Elts[] = {CoverageNotesFile, CoverageDataFile, CU};
5638  GCov->addOperand(llvm::MDNode::get(Ctx, Elts));
5639  }
5640 }
5641 
5642 llvm::Constant *CodeGenModule::EmitUuidofInitializer(StringRef Uuid) {
5643  // Sema has checked that all uuid strings are of the form
5644  // "12345678-1234-1234-1234-1234567890ab".
5645  assert(Uuid.size() == 36);
5646  for (unsigned i = 0; i < 36; ++i) {
5647  if (i == 8 || i == 13 || i == 18 || i == 23) assert(Uuid[i] == '-');
5648  else assert(isHexDigit(Uuid[i]));
5649  }
5650 
5651  // The starts of all bytes of Field3 in Uuid. Field 3 is "1234-1234567890ab".
5652  const unsigned Field3ValueOffsets[8] = { 19, 21, 24, 26, 28, 30, 32, 34 };
5653 
5654  llvm::Constant *Field3[8];
5655  for (unsigned Idx = 0; Idx < 8; ++Idx)
5656  Field3[Idx] = llvm::ConstantInt::get(
5657  Int8Ty, Uuid.substr(Field3ValueOffsets[Idx], 2), 16);
5658 
5659  llvm::Constant *Fields[4] = {
5660  llvm::ConstantInt::get(Int32Ty, Uuid.substr(0, 8), 16),
5661  llvm::ConstantInt::get(Int16Ty, Uuid.substr(9, 4), 16),
5662  llvm::ConstantInt::get(Int16Ty, Uuid.substr(14, 4), 16),
5663  llvm::ConstantArray::get(llvm::ArrayType::get(Int8Ty, 8), Field3)
5664  };
5665 
5666  return llvm::ConstantStruct::getAnon(Fields);
5667 }
5668 
5670  bool ForEH) {
5671  // Return a bogus pointer if RTTI is disabled, unless it's for EH.
5672  // FIXME: should we even be calling this method if RTTI is disabled
5673  // and it's not for EH?
5674  if ((!ForEH && !getLangOpts().RTTI) || getLangOpts().CUDAIsDevice)
5675  return llvm::Constant::getNullValue(Int8PtrTy);
5676 
5677  if (ForEH && Ty->isObjCObjectPointerType() &&
5678  LangOpts.ObjCRuntime.isGNUFamily())
5679  return ObjCRuntime->GetEHType(Ty);
5680 
5681  return getCXXABI().getAddrOfRTTIDescriptor(Ty);
5682 }
5683 
5685  // Do not emit threadprivates in simd-only mode.
5686  if (LangOpts.OpenMP && LangOpts.OpenMPSimd)
5687  return;
5688  for (auto RefExpr : D->varlists()) {
5689  auto *VD = cast<VarDecl>(cast<DeclRefExpr>(RefExpr)->getDecl());
5690  bool PerformInit =
5691  VD->getAnyInitializer() &&
5692  !VD->getAnyInitializer()->isConstantInitializer(getContext(),
5693  /*ForRef=*/false);
5694 
5696  if (auto InitFunction = getOpenMPRuntime().emitThreadPrivateVarDefinition(
5697  VD, Addr, RefExpr->getBeginLoc(), PerformInit))
5698  CXXGlobalInits.push_back(InitFunction);
5699  }
5700 }
5701 
5702 llvm::Metadata *
5703 CodeGenModule::CreateMetadataIdentifierImpl(QualType T, MetadataTypeMap &Map,
5704  StringRef Suffix) {
5705  llvm::Metadata *&InternalId = Map[T.getCanonicalType()];
5706  if (InternalId)
5707  return InternalId;
5708 
5709  if (isExternallyVisible(T->getLinkage())) {
5710  std::string OutName;
5711  llvm::raw_string_ostream Out(OutName);
5713  Out << Suffix;
5714 
5715  InternalId = llvm::MDString::get(getLLVMContext(), Out.str());
5716  } else {
5717  InternalId = llvm::MDNode::getDistinct(getLLVMContext(),
5719  }
5720 
5721  return InternalId;
5722 }
5723 
5725  return CreateMetadataIdentifierImpl(T, MetadataIdMap, "");
5726 }
5727 
5728 llvm::Metadata *
5730  return CreateMetadataIdentifierImpl(T, VirtualMetadataIdMap, ".virtual");
5731 }
5732 
5733 // Generalize pointer types to a void pointer with the qualifiers of the
5734 // originally pointed-to type, e.g. 'const char *' and 'char * const *'
5735 // generalize to 'const void *' while 'char *' and 'const char **' generalize to
5736 // 'void *'.
5738  if (!Ty->isPointerType())
5739  return Ty;
5740 
5741  return Ctx.getPointerType(
5743  Ty->getPointeeType().getCVRQualifiers()));
5744 }
5745 
5746 // Apply type generalization to a FunctionType's return and argument types
5748  if (auto *FnType = Ty->getAs<FunctionProtoType>()) {
5749  SmallVector<QualType, 8> GeneralizedParams;
5750  for (auto &Param : FnType->param_types())
5751  GeneralizedParams.push_back(GeneralizeType(Ctx, Param));
5752 
5753  return Ctx.getFunctionType(
5754  GeneralizeType(Ctx, FnType->getReturnType()),
5755  GeneralizedParams, FnType->getExtProtoInfo());