clang  14.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  Func = CodeGenFunction(CGM)
140  .generateDestroyHelper(Addr, Type, CGF.getDestroyer(DtorKind),
141  CGF.needsEHCleanup(DtorKind), &D);
142  Argument = llvm::Constant::getNullValue(CGF.Int8PtrTy);
143  }
144 
145  CGM.getCXXABI().registerGlobalDtor(CGF, D, Func, Argument);
146 }
147 
148 /// Emit code to cause the variable at the given address to be considered as
149 /// constant from this point onwards.
150 static void EmitDeclInvariant(CodeGenFunction &CGF, const VarDecl &D,
151  llvm::Constant *Addr) {
152  return CGF.EmitInvariantStart(
153  Addr, CGF.getContext().getTypeSizeInChars(D.getType()));
154 }
155 
156 void CodeGenFunction::EmitInvariantStart(llvm::Constant *Addr, CharUnits Size) {
157  // Do not emit the intrinsic if we're not optimizing.
158  if (!CGM.getCodeGenOpts().OptimizationLevel)
159  return;
160 
161  // Grab the llvm.invariant.start intrinsic.
162  llvm::Intrinsic::ID InvStartID = llvm::Intrinsic::invariant_start;
163  // Overloaded address space type.
164  llvm::Type *ObjectPtr[1] = {Int8PtrTy};
165  llvm::Function *InvariantStart = CGM.getIntrinsic(InvStartID, ObjectPtr);
166 
167  // Emit a call with the size in bytes of the object.
168  uint64_t Width = Size.getQuantity();
169  llvm::Value *Args[2] = { llvm::ConstantInt::getSigned(Int64Ty, Width),
170  llvm::ConstantExpr::getBitCast(Addr, Int8PtrTy)};
171  Builder.CreateCall(InvariantStart, Args);
172 }
173 
175  llvm::Constant *DeclPtr,
176  bool PerformInit) {
177 
178  const Expr *Init = D.getInit();
179  QualType T = D.getType();
180 
181  // The address space of a static local variable (DeclPtr) may be different
182  // from the address space of the "this" argument of the constructor. In that
183  // case, we need an addrspacecast before calling the constructor.
184  //
185  // struct StructWithCtor {
186  // __device__ StructWithCtor() {...}
187  // };
188  // __device__ void foo() {
189  // __shared__ StructWithCtor s;
190  // ...
191  // }
192  //
193  // For example, in the above CUDA code, the static local variable s has a
194  // "shared" address space qualifier, but the constructor of StructWithCtor
195  // expects "this" in the "generic" address space.
196  unsigned ExpectedAddrSpace = getContext().getTargetAddressSpace(T);
197  unsigned ActualAddrSpace = DeclPtr->getType()->getPointerAddressSpace();
198  if (ActualAddrSpace != ExpectedAddrSpace) {
199  llvm::Type *LTy = CGM.getTypes().ConvertTypeForMem(T);
200  llvm::PointerType *PTy = llvm::PointerType::get(LTy, ExpectedAddrSpace);
201  DeclPtr = llvm::ConstantExpr::getAddrSpaceCast(DeclPtr, PTy);
202  }
203 
204  ConstantAddress DeclAddr(DeclPtr, getContext().getDeclAlign(&D));
205 
206  if (!T->isReferenceType()) {
207  if (getLangOpts().OpenMP && !getLangOpts().OpenMPSimd &&
208  D.hasAttr<OMPThreadPrivateDeclAttr>()) {
210  &D, DeclAddr, D.getAttr<OMPThreadPrivateDeclAttr>()->getLocation(),
211  PerformInit, this);
212  }
213  if (PerformInit)
214  EmitDeclInit(*this, D, DeclAddr);
215  if (CGM.isTypeConstant(D.getType(), true))
216  EmitDeclInvariant(*this, D, DeclPtr);
217  else
218  EmitDeclDestroy(*this, D, DeclAddr);
219  return;
220  }
221 
222  assert(PerformInit && "cannot have constant initializer which needs "
223  "destruction for reference");
225  EmitStoreOfScalar(RV.getScalarVal(), DeclAddr, false, T);
226 }
227 
228 /// Create a stub function, suitable for being passed to atexit,
229 /// which passes the given address to the given destructor function.
230 llvm::Function *CodeGenFunction::createAtExitStub(const VarDecl &VD,
231  llvm::FunctionCallee dtor,
232  llvm::Constant *addr) {
233  // Get the destructor function type, void(*)(void).
234  llvm::FunctionType *ty = llvm::FunctionType::get(CGM.VoidTy, false);
235  SmallString<256> FnName;
236  {
237  llvm::raw_svector_ostream Out(FnName);
239  }
240 
242  llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction(
243  ty, FnName.str(), FI, VD.getLocation());
244 
245  CodeGenFunction CGF(CGM);
246 
248  CGM.getContext().VoidTy, fn, FI, FunctionArgList(),
249  VD.getLocation(), VD.getInit()->getExprLoc());
250  // Emit an artificial location for this function.
252 
253  llvm::CallInst *call = CGF.Builder.CreateCall(dtor, addr);
254 
255  // Make sure the call and the callee agree on calling convention.
256  if (auto *dtorFn = dyn_cast<llvm::Function>(
257  dtor.getCallee()->stripPointerCastsAndAliases()))
258  call->setCallingConv(dtorFn->getCallingConv());
259 
260  CGF.FinishFunction();
261 
262  return fn;
263 }
264 
265 /// Create a stub function, suitable for being passed to __pt_atexit_np,
266 /// which passes the given address to the given destructor function.
268  const VarDecl &D, llvm::FunctionCallee Dtor, llvm::Constant *Addr,
269  llvm::FunctionCallee &AtExit) {
270  SmallString<256> FnName;
271  {
272  llvm::raw_svector_ostream Out(FnName);
274  }
275 
277  getContext().IntTy, /*instanceMethod=*/false, /*chainCall=*/false,
279 
280  // Get the stub function type, int(*)(int,...).
281  llvm::FunctionType *StubTy =
282  llvm::FunctionType::get(CGM.IntTy, {CGM.IntTy}, true);
283 
284  llvm::Function *DtorStub = CGM.CreateGlobalInitOrCleanUpFunction(
285  StubTy, FnName.str(), FI, D.getLocation());
286 
287  CodeGenFunction CGF(CGM);
288 
289  FunctionArgList Args;
292  Args.push_back(&IPD);
293  QualType ResTy = CGM.getContext().IntTy;
294 
295  CGF.StartFunction(GlobalDecl(&D, DynamicInitKind::AtExit), ResTy, DtorStub,
296  FI, Args, D.getLocation(), D.getInit()->getExprLoc());
297 
298  // Emit an artificial location for this function.
300 
301  llvm::CallInst *call = CGF.Builder.CreateCall(Dtor, Addr);
302 
303  // Make sure the call and the callee agree on calling convention.
304  if (auto *DtorFn = dyn_cast<llvm::Function>(
305  Dtor.getCallee()->stripPointerCastsAndAliases()))
306  call->setCallingConv(DtorFn->getCallingConv());
307 
308  // Return 0 from function
309  CGF.Builder.CreateStore(llvm::Constant::getNullValue(CGM.IntTy),
310  CGF.ReturnValue);
311 
312  CGF.FinishFunction();
313 
314  return DtorStub;
315 }
316 
317 /// Register a global destructor using the C atexit runtime function.
319  llvm::FunctionCallee dtor,
320  llvm::Constant *addr) {
321  // Create a function which calls the destructor.
322  llvm::Constant *dtorStub = createAtExitStub(VD, dtor, addr);
324 }
325 
326 void CodeGenFunction::registerGlobalDtorWithAtExit(llvm::Constant *dtorStub) {
327  // extern "C" int atexit(void (*f)(void));
328  assert(dtorStub->getType() ==
329  llvm::PointerType::get(
330  llvm::FunctionType::get(CGM.VoidTy, false),
331  dtorStub->getType()->getPointerAddressSpace()) &&
332  "Argument to atexit has a wrong type.");
333 
334  llvm::FunctionType *atexitTy =
335  llvm::FunctionType::get(IntTy, dtorStub->getType(), false);
336 
337  llvm::FunctionCallee atexit =
338  CGM.CreateRuntimeFunction(atexitTy, "atexit", llvm::AttributeList(),
339  /*Local=*/true);
340  if (llvm::Function *atexitFn = dyn_cast<llvm::Function>(atexit.getCallee()))
341  atexitFn->setDoesNotThrow();
342 
343  EmitNounwindRuntimeCall(atexit, dtorStub);
344 }
345 
346 llvm::Value *
348  // The unatexit subroutine unregisters __dtor functions that were previously
349  // registered by the atexit subroutine. If the referenced function is found,
350  // it is removed from the list of functions that are called at normal program
351  // termination and the unatexit returns a value of 0, otherwise a non-zero
352  // value is returned.
353  //
354  // extern "C" int unatexit(void (*f)(void));
355  assert(dtorStub->getType() ==
356  llvm::PointerType::get(
357  llvm::FunctionType::get(CGM.VoidTy, false),
358  dtorStub->getType()->getPointerAddressSpace()) &&
359  "Argument to unatexit has a wrong type.");
360 
361  llvm::FunctionType *unatexitTy =
362  llvm::FunctionType::get(IntTy, {dtorStub->getType()}, /*isVarArg=*/false);
363 
364  llvm::FunctionCallee unatexit =
365  CGM.CreateRuntimeFunction(unatexitTy, "unatexit", llvm::AttributeList());
366 
367  cast<llvm::Function>(unatexit.getCallee())->setDoesNotThrow();
368 
369  return EmitNounwindRuntimeCall(unatexit, dtorStub);
370 }
371 
373  llvm::GlobalVariable *DeclPtr,
374  bool PerformInit) {
375  // If we've been asked to forbid guard variables, emit an error now.
376  // This diagnostic is hard-coded for Darwin's use case; we can find
377  // better phrasing if someone else needs it.
378  if (CGM.getCodeGenOpts().ForbidGuardVariables)
379  CGM.Error(D.getLocation(),
380  "this initialization requires a guard variable, which "
381  "the kernel does not support");
382 
383  CGM.getCXXABI().EmitGuardedInit(*this, D, DeclPtr, PerformInit);
384 }
385 
387  llvm::BasicBlock *InitBlock,
388  llvm::BasicBlock *NoInitBlock,
389  GuardKind Kind,
390  const VarDecl *D) {
391  assert((Kind == GuardKind::TlsGuard || D) && "no guarded variable");
392 
393  // A guess at how many times we will enter the initialization of a
394  // variable, depending on the kind of variable.
395  static const uint64_t InitsPerTLSVar = 1024;
396  static const uint64_t InitsPerLocalVar = 1024 * 1024;
397 
398  llvm::MDNode *Weights;
399  if (Kind == GuardKind::VariableGuard && !D->isLocalVarDecl()) {
400  // For non-local variables, don't apply any weighting for now. Due to our
401  // use of COMDATs, we expect there to be at most one initialization of the
402  // variable per DSO, but we have no way to know how many DSOs will try to
403  // initialize the variable.
404  Weights = nullptr;
405  } else {
406  uint64_t NumInits;
407  // FIXME: For the TLS case, collect and use profiling information to
408  // determine a more accurate brach weight.
409  if (Kind == GuardKind::TlsGuard || D->getTLSKind())
410  NumInits = InitsPerTLSVar;
411  else
412  NumInits = InitsPerLocalVar;
413 
414  // The probability of us entering the initializer is
415  // 1 / (total number of times we attempt to initialize the variable).
416  llvm::MDBuilder MDHelper(CGM.getLLVMContext());
417  Weights = MDHelper.createBranchWeights(1, NumInits - 1);
418  }
419 
420  Builder.CreateCondBr(NeedsInit, InitBlock, NoInitBlock, Weights);
421 }
422 
424  llvm::FunctionType *FTy, const Twine &Name, const CGFunctionInfo &FI,
425  SourceLocation Loc, bool TLS) {
426  llvm::Function *Fn = llvm::Function::Create(
428 
429  if (!getLangOpts().AppleKext && !TLS) {
430  // Set the section if needed.
431  if (const char *Section = getTarget().getStaticInitSectionSpecifier())
432  Fn->setSection(Section);
433  }
434 
436 
437  Fn->setCallingConv(getRuntimeCC());
438 
439  if (!getLangOpts().Exceptions)
440  Fn->setDoesNotThrow();
441 
442  if (getLangOpts().Sanitize.has(SanitizerKind::Address) &&
443  !isInNoSanitizeList(SanitizerKind::Address, Fn, Loc))
444  Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
445 
446  if (getLangOpts().Sanitize.has(SanitizerKind::KernelAddress) &&
447  !isInNoSanitizeList(SanitizerKind::KernelAddress, Fn, Loc))
448  Fn->addFnAttr(llvm::Attribute::SanitizeAddress);
449 
450  if (getLangOpts().Sanitize.has(SanitizerKind::HWAddress) &&
451  !isInNoSanitizeList(SanitizerKind::HWAddress, Fn, Loc))
452  Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
453 
454  if (getLangOpts().Sanitize.has(SanitizerKind::KernelHWAddress) &&
455  !isInNoSanitizeList(SanitizerKind::KernelHWAddress, Fn, Loc))
456  Fn->addFnAttr(llvm::Attribute::SanitizeHWAddress);
457 
458  if (getLangOpts().Sanitize.has(SanitizerKind::MemTag) &&
459  !isInNoSanitizeList(SanitizerKind::MemTag, Fn, Loc))
460  Fn->addFnAttr(llvm::Attribute::SanitizeMemTag);
461 
462  if (getLangOpts().Sanitize.has(SanitizerKind::Thread) &&
463  !isInNoSanitizeList(SanitizerKind::Thread, Fn, Loc))
464  Fn->addFnAttr(llvm::Attribute::SanitizeThread);
465 
466  if (getLangOpts().Sanitize.has(SanitizerKind::Memory) &&
467  !isInNoSanitizeList(SanitizerKind::Memory, Fn, Loc))
468  Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
469 
470  if (getLangOpts().Sanitize.has(SanitizerKind::KernelMemory) &&
471  !isInNoSanitizeList(SanitizerKind::KernelMemory, Fn, Loc))
472  Fn->addFnAttr(llvm::Attribute::SanitizeMemory);
473 
474  if (getLangOpts().Sanitize.has(SanitizerKind::SafeStack) &&
475  !isInNoSanitizeList(SanitizerKind::SafeStack, Fn, Loc))
476  Fn->addFnAttr(llvm::Attribute::SafeStack);
477 
478  if (getLangOpts().Sanitize.has(SanitizerKind::ShadowCallStack) &&
479  !isInNoSanitizeList(SanitizerKind::ShadowCallStack, Fn, Loc))
480  Fn->addFnAttr(llvm::Attribute::ShadowCallStack);
481 
482  return Fn;
483 }
484 
485 /// Create a global pointer to a function that will initialize a global
486 /// variable. The user has requested that this pointer be emitted in a specific
487 /// section.
488 void CodeGenModule::EmitPointerToInitFunc(const VarDecl *D,
489  llvm::GlobalVariable *GV,
490  llvm::Function *InitFunc,
491  InitSegAttr *ISA) {
492  llvm::GlobalVariable *PtrArray = new llvm::GlobalVariable(
493  TheModule, InitFunc->getType(), /*isConstant=*/true,
494  llvm::GlobalValue::PrivateLinkage, InitFunc, "__cxx_init_fn_ptr");
495  PtrArray->setSection(ISA->getSection());
496  addUsedGlobal(PtrArray);
497 
498  // If the GV is already in a comdat group, then we have to join it.
499  if (llvm::Comdat *C = GV->getComdat())
500  PtrArray->setComdat(C);
501 }
502 
503 void
504 CodeGenModule::EmitCXXGlobalVarDeclInitFunc(const VarDecl *D,
505  llvm::GlobalVariable *Addr,
506  bool PerformInit) {
507 
508  // According to E.2.3.1 in CUDA-7.5 Programming guide: __device__,
509  // __constant__ and __shared__ variables defined in namespace scope,
510  // that are of class type, cannot have a non-empty constructor. All
511  // the checks have been done in Sema by now. Whatever initializers
512  // are allowed are empty and we just need to ignore them here.
513  if (getLangOpts().CUDAIsDevice && !getLangOpts().GPUAllowDeviceInit &&
514  (D->hasAttr<CUDADeviceAttr>() || D->hasAttr<CUDAConstantAttr>() ||
515  D->hasAttr<CUDASharedAttr>()))
516  return;
517 
518  if (getLangOpts().OpenMP &&
519  getOpenMPRuntime().emitDeclareTargetVarDefinition(D, Addr, PerformInit))
520  return;
521 
522  // Check if we've already initialized this decl.
523  auto I = DelayedCXXInitPosition.find(D);
524  if (I != DelayedCXXInitPosition.end() && I->second == ~0U)
525  return;
526 
527  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
528  SmallString<256> FnName;
529  {
530  llvm::raw_svector_ostream Out(FnName);
532  }
533 
534  // Create a variable initialization function.
535  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
536  FTy, FnName.str(), getTypes().arrangeNullaryFunction(), D->getLocation());
537 
538  auto *ISA = D->getAttr<InitSegAttr>();
540  PerformInit);
541 
542  llvm::GlobalVariable *COMDATKey =
543  supportsCOMDAT() && D->isExternallyVisible() ? Addr : nullptr;
544 
545  if (D->getTLSKind()) {
546  // FIXME: Should we support init_priority for thread_local?
547  // FIXME: We only need to register one __cxa_thread_atexit function for the
548  // entire TU.
549  CXXThreadLocalInits.push_back(Fn);
550  CXXThreadLocalInitVars.push_back(D);
551  } else if (PerformInit && ISA) {
552  EmitPointerToInitFunc(D, Addr, Fn, ISA);
553  } else if (auto *IPA = D->getAttr<InitPriorityAttr>()) {
554  OrderGlobalInitsOrStermFinalizers Key(IPA->getPriority(),
555  PrioritizedCXXGlobalInits.size());
556  PrioritizedCXXGlobalInits.push_back(std::make_pair(Key, Fn));
558  getContext().GetGVALinkageForVariable(D) == GVA_DiscardableODR ||
559  D->hasAttr<SelectAnyAttr>()) {
560  // C++ [basic.start.init]p2:
561  // Definitions of explicitly specialized class template static data
562  // members have ordered initialization. Other class template static data
563  // members (i.e., implicitly or explicitly instantiated specializations)
564  // have unordered initialization.
565  //
566  // As a consequence, we can put them into their own llvm.global_ctors entry.
567  //
568  // If the global is externally visible, put the initializer into a COMDAT
569  // group with the global being initialized. On most platforms, this is a
570  // minor startup time optimization. In the MS C++ ABI, there are no guard
571  // variables, so this COMDAT key is required for correctness.
572  //
573  // SelectAny globals will be comdat-folded. Put the initializer into a
574  // COMDAT group associated with the global, so the initializers get folded
575  // too.
576 
577  AddGlobalCtor(Fn, 65535, COMDATKey);
578  if (COMDATKey && (getTriple().isOSBinFormatELF() ||
579  getTarget().getCXXABI().isMicrosoft())) {
580  // When COMDAT is used on ELF or in the MS C++ ABI, the key must be in
581  // llvm.used to prevent linker GC.
582  addUsedGlobal(COMDATKey);
583  }
584 
585  // If we used a COMDAT key for the global ctor, the init function can be
586  // discarded if the global ctor entry is discarded.
587  // FIXME: Do we need to restrict this to ELF and Wasm?
588  llvm::Comdat *C = Addr->getComdat();
589  if (COMDATKey && C &&
590  (getTarget().getTriple().isOSBinFormatELF() ||
591  getTarget().getTriple().isOSBinFormatWasm())) {
592  Fn->setComdat(C);
593  }
594  } else {
595  I = DelayedCXXInitPosition.find(D); // Re-do lookup in case of re-hash.
596  if (I == DelayedCXXInitPosition.end()) {
597  CXXGlobalInits.push_back(Fn);
598  } else if (I->second != ~0U) {
599  assert(I->second < CXXGlobalInits.size() &&
600  CXXGlobalInits[I->second] == nullptr);
601  CXXGlobalInits[I->second] = Fn;
602  }
603  }
604 
605  // Remember that we already emitted the initializer for this global.
606  DelayedCXXInitPosition[D] = ~0U;
607 }
608 
609 void CodeGenModule::EmitCXXThreadLocalInitFunc() {
611  *this, CXXThreadLocals, CXXThreadLocalInits, CXXThreadLocalInitVars);
612 
613  CXXThreadLocalInits.clear();
614  CXXThreadLocalInitVars.clear();
615  CXXThreadLocals.clear();
616 }
617 
618 static SmallString<128> getTransformedFileName(llvm::Module &M) {
619  SmallString<128> FileName = llvm::sys::path::filename(M.getName());
620 
621  if (FileName.empty())
622  FileName = "<null>";
623 
624  for (size_t i = 0; i < FileName.size(); ++i) {
625  // Replace everything that's not [a-zA-Z0-9._] with a _. This set happens
626  // to be the set of C preprocessing numbers.
627  if (!isPreprocessingNumberBody(FileName[i]))
628  FileName[i] = '_';
629  }
630 
631  return FileName;
632 }
633 
634 static std::string getPrioritySuffix(unsigned int Priority) {
635  assert(Priority <= 65535 && "Priority should always be <= 65535.");
636 
637  // Compute the function suffix from priority. Prepend with zeroes to make
638  // sure the function names are also ordered as priorities.
639  std::string PrioritySuffix = llvm::utostr(Priority);
640  PrioritySuffix = std::string(6 - PrioritySuffix.size(), '0') + PrioritySuffix;
641 
642  return PrioritySuffix;
643 }
644 
645 void
646 CodeGenModule::EmitCXXGlobalInitFunc() {
647  while (!CXXGlobalInits.empty() && !CXXGlobalInits.back())
648  CXXGlobalInits.pop_back();
649 
650  if (CXXGlobalInits.empty() && PrioritizedCXXGlobalInits.empty())
651  return;
652 
653  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
655 
656  // Create our global prioritized initialization function.
657  if (!PrioritizedCXXGlobalInits.empty()) {
658  SmallVector<llvm::Function *, 8> LocalCXXGlobalInits;
659  llvm::array_pod_sort(PrioritizedCXXGlobalInits.begin(),
660  PrioritizedCXXGlobalInits.end());
661  // Iterate over "chunks" of ctors with same priority and emit each chunk
662  // into separate function. Note - everything is sorted first by priority,
663  // second - by lex order, so we emit ctor functions in proper order.
665  I = PrioritizedCXXGlobalInits.begin(),
666  E = PrioritizedCXXGlobalInits.end(); I != E; ) {
668  PrioE = std::upper_bound(I + 1, E, *I, GlobalInitPriorityCmp());
669 
670  LocalCXXGlobalInits.clear();
671 
672  unsigned int Priority = I->first.priority;
673  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
674  FTy, "_GLOBAL__I_" + getPrioritySuffix(Priority), FI);
675 
676  for (; I < PrioE; ++I)
677  LocalCXXGlobalInits.push_back(I->second);
678 
679  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, LocalCXXGlobalInits);
680  AddGlobalCtor(Fn, Priority);
681  }
682  PrioritizedCXXGlobalInits.clear();
683  }
684 
685  if (getCXXABI().useSinitAndSterm() && CXXGlobalInits.empty())
686  return;
687 
688  // Include the filename in the symbol name. Including "sub_" matches gcc
689  // and makes sure these symbols appear lexicographically behind the symbols
690  // with priority emitted above.
691  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
692  FTy, llvm::Twine("_GLOBAL__sub_I_", getTransformedFileName(getModule())),
693  FI);
694 
695  CodeGenFunction(*this).GenerateCXXGlobalInitFunc(Fn, CXXGlobalInits);
696  AddGlobalCtor(Fn);
697 
698  // In OpenCL global init functions must be converted to kernels in order to
699  // be able to launch them from the host.
700  // FIXME: Some more work might be needed to handle destructors correctly.
701  // Current initialization function makes use of function pointers callbacks.
702  // We can't support function pointers especially between host and device.
703  // However it seems global destruction has little meaning without any
704  // dynamic resource allocation on the device and program scope variables are
705  // destroyed by the runtime when program is released.
706  if (getLangOpts().OpenCL) {
708  Fn->setCallingConv(llvm::CallingConv::SPIR_KERNEL);
709  }
710 
711  assert(!getLangOpts().CUDA || !getLangOpts().CUDAIsDevice ||
712  getLangOpts().GPUAllowDeviceInit);
713  if (getLangOpts().HIP && getLangOpts().CUDAIsDevice) {
714  Fn->setCallingConv(llvm::CallingConv::AMDGPU_KERNEL);
715  Fn->addFnAttr("device-init");
716  }
717 
718  CXXGlobalInits.clear();
719 }
720 
721 void CodeGenModule::EmitCXXGlobalCleanUpFunc() {
722  if (CXXGlobalDtorsOrStermFinalizers.empty() &&
723  PrioritizedCXXStermFinalizers.empty())
724  return;
725 
726  llvm::FunctionType *FTy = llvm::FunctionType::get(VoidTy, false);
728 
729  // Create our global prioritized cleanup function.
730  if (!PrioritizedCXXStermFinalizers.empty()) {
731  SmallVector<CXXGlobalDtorsOrStermFinalizer_t, 8> LocalCXXStermFinalizers;
732  llvm::array_pod_sort(PrioritizedCXXStermFinalizers.begin(),
733  PrioritizedCXXStermFinalizers.end());
734  // Iterate over "chunks" of dtors with same priority and emit each chunk
735  // into separate function. Note - everything is sorted first by priority,
736  // second - by lex order, so we emit dtor functions in proper order.
738  I = PrioritizedCXXStermFinalizers.begin(),
739  E = PrioritizedCXXStermFinalizers.end();
740  I != E;) {
742  std::upper_bound(I + 1, E, *I, StermFinalizerPriorityCmp());
743 
744  LocalCXXStermFinalizers.clear();
745 
746  unsigned int Priority = I->first.priority;
747  llvm::Function *Fn = CreateGlobalInitOrCleanUpFunction(
748  FTy, "_GLOBAL__a_" + getPrioritySuffix(Priority), FI);
749 
750  for (; I < PrioE; ++I) {
751  llvm::FunctionCallee DtorFn = I->second;
752  LocalCXXStermFinalizers.emplace_back(DtorFn.getFunctionType(),
753  DtorFn.getCallee(), nullptr);
754  }
755 
757  Fn, LocalCXXStermFinalizers);
758  AddGlobalDtor(Fn, Priority);
759  }
760  PrioritizedCXXStermFinalizers.clear();
761  }
762 
763  if (CXXGlobalDtorsOrStermFinalizers.empty())
764  return;
765 
766  // Create our global cleanup function.
767  llvm::Function *Fn =
768  CreateGlobalInitOrCleanUpFunction(FTy, "_GLOBAL__D_a", FI);
769 
771  Fn, CXXGlobalDtorsOrStermFinalizers);
772  AddGlobalDtor(Fn);
773  CXXGlobalDtorsOrStermFinalizers.clear();
774 }
775 
776 /// Emit the code necessary to initialize the given global variable.
778  const VarDecl *D,
779  llvm::GlobalVariable *Addr,
780  bool PerformInit) {
781  // Check if we need to emit debug info for variable initializer.
782  if (D->hasAttr<NoDebugAttr>())
783  DebugInfo = nullptr; // disable debug info indefinitely for this function
784 
785  CurEHLocation = D->getBeginLoc();
786 
788  getContext().VoidTy, Fn, getTypes().arrangeNullaryFunction(),
789  FunctionArgList());
790  // Emit an artificial location for this function.
791  auto AL = ApplyDebugLocation::CreateArtificial(*this);
792 
793  // Use guarded initialization if the global variable is weak. This
794  // occurs for, e.g., instantiated static data members and
795  // definitions explicitly marked weak.
796  //
797  // Also use guarded initialization for a variable with dynamic TLS and
798  // unordered initialization. (If the initialization is ordered, the ABI
799  // layer will guard the whole-TU initialization for us.)
800  if (Addr->hasWeakLinkage() || Addr->hasLinkOnceLinkage() ||
801  (D->getTLSKind() == VarDecl::TLS_Dynamic &&
803  EmitCXXGuardedInit(*D, Addr, PerformInit);
804  } else {
805  EmitCXXGlobalVarDeclInit(*D, Addr, PerformInit);
806  }
807 
808  FinishFunction();
809 }
810 
811 void
814  ConstantAddress Guard) {
815  {
816  auto NL = ApplyDebugLocation::CreateEmpty(*this);
818  getTypes().arrangeNullaryFunction(), FunctionArgList());
819  // Emit an artificial location for this function.
820  auto AL = ApplyDebugLocation::CreateArtificial(*this);
821 
822  llvm::BasicBlock *ExitBlock = nullptr;
823  if (Guard.isValid()) {
824  // If we have a guard variable, check whether we've already performed
825  // these initializations. This happens for TLS initialization functions.
826  llvm::Value *GuardVal = Builder.CreateLoad(Guard);
827  llvm::Value *Uninit = Builder.CreateIsNull(GuardVal,
828  "guard.uninitialized");
829  llvm::BasicBlock *InitBlock = createBasicBlock("init");
830  ExitBlock = createBasicBlock("exit");
831  EmitCXXGuardedInitBranch(Uninit, InitBlock, ExitBlock,
832  GuardKind::TlsGuard, nullptr);
833  EmitBlock(InitBlock);
834  // Mark as initialized before initializing anything else. If the
835  // initializers use previously-initialized thread_local vars, that's
836  // probably supposed to be OK, but the standard doesn't say.
837  Builder.CreateStore(llvm::ConstantInt::get(GuardVal->getType(),1), Guard);
838 
839  // The guard variable can't ever change again.
841  Guard.getPointer(),
843  CGM.getDataLayout().getTypeAllocSize(GuardVal->getType())));
844  }
845 
846  RunCleanupsScope Scope(*this);
847 
848  // When building in Objective-C++ ARC mode, create an autorelease pool
849  // around the global initializers.
850  if (getLangOpts().ObjCAutoRefCount && getLangOpts().CPlusPlus) {
853  }
854 
855  for (unsigned i = 0, e = Decls.size(); i != e; ++i)
856  if (Decls[i])
857  EmitRuntimeCall(Decls[i]);
858 
859  Scope.ForceCleanup();
860 
861  if (ExitBlock) {
862  Builder.CreateBr(ExitBlock);
863  EmitBlock(ExitBlock);
864  }
865  }
866 
867  FinishFunction();
868 }
869 
871  llvm::Function *Fn,
872  ArrayRef<std::tuple<llvm::FunctionType *, llvm::WeakTrackingVH,
873  llvm::Constant *>>
874  DtorsOrStermFinalizers) {
875  {
876  auto NL = ApplyDebugLocation::CreateEmpty(*this);
878  getTypes().arrangeNullaryFunction(), FunctionArgList());
879  // Emit an artificial location for this function.
880  auto AL = ApplyDebugLocation::CreateArtificial(*this);
881 
882  // Emit the cleanups, in reverse order from construction.
883  for (unsigned i = 0, e = DtorsOrStermFinalizers.size(); i != e; ++i) {
884  llvm::FunctionType *CalleeTy;
885  llvm::Value *Callee;
886  llvm::Constant *Arg;
887  std::tie(CalleeTy, Callee, Arg) = DtorsOrStermFinalizers[e - i - 1];
888 
889  llvm::CallInst *CI = nullptr;
890  if (Arg == nullptr) {
891  assert(
893  "Arg could not be nullptr unless using sinit and sterm functions.");
894  CI = Builder.CreateCall(CalleeTy, Callee);
895  } else
896  CI = Builder.CreateCall(CalleeTy, Callee, Arg);
897 
898  // Make sure the call and the callee agree on calling convention.
899  if (llvm::Function *F = dyn_cast<llvm::Function>(Callee))
900  CI->setCallingConv(F->getCallingConv());
901  }
902  }
903 
904  FinishFunction();
905 }
906 
907 /// generateDestroyHelper - Generates a helper function which, when
908 /// invoked, destroys the given object. The address of the object
909 /// should be in global memory.
911  Address addr, QualType type, Destroyer *destroyer,
912  bool useEHCleanupForArray, const VarDecl *VD) {
913  FunctionArgList args;
916  args.push_back(&Dst);
917 
918  const CGFunctionInfo &FI =
920  llvm::FunctionType *FTy = CGM.getTypes().GetFunctionType(FI);
921  llvm::Function *fn = CGM.CreateGlobalInitOrCleanUpFunction(
922  FTy, "__cxx_global_array_dtor", FI, VD->getLocation());
923 
924  CurEHLocation = VD->getBeginLoc();
925 
927  getContext().VoidTy, fn, FI, args);
928  // Emit an artificial location for this function.
929  auto AL = ApplyDebugLocation::CreateArtificial(*this);
930 
931  emitDestroy(addr, type, destroyer, useEHCleanupForArray);
932 
933  FinishFunction();
934 
935  return fn;
936 }
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:121
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:3973
clang::InternalLinkage
@ InternalLinkage
Internal linkage, which indicates that the entity can be referred to from within the translation unit...
Definition: Linkage.h:31
clang::ASTContext::getTypeSizeInChars
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Definition: ASTContext.cpp:2450
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:1207
clang::CodeGen::CodeGenFunction::EmitObjCAutoreleasePoolCleanup
void EmitObjCAutoreleasePoolCleanup(llvm::Value *Ptr)
Definition: CGObjC.cpp:2845
clang::CodeGen::CodeGenModule::CreateGlobalInitOrCleanUpFunction
llvm::Function * CreateGlobalInitOrCleanUpFunction(llvm::FunctionType *ty, const Twine &name, const CGFunctionInfo &FI, SourceLocation Loc=SourceLocation(), bool TLS=false)
Definition: CGDeclCXX.cpp:423
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:74
clang::VarDecl::getTLSKind
TLSKind getTLSKind() const
Definition: Decl.cpp:2050
clang::CodeGen::CodeGenTypeCache::IntTy
llvm::IntegerType * IntTy
int
Definition: CodeGenTypeCache.h:42
clang::CodeGen::TEK_Aggregate
@ TEK_Aggregate
Definition: CodeGenFunction.h:115
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:3930
clang::CodeGen::OrderGlobalInitsOrStermFinalizers
Definition: CodeGenModule.h:106
type
clang::DeclaratorDecl::getBeginLoc
SourceLocation getBeginLoc() const LLVM_READONLY
Definition: Decl.h:780
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:603
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:386
clang::Decl::hasAttr
bool hasAttr() const
Definition: DeclBase.h:547
clang::VarDecl::hasGlobalStorage
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1143
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:107
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:2265
clang::CodeGen::AggValueSlot::DoesNotNeedGCBarriers
@ DoesNotNeedGCBarriers
Definition: CGValue.h:528
getPrioritySuffix
static std::string getPrioritySuffix(unsigned int Priority)
Definition: CGDeclCXX.cpp:634
clang::CodeGen::CodeGenFunction::EmitNounwindRuntimeCall
llvm::CallInst * EmitNounwindRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
clang::NamedDecl::isExternallyVisible
bool isExternallyVisible() const
Definition: Decl.h:407
clang::ASTContext::VoidTy
CanQualType VoidTy
Definition: ASTContext.h:1075
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:2118
clang::QualType::getQualifiers
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6455
clang::CodeGen::CGCXXABI::getMangleContext
MangleContext & getMangleContext()
Gets the mangle context.
Definition: CGCXXABI.h:97
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:812
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:673
Attr.h
clang::DynamicInitKind::AtExit
@ AtExit
clang::GVA_DiscardableODR
@ GVA_DiscardableODR
Definition: Linkage.h:76
AttributeLangSupport::C
@ C
Definition: SemaDeclAttr.cpp:54
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:702
clang::CodeGen::RequiredArgs::All
@ All
Definition: CGFunctionInfo.h:485
clang::CodeGen::Address::isValid
bool isValid() const
Definition: Address.h:35
getTransformedFileName
static SmallString< 128 > getTransformedFileName(llvm::Module &M)
Definition: CGDeclCXX.cpp:618
clang::CodeGen::CodeGenFunction::unregisterGlobalDtorWithUnAtExit
llvm::Value * unregisterGlobalDtorWithUnAtExit(llvm::Constant *dtorStub)
Call unatexit() with function dtorStub.
Definition: CGDeclCXX.cpp:347
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:2422
clang::CodeGen::CGBuilderTy::CreateStore
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:95
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:657
clang::CodeGen::CodeGenModule::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenModule.h:703
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::Decl::getAttr
T * getAttr() const
Definition: DeclBase.h:543
clang::CodeGen::CodeGenFunction::getDestroyer
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:2083
clang::CodeGen::LValue::isObjCWeak
bool isObjCWeak() const
Definition: CGValue.h:296
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:3831
clang::CodeGen::CodeGenModule::getTargetCodeGenInfo
const TargetCodeGenInfo & getTargetCodeGenInfo()
Definition: TargetInfo.cpp:11130
clang::CodeGen::CodeGenFunction::Builder
CGBuilderTy Builder
Definition: CodeGenFunction.h:274
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:150
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:152
clang::CodeGen::CodeGenTypes::ConvertType
llvm::Type * ConvertType(QualType T)
ConvertType - Convert type T into a llvm::Type.
Definition: CodeGenTypes.cpp:395
clang::Dtor_Complete
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
clang::Type
The base class of the type hierarchy.
Definition: Type.h:1490
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:616
clang::GlobalDecl
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
clang::Qualifiers::getAddressSpace
LangAS getAddressSpace() const
Definition: Type.h:361
U
clang::CodeGen::CodeGenTypes::ConvertTypeForMem
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
Definition: CodeGenTypes.cpp:90
clang::CodeGen::CodeGenFunction::EmitObjCAutoreleasePoolPush
llvm::Value * EmitObjCAutoreleasePoolPush()
Produce the code to do a objc_autoreleasepool_push.
Definition: CGObjC.cpp:2653
clang::QualType::DK_cxx_destructor
@ DK_cxx_destructor
Definition: Type.h:1204
clang::CodeGen::TargetCodeGenInfo::getAddrSpaceOfCxaAtexitPtrParam
virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const
Get address space of pointer parameter for __cxa_atexit.
Definition: TargetInfo.h:294
clang::Type::isReferenceType
bool isReferenceType() const
Definition: Type.h:6688
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:866
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:1632
clang::CodeGen::CodeGenFunction::GuardKind::VariableGuard
@ VariableGuard
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:2184
clang::CodeGen::CodeGenModule::getObjCRuntime
CGObjCRuntime & getObjCRuntime()
Return a reference to the configured Objective-C runtime.
Definition: CodeGenModule.h:599
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:741
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:910
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:708
clang::CPlusPlus
@ CPlusPlus
Definition: LangStandard.h:48
LangOptions.h
clang::CodeGen::CodeGenFunction::EmitRuntimeCall
llvm::CallInst * EmitRuntimeCall(llvm::FunctionCallee callee, const Twine &name="")
clang::CodeGen::TEK_Complex
@ TEK_Complex
Definition: CodeGenFunction.h:114
clang::QualType::DK_none
@ DK_none
Definition: Type.h:1203
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:156
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:777
clang::CodeGen::CodeGenModule::getModule
llvm::Module & getModule() const
Definition: CodeGenModule.h:709
clang::CXXDestructorDecl
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2668
clang::ImplicitParamDecl
Definition: Decl.h:1601
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:2068
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
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:318
clang::CodeGen::LValue::isObjCStrong
bool isObjCStrong() const
Definition: CGValue.h:299
clang::CodeGen::CodeGenModule::getTypes
CodeGenTypes & getTypes()
Definition: CodeGenModule.h:726
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:1760
clang::VarDecl::isLocalVarDecl
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1170
clang::CodeGen::AggValueSlot::IsDestructed
@ IsDestructed
Definition: CGValue.h:525
clang::CodeGen::CodeGenFunction::getContext
ASTContext & getContext() const
Definition: CodeGenFunction.h:1975
clang::CodeGen::CodeGenFunction::FinishFunction
void FinishFunction(SourceLocation EndLoc=SourceLocation())
FinishFunction - Complete IR generation of the current function.
Definition: CodeGenFunction.cpp:324
clang::CodeGen::Address
An aligned address.
Definition: Address.h:24
clang::CodeGen::CodeGenFunction::getEvaluationKind
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
Definition: CodeGenFunction.cpp:215
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:714
clang::CodeGen::CodeGenFunction::EmitCXXGlobalVarDeclInit
void EmitCXXGlobalVarDeclInit(const VarDecl &D, llvm::Constant *DeclPtr, bool PerformInit)
EmitCXXGlobalVarDeclInit - Create the initializer for a C++ variable with global storage.
Definition: CGDeclCXX.cpp:174
clang::ASTContext::getTargetAddressSpace
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.h:2726
clang::CodeGen::CodeGenTypes::GetFunctionType
llvm::FunctionType * GetFunctionType(const CGFunctionInfo &Info)
GetFunctionType - Get the LLVM function type for.
Definition: CGCall.cpp:1595
clang::CodeGen::CodeGenModule::getDataLayout
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenModule.h:711
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:708
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:255
clang::CodeGen::LValue
LValue - This represents an lvalue references.
Definition: CGValue.h:167
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:372
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:230
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:235
clang::ASTContext::IntTy
CanQualType IntTy
Definition: ASTContext.h:1084
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:719
clang::CodeGen::AggValueSlot::IsNotAliased
@ IsNotAliased
Definition: CGValue.h:524
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:229
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:284
clang::QualType::DK_objc_strong_lifetime
@ DK_objc_strong_lifetime
Definition: Type.h:1205
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:763
Value
Value
Definition: UninitializedValues.cpp:102
clang::CodeGen::CodeGenModule::getTriple
const llvm::Triple & getTriple() const
Definition: CodeGenModule.h:715
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:1991
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:575
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:358
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:2629
clang::QualType::DestructionKind
DestructionKind
Definition: Type.h:1202
clang::CodeGen::CodeGenFunction::getTypes
CodeGenTypes & getTypes() const
Definition: CodeGenFunction.h:1974
clang::VarDecl::getInit
const Expr * getInit() const
Definition: Decl.h:1285
clang::QualType::DK_objc_weak_lifetime
@ DK_objc_weak_lifetime
Definition: Type.h:1206
clang::FunctionType::ExtInfo
A class which abstracts out some details necessary for making a call.
Definition: Type.h:3657
clang::CodeGen::CodeGenFunction::MakeAddrLValue
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
Definition: CodeGenFunction.h:2484
clang::CXXRecordDecl::hasTrivialDestructor
bool hasTrivialDestructor() const
Determine whether this class has a trivial destructor (C++ [class.dtor]p3)
Definition: DeclCXX.h:1311
Priority
int Priority
Definition: Format.cpp:2383
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:4848
clang::CXXRecordDecl::getDestructor
CXXDestructorDecl * getDestructor() const
Returns the destructor decl for this class.
Definition: DeclCXX.cpp:1889
clang::CodeGen::CodeGenModule::supportsCOMDAT
bool supportsCOMDAT() const
Definition: TargetInfo.cpp:11126
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:83
clang::CodeGen::CGFunctionInfo
CGFunctionInfo - Class to encapsulate the information about a function definition.
Definition: CGFunctionInfo.h:546
CGCXXABI.h
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:267
clang::CodeGen::CodeGenFunction::CGM
CodeGenModule & CGM
Definition: CodeGenFunction.h:266
clang::CodeGen::TEK_Scalar
@ TEK_Scalar
Definition: CodeGenFunction.h:113
clang::VarDecl::TLS_Dynamic
@ TLS_Dynamic
TLS with a dynamic initializer.
Definition: Decl.h:899
clang::Builtin::ID
ID
Definition: Builtins.h:48
clang
Definition: CalledOnceCheck.h:17
clang::CodeGen::ApplyDebugLocation::CreateArtificial
static ApplyDebugLocation CreateArtificial(CodeGenFunction &CGF)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:817
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:870
clang::CodeGen::CodeGenFunction::GuardKind
GuardKind
Definition: CodeGenFunction.h:4438
clang::CodeGen::RValue::getScalarVal
llvm::Value * getScalarVal() const
getScalarVal() - Return the Value* of this scalar value.
Definition: CGValue.h:59
clang::CodeGen::ApplyDebugLocation::CreateEmpty
static ApplyDebugLocation CreateEmpty(CodeGenFunction &CGF)
Set the IRBuilder to not attach debug locations.
Definition: CGDebugInfo.h:834
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:111
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:2693
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:1780
llvm::SmallVectorImpl
Definition: LLVM.h:39
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:687
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:1101
clang::CodeGen::CodeGenModule::getIntrinsic
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
Definition: CodeGenModule.cpp:5181
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:430
clang::CodeGen::CodeGenFunction::EmitBlock
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:532
clang::CodeGen::CodeGenModule::Error
void Error(SourceLocation loc, StringRef error)
Emit a general error that something can't be done.
Definition: CodeGenModule.cpp:998
clang::CodeGen::CodeGenModule::getLLVMContext
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenModule.h:720
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:765
clang::CodeGen::CodeGenFunction::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenFunction.h:1988
clang::CodeGen::CodeGenTypes::arrangeNullaryFunction
const CGFunctionInfo & arrangeNullaryFunction()
A nullary function is a freestanding function of type 'void ()'.
Definition: CGCall.cpp:699
clang::ImplicitParamDecl::Other
@ Other
Other implicit parameter.
Definition: Decl.h:1625
clang::CodeGen::CGBuilderTy::CreateLoad
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:68
clang::CodeGen::CodeGenFunction::ReturnValue
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
Definition: CodeGenFunction.h:361
clang::CodeGen::AggValueSlot::DoesNotOverlap
@ DoesNotOverlap
Definition: CGValue.h:527
clang::CodeGen::CodeGenModule::isInNoSanitizeList
bool isInNoSanitizeList(SanitizerMask Kind, llvm::Function *Fn, SourceLocation Loc) const
Definition: CodeGenModule.cpp:2670