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