clang  15.0.0git
CGDeclCXX.cpp
Go to the documentation of this file.
1 //===--- CGDeclCXX.cpp - Emit LLVM Code for C++ declarations --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This contains code dealing with code generation of C++ declarations
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGCXXABI.h"
14 #include "CGObjCRuntime.h"
15 #include "CGOpenMPRuntime.h"
16 #include "CodeGenFunction.h"
17 #include "TargetInfo.h"
18 #include "clang/AST/Attr.h"
20 #include "llvm/ADT/StringExtras.h"
21 #include "llvm/IR/Intrinsics.h"
22 #include "llvm/IR/MDBuilder.h"
23 #include "llvm/Support/Path.h"
24 
25 using namespace clang;
26 using namespace CodeGen;
27 
28 static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D,
29  ConstantAddress DeclPtr) {
30  assert(
31  (D.hasGlobalStorage() ||
32  (D.hasLocalStorage() && CGF.getContext().getLangOpts().OpenCLCPlusPlus)) &&
33  "VarDecl must have global or local (in the case of OpenCL) storage!");
34  assert(!D.getType()->isReferenceType() &&
35  "Should not call EmitDeclInit on a reference!");
36 
37  QualType type = D.getType();
38  LValue lv = CGF.MakeAddrLValue(DeclPtr, type);
39 
40  const Expr *Init = D.getInit();
41  switch (CGF.getEvaluationKind(type)) {
42  case TEK_Scalar: {
43  CodeGenModule &CGM = CGF.CGM;
44  if (lv.isObjCStrong())
46  DeclPtr, D.getTLSKind());
47  else if (lv.isObjCWeak())
49  DeclPtr);
50  else
51  CGF.EmitScalarInit(Init, &D, lv, false);
52  return;
53  }
54  case TEK_Complex:
55  CGF.EmitComplexExprIntoLValue(Init, lv, /*isInit*/ true);
56  return;
57  case TEK_Aggregate:
58  CGF.EmitAggExpr(Init,
63  return;
64  }
65  llvm_unreachable("bad evaluation kind");
66 }
67 
68 /// Emit code to cause the destruction of the given variable with
69 /// static storage duration.
70 static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D,
71  ConstantAddress Addr) {
72  // Honor __attribute__((no_destroy)) and bail instead of attempting
73  // to emit a reference to a possibly nonexistent destructor, which
74  // in turn can cause a crash. This will result in a global constructor
75  // that isn't balanced out by a destructor call as intended by the
76  // attribute. This also checks for -fno-c++-static-destructors and
77  // bails even if the attribute is not present.
79 
80  // FIXME: __attribute__((cleanup)) ?
81 
82  switch (DtorKind) {
83  case QualType::DK_none:
84  return;
85 
87  break;
88 
92  // We don't care about releasing objects during process teardown.
93  assert(!D.getTLSKind() && "should have rejected this");
94  return;
95  }
96 
97  llvm::FunctionCallee Func;
98  llvm::Constant *Argument;
99 
100  CodeGenModule &CGM = CGF.CGM;
101  QualType Type = D.getType();
102 
103  // Special-case non-array C++ destructors, if they have the right signature.
104  // Under some ABIs, destructors return this instead of void, and cannot be
105  // passed directly to __cxa_atexit if the target does not allow this
106  // mismatch.
107  const CXXRecordDecl *Record = Type->getAsCXXRecordDecl();
108  bool CanRegisterDestructor =
109  Record && (!CGM.getCXXABI().HasThisReturn(
110  GlobalDecl(Record->getDestructor(), Dtor_Complete)) ||
112  // If __cxa_atexit is disabled via a flag, a different helper function is
113  // generated elsewhere which uses atexit instead, and it takes the destructor
114  // directly.
115  bool UsingExternalHelper = !CGM.getCodeGenOpts().CXAAtExit;
116  if (Record && (CanRegisterDestructor || UsingExternalHelper)) {
117  assert(!Record->hasTrivialDestructor());
118  CXXDestructorDecl *Dtor = Record->getDestructor();
119 
121  if (CGF.getContext().getLangOpts().OpenCL) {
122  auto DestAS =
124  auto DestTy = CGF.getTypes().ConvertType(Type)->getPointerTo(
125  CGM.getContext().getTargetAddressSpace(DestAS));
126  auto SrcAS = D.getType().getQualifiers().getAddressSpace();
127  if (DestAS == SrcAS)
128  Argument = llvm::ConstantExpr::getBitCast(Addr.getPointer(), DestTy);
129  else
130  // FIXME: On addr space mismatch we are passing NULL. The generation
131  // of the global destructor function should be adjusted accordingly.
132  Argument = llvm::ConstantPointerNull::get(DestTy);
133  } else {
134  Argument = llvm::ConstantExpr::getBitCast(
135  Addr.getPointer(), CGF.getTypes().ConvertType(Type)->getPointerTo());
136  }
137  // Otherwise, the standard logic requires a helper function.
138  } else {
139  Addr = Addr.getElementBitCast(CGF.ConvertTypeForMem(Type));
140  Func = CodeGenFunction(CGM)
141  .generateDestroyHelper(Addr, Type, CGF.getDestroyer(DtorKind),
142  CGF.needsEHCleanup(DtorKind), &D);
143  Argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
144  }
145 
146  CGM.getCXXABI().registerGlobalDtor(CGF, D, Func, Argument);
147 }
148 
149 /// Emit code to cause the variable at the given address to be considered as
150 /// constant from this point onwards.
151 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
152  llvm::Constant *Addr) {
153  return CGF.EmitInvariantStart(
154  Addr, CGF.getContext().getTypeSizeInChars(D.getType()));
155 }
156 
157 void CodeGenFunction::EmitInvariantStart(llvm::Constant *Addr, CharUnits Size) {
158  // Do not emit the intrinsic if we're not optimizing.
159  if (!CGM.getCodeGenOpts().OptimizationLevel)
160  return;
161 
162  // Grab the llvm.invariant.start intrinsic.
163  llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
164  // Overloaded address space type.
165  llvm::Type *ObjectPtr[1] = {Int8PtrTy};
166  llvm::Function *InvariantStart = CGM.getIntrinsic(InvStartID, ObjectPtr);
167 
168  // Emit a call with the size in bytes of the object.
169  uint64_t Width = Size.getQuantity();
170  llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(Int64Ty, Width),
171  llvm::ConstantExpr::getBitCast(Addr, Int8PtrTy)};
172  Builder.CreateCall(InvariantStart, Args);
173 }
174 
176  llvm::GlobalVariable *GV,
177  bool PerformInit) {
178 
179  const Expr *Init = D.getInit();
180  QualType T = D.getType();
181 
182  // The address space of a static local variable (DeclPtr) may be different
183  // from the address space of the "this" argument of the constructor. In that
184  // case, we need an addrspacecast before calling the constructor.
185  //
186  // struct StructWithCtor {
187  // __device__ StructWithCtor() {...}
188  // };
189  // __device__ void foo() {
190  // __shared__ StructWithCtor s;
191  // ...
192  // }
193  //
194  // For example, in the above CUDA code, the static local variable s has a
195  // "shared" address space qualifier, but the constructor of StructWithCtor
196  // expects "this" in the "generic" address space.
197  unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(T);
198  unsigned ActualAddrSpace = GV->getAddressSpace();
199  llvm::Constant *DeclPtr = GV;
200  if (ActualAddrSpace != ExpectedAddrSpace) {
201  llvm::PointerType *PTy = llvm::PointerType::getWithSamePointeeType(
202  GV->getType(), ExpectedAddrSpace);
203  DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy);
204  }
205 
206  ConstantAddress DeclAddr(
207  DeclPtr, GV->getValueType(), getContext().getDeclAlign(&D));
208 
209  if (!T->isReferenceType()) {
210  if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
211  D.hasAttr<OMPThreadPrivateDeclAttr>()) {
213  &D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
214  PerformInit, this);
215  }
216  if (PerformInit)
217  EmitDeclInit(*this, D, DeclAddr);
218  if (CGM.isTypeConstant(D.getType(), true))
219  EmitDeclInvariant(*this, D, DeclPtr);
220  else
221  EmitDeclDestroy(*this, D, DeclAddr);
222  return;
223  }
224 
225  assert(PerformInit && "cannot have constant initializer which needs "
226  "destruction for reference");
228  EmitStoreOfScalar(RV.getScalarVal(), DeclAddr, false, T);
229 }
230 
231 /// Create a stub function, suitable for being passed to atexit,
232 /// which passes the given address to the given destructor function.
233 llvm::Function *CodeGenFunction::createAtExitStub(const VarDecl &VD,
234  llvm::FunctionCallee dtor,
235  llvm::Constant *addr) {
236  // Get the destructor function type, void(*)(void).
237  llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
238  SmallString<256> FnName;
239  {
240  llvm::raw_svector_ostream Out(FnName);
242  }
243 
245  llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction(
246  ty, FnName.str(), FI, VD.getLocation());
247 
248  CodeGenFunction CGF(CGM);
249 
251  CGM.getContext().VoidTy, fn, FI, FunctionArgList(),
252  VD.getLocation(), VD.getInit()->getExprLoc());
253  // Emit an artificial location for this function.
255 
256  llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
257 
258  // Make sure the call and the callee agree on calling convention.
259  if (auto *dtorFn = dyn_cast<llvm::Function>(
260  dtor.getCallee()->stripPointerCastsAndAliases()))
261  call->setCallingConv(dtorFn->getCallingConv());
262 
263  CGF.FinishFunction();
264 
265  return fn;
266 }
267 
268 /// Create a stub function, suitable for being passed to __pt_atexit_np,
269 /// which passes the given address to the given destructor function.
271  const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr,
272  llvm::FunctionCallee &AtExit) {
273  SmallString<256> FnName;
274  {
275  llvm::raw_svector_ostream Out(FnName);
277  }
278 
280  getContext().IntTy, /*instanceMethod=*/false, /*chainCall=*/false,
282 
283  // Get the stub function type, int(*)(int,...).
284  llvm::FunctionType *StubTy =
285  llvm::FunctionType::get(CGM.IntTy, {CGM.IntTy}, true);
286 
287  llvm::Function *DtorStub = CGM.CreateGlobalInitOrCleanUpFunction(
288  StubTy, FnName.str(), FI, D.getLocation());
289 
290  CodeGenFunction CGF(CGM);
291 
292  FunctionArgList Args;
295  Args.push_back(&IPD);
296  QualType ResTy = CGM.getContext().IntTy;
297 
298  CGF.StartFunction(GlobalDecl(&D, DynamicInitKind::AtExit), ResTy, DtorStub,
299  FI, Args, D.getLocation(), D.getInit()->getExprLoc());
300 
301  // Emit an artificial location for this function.
303 
304  llvm::CallInst *call = CGF.Builder.CreateCall(Dtor, Addr);
305 
306  // Make sure the call and the callee agree on calling convention.
307  if (auto *DtorFn = dyn_cast<llvm::Function>(
308  Dtor.getCallee()->stripPointerCastsAndAliases()))
309  call->setCallingConv(DtorFn->getCallingConv());
310 
311  // Return 0 from function
312  CGF.Builder.CreateStore(llvm::Constant::getNullValue(CGM.IntTy),
313  CGF.ReturnValue);
314 
315  CGF.FinishFunction();
316 
317  return DtorStub;
318 }
319 
320 /// Register a global destructor using the C atexit runtime function.
322  llvm::FunctionCallee dtor,
323  llvm::Constant *addr) {
324  // Create a function which calls the destructor.
325  llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
327 }
328 
329 void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) {
330  // extern "C" int atexit(void (*f)(void));
331  assert(dtorStub->getType() ==
332  llvm::PointerType::get(
333  llvm::FunctionType::get(CGM.VoidTy, false),
334  dtorStub->getType()->getPointerAddressSpace()) &&
335  "Argument to atexit has a wrong type.");
336 
337  llvm::FunctionType *atexitTy =
338  llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
339 
340  llvm::FunctionCallee atexit =
341  CGM.CreateRuntimeFunction(atexitTy, "atexit", llvm::AttributeList(),
342  /*Local=*/true);
343  if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit.getCallee()))
344  atexitFn->setDoesNotThrow();
345 
346  EmitNounwindRuntimeCall(atexit, dtorStub);
347 }
348 
349 llvm::Value *
351  // The unatexit subroutine unregisters __dtor functions that were previously
352  // registered by the atexit subroutine. If the referenced function is found,
353  // it is removed from the list of functions that are called at normal program
354  // termination and the unatexit returns a value of 0, otherwise a non-zero
355  // value is returned.
356  //
357  // extern "C" int unatexit(void (*f)(void));
358  assert(dtorStub->getType() ==
359  llvm::PointerType::get(
360  llvm::FunctionType::get(CGM.VoidTy, false),
361  dtorStub->getType()->getPointerAddressSpace()) &&
362  "Argument to unatexit has a wrong type.");
363 
364  llvm::FunctionType *unatexitTy =
365  llvm::FunctionType::get(IntTy, {dtorStub->getType()}, /*isVarArg=*/false);
366 
367  llvm::FunctionCallee unatexit =
368  CGM.CreateRuntimeFunction(unatexitTy, "unatexit", llvm::AttributeList());
369 
370  cast<llvm::Function>(unatexit.getCallee())->setDoesNotThrow();
371 
372  return EmitNounwindRuntimeCall(unatexit, dtorStub);
373 }
374 
376  llvm::GlobalVariable *DeclPtr,
377  bool PerformInit) {
378  // If we've been asked to forbid guard variables, emit an error now.
379  // This diagnostic is hard-coded for Darwin's use case; we can find
380  // better phrasing if someone else needs it.
381  if (CGM.getCodeGenOpts().ForbidGuardVariables)
382  CGM.Error(D.getLocation(),
383  "this initialization requires a guard variable, which "
384  "the kernel does not support");
385 
386  CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
387 }
388 
389 void CodeGenFunction::EmitCXXGuardedInitBranch(llvm::Value *NeedsInit,
390  llvm::BasicBlock *InitBlock,
391  llvm::BasicBlock *NoInitBlock,
392  GuardKind Kind,
393  const VarDecl *D) {
394  assert((Kind == GuardKind::TlsGuard || D) && "no guarded variable");
395 
396  // A guess at how many times we will enter the initialization of a
397  // variable, depending on the kind of variable.
398  static const uint64_t InitsPerTLSVar = 1024;
399  static const uint64_t InitsPerLocalVar = 1024 * 1024;
400 
401  llvm::MDNode *Weights;
402  if (Kind == GuardKind::VariableGuard && !D->isLocalVarDecl()) {
403  // For non-local variables, don't apply any weighting for now. Due to our
404  // use of COMDATs, we expect there to be at most one initialization of the
405  // variable per DSO, but we have no way to know how many DSOs will try to
406  // initialize the variable.
407  Weights = nullptr;
408  } else {
409  uint64_t NumInits;
410  // FIXME: For the TLS case, collect and use profiling information to
411  // determine a more accurate brach weight.
412  if (Kind == GuardKind::TlsGuard || D->getTLSKind())
413  NumInits = InitsPerTLSVar;
414  else
415  NumInits = InitsPerLocalVar;
416 
417  // The probability of us entering the initializer is
418  // 1 / (total number of times we attempt to initialize the variable).
419  llvm::MDBuilder MDHelper(CGM.getLLVMContext());
420  Weights = MDHelper.createBranchWeights(1, NumInits - 1);
421  }
422 
423  Builder.CreateCondBr(NeedsInit, InitBlock, NoInitBlock, Weights);
424 }
425 
427  llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI,
428  SourceLocation Loc, bool TLS, llvm::GlobalVariable::LinkageTypes Linkage) {
429  llvm::Function *Fn = llvm::Function::Create(FTy, Linkage, Name, &getModule());
430 
431  if (!getLangOpts().AppleKext && !TLS) {
432  // Set the section if needed.
433  if (const char *Section = getTarget().getStaticInitSectionSpecifier())
434  Fn->setSection(Section);
435  }
436 
439 
440  Fn->setCallingConv(getRuntimeCC());
441 
442  if (!getLangOpts().Exceptions)
443  Fn->setDoesNotThrow();
444 
445  if (getLangOpts().Sanitize.has(SanitizerKind::Address) &&
446  !isInNoSanitizeList(SanitizerKind::Address, Fn, Loc))
447  Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
448 
449  if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) &&
450  !isInNoSanitizeList(SanitizerKind::KernelAddress, Fn, Loc))
451  Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
452 
453  if (getLangOpts().Sanitize.has(SanitizerKind::HWAddress) &&
454  !isInNoSanitizeList(SanitizerKind::HWAddress, Fn, Loc))
455  Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
456 
457  if (getLangOpts().Sanitize.has(SanitizerKind::KernelHWAddress) &&
458  !isInNoSanitizeList(SanitizerKind::KernelHWAddress, Fn, Loc))
459  Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
460 
461  if (getLangOpts().Sanitize.has(SanitizerKind::MemtagStack) &&
462  !isInNoSanitizeList(SanitizerKind::MemtagStack, Fn, Loc))
463  Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
464 
465  if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
466  !isInNoSanitizeList(SanitizerKind::Thread, Fn, Loc))
467  Fn->addFnAttr(llvm::Attribute::SanitizeThread);
468 
469  if (getLangOpts().Sanitize.has(SanitizerKind::Memory) &&
470  !isInNoSanitizeList(SanitizerKind::Memory, Fn, Loc))
471  Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
472 
473  if (getLangOpts().Sanitize.has(SanitizerKind::KernelMemory) &&
474  !isInNoSanitizeList(SanitizerKind::KernelMemory, Fn, Loc))
475  Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
476 
477  if (getLangOpts().Sanitize.has(SanitizerKind::SafeStack) &&
478  !isInNoSanitizeList(SanitizerKind::SafeStack, Fn, Loc))
479  Fn->addFnAttr(llvm::Attribute::SafeStack);
480 
481  if (getLangOpts().Sanitize.has(SanitizerKind::ShadowCallStack) &&
482  !isInNoSanitizeList(SanitizerKind::ShadowCallStack, Fn, Loc))
483  Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
484 
485  return Fn;
486 }
487 
488 /// Create a global pointer to a function that will initialize a global
489 /// variable. The user has requested that this pointer be emitted in a specific
490 /// section.
491 void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
492  llvm::GlobalVariable *GV,
493  llvm::Function *InitFunc,
494  InitSegAttr *ISA) {
495  llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
496  TheModule, InitFunc->getType(), /*isConstant=*/true,
497  llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
498  PtrArray->setSection(ISA->getSection());
499  addUsedGlobal(PtrArray);
500 
501  // If the GV is already in a comdat group, then we have to join it.
502  if (llvm::Comdat *C = GV->getComdat())
503  PtrArray->setComdat(C);
504 }
505 
506 void
507 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
508  llvm::GlobalVariable *Addr,
509  bool PerformInit) {
510 
511  // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
512  // __constant__ and __shared__ variables defined in namespace scope,
513  // that are of class type, cannot have a non-empty constructor. All
514  // the checks have been done in Sema by now. Whatever initializers
515  // are allowed are empty and we just need to ignore them here.
516  if (getLangOpts().CUDAIsDevice && !getLangOpts().GPUAllowDeviceInit &&
517  (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
518  D->hasAttr<CUDASharedAttr>()))
519  return;
520 
521  if (getLangOpts().OpenMP &&
522  getOpenMPRuntime().emitDeclareTargetVarDefinition(D, Addr, PerformInit))
523  return;
524 
525  // Check if we've already initialized this decl.
526  auto I = DelayedCXXInitPosition.find(D);
527  if (I != DelayedCXXInitPosition.end() && I->second == ~0U)
528  return;
529 
530  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
531  SmallString<256> FnName;
532  {
533  llvm::raw_svector_ostream Out(FnName);
535  }
536 
537  // Create a variable initialization function.
538  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
539  FTy, FnName.str(), getTypes().arrangeNullaryFunction(), D->getLocation());
540 
541  auto *ISA = D->getAttr<InitSegAttr>();
543  PerformInit);
544 
545  llvm::GlobalVariable *COMDATKey =
546  supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr;
547 
548  if (D->getTLSKind()) {
549  // FIXME: Should we support init_priority for thread_local?
550  // FIXME: We only need to register one __cxa_thread_atexit function for the
551  // entire TU.
552  CXXThreadLocalInits.push_back(Fn);
553  CXXThreadLocalInitVars.push_back(D);
554  } else if (PerformInit && ISA) {
555  EmitPointerToInitFunc(D, Addr, Fn, ISA);
556  } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
557  OrderGlobalInitsOrStermFinalizers Key(IPA->getPriority(),
558  PrioritizedCXXGlobalInits.size());
559  PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
561  getContext().GetGVALinkageForVariable(D) == GVA_DiscardableODR ||
562  D->hasAttr<SelectAnyAttr>()) {
563  // C++ [basic.start.init]p2:
564  // Definitions of explicitly specialized class template static data
565  // members have ordered initialization. Other class template static data
566  // members (i.e., implicitly or explicitly instantiated specializations)
567  // have unordered initialization.
568  //
569  // As a consequence, we can put them into their own llvm.global_ctors entry.
570  //
571  // If the global is externally visible, put the initializer into a COMDAT
572  // group with the global being initialized. On most platforms, this is a
573  // minor startup time optimization. In the MS C++ ABI, there are no guard
574  // variables, so this COMDAT key is required for correctness.
575  //
576  // SelectAny globals will be comdat-folded. Put the initializer into a
577  // COMDAT group associated with the global, so the initializers get folded
578  // too.
579 
580  AddGlobalCtor(Fn, 65535, COMDATKey);
581  if (COMDATKey && (getTriple().isOSBinFormatELF() ||
582  getTarget().getCXXABI().isMicrosoft())) {
583  // When COMDAT is used on ELF or in the MS C++ ABI, the key must be in
584  // llvm.used to prevent linker GC.
585  addUsedGlobal(COMDATKey);
586  }
587 
588  // If we used a COMDAT key for the global ctor, the init function can be
589  // discarded if the global ctor entry is discarded.
590  // FIXME: Do we need to restrict this to ELF and Wasm?
591  llvm::Comdat *C = Addr->getComdat();
592  if (COMDATKey && C &&
593  (getTarget().getTriple().isOSBinFormatELF() ||
594  getTarget().getTriple().isOSBinFormatWasm())) {
595  Fn->setComdat(C);
596  }
597  } else {
598  I = DelayedCXXInitPosition.find(D); // Re-do lookup in case of re-hash.
599  if (I == DelayedCXXInitPosition.end()) {
600  CXXGlobalInits.push_back(Fn);
601  } else if (I->second != ~0U) {
602  assert(I->second < CXXGlobalInits.size() &&
603  CXXGlobalInits[I->second] == nullptr);
604  CXXGlobalInits[I->second] = Fn;
605  }
606  }
607 
608  // Remember that we already emitted the initializer for this global.
609  DelayedCXXInitPosition[D] = ~0U;
610 }
611 
612 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
614  *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
615 
616  CXXThreadLocalInits.clear();
617  CXXThreadLocalInitVars.clear();
618  CXXThreadLocals.clear();
619 }
620 
621 static SmallString<128> getTransformedFileName(llvm::Module &M) {
622  SmallString<128> FileName = llvm::sys::path::filename(M.getName());
623 
624  if (FileName.empty())
625  FileName = "<null>";
626 
627  for (size_t i = 0; i < FileName.size(); ++i) {
628  // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
629  // to be the set of C preprocessing numbers.
630  if (!isPreprocessingNumberBody(FileName[i]))
631  FileName[i] = '_';
632  }
633 
634  return FileName;
635 }
636 
637 static std::string getPrioritySuffix(unsigned int Priority) {
638  assert(Priority <= 65535 && "Priority should always be <= 65535.");
639 
640  // Compute the function suffix from priority. Prepend with zeroes to make
641  // sure the function names are also ordered as priorities.
642  std::string PrioritySuffix = llvm::utostr(Priority);
643  PrioritySuffix = std::string(6 - PrioritySuffix.size(), '0') + PrioritySuffix;
644 
645  return PrioritySuffix;
646 }
647 
648 void
649 CodeGenModule::EmitCXXGlobalInitFunc() {
650  while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
651  CXXGlobalInits.pop_back();
652 
653  if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
654  return;
655 
656  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
658 
659  // Create our global prioritized initialization function.
660  if (!PrioritizedCXXGlobalInits.empty()) {
661  SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
662  llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
663  PrioritizedCXXGlobalInits.end());
664  // Iterate over "chunks" of ctors with same priority and emit each chunk
665  // into separate function. Note - everything is sorted first by priority,
666  // second - by lex order, so we emit ctor functions in proper order.
668  I = PrioritizedCXXGlobalInits.begin(),
669  E = PrioritizedCXXGlobalInits.end(); I != E; ) {
671  PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
672 
673  LocalCXXGlobalInits.clear();
674 
675  unsigned int Priority = I->first.priority;
676  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
677  FTy, "_GLOBAL__I_" + getPrioritySuffix(Priority), FI);
678 
679  for (; I < PrioE; ++I)
680  LocalCXXGlobalInits.push_back(I->second);
681 
682  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
683  AddGlobalCtor(Fn, Priority);
684  }
685  PrioritizedCXXGlobalInits.clear();
686  }
687 
688  if (getCXXABI().useSinitAndSterm() && CXXGlobalInits.empty())
689  return;
690 
691  // Include the filename in the symbol name. Including "sub_" matches gcc
692  // and makes sure these symbols appear lexicographically behind the symbols
693  // with priority emitted above.
694  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
695  FTy, llvm::Twine("_GLOBAL__sub_I_", getTransformedFileName(getModule())),
696  FI);
697 
698  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
699  AddGlobalCtor(Fn);
700 
701  // In OpenCL global init functions must be converted to kernels in order to
702  // be able to launch them from the host.
703  // FIXME: Some more work might be needed to handle destructors correctly.
704  // Current initialization function makes use of function pointers callbacks.
705  // We can't support function pointers especially between host and device.
706  // However it seems global destruction has little meaning without any
707  // dynamic resource allocation on the device and program scope variables are
708  // destroyed by the runtime when program is released.
709  if (getLangOpts().OpenCL) {
711  Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
712  }
713 
714  assert(!getLangOpts().CUDA || !getLangOpts().CUDAIsDevice ||
715  getLangOpts().GPUAllowDeviceInit);
716  if (getLangOpts().HIP && getLangOpts().CUDAIsDevice) {
717  Fn->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
718  Fn->addFnAttr("device-init");
719  }
720 
721  CXXGlobalInits.clear();
722 }
723 
724 void CodeGenModule::EmitCXXGlobalCleanUpFunc() {
725  if (CXXGlobalDtorsOrStermFinalizers.empty() &&
726  PrioritizedCXXStermFinalizers.empty())
727  return;
728 
729  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
731 
732  // Create our global prioritized cleanup function.
733  if (!PrioritizedCXXStermFinalizers.empty()) {
734  SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8> LocalCXXStermFinalizers;
735  llvm::array_pod_sort(PrioritizedCXXStermFinalizers.begin(),
736  PrioritizedCXXStermFinalizers.end());
737  // Iterate over "chunks" of dtors with same priority and emit each chunk
738  // into separate function. Note - everything is sorted first by priority,
739  // second - by lex order, so we emit dtor functions in proper order.
741  I = PrioritizedCXXStermFinalizers.begin(),
742  E = PrioritizedCXXStermFinalizers.end();
743  I != E;) {
745  std::upper_bound(I + 1, E, *I, StermFinalizerPriorityCmp());
746 
747  LocalCXXStermFinalizers.clear();
748 
749  unsigned int Priority = I->first.priority;
750  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
751  FTy, "_GLOBAL__a_" + getPrioritySuffix(Priority), FI);
752 
753  for (; I < PrioE; ++I) {
754  llvm::FunctionCallee DtorFn = I->second;
755  LocalCXXStermFinalizers.emplace_back(DtorFn.getFunctionType(),
756  DtorFn.getCallee(), nullptr);
757  }
758 
760  Fn, LocalCXXStermFinalizers);
761  AddGlobalDtor(Fn, Priority);
762  }
763  PrioritizedCXXStermFinalizers.clear();
764  }
765 
766  if (CXXGlobalDtorsOrStermFinalizers.empty())
767  return;
768 
769  // Create our global cleanup function.
770  llvm::Function *Fn =
771  CreateGlobalInitOrCleanUpFunction(FTy, "_GLOBAL__D_a", FI);
772 
774  Fn, CXXGlobalDtorsOrStermFinalizers);
775  AddGlobalDtor(Fn);
776  CXXGlobalDtorsOrStermFinalizers.clear();
777 }
778 
779 /// Emit the code necessary to initialize the given global variable.
781  const VarDecl *D,
782  llvm::GlobalVariable *Addr,
783  bool PerformInit) {
784  // Check if we need to emit debug info for variable initializer.
785  if (D->hasAttr<NoDebugAttr>())
786  DebugInfo = nullptr; // disable debug info indefinitely for this function
787 
788  CurEHLocation = D->getBeginLoc();
789 
791  getContext().VoidTy, Fn, getTypes().arrangeNullaryFunction(),
792  FunctionArgList());
793  // Emit an artificial location for this function.
794  auto AL = ApplyDebugLocation::CreateArtificial(*this);
795 
796  // Use guarded initialization if the global variable is weak. This
797  // occurs for, e.g., instantiated static data members and
798  // definitions explicitly marked weak.
799  //
800  // Also use guarded initialization for a variable with dynamic TLS and
801  // unordered initialization. (If the initialization is ordered, the ABI
802  // layer will guard the whole-TU initialization for us.)
803  if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage() ||
804  (D->getTLSKind() == VarDecl::TLS_Dynamic &&
806  EmitCXXGuardedInit(*D, Addr, PerformInit);
807  } else {
808  EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
809  }
810 
811  FinishFunction();
812 }
813 
814 void
817  ConstantAddress Guard) {
818  {
819  auto NL = ApplyDebugLocation::CreateEmpty(*this);
821  getTypes().arrangeNullaryFunction(), FunctionArgList());
822  // Emit an artificial location for this function.
823  auto AL = ApplyDebugLocation::CreateArtificial(*this);
824 
825  llvm::BasicBlock *ExitBlock = nullptr;
826  if (Guard.isValid()) {
827  // If we have a guard variable, check whether we've already performed
828  // these initializations. This happens for TLS initialization functions.
829  llvm::Value *GuardVal = Builder.CreateLoad(Guard);
830  llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
831  "guard.uninitialized");
832  llvm::BasicBlock *InitBlock = createBasicBlock("init");
833  ExitBlock = createBasicBlock("exit");
834  EmitCXXGuardedInitBranch(Uninit, InitBlock, ExitBlock,
835  GuardKind::TlsGuard, nullptr);
836  EmitBlock(InitBlock);
837  // Mark as initialized before initializing anything else. If the
838  // initializers use previously-initialized thread_local vars, that's
839  // probably supposed to be OK, but the standard doesn't say.
840  Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
841 
842  // The guard variable can't ever change again.
844  Guard.getPointer(),
846  CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
847  }
848 
849  RunCleanupsScope Scope(*this);
850 
851  // When building in Objective-C++ ARC mode, create an autorelease pool
852  // around the global initializers.
853  if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
854  llvm::Value *token = EmitObjCAutoreleasePoolPush();
856  }
857 
858  for (unsigned i = 0, e = Decls.size(); i != e; ++i)
859  if (Decls[i])
860  EmitRuntimeCall(Decls[i]);
861 
862  Scope.ForceCleanup();
863 
864  if (ExitBlock) {
865  Builder.CreateBr(ExitBlock);
866  EmitBlock(ExitBlock);
867  }
868  }
869 
870  FinishFunction();
871 }
872 
874  llvm::Function *Fn,
875  ArrayRef<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
876  llvm::Constant *>>
877  DtorsOrStermFinalizers) {
878  {
879  auto NL = ApplyDebugLocation::CreateEmpty(*this);
881  getTypes().arrangeNullaryFunction(), FunctionArgList());
882  // Emit an artificial location for this function.
883  auto AL = ApplyDebugLocation::CreateArtificial(*this);
884 
885  // Emit the cleanups, in reverse order from construction.
886  for (unsigned i = 0, e = DtorsOrStermFinalizers.size(); i != e; ++i) {
887  llvm::FunctionType *CalleeTy;
888  llvm::Value *Callee;
889  llvm::Constant *Arg;
890  std::tie(CalleeTy, Callee, Arg) = DtorsOrStermFinalizers[e - i - 1];
891 
892  llvm::CallInst *CI = nullptr;
893  if (Arg == nullptr) {
894  assert(
896  "Arg could not be nullptr unless using sinit and sterm functions.");
897  CI = Builder.CreateCall(CalleeTy, Callee);
898  } else
899  CI = Builder.CreateCall(CalleeTy, Callee, Arg);
900 
901  // Make sure the call and the callee agree on calling convention.
902  if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
903  CI->setCallingConv(F->getCallingConv());
904  }
905  }
906 
907  FinishFunction();
908 }
909 
910 /// generateDestroyHelper - Generates a helper function which, when
911 /// invoked, destroys the given object. The address of the object
912 /// should be in global memory.
914  Address addr, QualType type, Destroyer *destroyer,
915  bool useEHCleanupForArray, const VarDecl *VD) {
916  FunctionArgList args;
919  args.push_back(&Dst);
920 
921  const CGFunctionInfo &FI =
923  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
924  llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction(
925  FTy, "__cxx_global_array_dtor", FI, VD->getLocation());
926 
927  CurEHLocation = VD->getBeginLoc();
928 
930  getContext().VoidTy, fn, FI, args);
931  // Emit an artificial location for this function.
932  auto AL = ApplyDebugLocation::CreateArtificial(*this);
933 
934  emitDestroy(addr, type, destroyer, useEHCleanupForArray);
935 
936  FinishFunction();
937 
938  return fn;
939 }
clang::CodeGen::CGCXXABI::canCallMismatchedFunctionType
virtual bool canCallMismatchedFunctionType() const
Returns true if the target allows calling a function through a pointer with a different signature tha...
Definition: CGCXXABI.h:135
clang::CodeGen::CodeGenModule::isTypeConstant
bool isTypeConstant(QualType QTy, bool ExcludeCtorDtor)
isTypeConstant - Determine whether an object of this type can be emitted as a constant.
Definition: CodeGenModule.cpp:4068
clang::InternalLinkage
@ InternalLinkage
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:31
clang::CodeGen::CodeGenFunction::ConvertTypeForMem
llvm::Type * ConvertTypeForMem(QualType T)
Definition: CodeGenFunction.cpp:208
clang::ASTContext::getTypeSizeInChars
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Definition: ASTContext.cpp:2462
clang::CodeGen::CodeGenFunction::EmitComplexExprIntoLValue
void EmitComplexExprIntoLValue(const Expr *E, LValue dest, bool isInit)
EmitComplexExprIntoLValue - Emit the given expression of complex type and place its result into the s...
Definition: CGExprComplex.cpp:1129
clang::QualType::DK_nontrivial_c_struct
@ DK_nontrivial_c_struct
Definition: Type.h:1215
clang::CodeGen::CodeGenFunction::EmitObjCAutoreleasePoolCleanup
void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr)
Definition: CGObjC.cpp:2844
clang::CodeGen::ConstantAddress::getElementBitCast
ConstantAddress getElementBitCast(llvm::Type *ElemTy) const
Definition: Address.h:156
clang::CodeGen::CodeGenTypeCache::Int8PtrTy
llvm::PointerType * Int8PtrTy
Definition: CodeGenTypeCache.h:57
clang::CodeGen::ConstantAddress
A specialization of Address that requires the address to be an LLVM Constant.
Definition: Address.h:140
clang::VarDecl::getTLSKind
TLSKind getTLSKind() const
Definition: Decl.cpp:2072
clang::CodeGen::CodeGenTypeCache::IntTy
llvm::IntegerType * IntTy
int
Definition: CodeGenTypeCache.h:42
clang::CodeGen::TEK_Aggregate
@ TEK_Aggregate
Definition: CodeGenFunction.h:109
clang::CodeGen::RValue
RValue - This trivial value class is used to represent the result of an expression that is evaluated.
Definition: CGValue.h:39
clang::CodeGen::CodeGenModule::CreateRuntimeFunction
llvm::FunctionCallee CreateRuntimeFunction(llvm::FunctionType *Ty, StringRef Name, llvm::AttributeList ExtraAttrs=llvm::AttributeList(), bool Local=false, bool AssumeConvergent=false)
Create or return a runtime function declaration with the specified type and name.
Definition: CodeGenModule.cpp:4025
clang::CodeGen::OrderGlobalInitsOrStermFinalizers
Definition: CodeGenModule.h:97
type
clang::DeclaratorDecl::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:778
EmitDeclDestroy
static void EmitDeclDestroy(CodeGenFunction &CGF, const VarDecl &D, ConstantAddress Addr)
Emit code to cause the destruction of the given variable with static storage duration.
Definition: CGDeclCXX.cpp:70
clang::CodeGen::CodeGenFunction::EmitReferenceBindingToExpr
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition: CGExpr.cpp:615
CodeGenFunction.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::CodeGen::CodeGenFunction::EmitCXXGuardedInitBranch
void EmitCXXGuardedInitBranch(llvm::Value *NeedsInit, llvm::BasicBlock *InitBlock, llvm::BasicBlock *NoInitBlock, GuardKind Kind, const VarDecl *D)
Emit a branch to select whether or not to perform guarded initialization.
Definition: CGDeclCXX.cpp:389
clang::Decl::hasAttr
bool hasAttr() const
Definition: DeclBase.h:542
clang::VarDecl::hasGlobalStorage
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1141
clang::CodeGen::CGCXXABI::HasThisReturn
virtual bool HasThisReturn(GlobalDecl GD) const
Returns true if the given constructor or destructor is one of the kinds that the ABI says returns 'th...
Definition: CGCXXABI.h:121
EmitDeclInit
static void EmitDeclInit(CodeGenFunction &CGF, const VarDecl &D, ConstantAddress DeclPtr)
Definition: CGDeclCXX.cpp:28
clang::CodeGen::CodeGenModule::addUsedGlobal
void addUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.used metadata.
Definition: CodeGenModule.cpp:2340
clang::CodeGen::AggValueSlot::DoesNotNeedGCBarriers
@ DoesNotNeedGCBarriers
Definition: CGValue.h:554
getPrioritySuffix
static std::string getPrioritySuffix(unsigned int Priority)
Definition: CGDeclCXX.cpp:637
clang::CodeGen::CodeGenFunction::EmitNounwindRuntimeCall
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
clang::NamedDecl::isExternallyVisible
bool isExternallyVisible() const
Definition: Decl.h:405
clang::ASTContext::VoidTy
CanQualType VoidTy
Definition: ASTContext.h:1096
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::CodeGen::CodeGenModule::SetInternalFunctionAttributes
void SetInternalFunctionAttributes(GlobalDecl GD, llvm::Function *F, const CGFunctionInfo &FI)
Set the attributes on the LLVM function for the given decl and function info.
Definition: CodeGenModule.cpp:2193
clang::QualType::getQualifiers
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6527
clang::CodeGen::CGCXXABI::getMangleContext
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:111
clang::CodeGen::CodeGenFunction::GenerateCXXGlobalInitFunc
void GenerateCXXGlobalInitFunc(llvm::Function *Fn, ArrayRef< llvm::Function * > CXXThreadLocals, ConstantAddress Guard=ConstantAddress::invalid())
GenerateCXXGlobalInitFunc - Generates code for initializing global variables.
Definition: CGDeclCXX.cpp:815
clang::CodeGen::CGCXXABI::EmitGuardedInit
virtual void EmitGuardedInit(CodeGenFunction &CGF, const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)=0
Emits the guarded initializer and destructor setup for the given variable, given that it couldn't be ...
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:675
clang::ASTContext::getTargetAddressSpace
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.cpp:11998
Attr.h
clang::DynamicInitKind::AtExit
@ AtExit
clang::GVA_DiscardableODR
@ GVA_DiscardableODR
Definition: Linkage.h:76
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:55
clang::DynamicInitKind::Initializer
@ Initializer
clang::MangleContext::mangleDynamicAtExitDestructor
virtual void mangleDynamicAtExitDestructor(const VarDecl *D, raw_ostream &)=0
clang::CodeGen::CodeGenModule::getContext
ASTContext & getContext() const
Definition: CodeGenModule.h:697
clang::CodeGen::RequiredArgs::All
@ All
Definition: CGFunctionInfo.h:485
clang::CodeGen::Address::isValid
bool isValid() const
Definition: Address.h:91
getTransformedFileName
static SmallString< 128 > getTransformedFileName(llvm::Module &M)
Definition: CGDeclCXX.cpp:621
clang::CodeGen::CodeGenFunction::unregisterGlobalDtorWithUnAtExit
llvm::Value * unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub)
Call unatexit() with function dtorStub.
Definition: CGDeclCXX.cpp:350
clang::CodeGen::CodeGenFunction::createBasicBlock
llvm::BasicBlock * createBasicBlock(const Twine &name="", llvm::Function *parent=nullptr, llvm::BasicBlock *before=nullptr)
createBasicBlock - Create an LLVM basic block.
Definition: CodeGenFunction.h:2430
clang::CodeGen::CGBuilderTy::CreateStore
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:97
CGObjCRuntime.h
TargetInfo.h
clang::CodeGen::CodeGenTypes::arrangeBuiltinFunctionDeclaration
const CGFunctionInfo & arrangeBuiltinFunctionDeclaration(QualType resultType, const FunctionArgList &args)
A builtin function is a freestanding function using the default C conventions.
Definition: CGCall.cpp:666
clang::CodeGen::CodeGenModule::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenModule.h:698
clang::CodeGen::CGCXXABI::registerGlobalDtor
virtual void registerGlobalDtor(CodeGenFunction &CGF, const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr)=0
Emit code to force the execution of a destructor during global teardown.
clang::CodeGen::CodeGenModule::CreateGlobalInitOrCleanUpFunction
llvm::Function * CreateGlobalInitOrCleanUpFunction(llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, SourceLocation Loc=SourceLocation(), bool TLS=false, llvm::GlobalVariable::LinkageTypes Linkage=llvm::GlobalVariable::InternalLinkage)
Definition: CGDeclCXX.cpp:426
clang::Decl::getAttr
T * getAttr() const
Definition: DeclBase.h:538
clang::CodeGen::CodeGenFunction::getDestroyer
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:2091
clang::CodeGen::LValue::isObjCWeak
bool isObjCWeak() const
Definition: CGValue.h:308
clang::CodeGen::CodeGenFunction::EmitStoreOfScalar
void EmitStoreOfScalar(llvm::Value *Value, Address Addr, bool Volatile, QualType Ty, AlignmentSource Source=AlignmentSource::Type, bool isInit=false, bool isNontemporal=false)
EmitStoreOfScalar - Store a scalar value to an address, taking care to appropriately convert from the...
Definition: CodeGenFunction.h:3848
clang::CodeGen::CodeGenModule::getTargetCodeGenInfo
const TargetCodeGenInfo & getTargetCodeGenInfo()
Definition: TargetInfo.cpp:11336
clang::CodeGen::CodeGenFunction::Builder
CGBuilderTy Builder
Definition: CodeGenFunction.h:269
EmitDeclInvariant
static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D, llvm::Constant *Addr)
Emit code to cause the variable at the given address to be considered as constant from this point onw...
Definition: CGDeclCXX.cpp:151
clang::isPreprocessingNumberBody
LLVM_READONLY bool isPreprocessingNumberBody(unsigned char c)
Return true if this is the body character of a C preprocessing number, which is [a-zA-Z0-9_.
Definition: CharInfo.h:153
clang::CodeGen::CodeGenTypes::ConvertType
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Definition: CodeGenTypes.cpp:401
clang::Dtor_Complete
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1500
clang::CodeGen::CodeGenTypeCache::VoidPtrTy
llvm::PointerType * VoidPtrTy
Definition: CodeGenTypeCache.h:56
clang::CodeGen::CodeGenModule::getOpenMPRuntime
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
Definition: CodeGenModule.h:605
clang::GlobalDecl
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
clang::Qualifiers::getAddressSpace
LangAS getAddressSpace() const
Definition: Type.h:363
U
clang::CodeGen::CodeGenFunction::EmitObjCAutoreleasePoolPush
llvm::Value * EmitObjCAutoreleasePoolPush()
Produce the code to do a objc_autoreleasepool_push.
Definition: CGObjC.cpp:2652
clang::QualType::DK_cxx_destructor
@ DK_cxx_destructor
Definition: Type.h:1212
clang::CodeGen::TargetCodeGenInfo::getAddrSpaceOfCxaAtexitPtrParam
virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const
Get address space of pointer parameter for __cxa_atexit.
Definition: TargetInfo.h:293
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6760
clang::CodeGen::CodeGenFunction::RunCleanupsScope
Enters a new scope for capturing cleanups, all of which will be executed once the scope is exited.
Definition: CodeGenFunction.h:872
clang::CodeGen::CodeGenModule::GenOpenCLArgMetadata
void GenOpenCLArgMetadata(llvm::Function *FN, const FunctionDecl *FD=nullptr, CodeGenFunction *CGF=nullptr)
OpenCL v1.2 s5.6.4.6 allows the compiler to store kernel argument information in the program executab...
Definition: CodeGenModule.cpp:1700
clang::CodeGen::CodeGenFunction::GuardKind::VariableGuard
@ VariableGuard
clang::Linkage
Linkage
Describes the different kinds of linkage (C++ [basic.link], C99 6.2.2) that an entity may have.
Definition: Linkage.h:23
clang::CodeGen::CodeGenFunction::emitDestroy
void emitDestroy(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
emitDestroy - Immediately perform the destruction of the given object.
Definition: CGDecl.cpp:2192
clang::CodeGen::CodeGenModule::getObjCRuntime
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
Definition: CodeGenModule.h:588
clang::CodeGen::CodeGenTypes::arrangeLLVMFunctionInfo
const CGFunctionInfo & arrangeLLVMFunctionInfo(CanQualType returnType, bool instanceMethod, bool chainCall, ArrayRef< CanQualType > argTypes, FunctionType::ExtInfo info, ArrayRef< FunctionProtoType::ExtParameterInfo > paramInfos, RequiredArgs args)
"Arrange" the LLVM information for a call or type with the given signature.
Definition: CGCall.cpp:750
clang::Scope
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
clang::CodeGen::CodeGenFunction::generateDestroyHelper
llvm::Function * generateDestroyHelper(Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray, const VarDecl *VD)
generateDestroyHelper - Generates a helper function which, when invoked, destroys the given object.
Definition: CGDeclCXX.cpp:913
clang::CharUnits::fromQuantity
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Definition: CharUnits.h:63
clang::CodeGen::CodeGenTypeCache::Int64Ty
llvm::IntegerType * Int64Ty
Definition: CodeGenTypeCache.h:37
CGOpenMPRuntime.h
clang::CodeGen::CodeGenModule::getCodeGenOpts
const CodeGenOptions & getCodeGenOpts() const
Definition: CodeGenModule.h:703
clang::CPlusPlus
@ CPlusPlus
Definition: LangStandard.h:53
LangOptions.h
clang::CodeGen::CodeGenFunction::EmitRuntimeCall
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
clang::CodeGen::TEK_Complex
@ TEK_Complex
Definition: CodeGenFunction.h:108
clang::QualType::DK_none
@ DK_none
Definition: Type.h:1211
clang::CodeGen::CodeGenTypeCache::VoidTy
llvm::Type * VoidTy
void
Definition: CodeGenTypeCache.h:34
clang::CodeGen::CodeGenFunction::EmitInvariantStart
void EmitInvariantStart(llvm::Constant *Addr, CharUnits Size)
Definition: CGDeclCXX.cpp:157
clang::CodeGen::CodeGenFunction::GenerateCXXGlobalVarDeclInitFunc
void GenerateCXXGlobalVarDeclInitFunc(llvm::Function *Fn, const VarDecl *D, llvm::GlobalVariable *Addr, bool PerformInit)
Emit the code necessary to initialize the given global variable.
Definition: CGDeclCXX.cpp:780
clang::CodeGen::CodeGenModule::getModule
llvm::Module & getModule() const
Definition: CodeGenModule.h:704
clang::CXXDestructorDecl
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2688
clang::ImplicitParamDecl
Definition: Decl.h:1613
llvm::SmallString
Definition: LLVM.h:37
clang::CodeGen::CodeGenFunction::needsEHCleanup
bool needsEHCleanup(QualType::DestructionKind kind)
Determines whether an EH cleanup is required to destroy a type with the given destruction kind.
Definition: CodeGenFunction.h:2075
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:874
clang::CodeGen::CodeGenFunction::registerGlobalDtorWithAtExit
void registerGlobalDtorWithAtExit(const VarDecl &D, llvm::FunctionCallee fn, llvm::Constant *addr)
Call atexit() with a function that passes the given argument to the given function.
Definition: CGDeclCXX.cpp:321
clang::CodeGen::LValue::isObjCStrong
bool isObjCStrong() const
Definition: CGValue.h:311
clang::CodeGen::CodeGenModule::getTypes
CodeGenTypes & getTypes()
Definition: CodeGenModule.h:721
clang::Type::getAsCXXRecordDecl
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition: Type.cpp:1759
clang::VarDecl::isLocalVarDecl
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1168
clang::CodeGen::AggValueSlot::IsDestructed
@ IsDestructed
Definition: CGValue.h:551
clang::CodeGen::CodeGenFunction::getContext
ASTContext & getContext() const
Definition: CodeGenFunction.h:1982
clang::CodeGen::CodeGenFunction::FinishFunction
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
Definition: CodeGenFunction.cpp:325
clang::CodeGen::Address
An aligned address.
Definition: Address.h:74
clang::CodeGen::CodeGenFunction::getEvaluationKind
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
Definition: CodeGenFunction.cpp:216
clang::CodeGen::CodeGenModule::getAddrAndTypeOfCXXStructor
llvm::FunctionCallee getAddrAndTypeOfCXXStructor(GlobalDecl GD, const CGFunctionInfo *FnInfo=nullptr, llvm::FunctionType *FnType=nullptr, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Definition: CGCXX.cpp:221
clang::DynamicInitKind::GlobalArrayDestructor
@ GlobalArrayDestructor
clang::CodeGen::CodeGenModule::getTarget
const TargetInfo & getTarget() const
Definition: CodeGenModule.h:709
clang::CodeGen::CodeGenTypes::GetFunctionType
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1613
clang::CodeGen::CodeGenModule::getDataLayout
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenModule.h:706
clang::CodeGen::CodeGenFunction::StartFunction
void StartFunction(GlobalDecl GD, QualType RetTy, llvm::Function *Fn, const CGFunctionInfo &FnInfo, const FunctionArgList &Args, SourceLocation Loc=SourceLocation(), SourceLocation StartLoc=SourceLocation())
Emit code for the start of a function.
Definition: CodeGenFunction.cpp:709
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
clang::CodeGen::LValue
LValue - This represents an lvalue references.
Definition: CGValue.h:171
clang::CodeGen::CodeGenFunction::EmitCXXGuardedInit
void EmitCXXGuardedInit(const VarDecl &D, llvm::GlobalVariable *DeclPtr, bool PerformInit)
Emit code in this function to perform a guarded variable initialization.
Definition: CGDeclCXX.cpp:375
clang::CodeGen::CodeGenFunction::createAtExitStub
llvm::Function * createAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr)
Create a stub function, suitable for being passed to atexit, which passes the given address to the gi...
Definition: CGDeclCXX.cpp:233
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:231
clang::ASTContext::IntTy
CanQualType IntTy
Definition: ASTContext.h:1105
clang::CodeGen::CGObjCRuntime::EmitObjCWeakAssign
virtual void EmitObjCWeakAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest)=0
clang::CodeGen::CodeGenModule::getCXXABI
CGCXXABI & getCXXABI() const
Definition: CodeGenModule.h:714
clang::CodeGen::AggValueSlot::IsNotAliased
@ IsNotAliased
Definition: CGValue.h:550
clang::Expr::getExprLoc
SourceLocation getExprLoc() const LLVM_READONLY
getExprLoc - Return the preferred location for the arrow when diagnosing a problem with a generic exp...
Definition: Expr.cpp:247
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:275
clang::QualType::DK_objc_strong_lifetime
@ DK_objc_strong_lifetime
Definition: Type.h:1213
llvm::ArrayRef
Definition: LLVM.h:34
clang::CodeGen::CodeGenFunction::EmitScalarInit
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:774
clang::CodeGen::CodeGenModule::getTriple
const llvm::Triple & getTriple() const
Definition: CodeGenModule.h:710
clang::CodeGen::CodeGenFunction::EmitAggExpr
void EmitAggExpr(const Expr *E, AggValueSlot AS)
EmitAggExpr - Emit the computation of the specified expression of aggregate type.
Definition: CGExprAgg.cpp:1993
clang::CodeGen::AggValueSlot::forLValue
static AggValueSlot forLValue(const LValue &LV, CodeGenFunction &CGF, IsDestructed_t isDestructed, NeedsGCBarriers_t needsGC, IsAliased_t isAliased, Overlap_t mayOverlap, IsZeroed_t isZeroed=IsNotZeroed, IsSanitizerChecked_t isChecked=IsNotSanitizerChecked)
Definition: CGValue.h:587
clang::CodeGen::CodeGenTypeCache::getRuntimeCC
llvm::CallingConv::ID getRuntimeCC() const
Definition: CodeGenTypeCache.h:122
clang::CodeGen::FunctionArgList
FunctionArgList - Type for representing both the decl and type of parameters to a function.
Definition: CGCall.h:356
clang::VarDecl::getTemplateSpecializationKind
TemplateSpecializationKind getTemplateSpecializationKind() const
If this variable is an instantiation of a variable template or a static data member of a class templa...
Definition: Decl.cpp:2651
clang::QualType::DestructionKind
DestructionKind
Definition: Type.h:1210
clang::CodeGen::CodeGenFunction::getTypes
CodeGenTypes & getTypes() const
Definition: CodeGenFunction.h:1981
clang::VarDecl::getInit
const Expr * getInit() const
Definition: Decl.h:1283
clang::QualType::DK_objc_weak_lifetime
@ DK_objc_weak_lifetime
Definition: Type.h:1214
clang::FunctionType::ExtInfo
A class which abstracts out some details necessary for making a call.
Definition: Type.h:3670
clang::CodeGen::CodeGenFunction::MakeAddrLValue
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
Definition: CodeGenFunction.h:2492
clang::CXXRecordDecl::hasTrivialDestructor
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1321
Priority
int Priority
Definition: Format.cpp:2555
clang::CodeGen::CodeGenFunction::EmitScalarExpr
llvm::Value * EmitScalarExpr(const Expr *E, bool IgnoreResultAssign=false)
EmitScalarExpr - Emit the computation of the specified expression of LLVM scalar type,...
Definition: CGExprScalar.cpp:4869
clang::CXXRecordDecl::getDestructor
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1888
clang::CodeGen::CodeGenModule::supportsCOMDAT
bool supportsCOMDAT() const
Definition: TargetInfo.cpp:11332
clang::MangleContext::mangleDynamicInitializer
virtual void mangleDynamicInitializer(const VarDecl *D, raw_ostream &)=0
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::CodeGen::ConstantAddress::getPointer
llvm::Constant * getPointer() const
Definition: Address.h:152
clang::CodeGen::CGFunctionInfo
CGFunctionInfo - Class to encapsulate the information about a function definition.
Definition: CGFunctionInfo.h:546
CGCXXABI.h
clang::CodeGen::CodeGenFunction::EmitCXXGlobalVarDeclInit
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::GlobalVariable *GV, bool PerformInit)
EmitCXXGlobalVarDeclInit - Create the initializer for a C++ variable with global storage.
Definition: CGDeclCXX.cpp:175
clang::CodeGen::CodeGenFunction::createTLSAtExitStub
llvm::Function * createTLSAtExitStub(const VarDecl &VD, llvm::FunctionCallee Dtor, llvm::Constant *Addr, llvm::FunctionCallee &AtExit)
Create a stub function, suitable for being passed to __pt_atexit_np, which passes the given address t...
Definition: CGDeclCXX.cpp:270
clang::CodeGen::CodeGenFunction::CGM
CodeGenModule & CGM
Definition: CodeGenFunction.h:261
clang::CodeGen::TEK_Scalar
@ TEK_Scalar
Definition: CodeGenFunction.h:107
clang::VarDecl::TLS_Dynamic
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition: Decl.h:897
clang::Builtin::ID
ID
Definition: Builtins.h:51
clang
Definition: CalledOnceCheck.h:17
clang::CodeGen::ApplyDebugLocation::CreateArtificial
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:830
clang::CodeGen::CodeGenFunction::GenerateCXXGlobalCleanUpFunc
void GenerateCXXGlobalCleanUpFunc(llvm::Function *Fn, ArrayRef< std::tuple< llvm::FunctionType *, llvm::WeakTrackingVH, llvm::Constant * >> DtorsOrStermFinalizers)
GenerateCXXGlobalCleanUpFunc - Generates code for cleaning up global variables.
Definition: CGDeclCXX.cpp:873
clang::CodeGen::CodeGenFunction::GuardKind
GuardKind
Definition: CodeGenFunction.h:4456
clang::CodeGen::RValue::getScalarVal
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:61
clang::CodeGen::ApplyDebugLocation::CreateEmpty
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
Definition: CGDebugInfo.h:847
clang::CodeGen::CGObjCRuntime::EmitObjCGlobalAssign
virtual void EmitObjCGlobalAssign(CodeGen::CodeGenFunction &CGF, llvm::Value *src, Address dest, bool threadlocal=false)=0
clang::CodeGen::CodeGenFunction::GuardKind::TlsGuard
@ TlsGuard
clang::CodeGen::CGCXXABI::useSinitAndSterm
virtual bool useSinitAndSterm() const
Definition: CGCXXABI.h:125
clang::VarDecl::needsDestruction
QualType::DestructionKind needsDestruction(const ASTContext &Ctx) const
Would the destruction of this variable have any effect, and if so, what kind?
Definition: Decl.cpp:2715
clang::CharUnits
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
clang::isTemplateInstantiation
bool isTemplateInstantiation(TemplateSpecializationKind Kind)
Determine whether this template specialization kind refers to an instantiation of an entity (as oppos...
Definition: Specifiers.h:197
clang::CodeGen::CGOpenMPRuntime::emitThreadPrivateVarDefinition
virtual llvm::Function * emitThreadPrivateVarDefinition(const VarDecl *VD, Address VDAddr, SourceLocation Loc, bool PerformInit, CodeGenFunction *CGF=nullptr)
Emit a code for initialization of threadprivate variable.
Definition: CGOpenMPRuntime.cpp:1716
llvm::SmallVectorImpl
Definition: Randstruct.h:18
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:685
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::VarDecl::hasLocalStorage
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1099
clang::CodeGen::CodeGenModule::getIntrinsic
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
Definition: CodeGenModule.cpp:5299
clang::CodeGen::CGCXXABI::EmitThreadLocalInitFuncs
virtual void EmitThreadLocalInitFuncs(CodeGenModule &CGM, ArrayRef< const VarDecl * > CXXThreadLocals, ArrayRef< llvm::Function * > CXXThreadLocalInits, ArrayRef< const VarDecl * > CXXThreadLocalInitVars)=0
Emits ABI-required functions necessary to initialize thread_local variables in this translation unit.
clang::Decl::getLocation
SourceLocation getLocation() const
Definition: DeclBase.h:425
clang::CodeGen::CodeGenFunction::EmitBlock
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:544
clang::CodeGen::CodeGenModule::Error
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
Definition: CodeGenModule.cpp:1052
clang::CodeGen::CodeGenModule::getLLVMContext
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenModule.h:715
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:786
clang::CodeGen::CodeGenFunction::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenFunction.h:1995
clang::CodeGen::CodeGenTypes::arrangeNullaryFunction
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition: CGCall.cpp:708
clang::ImplicitParamDecl::Other
@ Other
Other implicit parameter.
Definition: Decl.h:1640
clang::CodeGen::CGBuilderTy::CreateLoad
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:69
clang::CodeGen::CodeGenFunction::ReturnValue
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
Definition: CodeGenFunction.h:356
clang::CodeGen::AggValueSlot::DoesNotOverlap
@ DoesNotOverlap
Definition: CGValue.h:553
clang::CodeGen::CodeGenModule::isInNoSanitizeList
bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, SourceLocation Loc) const
Definition: CodeGenModule.cpp:2745