clang  16.0.0git
CGDecl.cpp
Go to the documentation of this file.
1 //===--- CGDecl.cpp - Emit LLVM Code for 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 to emit Decl nodes as LLVM code.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "CGBlocks.h"
14 #include "CGCXXABI.h"
15 #include "CGCleanup.h"
16 #include "CGDebugInfo.h"
17 #include "CGOpenCLRuntime.h"
18 #include "CGOpenMPRuntime.h"
19 #include "CodeGenFunction.h"
20 #include "CodeGenModule.h"
21 #include "ConstantEmitter.h"
22 #include "PatternInit.h"
23 #include "TargetInfo.h"
24 #include "clang/AST/ASTContext.h"
25 #include "clang/AST/Attr.h"
26 #include "clang/AST/CharUnits.h"
27 #include "clang/AST/Decl.h"
28 #include "clang/AST/DeclObjC.h"
29 #include "clang/AST/DeclOpenMP.h"
32 #include "clang/Basic/TargetInfo.h"
34 #include "clang/Sema/Sema.h"
35 #include "llvm/Analysis/ValueTracking.h"
36 #include "llvm/IR/DataLayout.h"
37 #include "llvm/IR/GlobalVariable.h"
38 #include "llvm/IR/Intrinsics.h"
39 #include "llvm/IR/Type.h"
40 
41 using namespace clang;
42 using namespace CodeGen;
43 
44 static_assert(clang::Sema::MaximumAlignment <= llvm::Value::MaximumAlignment,
45  "Clang max alignment greater than what LLVM supports?");
46 
48  switch (D.getKind()) {
49  case Decl::BuiltinTemplate:
50  case Decl::TranslationUnit:
51  case Decl::ExternCContext:
52  case Decl::Namespace:
53  case Decl::UnresolvedUsingTypename:
54  case Decl::ClassTemplateSpecialization:
55  case Decl::ClassTemplatePartialSpecialization:
56  case Decl::VarTemplateSpecialization:
57  case Decl::VarTemplatePartialSpecialization:
58  case Decl::TemplateTypeParm:
59  case Decl::UnresolvedUsingValue:
60  case Decl::NonTypeTemplateParm:
61  case Decl::CXXDeductionGuide:
62  case Decl::CXXMethod:
63  case Decl::CXXConstructor:
64  case Decl::CXXDestructor:
65  case Decl::CXXConversion:
66  case Decl::Field:
67  case Decl::MSProperty:
68  case Decl::IndirectField:
69  case Decl::ObjCIvar:
70  case Decl::ObjCAtDefsField:
71  case Decl::ParmVar:
72  case Decl::ImplicitParam:
73  case Decl::ClassTemplate:
74  case Decl::VarTemplate:
75  case Decl::FunctionTemplate:
76  case Decl::TypeAliasTemplate:
77  case Decl::TemplateTemplateParm:
78  case Decl::ObjCMethod:
79  case Decl::ObjCCategory:
80  case Decl::ObjCProtocol:
81  case Decl::ObjCInterface:
82  case Decl::ObjCCategoryImpl:
83  case Decl::ObjCImplementation:
84  case Decl::ObjCProperty:
85  case Decl::ObjCCompatibleAlias:
86  case Decl::PragmaComment:
87  case Decl::PragmaDetectMismatch:
88  case Decl::AccessSpec:
89  case Decl::LinkageSpec:
90  case Decl::Export:
91  case Decl::ObjCPropertyImpl:
92  case Decl::FileScopeAsm:
93  case Decl::Friend:
94  case Decl::FriendTemplate:
95  case Decl::Block:
96  case Decl::Captured:
97  case Decl::ClassScopeFunctionSpecialization:
98  case Decl::UsingShadow:
99  case Decl::ConstructorUsingShadow:
100  case Decl::ObjCTypeParam:
101  case Decl::Binding:
102  case Decl::UnresolvedUsingIfExists:
103  case Decl::HLSLBuffer:
104  llvm_unreachable("Declaration should not be in declstmts!");
105  case Decl::Record: // struct/union/class X;
106  case Decl::CXXRecord: // struct/union/class X; [C++]
107  if (CGDebugInfo *DI = getDebugInfo())
108  if (cast<RecordDecl>(D).getDefinition())
109  DI->EmitAndRetainType(getContext().getRecordType(cast<RecordDecl>(&D)));
110  return;
111  case Decl::Enum: // enum X;
112  if (CGDebugInfo *DI = getDebugInfo())
113  if (cast<EnumDecl>(D).getDefinition())
114  DI->EmitAndRetainType(getContext().getEnumType(cast<EnumDecl>(&D)));
115  return;
116  case Decl::Function: // void X();
117  case Decl::EnumConstant: // enum ? { X = ? }
118  case Decl::StaticAssert: // static_assert(X, ""); [C++0x]
119  case Decl::Label: // __label__ x;
120  case Decl::Import:
121  case Decl::MSGuid: // __declspec(uuid("..."))
122  case Decl::UnnamedGlobalConstant:
123  case Decl::TemplateParamObject:
124  case Decl::OMPThreadPrivate:
125  case Decl::OMPAllocate:
126  case Decl::OMPCapturedExpr:
127  case Decl::OMPRequires:
128  case Decl::Empty:
129  case Decl::Concept:
130  case Decl::ImplicitConceptSpecialization:
131  case Decl::LifetimeExtendedTemporary:
132  case Decl::RequiresExprBody:
133  // None of these decls require codegen support.
134  return;
135 
136  case Decl::NamespaceAlias:
137  if (CGDebugInfo *DI = getDebugInfo())
138  DI->EmitNamespaceAlias(cast<NamespaceAliasDecl>(D));
139  return;
140  case Decl::Using: // using X; [C++]
141  if (CGDebugInfo *DI = getDebugInfo())
142  DI->EmitUsingDecl(cast<UsingDecl>(D));
143  return;
144  case Decl::UsingEnum: // using enum X; [C++]
145  if (CGDebugInfo *DI = getDebugInfo())
146  DI->EmitUsingEnumDecl(cast<UsingEnumDecl>(D));
147  return;
148  case Decl::UsingPack:
149  for (auto *Using : cast<UsingPackDecl>(D).expansions())
150  EmitDecl(*Using);
151  return;
152  case Decl::UsingDirective: // using namespace X; [C++]
153  if (CGDebugInfo *DI = getDebugInfo())
154  DI->EmitUsingDirective(cast<UsingDirectiveDecl>(D));
155  return;
156  case Decl::Var:
157  case Decl::Decomposition: {
158  const VarDecl &VD = cast<VarDecl>(D);
159  assert(VD.isLocalVarDecl() &&
160  "Should not see file-scope variables inside a function!");
161  EmitVarDecl(VD);
162  if (auto *DD = dyn_cast<DecompositionDecl>(&VD))
163  for (auto *B : DD->bindings())
164  if (auto *HD = B->getHoldingVar())
165  EmitVarDecl(*HD);
166  return;
167  }
168 
169  case Decl::OMPDeclareReduction:
170  return CGM.EmitOMPDeclareReduction(cast<OMPDeclareReductionDecl>(&D), this);
171 
172  case Decl::OMPDeclareMapper:
173  return CGM.EmitOMPDeclareMapper(cast<OMPDeclareMapperDecl>(&D), this);
174 
175  case Decl::Typedef: // typedef int X;
176  case Decl::TypeAlias: { // using X = int; [C++0x]
177  QualType Ty = cast<TypedefNameDecl>(D).getUnderlyingType();
178  if (CGDebugInfo *DI = getDebugInfo())
179  DI->EmitAndRetainType(Ty);
180  if (Ty->isVariablyModifiedType())
182  return;
183  }
184  }
185 }
186 
187 /// EmitVarDecl - This method handles emission of any variable declaration
188 /// inside a function, including static vars etc.
190  if (D.hasExternalStorage())
191  // Don't emit it now, allow it to be emitted lazily on its first use.
192  return;
193 
194  // Some function-scope variable does not have static storage but still
195  // needs to be emitted like a static variable, e.g. a function-scope
196  // variable in constant address space in OpenCL.
197  if (D.getStorageDuration() != SD_Automatic) {
198  // Static sampler variables translated to function calls.
199  if (D.getType()->isSamplerT())
200  return;
201 
202  llvm::GlobalValue::LinkageTypes Linkage =
203  CGM.getLLVMLinkageVarDefinition(&D, /*IsConstant=*/false);
204 
205  // FIXME: We need to force the emission/use of a guard variable for
206  // some variables even if we can constant-evaluate them because
207  // we can't guarantee every translation unit will constant-evaluate them.
208 
209  return EmitStaticVarDecl(D, Linkage);
210  }
211 
214 
215  assert(D.hasLocalStorage());
216  return EmitAutoVarDecl(D);
217 }
218 
220  if (CGM.getLangOpts().CPlusPlus)
221  return CGM.getMangledName(&D).str();
222 
223  // If this isn't C++, we don't need a mangled name, just a pretty one.
224  assert(!D.isExternallyVisible() && "name shouldn't matter");
225  std::string ContextName;
226  const DeclContext *DC = D.getDeclContext();
227  if (auto *CD = dyn_cast<CapturedDecl>(DC))
228  DC = cast<DeclContext>(CD->getNonClosureContext());
229  if (const auto *FD = dyn_cast<FunctionDecl>(DC))
230  ContextName = std::string(CGM.getMangledName(FD));
231  else if (const auto *BD = dyn_cast<BlockDecl>(DC))
232  ContextName = std::string(CGM.getBlockMangledName(GlobalDecl(), BD));
233  else if (const auto *OMD = dyn_cast<ObjCMethodDecl>(DC))
234  ContextName = OMD->getSelector().getAsString();
235  else
236  llvm_unreachable("Unknown context for static var decl");
237 
238  ContextName += "." + D.getNameAsString();
239  return ContextName;
240 }
241 
243  const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage) {
244  // In general, we don't always emit static var decls once before we reference
245  // them. It is possible to reference them before emitting the function that
246  // contains them, and it is possible to emit the containing function multiple
247  // times.
248  if (llvm::Constant *ExistingGV = StaticLocalDeclMap[&D])
249  return ExistingGV;
250 
251  QualType Ty = D.getType();
252  assert(Ty->isConstantSizeType() && "VLAs can't be static");
253 
254  // Use the label if the variable is renamed with the asm-label extension.
255  std::string Name;
256  if (D.hasAttr<AsmLabelAttr>())
257  Name = std::string(getMangledName(&D));
258  else
259  Name = getStaticDeclName(*this, D);
260 
261  llvm::Type *LTy = getTypes().ConvertTypeForMem(Ty);
263  unsigned TargetAS = getContext().getTargetAddressSpace(AS);
264 
265  // OpenCL variables in local address space and CUDA shared
266  // variables cannot have an initializer.
267  llvm::Constant *Init = nullptr;
269  D.hasAttr<CUDASharedAttr>() || D.hasAttr<LoaderUninitializedAttr>())
270  Init = llvm::UndefValue::get(LTy);
271  else
272  Init = EmitNullConstant(Ty);
273 
274  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
275  getModule(), LTy, Ty.isConstant(getContext()), Linkage, Init, Name,
276  nullptr, llvm::GlobalVariable::NotThreadLocal, TargetAS);
277  GV->setAlignment(getContext().getDeclAlign(&D).getAsAlign());
278 
279  if (supportsCOMDAT() && GV->isWeakForLinker())
280  GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
281 
282  if (D.getTLSKind())
283  setTLSMode(GV, D);
284 
285  setGVProperties(GV, &D);
286 
287  // Make sure the result is of the correct type.
288  LangAS ExpectedAS = Ty.getAddressSpace();
289  llvm::Constant *Addr = GV;
290  if (AS != ExpectedAS) {
292  *this, GV, AS, ExpectedAS,
293  LTy->getPointerTo(getContext().getTargetAddressSpace(ExpectedAS)));
294  }
295 
296  setStaticLocalDeclAddress(&D, Addr);
297 
298  // Ensure that the static local gets initialized by making sure the parent
299  // function gets emitted eventually.
300  const Decl *DC = cast<Decl>(D.getDeclContext());
301 
302  // We can't name blocks or captured statements directly, so try to emit their
303  // parents.
304  if (isa<BlockDecl>(DC) || isa<CapturedDecl>(DC)) {
305  DC = DC->getNonClosureContext();
306  // FIXME: Ensure that global blocks get emitted.
307  if (!DC)
308  return Addr;
309  }
310 
311  GlobalDecl GD;
312  if (const auto *CD = dyn_cast<CXXConstructorDecl>(DC))
313  GD = GlobalDecl(CD, Ctor_Base);
314  else if (const auto *DD = dyn_cast<CXXDestructorDecl>(DC))
315  GD = GlobalDecl(DD, Dtor_Base);
316  else if (const auto *FD = dyn_cast<FunctionDecl>(DC))
317  GD = GlobalDecl(FD);
318  else {
319  // Don't do anything for Obj-C method decls or global closures. We should
320  // never defer them.
321  assert(isa<ObjCMethodDecl>(DC) && "unexpected parent code decl");
322  }
323  if (GD.getDecl()) {
324  // Disable emission of the parent function for the OpenMP device codegen.
326  (void)GetAddrOfGlobal(GD);
327  }
328 
329  return Addr;
330 }
331 
332 /// AddInitializerToStaticVarDecl - Add the initializer for 'D' to the
333 /// global variable that has already been created for it. If the initializer
334 /// has a different type than GV does, this may free GV and return a different
335 /// one. Otherwise it just returns GV.
336 llvm::GlobalVariable *
338  llvm::GlobalVariable *GV) {
339  ConstantEmitter emitter(*this);
340  llvm::Constant *Init = emitter.tryEmitForInitializer(D);
341 
342  // If constant emission failed, then this should be a C++ static
343  // initializer.
344  if (!Init) {
345  if (!getLangOpts().CPlusPlus)
346  CGM.ErrorUnsupported(D.getInit(), "constant l-value expression");
347  else if (D.hasFlexibleArrayInit(getContext()))
348  CGM.ErrorUnsupported(D.getInit(), "flexible array initializer");
349  else if (HaveInsertPoint()) {
350  // Since we have a static initializer, this global variable can't
351  // be constant.
352  GV->setConstant(false);
353 
354  EmitCXXGuardedInit(D, GV, /*PerformInit*/true);
355  }
356  return GV;
357  }
358 
359 #ifndef NDEBUG
363  CGM.getDataLayout().getTypeAllocSize(Init->getType()));
364  assert(VarSize == CstSize && "Emitted constant has unexpected size");
365 #endif
366 
367  // The initializer may differ in type from the global. Rewrite
368  // the global to match the initializer. (We have to do this
369  // because some types, like unions, can't be completely represented
370  // in the LLVM type system.)
371  if (GV->getValueType() != Init->getType()) {
372  llvm::GlobalVariable *OldGV = GV;
373 
374  GV = new llvm::GlobalVariable(
375  CGM.getModule(), Init->getType(), OldGV->isConstant(),
376  OldGV->getLinkage(), Init, "",
377  /*InsertBefore*/ OldGV, OldGV->getThreadLocalMode(),
378  OldGV->getType()->getPointerAddressSpace());
379  GV->setVisibility(OldGV->getVisibility());
380  GV->setDSOLocal(OldGV->isDSOLocal());
381  GV->setComdat(OldGV->getComdat());
382 
383  // Steal the name of the old global
384  GV->takeName(OldGV);
385 
386  // Replace all uses of the old global with the new global
387  llvm::Constant *NewPtrForOldDecl =
388  llvm::ConstantExpr::getBitCast(GV, OldGV->getType());
389  OldGV->replaceAllUsesWith(NewPtrForOldDecl);
390 
391  // Erase the old global, since it is no longer used.
392  OldGV->eraseFromParent();
393  }
394 
395  GV->setConstant(CGM.isTypeConstant(D.getType(), true));
396  GV->setInitializer(Init);
397 
398  emitter.finalize(GV);
399 
401  HaveInsertPoint()) {
402  // We have a constant initializer, but a nontrivial destructor. We still
403  // need to perform a guarded "initialization" in order to register the
404  // destructor.
405  EmitCXXGuardedInit(D, GV, /*PerformInit*/false);
406  }
407 
408  return GV;
409 }
410 
412  llvm::GlobalValue::LinkageTypes Linkage) {
413  // Check to see if we already have a global variable for this
414  // declaration. This can happen when double-emitting function
415  // bodies, e.g. with complete and base constructors.
416  llvm::Constant *addr = CGM.getOrCreateStaticVarDecl(D, Linkage);
417  CharUnits alignment = getContext().getDeclAlign(&D);
418 
419  // Store into LocalDeclMap before generating initializer to handle
420  // circular references.
421  llvm::Type *elemTy = ConvertTypeForMem(D.getType());
422  setAddrOfLocalVar(&D, Address(addr, elemTy, alignment));
423 
424  // We can't have a VLA here, but we can have a pointer to a VLA,
425  // even though that doesn't really make any sense.
426  // Make sure to evaluate VLA bounds now so that we have them for later.
427  if (D.getType()->isVariablyModifiedType())
429 
430  // Save the type in case adding the initializer forces a type change.
431  llvm::Type *expectedType = addr->getType();
432 
433  llvm::GlobalVariable *var =
434  cast<llvm::GlobalVariable>(addr->stripPointerCasts());
435 
436  // CUDA's local and local static __shared__ variables should not
437  // have any non-empty initializers. This is ensured by Sema.
438  // Whatever initializer such variable may have when it gets here is
439  // a no-op and should not be emitted.
440  bool isCudaSharedVar = getLangOpts().CUDA && getLangOpts().CUDAIsDevice &&
441  D.hasAttr<CUDASharedAttr>();
442  // If this value has an initializer, emit it.
443  if (D.getInit() && !isCudaSharedVar)
445 
446  var->setAlignment(alignment.getAsAlign());
447 
448  if (D.hasAttr<AnnotateAttr>())
450 
451  if (auto *SA = D.getAttr<PragmaClangBSSSectionAttr>())
452  var->addAttribute("bss-section", SA->getName());
453  if (auto *SA = D.getAttr<PragmaClangDataSectionAttr>())
454  var->addAttribute("data-section", SA->getName());
455  if (auto *SA = D.getAttr<PragmaClangRodataSectionAttr>())
456  var->addAttribute("rodata-section", SA->getName());
457  if (auto *SA = D.getAttr<PragmaClangRelroSectionAttr>())
458  var->addAttribute("relro-section", SA->getName());
459 
460  if (const SectionAttr *SA = D.getAttr<SectionAttr>())
461  var->setSection(SA->getName());
462 
463  if (D.hasAttr<RetainAttr>())
465  else if (D.hasAttr<UsedAttr>())
467 
468  // We may have to cast the constant because of the initializer
469  // mismatch above.
470  //
471  // FIXME: It is really dangerous to store this in the map; if anyone
472  // RAUW's the GV uses of this constant will be invalid.
473  llvm::Constant *castedAddr =
474  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(var, expectedType);
475  LocalDeclMap.find(&D)->second = Address(castedAddr, elemTy, alignment);
476  CGM.setStaticLocalDeclAddress(&D, castedAddr);
477 
479 
480  // Emit global variable debug descriptor for static vars.
481  CGDebugInfo *DI = getDebugInfo();
482  if (DI && CGM.getCodeGenOpts().hasReducedDebugInfo()) {
483  DI->setLocation(D.getLocation());
484  DI->EmitGlobalVariable(var, &D);
485  }
486 }
487 
488 namespace {
489  struct DestroyObject final : EHScopeStack::Cleanup {
490  DestroyObject(Address addr, QualType type,
491  CodeGenFunction::Destroyer *destroyer,
492  bool useEHCleanupForArray)
493  : addr(addr), type(type), destroyer(destroyer),
494  useEHCleanupForArray(useEHCleanupForArray) {}
495 
496  Address addr;
497  QualType type;
498  CodeGenFunction::Destroyer *destroyer;
499  bool useEHCleanupForArray;
500 
501  void Emit(CodeGenFunction &CGF, Flags flags) override {
502  // Don't use an EH cleanup recursively from an EH cleanup.
503  bool useEHCleanupForArray =
504  flags.isForNormalCleanup() && this->useEHCleanupForArray;
505 
506  CGF.emitDestroy(addr, type, destroyer, useEHCleanupForArray);
507  }
508  };
509 
510  template <class Derived>
511  struct DestroyNRVOVariable : EHScopeStack::Cleanup {
512  DestroyNRVOVariable(Address addr, QualType type, llvm::Value *NRVOFlag)
513  : NRVOFlag(NRVOFlag), Loc(addr), Ty(type) {}
514 
515  llvm::Value *NRVOFlag;
516  Address Loc;
517  QualType Ty;
518 
519  void Emit(CodeGenFunction &CGF, Flags flags) override {
520  // Along the exceptions path we always execute the dtor.
521  bool NRVO = flags.isForNormalCleanup() && NRVOFlag;
522 
523  llvm::BasicBlock *SkipDtorBB = nullptr;
524  if (NRVO) {
525  // If we exited via NRVO, we skip the destructor call.
526  llvm::BasicBlock *RunDtorBB = CGF.createBasicBlock("nrvo.unused");
527  SkipDtorBB = CGF.createBasicBlock("nrvo.skipdtor");
528  llvm::Value *DidNRVO =
529  CGF.Builder.CreateFlagLoad(NRVOFlag, "nrvo.val");
530  CGF.Builder.CreateCondBr(DidNRVO, SkipDtorBB, RunDtorBB);
531  CGF.EmitBlock(RunDtorBB);
532  }
533 
534  static_cast<Derived *>(this)->emitDestructorCall(CGF);
535 
536  if (NRVO) CGF.EmitBlock(SkipDtorBB);
537  }
538 
539  virtual ~DestroyNRVOVariable() = default;
540  };
541 
542  struct DestroyNRVOVariableCXX final
543  : DestroyNRVOVariable<DestroyNRVOVariableCXX> {
544  DestroyNRVOVariableCXX(Address addr, QualType type,
545  const CXXDestructorDecl *Dtor, llvm::Value *NRVOFlag)
546  : DestroyNRVOVariable<DestroyNRVOVariableCXX>(addr, type, NRVOFlag),
547  Dtor(Dtor) {}
548 
549  const CXXDestructorDecl *Dtor;
550 
551  void emitDestructorCall(CodeGenFunction &CGF) {
553  /*ForVirtualBase=*/false,
554  /*Delegating=*/false, Loc, Ty);
555  }
556  };
557 
558  struct DestroyNRVOVariableC final
559  : DestroyNRVOVariable<DestroyNRVOVariableC> {
560  DestroyNRVOVariableC(Address addr, llvm::Value *NRVOFlag, QualType Ty)
561  : DestroyNRVOVariable<DestroyNRVOVariableC>(addr, Ty, NRVOFlag) {}
562 
563  void emitDestructorCall(CodeGenFunction &CGF) {
564  CGF.destroyNonTrivialCStruct(CGF, Loc, Ty);
565  }
566  };
567 
568  struct CallStackRestore final : EHScopeStack::Cleanup {
569  Address Stack;
570  CallStackRestore(Address Stack) : Stack(Stack) {}
571  bool isRedundantBeforeReturn() override { return true; }
572  void Emit(CodeGenFunction &CGF, Flags flags) override {
573  llvm::Value *V = CGF.Builder.CreateLoad(Stack);
574  llvm::Function *F = CGF.CGM.getIntrinsic(llvm::Intrinsic::stackrestore);
575  CGF.Builder.CreateCall(F, V);
576  }
577  };
578 
579  struct ExtendGCLifetime final : EHScopeStack::Cleanup {
580  const VarDecl &Var;
581  ExtendGCLifetime(const VarDecl *var) : Var(*var) {}
582 
583  void Emit(CodeGenFunction &CGF, Flags flags) override {
584  // Compute the address of the local variable, in case it's a
585  // byref or something.
586  DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
587  Var.getType(), VK_LValue, SourceLocation());
588  llvm::Value *value = CGF.EmitLoadOfScalar(CGF.EmitDeclRefLValue(&DRE),
589  SourceLocation());
590  CGF.EmitExtendGCLifetime(value);
591  }
592  };
593 
594  struct CallCleanupFunction final : EHScopeStack::Cleanup {
595  llvm::Constant *CleanupFn;
596  const CGFunctionInfo &FnInfo;
597  const VarDecl &Var;
598 
599  CallCleanupFunction(llvm::Constant *CleanupFn, const CGFunctionInfo *Info,
600  const VarDecl *Var)
601  : CleanupFn(CleanupFn), FnInfo(*Info), Var(*Var) {}
602 
603  void Emit(CodeGenFunction &CGF, Flags flags) override {
604  DeclRefExpr DRE(CGF.getContext(), const_cast<VarDecl *>(&Var), false,
605  Var.getType(), VK_LValue, SourceLocation());
606  // Compute the address of the local variable, in case it's a byref
607  // or something.
608  llvm::Value *Addr = CGF.EmitDeclRefLValue(&DRE).getPointer(CGF);
609 
610  // In some cases, the type of the function argument will be different from
611  // the type of the pointer. An example of this is
612  // void f(void* arg);
613  // __attribute__((cleanup(f))) void *g;
614  //
615  // To fix this we insert a bitcast here.
616  QualType ArgTy = FnInfo.arg_begin()->type;
617  llvm::Value *Arg =
618  CGF.Builder.CreateBitCast(Addr, CGF.ConvertType(ArgTy));
619 
620  CallArgList Args;
621  Args.add(RValue::get(Arg),
622  CGF.getContext().getPointerType(Var.getType()));
623  auto Callee = CGCallee::forDirect(CleanupFn);
624  CGF.EmitCall(FnInfo, Callee, ReturnValueSlot(), Args);
625  }
626  };
627 } // end anonymous namespace
628 
629 /// EmitAutoVarWithLifetime - Does the setup required for an automatic
630 /// variable with lifetime.
632  Address addr,
633  Qualifiers::ObjCLifetime lifetime) {
634  switch (lifetime) {
636  llvm_unreachable("present but none");
637 
639  // nothing to do
640  break;
641 
642  case Qualifiers::OCL_Strong: {
643  CodeGenFunction::Destroyer *destroyer =
644  (var.hasAttr<ObjCPreciseLifetimeAttr>()
647 
648  CleanupKind cleanupKind = CGF.getARCCleanupKind();
649  CGF.pushDestroy(cleanupKind, addr, var.getType(), destroyer,
650  cleanupKind & EHCleanup);
651  break;
652  }
654  // nothing to do
655  break;
656 
658  // __weak objects always get EH cleanups; otherwise, exceptions
659  // could cause really nasty crashes instead of mere leaks.
660  CGF.pushDestroy(NormalAndEHCleanup, addr, var.getType(),
662  /*useEHCleanup*/ true);
663  break;
664  }
665 }
666 
667 static bool isAccessedBy(const VarDecl &var, const Stmt *s) {
668  if (const Expr *e = dyn_cast<Expr>(s)) {
669  // Skip the most common kinds of expressions that make
670  // hierarchy-walking expensive.
671  s = e = e->IgnoreParenCasts();
672 
673  if (const DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e))
674  return (ref->getDecl() == &var);
675  if (const BlockExpr *be = dyn_cast<BlockExpr>(e)) {
676  const BlockDecl *block = be->getBlockDecl();
677  for (const auto &I : block->captures()) {
678  if (I.getVariable() == &var)
679  return true;
680  }
681  }
682  }
683 
684  for (const Stmt *SubStmt : s->children())
685  // SubStmt might be null; as in missing decl or conditional of an if-stmt.
686  if (SubStmt && isAccessedBy(var, SubStmt))
687  return true;
688 
689  return false;
690 }
691 
692 static bool isAccessedBy(const ValueDecl *decl, const Expr *e) {
693  if (!decl) return false;
694  if (!isa<VarDecl>(decl)) return false;
695  const VarDecl *var = cast<VarDecl>(decl);
696  return isAccessedBy(*var, e);
697 }
698 
700  const LValue &destLV, const Expr *init) {
701  bool needsCast = false;
702 
703  while (auto castExpr = dyn_cast<CastExpr>(init->IgnoreParens())) {
704  switch (castExpr->getCastKind()) {
705  // Look through casts that don't require representation changes.
706  case CK_NoOp:
707  case CK_BitCast:
708  case CK_BlockPointerToObjCPointerCast:
709  needsCast = true;
710  break;
711 
712  // If we find an l-value to r-value cast from a __weak variable,
713  // emit this operation as a copy or move.
714  case CK_LValueToRValue: {
715  const Expr *srcExpr = castExpr->getSubExpr();
716  if (srcExpr->getType().getObjCLifetime() != Qualifiers::OCL_Weak)
717  return false;
718 
719  // Emit the source l-value.
720  LValue srcLV = CGF.EmitLValue(srcExpr);
721 
722  // Handle a formal type change to avoid asserting.
723  auto srcAddr = srcLV.getAddress(CGF);
724  if (needsCast) {
725  srcAddr = CGF.Builder.CreateElementBitCast(
726  srcAddr, destLV.getAddress(CGF).getElementType());
727  }
728 
729  // If it was an l-value, use objc_copyWeak.
730  if (srcExpr->isLValue()) {
731  CGF.EmitARCCopyWeak(destLV.getAddress(CGF), srcAddr);
732  } else {
733  assert(srcExpr->isXValue());
734  CGF.EmitARCMoveWeak(destLV.getAddress(CGF), srcAddr);
735  }
736  return true;
737  }
738 
739  // Stop at anything else.
740  default:
741  return false;
742  }
743 
744  init = castExpr->getSubExpr();
745  }
746  return false;
747 }
748 
750  LValue &lvalue,
751  const VarDecl *var) {
752  lvalue.setAddress(CGF.emitBlockByrefAddress(lvalue.getAddress(CGF), var));
753 }
754 
756  SourceLocation Loc) {
757  if (!SanOpts.has(SanitizerKind::NullabilityAssign))
758  return;
759 
760  auto Nullability = LHS.getType()->getNullability(getContext());
762  return;
763 
764  // Check if the right hand side of the assignment is nonnull, if the left
765  // hand side must be nonnull.
766  SanitizerScope SanScope(this);
767  llvm::Value *IsNotNull = Builder.CreateIsNotNull(RHS);
768  llvm::Constant *StaticData[] = {
770  llvm::ConstantInt::get(Int8Ty, 0), // The LogAlignment info is unused.
771  llvm::ConstantInt::get(Int8Ty, TCK_NonnullAssign)};
772  EmitCheck({{IsNotNull, SanitizerKind::NullabilityAssign}},
773  SanitizerHandler::TypeMismatch, StaticData, RHS);
774 }
775 
776 void CodeGenFunction::EmitScalarInit(const Expr *init, const ValueDecl *D,
777  LValue lvalue, bool capturedByInit) {
778  Qualifiers::ObjCLifetime lifetime = lvalue.getObjCLifetime();
779  if (!lifetime) {
780  llvm::Value *value = EmitScalarExpr(init);
781  if (capturedByInit)
782  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
783  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
784  EmitStoreThroughLValue(RValue::get(value), lvalue, true);
785  return;
786  }
787 
788  if (const CXXDefaultInitExpr *DIE = dyn_cast<CXXDefaultInitExpr>(init))
789  init = DIE->getExpr();
790 
791  // If we're emitting a value with lifetime, we have to do the
792  // initialization *before* we leave the cleanup scopes.
793  if (auto *EWC = dyn_cast<ExprWithCleanups>(init)) {
795  return EmitScalarInit(EWC->getSubExpr(), D, lvalue, capturedByInit);
796  }
797 
798  // We have to maintain the illusion that the variable is
799  // zero-initialized. If the variable might be accessed in its
800  // initializer, zero-initialize before running the initializer, then
801  // actually perform the initialization with an assign.
802  bool accessedByInit = false;
803  if (lifetime != Qualifiers::OCL_ExplicitNone)
804  accessedByInit = (capturedByInit || isAccessedBy(D, init));
805  if (accessedByInit) {
806  LValue tempLV = lvalue;
807  // Drill down to the __block object if necessary.
808  if (capturedByInit) {
809  // We can use a simple GEP for this because it can't have been
810  // moved yet.
811  tempLV.setAddress(emitBlockByrefAddress(tempLV.getAddress(*this),
812  cast<VarDecl>(D),
813  /*follow*/ false));
814  }
815 
816  auto ty =
817  cast<llvm::PointerType>(tempLV.getAddress(*this).getElementType());
818  llvm::Value *zero = CGM.getNullPointer(ty, tempLV.getType());
819 
820  // If __weak, we want to use a barrier under certain conditions.
821  if (lifetime == Qualifiers::OCL_Weak)
822  EmitARCInitWeak(tempLV.getAddress(*this), zero);
823 
824  // Otherwise just do a simple store.
825  else
826  EmitStoreOfScalar(zero, tempLV, /* isInitialization */ true);
827  }
828 
829  // Emit the initializer.
830  llvm::Value *value = nullptr;
831 
832  switch (lifetime) {
834  llvm_unreachable("present but none");
835 
836  case Qualifiers::OCL_Strong: {
837  if (!D || !isa<VarDecl>(D) || !cast<VarDecl>(D)->isARCPseudoStrong()) {
838  value = EmitARCRetainScalarExpr(init);
839  break;
840  }
841  // If D is pseudo-strong, treat it like __unsafe_unretained here. This means
842  // that we omit the retain, and causes non-autoreleased return values to be
843  // immediately released.
844  [[fallthrough]];
845  }
846 
848  value = EmitARCUnsafeUnretainedScalarExpr(init);
849  break;
850 
851  case Qualifiers::OCL_Weak: {
852  // If it's not accessed by the initializer, try to emit the
853  // initialization with a copy or move.
854  if (!accessedByInit && tryEmitARCCopyWeakInit(*this, lvalue, init)) {
855  return;
856  }
857 
858  // No way to optimize a producing initializer into this. It's not
859  // worth optimizing for, because the value will immediately
860  // disappear in the common case.
861  value = EmitScalarExpr(init);
862 
863  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
864  if (accessedByInit)
865  EmitARCStoreWeak(lvalue.getAddress(*this), value, /*ignored*/ true);
866  else
867  EmitARCInitWeak(lvalue.getAddress(*this), value);
868  return;
869  }
870 
873  break;
874  }
875 
876  if (capturedByInit) drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
877 
878  EmitNullabilityCheck(lvalue, value, init->getExprLoc());
879 
880  // If the variable might have been accessed by its initializer, we
881  // might have to initialize with a barrier. We have to do this for
882  // both __weak and __strong, but __weak got filtered out above.
883  if (accessedByInit && lifetime == Qualifiers::OCL_Strong) {
884  llvm::Value *oldValue = EmitLoadOfScalar(lvalue, init->getExprLoc());
885  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
887  return;
888  }
889 
890  EmitStoreOfScalar(value, lvalue, /* isInitialization */ true);
891 }
892 
893 /// Decide whether we can emit the non-zero parts of the specified initializer
894 /// with equal or fewer than NumStores scalar stores.
895 static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init,
896  unsigned &NumStores) {
897  // Zero and Undef never requires any extra stores.
898  if (isa<llvm::ConstantAggregateZero>(Init) ||
899  isa<llvm::ConstantPointerNull>(Init) ||
900  isa<llvm::UndefValue>(Init))
901  return true;
902  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
903  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
904  isa<llvm::ConstantExpr>(Init))
905  return Init->isNullValue() || NumStores--;
906 
907  // See if we can emit each element.
908  if (isa<llvm::ConstantArray>(Init) || isa<llvm::ConstantStruct>(Init)) {
909  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
910  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
911  if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
912  return false;
913  }
914  return true;
915  }
916 
917  if (llvm::ConstantDataSequential *CDS =
918  dyn_cast<llvm::ConstantDataSequential>(Init)) {
919  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
920  llvm::Constant *Elt = CDS->getElementAsConstant(i);
921  if (!canEmitInitWithFewStoresAfterBZero(Elt, NumStores))
922  return false;
923  }
924  return true;
925  }
926 
927  // Anything else is hard and scary.
928  return false;
929 }
930 
931 /// For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit
932 /// the scalar stores that would be required.
934  llvm::Constant *Init, Address Loc,
935  bool isVolatile, CGBuilderTy &Builder,
936  bool IsAutoInit) {
937  assert(!Init->isNullValue() && !isa<llvm::UndefValue>(Init) &&
938  "called emitStoresForInitAfterBZero for zero or undef value.");
939 
940  if (isa<llvm::ConstantInt>(Init) || isa<llvm::ConstantFP>(Init) ||
941  isa<llvm::ConstantVector>(Init) || isa<llvm::BlockAddress>(Init) ||
942  isa<llvm::ConstantExpr>(Init)) {
943  auto *I = Builder.CreateStore(Init, Loc, isVolatile);
944  if (IsAutoInit)
945  I->addAnnotationMetadata("auto-init");
946  return;
947  }
948 
949  if (llvm::ConstantDataSequential *CDS =
950  dyn_cast<llvm::ConstantDataSequential>(Init)) {
951  for (unsigned i = 0, e = CDS->getNumElements(); i != e; ++i) {
952  llvm::Constant *Elt = CDS->getElementAsConstant(i);
953 
954  // If necessary, get a pointer to the element and emit it.
955  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
957  CGM, Elt, Builder.CreateConstInBoundsGEP2_32(Loc, 0, i), isVolatile,
958  Builder, IsAutoInit);
959  }
960  return;
961  }
962 
963  assert((isa<llvm::ConstantStruct>(Init) || isa<llvm::ConstantArray>(Init)) &&
964  "Unknown value type!");
965 
966  for (unsigned i = 0, e = Init->getNumOperands(); i != e; ++i) {
967  llvm::Constant *Elt = cast<llvm::Constant>(Init->getOperand(i));
968 
969  // If necessary, get a pointer to the element and emit it.
970  if (!Elt->isNullValue() && !isa<llvm::UndefValue>(Elt))
972  Builder.CreateConstInBoundsGEP2_32(Loc, 0, i),
973  isVolatile, Builder, IsAutoInit);
974  }
975 }
976 
977 /// Decide whether we should use bzero plus some stores to initialize a local
978 /// variable instead of using a memcpy from a constant global. It is beneficial
979 /// to use bzero if the global is all zeros, or mostly zeros and large.
980 static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init,
981  uint64_t GlobalSize) {
982  // If a global is all zeros, always use a bzero.
983  if (isa<llvm::ConstantAggregateZero>(Init)) return true;
984 
985  // If a non-zero global is <= 32 bytes, always use a memcpy. If it is large,
986  // do it if it will require 6 or fewer scalar stores.
987  // TODO: Should budget depends on the size? Avoiding a large global warrants
988  // plopping in more stores.
989  unsigned StoreBudget = 6;
990  uint64_t SizeLimit = 32;
991 
992  return GlobalSize > SizeLimit &&
993  canEmitInitWithFewStoresAfterBZero(Init, StoreBudget);
994 }
995 
996 /// Decide whether we should use memset to initialize a local variable instead
997 /// of using a memcpy from a constant global. Assumes we've already decided to
998 /// not user bzero.
999 /// FIXME We could be more clever, as we are for bzero above, and generate
1000 /// memset followed by stores. It's unclear that's worth the effort.
1001 static llvm::Value *shouldUseMemSetToInitialize(llvm::Constant *Init,
1002  uint64_t GlobalSize,
1003  const llvm::DataLayout &DL) {
1004  uint64_t SizeLimit = 32;
1005  if (GlobalSize <= SizeLimit)
1006  return nullptr;
1007  return llvm::isBytewiseValue(Init, DL);
1008 }
1009 
1010 /// Decide whether we want to split a constant structure or array store into a
1011 /// sequence of its fields' stores. This may cost us code size and compilation
1012 /// speed, but plays better with store optimizations.
1014  uint64_t GlobalByteSize) {
1015  // Don't break things that occupy more than one cacheline.
1016  uint64_t ByteSizeLimit = 64;
1017  if (CGM.getCodeGenOpts().OptimizationLevel == 0)
1018  return false;
1019  if (GlobalByteSize <= ByteSizeLimit)
1020  return true;
1021  return false;
1022 }
1023 
1024 enum class IsPattern { No, Yes };
1025 
1026 /// Generate a constant filled with either a pattern or zeroes.
1027 static llvm::Constant *patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern,
1028  llvm::Type *Ty) {
1029  if (isPattern == IsPattern::Yes)
1030  return initializationPatternFor(CGM, Ty);
1031  else
1032  return llvm::Constant::getNullValue(Ty);
1033 }
1034 
1035 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1036  llvm::Constant *constant);
1037 
1038 /// Helper function for constWithPadding() to deal with padding in structures.
1039 static llvm::Constant *constStructWithPadding(CodeGenModule &CGM,
1040  IsPattern isPattern,
1041  llvm::StructType *STy,
1042  llvm::Constant *constant) {
1043  const llvm::DataLayout &DL = CGM.getDataLayout();
1044  const llvm::StructLayout *Layout = DL.getStructLayout(STy);
1045  llvm::Type *Int8Ty = llvm::IntegerType::getInt8Ty(CGM.getLLVMContext());
1046  unsigned SizeSoFar = 0;
1048  bool NestedIntact = true;
1049  for (unsigned i = 0, e = STy->getNumElements(); i != e; i++) {
1050  unsigned CurOff = Layout->getElementOffset(i);
1051  if (SizeSoFar < CurOff) {
1052  assert(!STy->isPacked());
1053  auto *PadTy = llvm::ArrayType::get(Int8Ty, CurOff - SizeSoFar);
1054  Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1055  }
1056  llvm::Constant *CurOp;
1057  if (constant->isZeroValue())
1058  CurOp = llvm::Constant::getNullValue(STy->getElementType(i));
1059  else
1060  CurOp = cast<llvm::Constant>(constant->getAggregateElement(i));
1061  auto *NewOp = constWithPadding(CGM, isPattern, CurOp);
1062  if (CurOp != NewOp)
1063  NestedIntact = false;
1064  Values.push_back(NewOp);
1065  SizeSoFar = CurOff + DL.getTypeAllocSize(CurOp->getType());
1066  }
1067  unsigned TotalSize = Layout->getSizeInBytes();
1068  if (SizeSoFar < TotalSize) {
1069  auto *PadTy = llvm::ArrayType::get(Int8Ty, TotalSize - SizeSoFar);
1070  Values.push_back(patternOrZeroFor(CGM, isPattern, PadTy));
1071  }
1072  if (NestedIntact && Values.size() == STy->getNumElements())
1073  return constant;
1074  return llvm::ConstantStruct::getAnon(Values, STy->isPacked());
1075 }
1076 
1077 /// Replace all padding bytes in a given constant with either a pattern byte or
1078 /// 0x00.
1079 static llvm::Constant *constWithPadding(CodeGenModule &CGM, IsPattern isPattern,
1080  llvm::Constant *constant) {
1081  llvm::Type *OrigTy = constant->getType();
1082  if (const auto STy = dyn_cast<llvm::StructType>(OrigTy))
1083  return constStructWithPadding(CGM, isPattern, STy, constant);
1084  if (auto *ArrayTy = dyn_cast<llvm::ArrayType>(OrigTy)) {
1086  uint64_t Size = ArrayTy->getNumElements();
1087  if (!Size)
1088  return constant;
1089  llvm::Type *ElemTy = ArrayTy->getElementType();
1090  bool ZeroInitializer = constant->isNullValue();
1091  llvm::Constant *OpValue, *PaddedOp;
1092  if (ZeroInitializer) {
1093  OpValue = llvm::Constant::getNullValue(ElemTy);
1094  PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1095  }
1096  for (unsigned Op = 0; Op != Size; ++Op) {
1097  if (!ZeroInitializer) {
1098  OpValue = constant->getAggregateElement(Op);
1099  PaddedOp = constWithPadding(CGM, isPattern, OpValue);
1100  }
1101  Values.push_back(PaddedOp);
1102  }
1103  auto *NewElemTy = Values[0]->getType();
1104  if (NewElemTy == ElemTy)
1105  return constant;
1106  auto *NewArrayTy = llvm::ArrayType::get(NewElemTy, Size);
1107  return llvm::ConstantArray::get(NewArrayTy, Values);
1108  }
1109  // FIXME: Add handling for tail padding in vectors. Vectors don't
1110  // have padding between or inside elements, but the total amount of
1111  // data can be less than the allocated size.
1112  return constant;
1113 }
1114 
1116  llvm::Constant *Constant,
1117  CharUnits Align) {
1118  auto FunctionName = [&](const DeclContext *DC) -> std::string {
1119  if (const auto *FD = dyn_cast<FunctionDecl>(DC)) {
1120  if (const auto *CC = dyn_cast<CXXConstructorDecl>(FD))
1121  return CC->getNameAsString();
1122  if (const auto *CD = dyn_cast<CXXDestructorDecl>(FD))
1123  return CD->getNameAsString();
1124  return std::string(getMangledName(FD));
1125  } else if (const auto *OM = dyn_cast<ObjCMethodDecl>(DC)) {
1126  return OM->getNameAsString();
1127  } else if (isa<BlockDecl>(DC)) {
1128  return "<block>";
1129  } else if (isa<CapturedDecl>(DC)) {
1130  return "<captured>";
1131  } else {
1132  llvm_unreachable("expected a function or method");
1133  }
1134  };
1135 
1136  // Form a simple per-variable cache of these values in case we find we
1137  // want to reuse them.
1138  llvm::GlobalVariable *&CacheEntry = InitializerConstants[&D];
1139  if (!CacheEntry || CacheEntry->getInitializer() != Constant) {
1140  auto *Ty = Constant->getType();
1141  bool isConstant = true;
1142  llvm::GlobalVariable *InsertBefore = nullptr;
1143  unsigned AS =
1145  std::string Name;
1146  if (D.hasGlobalStorage())
1147  Name = getMangledName(&D).str() + ".const";
1148  else if (const DeclContext *DC = D.getParentFunctionOrMethod())
1149  Name = ("__const." + FunctionName(DC) + "." + D.getName()).str();
1150  else
1151  llvm_unreachable("local variable has no parent function or method");
1152  llvm::GlobalVariable *GV = new llvm::GlobalVariable(
1153  getModule(), Ty, isConstant, llvm::GlobalValue::PrivateLinkage,
1154  Constant, Name, InsertBefore, llvm::GlobalValue::NotThreadLocal, AS);
1155  GV->setAlignment(Align.getAsAlign());
1156  GV->setUnnamedAddr(llvm::GlobalValue::UnnamedAddr::Global);
1157  CacheEntry = GV;
1158  } else if (CacheEntry->getAlignment() < uint64_t(Align.getQuantity())) {
1159  CacheEntry->setAlignment(Align.getAsAlign());
1160  }
1161 
1162  return Address(CacheEntry, CacheEntry->getValueType(), Align);
1163 }
1164 
1166  const VarDecl &D,
1167  CGBuilderTy &Builder,
1168  llvm::Constant *Constant,
1169  CharUnits Align) {
1170  Address SrcPtr = CGM.createUnnamedGlobalFrom(D, Constant, Align);
1171  return Builder.CreateElementBitCast(SrcPtr, CGM.Int8Ty);
1172 }
1173 
1174 static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D,
1175  Address Loc, bool isVolatile,
1176  CGBuilderTy &Builder,
1177  llvm::Constant *constant, bool IsAutoInit) {
1178  auto *Ty = constant->getType();
1179  uint64_t ConstantSize = CGM.getDataLayout().getTypeAllocSize(Ty);
1180  if (!ConstantSize)
1181  return;
1182 
1183  bool canDoSingleStore = Ty->isIntOrIntVectorTy() ||
1184  Ty->isPtrOrPtrVectorTy() || Ty->isFPOrFPVectorTy();
1185  if (canDoSingleStore) {
1186  auto *I = Builder.CreateStore(constant, Loc, isVolatile);
1187  if (IsAutoInit)
1188  I->addAnnotationMetadata("auto-init");
1189  return;
1190  }
1191 
1192  auto *SizeVal = llvm::ConstantInt::get(CGM.IntPtrTy, ConstantSize);
1193 
1194  // If the initializer is all or mostly the same, codegen with bzero / memset
1195  // then do a few stores afterward.
1196  if (shouldUseBZeroPlusStoresToInitialize(constant, ConstantSize)) {
1197  auto *I = Builder.CreateMemSet(Loc, llvm::ConstantInt::get(CGM.Int8Ty, 0),
1198  SizeVal, isVolatile);
1199  if (IsAutoInit)
1200  I->addAnnotationMetadata("auto-init");
1201 
1202  bool valueAlreadyCorrect =
1203  constant->isNullValue() || isa<llvm::UndefValue>(constant);
1204  if (!valueAlreadyCorrect) {
1205  Loc = Builder.CreateElementBitCast(Loc, Ty);
1206  emitStoresForInitAfterBZero(CGM, constant, Loc, isVolatile, Builder,
1207  IsAutoInit);
1208  }
1209  return;
1210  }
1211 
1212  // If the initializer is a repeated byte pattern, use memset.
1213  llvm::Value *Pattern =
1214  shouldUseMemSetToInitialize(constant, ConstantSize, CGM.getDataLayout());
1215  if (Pattern) {
1216  uint64_t Value = 0x00;
1217  if (!isa<llvm::UndefValue>(Pattern)) {
1218  const llvm::APInt &AP = cast<llvm::ConstantInt>(Pattern)->getValue();
1219  assert(AP.getBitWidth() <= 8);
1220  Value = AP.getLimitedValue();
1221  }
1222  auto *I = Builder.CreateMemSet(
1223  Loc, llvm::ConstantInt::get(CGM.Int8Ty, Value), SizeVal, isVolatile);
1224  if (IsAutoInit)
1225  I->addAnnotationMetadata("auto-init");
1226  return;
1227  }
1228 
1229  // If the initializer is small, use a handful of stores.
1230  if (shouldSplitConstantStore(CGM, ConstantSize)) {
1231  if (auto *STy = dyn_cast<llvm::StructType>(Ty)) {
1232  // FIXME: handle the case when STy != Loc.getElementType().
1233  if (STy == Loc.getElementType()) {
1234  for (unsigned i = 0; i != constant->getNumOperands(); i++) {
1235  Address EltPtr = Builder.CreateStructGEP(Loc, i);
1237  CGM, D, EltPtr, isVolatile, Builder,
1238  cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)),
1239  IsAutoInit);
1240  }
1241  return;
1242  }
1243  } else if (auto *ATy = dyn_cast<llvm::ArrayType>(Ty)) {
1244  // FIXME: handle the case when ATy != Loc.getElementType().
1245  if (ATy == Loc.getElementType()) {
1246  for (unsigned i = 0; i != ATy->getNumElements(); i++) {
1247  Address EltPtr = Builder.CreateConstArrayGEP(Loc, i);
1249  CGM, D, EltPtr, isVolatile, Builder,
1250  cast<llvm::Constant>(Builder.CreateExtractValue(constant, i)),
1251  IsAutoInit);
1252  }
1253  return;
1254  }
1255  }
1256  }
1257 
1258  // Copy from a global.
1259  auto *I =
1260  Builder.CreateMemCpy(Loc,
1262  CGM, D, Builder, constant, Loc.getAlignment()),
1263  SizeVal, isVolatile);
1264  if (IsAutoInit)
1265  I->addAnnotationMetadata("auto-init");
1266 }
1267 
1268 static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D,
1269  Address Loc, bool isVolatile,
1270  CGBuilderTy &Builder) {
1271  llvm::Type *ElTy = Loc.getElementType();
1272  llvm::Constant *constant =
1273  constWithPadding(CGM, IsPattern::No, llvm::Constant::getNullValue(ElTy));
1274  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant,
1275  /*IsAutoInit=*/true);
1276 }
1277 
1279  Address Loc, bool isVolatile,
1280  CGBuilderTy &Builder) {
1281  llvm::Type *ElTy = Loc.getElementType();
1282  llvm::Constant *constant = constWithPadding(
1283  CGM, IsPattern::Yes, initializationPatternFor(CGM, ElTy));
1284  assert(!isa<llvm::UndefValue>(constant));
1285  emitStoresForConstant(CGM, D, Loc, isVolatile, Builder, constant,
1286  /*IsAutoInit=*/true);
1287 }
1288 
1289 static bool containsUndef(llvm::Constant *constant) {
1290  auto *Ty = constant->getType();
1291  if (isa<llvm::UndefValue>(constant))
1292  return true;
1293  if (Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy())
1294  for (llvm::Use &Op : constant->operands())
1295  if (containsUndef(cast<llvm::Constant>(Op)))
1296  return true;
1297  return false;
1298 }
1299 
1300 static llvm::Constant *replaceUndef(CodeGenModule &CGM, IsPattern isPattern,
1301  llvm::Constant *constant) {
1302  auto *Ty = constant->getType();
1303  if (isa<llvm::UndefValue>(constant))
1304  return patternOrZeroFor(CGM, isPattern, Ty);
1305  if (!(Ty->isStructTy() || Ty->isArrayTy() || Ty->isVectorTy()))
1306  return constant;
1307  if (!containsUndef(constant))
1308  return constant;
1309  llvm::SmallVector<llvm::Constant *, 8> Values(constant->getNumOperands());
1310  for (unsigned Op = 0, NumOp = constant->getNumOperands(); Op != NumOp; ++Op) {
1311  auto *OpValue = cast<llvm::Constant>(constant->getOperand(Op));
1312  Values[Op] = replaceUndef(CGM, isPattern, OpValue);
1313  }
1314  if (Ty->isStructTy())
1315  return llvm::ConstantStruct::get(cast<llvm::StructType>(Ty), Values);
1316  if (Ty->isArrayTy())
1317  return llvm::ConstantArray::get(cast<llvm::ArrayType>(Ty), Values);
1318  assert(Ty->isVectorTy());
1319  return llvm::ConstantVector::get(Values);
1320 }
1321 
1322 /// EmitAutoVarDecl - Emit code and set up an entry in LocalDeclMap for a
1323 /// variable declaration with auto, register, or no storage class specifier.
1324 /// These turn into simple stack objects, or GlobalValues depending on target.
1326  AutoVarEmission emission = EmitAutoVarAlloca(D);
1327  EmitAutoVarInit(emission);
1328  EmitAutoVarCleanups(emission);
1329 }
1330 
1331 /// Emit a lifetime.begin marker if some criteria are satisfied.
1332 /// \return a pointer to the temporary size Value if a marker was emitted, null
1333 /// otherwise
1334 llvm::Value *CodeGenFunction::EmitLifetimeStart(llvm::TypeSize Size,
1335  llvm::Value *Addr) {
1336  if (!ShouldEmitLifetimeMarkers)
1337  return nullptr;
1338 
1339  assert(Addr->getType()->getPointerAddressSpace() ==
1340  CGM.getDataLayout().getAllocaAddrSpace() &&
1341  "Pointer should be in alloca address space");
1342  llvm::Value *SizeV = llvm::ConstantInt::get(
1343  Int64Ty, Size.isScalable() ? -1 : Size.getFixedValue());
1344  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1345  llvm::CallInst *C =
1346  Builder.CreateCall(CGM.getLLVMLifetimeStartFn(), {SizeV, Addr});
1347  C->setDoesNotThrow();
1348  return SizeV;
1349 }
1350 
1351 void CodeGenFunction::EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr) {
1352  assert(Addr->getType()->getPointerAddressSpace() ==
1353  CGM.getDataLayout().getAllocaAddrSpace() &&
1354  "Pointer should be in alloca address space");
1355  Addr = Builder.CreateBitCast(Addr, AllocaInt8PtrTy);
1356  llvm::CallInst *C =
1357  Builder.CreateCall(CGM.getLLVMLifetimeEndFn(), {Size, Addr});
1358  C->setDoesNotThrow();
1359 }
1360 
1362  CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo) {
1363  // For each dimension stores its QualType and corresponding
1364  // size-expression Value.
1366  SmallVector<IdentifierInfo *, 4> VLAExprNames;
1367 
1368  // Break down the array into individual dimensions.
1369  QualType Type1D = D.getType();
1370  while (getContext().getAsVariableArrayType(Type1D)) {
1371  auto VlaSize = getVLAElements1D(Type1D);
1372  if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1373  Dimensions.emplace_back(C, Type1D.getUnqualifiedType());
1374  else {
1375  // Generate a locally unique name for the size expression.
1376  Twine Name = Twine("__vla_expr") + Twine(VLAExprCounter++);
1377  SmallString<12> Buffer;
1378  StringRef NameRef = Name.toStringRef(Buffer);
1379  auto &Ident = getContext().Idents.getOwn(NameRef);
1380  VLAExprNames.push_back(&Ident);
1381  auto SizeExprAddr =
1382  CreateDefaultAlignTempAlloca(VlaSize.NumElts->getType(), NameRef);
1383  Builder.CreateStore(VlaSize.NumElts, SizeExprAddr);
1384  Dimensions.emplace_back(SizeExprAddr.getPointer(),
1385  Type1D.getUnqualifiedType());
1386  }
1387  Type1D = VlaSize.Type;
1388  }
1389 
1390  if (!EmitDebugInfo)
1391  return;
1392 
1393  // Register each dimension's size-expression with a DILocalVariable,
1394  // so that it can be used by CGDebugInfo when instantiating a DISubrange
1395  // to describe this array.
1396  unsigned NameIdx = 0;
1397  for (auto &VlaSize : Dimensions) {
1398  llvm::Metadata *MD;
1399  if (auto *C = dyn_cast<llvm::ConstantInt>(VlaSize.NumElts))
1400  MD = llvm::ConstantAsMetadata::get(C);
1401  else {
1402  // Create an artificial VarDecl to generate debug info for.
1403  IdentifierInfo *NameIdent = VLAExprNames[NameIdx++];
1404  assert(cast<llvm::PointerType>(VlaSize.NumElts->getType())
1405  ->isOpaqueOrPointeeTypeMatches(SizeTy) &&
1406  "Number of VLA elements must be SizeTy");
1407  auto QT = getContext().getIntTypeForBitwidth(
1408  SizeTy->getScalarSizeInBits(), false);
1409  auto *ArtificialDecl = VarDecl::Create(
1410  getContext(), const_cast<DeclContext *>(D.getDeclContext()),
1411  D.getLocation(), D.getLocation(), NameIdent, QT,
1412  getContext().CreateTypeSourceInfo(QT), SC_Auto);
1413  ArtificialDecl->setImplicit();
1414 
1415  MD = DI->EmitDeclareOfAutoVariable(ArtificialDecl, VlaSize.NumElts,
1416  Builder);
1417  }
1418  assert(MD && "No Size expression debug node created");
1419  DI->registerVLASizeExpression(VlaSize.Type, MD);
1420  }
1421 }
1422 
1423 /// EmitAutoVarAlloca - Emit the alloca and debug information for a
1424 /// local variable. Does not emit initialization or destruction.
1427  QualType Ty = D.getType();
1428  assert(
1429  Ty.getAddressSpace() == LangAS::Default ||
1431 
1432  AutoVarEmission emission(D);
1433 
1434  bool isEscapingByRef = D.isEscapingByref();
1435  emission.IsEscapingByRef = isEscapingByRef;
1436 
1437  CharUnits alignment = getContext().getDeclAlign(&D);
1438 
1439  // If the type is variably-modified, emit all the VLA sizes for it.
1440  if (Ty->isVariablyModifiedType())
1442 
1443  auto *DI = getDebugInfo();
1444  bool EmitDebugInfo = DI && CGM.getCodeGenOpts().hasReducedDebugInfo();
1445 
1446  Address address = Address::invalid();
1447  Address AllocaAddr = Address::invalid();
1448  Address OpenMPLocalAddr = Address::invalid();
1449  if (CGM.getLangOpts().OpenMPIRBuilder)
1450  OpenMPLocalAddr = OMPBuilderCBHelpers::getAddressOfLocalVariable(*this, &D);
1451  else
1452  OpenMPLocalAddr =
1453  getLangOpts().OpenMP
1455  : Address::invalid();
1456 
1457  bool NRVO = getLangOpts().ElideConstructors && D.isNRVOVariable();
1458 
1459  if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
1460  address = OpenMPLocalAddr;
1461  AllocaAddr = OpenMPLocalAddr;
1462  } else if (Ty->isConstantSizeType()) {
1463  // If this value is an array or struct with a statically determinable
1464  // constant initializer, there are optimizations we can do.
1465  //
1466  // TODO: We should constant-evaluate the initializer of any variable,
1467  // as long as it is initialized by a constant expression. Currently,
1468  // isConstantInitializer produces wrong answers for structs with
1469  // reference or bitfield members, and a few other cases, and checking
1470  // for POD-ness protects us from some of these.
1471  if (D.getInit() && (Ty->isArrayType() || Ty->isRecordType()) &&
1472  (D.isConstexpr() ||
1473  ((Ty.isPODType(getContext()) ||
1474  getContext().getBaseElementType(Ty)->isObjCObjectPointerType()) &&
1475  D.getInit()->isConstantInitializer(getContext(), false)))) {
1476 
1477  // If the variable's a const type, and it's neither an NRVO
1478  // candidate nor a __block variable and has no mutable members,
1479  // emit it as a global instead.
1480  // Exception is if a variable is located in non-constant address space
1481  // in OpenCL.
1482  if ((!getLangOpts().OpenCL ||
1484  (CGM.getCodeGenOpts().MergeAllConstants && !NRVO &&
1485  !isEscapingByRef && CGM.isTypeConstant(Ty, true))) {
1487 
1488  // Signal this condition to later callbacks.
1489  emission.Addr = Address::invalid();
1490  assert(emission.wasEmittedAsGlobal());
1491  return emission;
1492  }
1493 
1494  // Otherwise, tell the initialization code that we're in this case.
1495  emission.IsConstantAggregate = true;
1496  }
1497 
1498  // A normal fixed sized variable becomes an alloca in the entry block,
1499  // unless:
1500  // - it's an NRVO variable.
1501  // - we are compiling OpenMP and it's an OpenMP local variable.
1502  if (NRVO) {
1503  // The named return value optimization: allocate this variable in the
1504  // return slot, so that we can elide the copy when returning this
1505  // variable (C++0x [class.copy]p34).
1506  address = ReturnValue;
1507  AllocaAddr = ReturnValue;
1508 
1509  if (const RecordType *RecordTy = Ty->getAs<RecordType>()) {
1510  const auto *RD = RecordTy->getDecl();
1511  const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1512  if ((CXXRD && !CXXRD->hasTrivialDestructor()) ||
1513  RD->isNonTrivialToPrimitiveDestroy()) {
1514  // Create a flag that is used to indicate when the NRVO was applied
1515  // to this variable. Set it to zero to indicate that NRVO was not
1516  // applied.
1517  llvm::Value *Zero = Builder.getFalse();
1518  Address NRVOFlag =
1519  CreateTempAlloca(Zero->getType(), CharUnits::One(), "nrvo",
1520  /*ArraySize=*/nullptr, &AllocaAddr);
1522  Builder.CreateStore(Zero, NRVOFlag);
1523 
1524  // Record the NRVO flag for this variable.
1525  NRVOFlags[&D] = NRVOFlag.getPointer();
1526  emission.NRVOFlag = NRVOFlag.getPointer();
1527  }
1528  }
1529  } else {
1530  CharUnits allocaAlignment;
1531  llvm::Type *allocaTy;
1532  if (isEscapingByRef) {
1533  auto &byrefInfo = getBlockByrefInfo(&D);
1534  allocaTy = byrefInfo.Type;
1535  allocaAlignment = byrefInfo.ByrefAlignment;
1536  } else {
1537  allocaTy = ConvertTypeForMem(Ty);
1538  allocaAlignment = alignment;
1539  }
1540 
1541  // Create the alloca. Note that we set the name separately from
1542  // building the instruction so that it's there even in no-asserts
1543  // builds.
1544  address = CreateTempAlloca(allocaTy, allocaAlignment, D.getName(),
1545  /*ArraySize=*/nullptr, &AllocaAddr);
1546 
1547  // Don't emit lifetime markers for MSVC catch parameters. The lifetime of
1548  // the catch parameter starts in the catchpad instruction, and we can't
1549  // insert code in those basic blocks.
1550  bool IsMSCatchParam =
1552 
1553  // Emit a lifetime intrinsic if meaningful. There's no point in doing this
1554  // if we don't have a valid insertion point (?).
1555  if (HaveInsertPoint() && !IsMSCatchParam) {
1556  // If there's a jump into the lifetime of this variable, its lifetime
1557  // gets broken up into several regions in IR, which requires more work
1558  // to handle correctly. For now, just omit the intrinsics; this is a
1559  // rare case, and it's better to just be conservatively correct.
1560  // PR28267.
1561  //
1562  // We have to do this in all language modes if there's a jump past the
1563  // declaration. We also have to do it in C if there's a jump to an
1564  // earlier point in the current block because non-VLA lifetimes begin as
1565  // soon as the containing block is entered, not when its variables
1566  // actually come into scope; suppressing the lifetime annotations
1567  // completely in this case is unnecessarily pessimistic, but again, this
1568  // is rare.
1569  if (!Bypasses.IsBypassed(&D) &&
1571  llvm::TypeSize Size = CGM.getDataLayout().getTypeAllocSize(allocaTy);
1572  emission.SizeForLifetimeMarkers =
1573  EmitLifetimeStart(Size, AllocaAddr.getPointer());
1574  }
1575  } else {
1576  assert(!emission.useLifetimeMarkers());
1577  }
1578  }
1579  } else {
1581 
1582  if (!DidCallStackSave) {
1583  // Save the stack.
1584  Address Stack =
1585  CreateTempAlloca(Int8PtrTy, getPointerAlign(), "saved_stack");
1586 
1587  llvm::Function *F = CGM.getIntrinsic(llvm::Intrinsic::stacksave);
1588  llvm::Value *V = Builder.CreateCall(F);
1589  Builder.CreateStore(V, Stack);
1590 
1591  DidCallStackSave = true;
1592 
1593  // Push a cleanup block and restore the stack there.
1594  // FIXME: in general circumstances, this should be an EH cleanup.
1596  }
1597 
1598  auto VlaSize = getVLASize(Ty);
1599  llvm::Type *llvmTy = ConvertTypeForMem(VlaSize.Type);
1600 
1601  // Allocate memory for the array.
1602  address = CreateTempAlloca(llvmTy, alignment, "vla", VlaSize.NumElts,
1603  &AllocaAddr);
1604 
1605  // If we have debug info enabled, properly describe the VLA dimensions for
1606  // this type by registering the vla size expression for each of the
1607  // dimensions.
1608  EmitAndRegisterVariableArrayDimensions(DI, D, EmitDebugInfo);
1609  }
1610 
1611  setAddrOfLocalVar(&D, address);
1612  emission.Addr = address;
1613  emission.AllocaAddr = AllocaAddr;
1614 
1615  // Emit debug info for local var declaration.
1616  if (EmitDebugInfo && HaveInsertPoint()) {
1617  Address DebugAddr = address;
1618  bool UsePointerValue = NRVO && ReturnValuePointer.isValid();
1619  DI->setLocation(D.getLocation());
1620 
1621  // If NRVO, use a pointer to the return address.
1622  if (UsePointerValue) {
1623  DebugAddr = ReturnValuePointer;
1624  AllocaAddr = ReturnValuePointer;
1625  }
1626  (void)DI->EmitDeclareOfAutoVariable(&D, AllocaAddr.getPointer(), Builder,
1627  UsePointerValue);
1628  }
1629 
1630  if (D.hasAttr<AnnotateAttr>() && HaveInsertPoint())
1631  EmitVarAnnotations(&D, address.getPointer());
1632 
1633  // Make sure we call @llvm.lifetime.end.
1634  if (emission.useLifetimeMarkers())
1636  emission.getOriginalAllocatedAddress(),
1637  emission.getSizeForLifetimeMarkers());
1638 
1639  return emission;
1640 }
1641 
1642 static bool isCapturedBy(const VarDecl &, const Expr *);
1643 
1644 /// Determines whether the given __block variable is potentially
1645 /// captured by the given statement.
1646 static bool isCapturedBy(const VarDecl &Var, const Stmt *S) {
1647  if (const Expr *E = dyn_cast<Expr>(S))
1648  return isCapturedBy(Var, E);
1649  for (const Stmt *SubStmt : S->children())
1650  if (isCapturedBy(Var, SubStmt))
1651  return true;
1652  return false;
1653 }
1654 
1655 /// Determines whether the given __block variable is potentially
1656 /// captured by the given expression.
1657 static bool isCapturedBy(const VarDecl &Var, const Expr *E) {
1658  // Skip the most common kinds of expressions that make
1659  // hierarchy-walking expensive.
1660  E = E->IgnoreParenCasts();
1661 
1662  if (const BlockExpr *BE = dyn_cast<BlockExpr>(E)) {
1663  const BlockDecl *Block = BE->getBlockDecl();
1664  for (const auto &I : Block->captures()) {
1665  if (I.getVariable() == &Var)
1666  return true;
1667  }
1668 
1669  // No need to walk into the subexpressions.
1670  return false;
1671  }
1672 
1673  if (const StmtExpr *SE = dyn_cast<StmtExpr>(E)) {
1674  const CompoundStmt *CS = SE->getSubStmt();
1675  for (const auto *BI : CS->body())
1676  if (const auto *BIE = dyn_cast<Expr>(BI)) {
1677  if (isCapturedBy(Var, BIE))
1678  return true;
1679  }
1680  else if (const auto *DS = dyn_cast<DeclStmt>(BI)) {
1681  // special case declarations
1682  for (const auto *I : DS->decls()) {
1683  if (const auto *VD = dyn_cast<VarDecl>((I))) {
1684  const Expr *Init = VD->getInit();
1685  if (Init && isCapturedBy(Var, Init))
1686  return true;
1687  }
1688  }
1689  }
1690  else
1691  // FIXME. Make safe assumption assuming arbitrary statements cause capturing.
1692  // Later, provide code to poke into statements for capture analysis.
1693  return true;
1694  return false;
1695  }
1696 
1697  for (const Stmt *SubStmt : E->children())
1698  if (isCapturedBy(Var, SubStmt))
1699  return true;
1700 
1701  return false;
1702 }
1703 
1704 /// Determine whether the given initializer is trivial in the sense
1705 /// that it requires no code to be generated.
1707  if (!Init)
1708  return true;
1709 
1710  if (const CXXConstructExpr *Construct = dyn_cast<CXXConstructExpr>(Init))
1711  if (CXXConstructorDecl *Constructor = Construct->getConstructor())
1712  if (Constructor->isTrivial() &&
1713  Constructor->isDefaultConstructor() &&
1714  !Construct->requiresZeroInitialization())
1715  return true;
1716 
1717  return false;
1718 }
1719 
1720 void CodeGenFunction::emitZeroOrPatternForAutoVarInit(QualType type,
1721  const VarDecl &D,
1722  Address Loc) {
1723  auto trivialAutoVarInit = getContext().getLangOpts().getTrivialAutoVarInit();
1725  bool isVolatile = type.isVolatileQualified();
1726  if (!Size.isZero()) {
1727  switch (trivialAutoVarInit) {
1729  llvm_unreachable("Uninitialized handled by caller");
1731  if (CGM.stopAutoInit())
1732  return;
1733  emitStoresForZeroInit(CGM, D, Loc, isVolatile, Builder);
1734  break;
1736  if (CGM.stopAutoInit())
1737  return;
1738  emitStoresForPatternInit(CGM, D, Loc, isVolatile, Builder);
1739  break;
1740  }
1741  return;
1742  }
1743 
1744  // VLAs look zero-sized to getTypeInfo. We can't emit constant stores to
1745  // them, so emit a memcpy with the VLA size to initialize each element.
1746  // Technically zero-sized or negative-sized VLAs are undefined, and UBSan
1747  // will catch that code, but there exists code which generates zero-sized
1748  // VLAs. Be nice and initialize whatever they requested.
1749  const auto *VlaType = getContext().getAsVariableArrayType(type);
1750  if (!VlaType)
1751  return;
1752  auto VlaSize = getVLASize(VlaType);
1753  auto SizeVal = VlaSize.NumElts;
1754  CharUnits EltSize = getContext().getTypeSizeInChars(VlaSize.Type);
1755  switch (trivialAutoVarInit) {
1757  llvm_unreachable("Uninitialized handled by caller");
1758 
1760  if (CGM.stopAutoInit())
1761  return;
1762  if (!EltSize.isOne())
1763  SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1764  auto *I = Builder.CreateMemSet(Loc, llvm::ConstantInt::get(Int8Ty, 0),
1765  SizeVal, isVolatile);
1766  I->addAnnotationMetadata("auto-init");
1767  break;
1768  }
1769 
1771  if (CGM.stopAutoInit())
1772  return;
1773  llvm::Type *ElTy = Loc.getElementType();
1774  llvm::Constant *Constant = constWithPadding(
1776  CharUnits ConstantAlign = getContext().getTypeAlignInChars(VlaSize.Type);
1777  llvm::BasicBlock *SetupBB = createBasicBlock("vla-setup.loop");
1778  llvm::BasicBlock *LoopBB = createBasicBlock("vla-init.loop");
1779  llvm::BasicBlock *ContBB = createBasicBlock("vla-init.cont");
1780  llvm::Value *IsZeroSizedVLA = Builder.CreateICmpEQ(
1781  SizeVal, llvm::ConstantInt::get(SizeVal->getType(), 0),
1782  "vla.iszerosized");
1783  Builder.CreateCondBr(IsZeroSizedVLA, ContBB, SetupBB);
1784  EmitBlock(SetupBB);
1785  if (!EltSize.isOne())
1786  SizeVal = Builder.CreateNUWMul(SizeVal, CGM.getSize(EltSize));
1787  llvm::Value *BaseSizeInChars =
1788  llvm::ConstantInt::get(IntPtrTy, EltSize.getQuantity());
1789  Address Begin = Builder.CreateElementBitCast(Loc, Int8Ty, "vla.begin");
1790  llvm::Value *End = Builder.CreateInBoundsGEP(
1791  Begin.getElementType(), Begin.getPointer(), SizeVal, "vla.end");
1792  llvm::BasicBlock *OriginBB = Builder.GetInsertBlock();
1793  EmitBlock(LoopBB);
1794  llvm::PHINode *Cur = Builder.CreatePHI(Begin.getType(), 2, "vla.cur");
1795  Cur->addIncoming(Begin.getPointer(), OriginBB);
1796  CharUnits CurAlign = Loc.getAlignment().alignmentOfArrayElement(EltSize);
1797  auto *I =
1798  Builder.CreateMemCpy(Address(Cur, Int8Ty, CurAlign),
1800  CGM, D, Builder, Constant, ConstantAlign),
1801  BaseSizeInChars, isVolatile);
1802  I->addAnnotationMetadata("auto-init");
1803  llvm::Value *Next =
1804  Builder.CreateInBoundsGEP(Int8Ty, Cur, BaseSizeInChars, "vla.next");
1805  llvm::Value *Done = Builder.CreateICmpEQ(Next, End, "vla-init.isdone");
1806  Builder.CreateCondBr(Done, ContBB, LoopBB);
1807  Cur->addIncoming(Next, LoopBB);
1808  EmitBlock(ContBB);
1809  } break;
1810  }
1811 }
1812 
1814  assert(emission.Variable && "emission was not valid!");
1815 
1816  // If this was emitted as a global constant, we're done.
1817  if (emission.wasEmittedAsGlobal()) return;
1818 
1819  const VarDecl &D = *emission.Variable;
1821  QualType type = D.getType();
1822 
1823  // If this local has an initializer, emit it now.
1824  const Expr *Init = D.getInit();
1825 
1826  // If we are at an unreachable point, we don't need to emit the initializer
1827  // unless it contains a label.
1828  if (!HaveInsertPoint()) {
1829  if (!Init || !ContainsLabel(Init)) return;
1831  }
1832 
1833  // Initialize the structure of a __block variable.
1834  if (emission.IsEscapingByRef)
1835  emitByrefStructureInit(emission);
1836 
1837  // Initialize the variable here if it doesn't have a initializer and it is a
1838  // C struct that is non-trivial to initialize or an array containing such a
1839  // struct.
1840  if (!Init &&
1841  type.isNonTrivialToPrimitiveDefaultInitialize() ==
1843  LValue Dst = MakeAddrLValue(emission.getAllocatedAddress(), type);
1844  if (emission.IsEscapingByRef)
1845  drillIntoBlockVariable(*this, Dst, &D);
1847  return;
1848  }
1849 
1850  // Check whether this is a byref variable that's potentially
1851  // captured and moved by its own initializer. If so, we'll need to
1852  // emit the initializer first, then copy into the variable.
1853  bool capturedByInit =
1854  Init && emission.IsEscapingByRef && isCapturedBy(D, Init);
1855 
1856  bool locIsByrefHeader = !capturedByInit;
1857  const Address Loc =
1858  locIsByrefHeader ? emission.getObjectAddress(*this) : emission.Addr;
1859 
1860  // Note: constexpr already initializes everything correctly.
1861  LangOptions::TrivialAutoVarInitKind trivialAutoVarInit =
1862  (D.isConstexpr()
1864  : (D.getAttr<UninitializedAttr>()
1866  : getContext().getLangOpts().getTrivialAutoVarInit()));
1867 
1868  auto initializeWhatIsTechnicallyUninitialized = [&](Address Loc) {
1869  if (trivialAutoVarInit ==
1871  return;
1872 
1873  // Only initialize a __block's storage: we always initialize the header.
1874  if (emission.IsEscapingByRef && !locIsByrefHeader)
1875  Loc = emitBlockByrefAddress(Loc, &D, /*follow=*/false);
1876 
1877  return emitZeroOrPatternForAutoVarInit(type, D, Loc);
1878  };
1879 
1880  if (isTrivialInitializer(Init))
1881  return initializeWhatIsTechnicallyUninitialized(Loc);
1882 
1883  llvm::Constant *constant = nullptr;
1884  if (emission.IsConstantAggregate ||
1886  assert(!capturedByInit && "constant init contains a capturing block?");
1887  constant = ConstantEmitter(*this).tryEmitAbstractForInitializer(D);
1888  if (constant && !constant->isZeroValue() &&
1889  (trivialAutoVarInit !=
1891  IsPattern isPattern =
1892  (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Pattern)
1893  ? IsPattern::Yes
1894  : IsPattern::No;
1895  // C guarantees that brace-init with fewer initializers than members in
1896  // the aggregate will initialize the rest of the aggregate as-if it were
1897  // static initialization. In turn static initialization guarantees that
1898  // padding is initialized to zero bits. We could instead pattern-init if D
1899  // has any ImplicitValueInitExpr, but that seems to be unintuitive
1900  // behavior.
1901  constant = constWithPadding(CGM, IsPattern::No,
1902  replaceUndef(CGM, isPattern, constant));
1903  }
1904  }
1905 
1906  if (!constant) {
1907  initializeWhatIsTechnicallyUninitialized(Loc);
1908  LValue lv = MakeAddrLValue(Loc, type);
1909  lv.setNonGC(true);
1910  return EmitExprAsInit(Init, &D, lv, capturedByInit);
1911  }
1912 
1913  if (!emission.IsConstantAggregate) {
1914  // For simple scalar/complex initialization, store the value directly.
1915  LValue lv = MakeAddrLValue(Loc, type);
1916  lv.setNonGC(true);
1917  return EmitStoreThroughLValue(RValue::get(constant), lv, true);
1918  }
1919 
1921  type.isVolatileQualified(), Builder, constant,
1922  /*IsAutoInit=*/false);
1923 }
1924 
1925 /// Emit an expression as an initializer for an object (variable, field, etc.)
1926 /// at the given location. The expression is not necessarily the normal
1927 /// initializer for the object, and the address is not necessarily
1928 /// its normal location.
1929 ///
1930 /// \param init the initializing expression
1931 /// \param D the object to act as if we're initializing
1932 /// \param lvalue the lvalue to initialize
1933 /// \param capturedByInit true if \p D is a __block variable
1934 /// whose address is potentially changed by the initializer
1936  LValue lvalue, bool capturedByInit) {
1937  QualType type = D->getType();
1938 
1939  if (type->isReferenceType()) {
1940  RValue rvalue = EmitReferenceBindingToExpr(init);
1941  if (capturedByInit)
1942  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1943  EmitStoreThroughLValue(rvalue, lvalue, true);
1944  return;
1945  }
1946  switch (getEvaluationKind(type)) {
1947  case TEK_Scalar:
1948  EmitScalarInit(init, D, lvalue, capturedByInit);
1949  return;
1950  case TEK_Complex: {
1951  ComplexPairTy complex = EmitComplexExpr(init);
1952  if (capturedByInit)
1953  drillIntoBlockVariable(*this, lvalue, cast<VarDecl>(D));
1954  EmitStoreOfComplex(complex, lvalue, /*init*/ true);
1955  return;
1956  }
1957  case TEK_Aggregate:
1958  if (type->isAtomicType()) {
1959  EmitAtomicInit(const_cast<Expr*>(init), lvalue);
1960  } else {
1962  if (isa<VarDecl>(D))
1963  Overlap = AggValueSlot::DoesNotOverlap;
1964  else if (auto *FD = dyn_cast<FieldDecl>(D))
1965  Overlap = getOverlapForFieldInit(FD);
1966  // TODO: how can we delay here if D is captured by its initializer?
1968  lvalue, *this, AggValueSlot::IsDestructed,
1970  AggValueSlot::IsNotAliased, Overlap));
1971  }
1972  return;
1973  }
1974  llvm_unreachable("bad evaluation kind");
1975 }
1976 
1977 /// Enter a destroy cleanup for the given local variable.
1979  const CodeGenFunction::AutoVarEmission &emission,
1980  QualType::DestructionKind dtorKind) {
1981  assert(dtorKind != QualType::DK_none);
1982 
1983  // Note that for __block variables, we want to destroy the
1984  // original stack object, not the possibly forwarded object.
1985  Address addr = emission.getObjectAddress(*this);
1986 
1987  const VarDecl *var = emission.Variable;
1988  QualType type = var->getType();
1989 
1990  CleanupKind cleanupKind = NormalAndEHCleanup;
1991  CodeGenFunction::Destroyer *destroyer = nullptr;
1992 
1993  switch (dtorKind) {
1994  case QualType::DK_none:
1995  llvm_unreachable("no cleanup for trivially-destructible variable");
1996 
1998  // If there's an NRVO flag on the emission, we need a different
1999  // cleanup.
2000  if (emission.NRVOFlag) {
2001  assert(!type->isArrayType());
2002  CXXDestructorDecl *dtor = type->getAsCXXRecordDecl()->getDestructor();
2003  EHStack.pushCleanup<DestroyNRVOVariableCXX>(cleanupKind, addr, type, dtor,
2004  emission.NRVOFlag);
2005  return;
2006  }
2007  break;
2008 
2010  // Suppress cleanups for pseudo-strong variables.
2011  if (var->isARCPseudoStrong()) return;
2012 
2013  // Otherwise, consider whether to use an EH cleanup or not.
2014  cleanupKind = getARCCleanupKind();
2015 
2016  // Use the imprecise destroyer by default.
2017  if (!var->hasAttr<ObjCPreciseLifetimeAttr>())
2019  break;
2020 
2022  break;
2023 
2026  if (emission.NRVOFlag) {
2027  assert(!type->isArrayType());
2028  EHStack.pushCleanup<DestroyNRVOVariableC>(cleanupKind, addr,
2029  emission.NRVOFlag, type);
2030  return;
2031  }
2032  break;
2033  }
2034 
2035  // If we haven't chosen a more specific destroyer, use the default.
2036  if (!destroyer) destroyer = getDestroyer(dtorKind);
2037 
2038  // Use an EH cleanup in array destructors iff the destructor itself
2039  // is being pushed as an EH cleanup.
2040  bool useEHCleanup = (cleanupKind & EHCleanup);
2041  EHStack.pushCleanup<DestroyObject>(cleanupKind, addr, type, destroyer,
2042  useEHCleanup);
2043 }
2044 
2046  assert(emission.Variable && "emission was not valid!");
2047 
2048  // If this was emitted as a global constant, we're done.
2049  if (emission.wasEmittedAsGlobal()) return;
2050 
2051  // If we don't have an insertion point, we're done. Sema prevents
2052  // us from jumping into any of these scopes anyway.
2053  if (!HaveInsertPoint()) return;
2054 
2055  const VarDecl &D = *emission.Variable;
2056 
2057  // Check the type for a cleanup.
2059  emitAutoVarTypeCleanup(emission, dtorKind);
2060 
2061  // In GC mode, honor objc_precise_lifetime.
2062  if (getLangOpts().getGC() != LangOptions::NonGC &&
2063  D.hasAttr<ObjCPreciseLifetimeAttr>()) {
2064  EHStack.pushCleanup<ExtendGCLifetime>(NormalCleanup, &D);
2065  }
2066 
2067  // Handle the cleanup attribute.
2068  if (const CleanupAttr *CA = D.getAttr<CleanupAttr>()) {
2069  const FunctionDecl *FD = CA->getFunctionDecl();
2070 
2071  llvm::Constant *F = CGM.GetAddrOfFunction(FD);
2072  assert(F && "Could not find function!");
2073 
2075  EHStack.pushCleanup<CallCleanupFunction>(NormalAndEHCleanup, F, &Info, &D);
2076  }
2077 
2078  // If this is a block variable, call _Block_object_destroy
2079  // (on the unforwarded address). Don't enter this cleanup if we're in pure-GC
2080  // mode.
2081  if (emission.IsEscapingByRef &&
2082  CGM.getLangOpts().getGC() != LangOptions::GCOnly) {
2084  if (emission.Variable->getType().isObjCGCWeak())
2085  Flags |= BLOCK_FIELD_IS_WEAK;
2086  enterByrefCleanup(NormalAndEHCleanup, emission.Addr, Flags,
2087  /*LoadBlockVarAddr*/ false,
2088  cxxDestructorCanThrow(emission.Variable->getType()));
2089  }
2090 }
2091 
2094  switch (kind) {
2095  case QualType::DK_none: llvm_unreachable("no destroyer for trivial dtor");
2097  return destroyCXXObject;
2099  return destroyARCStrongPrecise;
2101  return destroyARCWeak;
2103  return destroyNonTrivialCStruct;
2104  }
2105  llvm_unreachable("Unknown DestructionKind");
2106 }
2107 
2108 /// pushEHDestroy - Push the standard destructor for the given type as
2109 /// an EH-only cleanup.
2111  Address addr, QualType type) {
2112  assert(dtorKind && "cannot push destructor for trivial type");
2113  assert(needsEHCleanup(dtorKind));
2114 
2115  pushDestroy(EHCleanup, addr, type, getDestroyer(dtorKind), true);
2116 }
2117 
2118 /// pushDestroy - Push the standard destructor for the given type as
2119 /// at least a normal cleanup.
2121  Address addr, QualType type) {
2122  assert(dtorKind && "cannot push destructor for trivial type");
2123 
2124  CleanupKind cleanupKind = getCleanupKind(dtorKind);
2125  pushDestroy(cleanupKind, addr, type, getDestroyer(dtorKind),
2126  cleanupKind & EHCleanup);
2127 }
2128 
2130  QualType type, Destroyer *destroyer,
2131  bool useEHCleanupForArray) {
2132  pushFullExprCleanup<DestroyObject>(cleanupKind, addr, type,
2133  destroyer, useEHCleanupForArray);
2134 }
2135 
2137  EHStack.pushCleanup<CallStackRestore>(Kind, SPMem);
2138 }
2139 
2141  Address addr, QualType type,
2142  Destroyer *destroyer,
2143  bool useEHCleanupForArray) {
2144  // If we're not in a conditional branch, we don't need to bother generating a
2145  // conditional cleanup.
2146  if (!isInConditionalBranch()) {
2147  // Push an EH-only cleanup for the object now.
2148  // FIXME: When popping normal cleanups, we need to keep this EH cleanup
2149  // around in case a temporary's destructor throws an exception.
2150  if (cleanupKind & EHCleanup)
2151  EHStack.pushCleanup<DestroyObject>(
2152  static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), addr, type,
2153  destroyer, useEHCleanupForArray);
2154 
2155  return pushCleanupAfterFullExprWithActiveFlag<DestroyObject>(
2156  cleanupKind, Address::invalid(), addr, type, destroyer, useEHCleanupForArray);
2157  }
2158 
2159  // Otherwise, we should only destroy the object if it's been initialized.
2160  // Re-use the active flag and saved address across both the EH and end of
2161  // scope cleanups.
2162 
2163  using SavedType = typename DominatingValue<Address>::saved_type;
2164  using ConditionalCleanupType =
2166  Destroyer *, bool>;
2167 
2168  Address ActiveFlag = createCleanupActiveFlag();
2169  SavedType SavedAddr = saveValueInCond(addr);
2170 
2171  if (cleanupKind & EHCleanup) {
2172  EHStack.pushCleanup<ConditionalCleanupType>(
2173  static_cast<CleanupKind>(cleanupKind & ~NormalCleanup), SavedAddr, type,
2174  destroyer, useEHCleanupForArray);
2175  initFullExprCleanupWithFlag(ActiveFlag);
2176  }
2177 
2178  pushCleanupAfterFullExprWithActiveFlag<ConditionalCleanupType>(
2179  cleanupKind, ActiveFlag, SavedAddr, type, destroyer,
2180  useEHCleanupForArray);
2181 }
2182 
2183 /// emitDestroy - Immediately perform the destruction of the given
2184 /// object.
2185 ///
2186 /// \param addr - the address of the object; a type*
2187 /// \param type - the type of the object; if an array type, all
2188 /// objects are destroyed in reverse order
2189 /// \param destroyer - the function to call to destroy individual
2190 /// elements
2191 /// \param useEHCleanupForArray - whether an EH cleanup should be
2192 /// used when destroying array elements, in case one of the
2193 /// destructions throws an exception
2195  Destroyer *destroyer,
2196  bool useEHCleanupForArray) {
2198  if (!arrayType)
2199  return destroyer(*this, addr, type);
2200 
2201  llvm::Value *length = emitArrayLength(arrayType, type, addr);
2202 
2203  CharUnits elementAlign =
2204  addr.getAlignment()
2205  .alignmentOfArrayElement(getContext().getTypeSizeInChars(type));
2206 
2207  // Normally we have to check whether the array is zero-length.
2208  bool checkZeroLength = true;
2209 
2210  // But if the array length is constant, we can suppress that.
2211  if (llvm::ConstantInt *constLength = dyn_cast<llvm::ConstantInt>(length)) {
2212  // ...and if it's constant zero, we can just skip the entire thing.
2213  if (constLength->isZero()) return;
2214  checkZeroLength = false;
2215  }
2216 
2217  llvm::Value *begin = addr.getPointer();
2218  llvm::Value *end =
2219  Builder.CreateInBoundsGEP(addr.getElementType(), begin, length);
2220  emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2221  checkZeroLength, useEHCleanupForArray);
2222 }
2223 
2224 /// emitArrayDestroy - Destroys all the elements of the given array,
2225 /// beginning from last to first. The array cannot be zero-length.
2226 ///
2227 /// \param begin - a type* denoting the first element of the array
2228 /// \param end - a type* denoting one past the end of the array
2229 /// \param elementType - the element type of the array
2230 /// \param destroyer - the function to call to destroy elements
2231 /// \param useEHCleanup - whether to push an EH cleanup to destroy
2232 /// the remaining elements in case the destruction of a single
2233 /// element throws
2234 void CodeGenFunction::emitArrayDestroy(llvm::Value *begin,
2235  llvm::Value *end,
2236  QualType elementType,
2237  CharUnits elementAlign,
2238  Destroyer *destroyer,
2239  bool checkZeroLength,
2240  bool useEHCleanup) {
2241  assert(!elementType->isArrayType());
2242 
2243  // The basic structure here is a do-while loop, because we don't
2244  // need to check for the zero-element case.
2245  llvm::BasicBlock *bodyBB = createBasicBlock("arraydestroy.body");
2246  llvm::BasicBlock *doneBB = createBasicBlock("arraydestroy.done");
2247 
2248  if (checkZeroLength) {
2249  llvm::Value *isEmpty = Builder.CreateICmpEQ(begin, end,
2250  "arraydestroy.isempty");
2251  Builder.CreateCondBr(isEmpty, doneBB, bodyBB);
2252  }
2253 
2254  // Enter the loop body, making that address the current address.
2255  llvm::BasicBlock *entryBB = Builder.GetInsertBlock();
2256  EmitBlock(bodyBB);
2257  llvm::PHINode *elementPast =
2258  Builder.CreatePHI(begin->getType(), 2, "arraydestroy.elementPast");
2259  elementPast->addIncoming(end, entryBB);
2260 
2261  // Shift the address back by one element.
2262  llvm::Value *negativeOne = llvm::ConstantInt::get(SizeTy, -1, true);
2263  llvm::Type *llvmElementType = ConvertTypeForMem(elementType);
2264  llvm::Value *element = Builder.CreateInBoundsGEP(
2265  llvmElementType, elementPast, negativeOne, "arraydestroy.element");
2266 
2267  if (useEHCleanup)
2268  pushRegularPartialArrayCleanup(begin, element, elementType, elementAlign,
2269  destroyer);
2270 
2271  // Perform the actual destruction there.
2272  destroyer(*this, Address(element, llvmElementType, elementAlign),
2273  elementType);
2274 
2275  if (useEHCleanup)
2276  PopCleanupBlock();
2277 
2278  // Check whether we've reached the end.
2279  llvm::Value *done = Builder.CreateICmpEQ(element, begin, "arraydestroy.done");
2280  Builder.CreateCondBr(done, doneBB, bodyBB);
2281  elementPast->addIncoming(element, Builder.GetInsertBlock());
2282 
2283  // Done.
2284  EmitBlock(doneBB);
2285 }
2286 
2287 /// Perform partial array destruction as if in an EH cleanup. Unlike
2288 /// emitArrayDestroy, the element type here may still be an array type.
2290  llvm::Value *begin, llvm::Value *end,
2291  QualType type, CharUnits elementAlign,
2292  CodeGenFunction::Destroyer *destroyer) {
2293  llvm::Type *elemTy = CGF.ConvertTypeForMem(type);
2294 
2295  // If the element type is itself an array, drill down.
2296  unsigned arrayDepth = 0;
2297  while (const ArrayType *arrayType = CGF.getContext().getAsArrayType(type)) {
2298  // VLAs don't require a GEP index to walk into.
2299  if (!isa<VariableArrayType>(arrayType))
2300  arrayDepth++;
2301  type = arrayType->getElementType();
2302  }
2303 
2304  if (arrayDepth) {
2305  llvm::Value *zero = llvm::ConstantInt::get(CGF.SizeTy, 0);
2306 
2307  SmallVector<llvm::Value*,4> gepIndices(arrayDepth+1, zero);
2308  begin = CGF.Builder.CreateInBoundsGEP(
2309  elemTy, begin, gepIndices, "pad.arraybegin");
2310  end = CGF.Builder.CreateInBoundsGEP(
2311  elemTy, end, gepIndices, "pad.arrayend");
2312  }
2313 
2314  // Destroy the array. We don't ever need an EH cleanup because we
2315  // assume that we're in an EH cleanup ourselves, so a throwing
2316  // destructor causes an immediate terminate.
2317  CGF.emitArrayDestroy(begin, end, type, elementAlign, destroyer,
2318  /*checkZeroLength*/ true, /*useEHCleanup*/ false);
2319 }
2320 
2321 namespace {
2322  /// RegularPartialArrayDestroy - a cleanup which performs a partial
2323  /// array destroy where the end pointer is regularly determined and
2324  /// does not need to be loaded from a local.
2325  class RegularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2326  llvm::Value *ArrayBegin;
2327  llvm::Value *ArrayEnd;
2328  QualType ElementType;
2329  CodeGenFunction::Destroyer *Destroyer;
2330  CharUnits ElementAlign;
2331  public:
2332  RegularPartialArrayDestroy(llvm::Value *arrayBegin, llvm::Value *arrayEnd,
2333  QualType elementType, CharUnits elementAlign,
2334  CodeGenFunction::Destroyer *destroyer)
2335  : ArrayBegin(arrayBegin), ArrayEnd(arrayEnd),
2336  ElementType(elementType), Destroyer(destroyer),
2337  ElementAlign(elementAlign) {}
2338 
2339  void Emit(CodeGenFunction &CGF, Flags flags) override {
2340  emitPartialArrayDestroy(CGF, ArrayBegin, ArrayEnd,
2341  ElementType, ElementAlign, Destroyer);
2342  }
2343  };
2344 
2345  /// IrregularPartialArrayDestroy - a cleanup which performs a
2346  /// partial array destroy where the end pointer is irregularly
2347  /// determined and must be loaded from a local.
2348  class IrregularPartialArrayDestroy final : public EHScopeStack::Cleanup {
2349  llvm::Value *ArrayBegin;
2350  Address ArrayEndPointer;
2351  QualType ElementType;
2352  CodeGenFunction::Destroyer *Destroyer;
2353  CharUnits ElementAlign;
2354  public:
2355  IrregularPartialArrayDestroy(llvm::Value *arrayBegin,
2356  Address arrayEndPointer,
2357  QualType elementType,
2358  CharUnits elementAlign,
2359  CodeGenFunction::Destroyer *destroyer)
2360  : ArrayBegin(arrayBegin), ArrayEndPointer(arrayEndPointer),
2361  ElementType(elementType), Destroyer(destroyer),
2362  ElementAlign(elementAlign) {}
2363 
2364  void Emit(CodeGenFunction &CGF, Flags flags) override {
2365  llvm::Value *arrayEnd = CGF.Builder.CreateLoad(ArrayEndPointer);
2366  emitPartialArrayDestroy(CGF, ArrayBegin, arrayEnd,
2367  ElementType, ElementAlign, Destroyer);
2368  }
2369  };
2370 } // end anonymous namespace
2371 
2372 /// pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy
2373 /// already-constructed elements of the given array. The cleanup
2374 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2375 ///
2376 /// \param elementType - the immediate element type of the array;
2377 /// possibly still an array type
2379  Address arrayEndPointer,
2380  QualType elementType,
2381  CharUnits elementAlign,
2382  Destroyer *destroyer) {
2383  pushFullExprCleanup<IrregularPartialArrayDestroy>(EHCleanup,
2384  arrayBegin, arrayEndPointer,
2385  elementType, elementAlign,
2386  destroyer);
2387 }
2388 
2389 /// pushRegularPartialArrayCleanup - Push an EH cleanup to destroy
2390 /// already-constructed elements of the given array. The cleanup
2391 /// may be popped with DeactivateCleanupBlock or PopCleanupBlock.
2392 ///
2393 /// \param elementType - the immediate element type of the array;
2394 /// possibly still an array type
2396  llvm::Value *arrayEnd,
2397  QualType elementType,
2398  CharUnits elementAlign,
2399  Destroyer *destroyer) {
2400  pushFullExprCleanup<RegularPartialArrayDestroy>(EHCleanup,
2401  arrayBegin, arrayEnd,
2402  elementType, elementAlign,
2403  destroyer);
2404 }
2405 
2406 /// Lazily declare the @llvm.lifetime.start intrinsic.
2408  if (LifetimeStartFn)
2409  return LifetimeStartFn;
2410  LifetimeStartFn = llvm::Intrinsic::getDeclaration(&getModule(),
2411  llvm::Intrinsic::lifetime_start, AllocaInt8PtrTy);
2412  return LifetimeStartFn;
2413 }
2414 
2415 /// Lazily declare the @llvm.lifetime.end intrinsic.
2417  if (LifetimeEndFn)
2418  return LifetimeEndFn;
2419  LifetimeEndFn = llvm::Intrinsic::getDeclaration(&getModule(),
2420  llvm::Intrinsic::lifetime_end, AllocaInt8PtrTy);
2421  return LifetimeEndFn;
2422 }
2423 
2424 namespace {
2425  /// A cleanup to perform a release of an object at the end of a
2426  /// function. This is used to balance out the incoming +1 of a
2427  /// ns_consumed argument when we can't reasonably do that just by
2428  /// not doing the initial retain for a __block argument.
2429  struct ConsumeARCParameter final : EHScopeStack::Cleanup {
2430  ConsumeARCParameter(llvm::Value *param,
2431  ARCPreciseLifetime_t precise)
2432  : Param(param), Precise(precise) {}
2433 
2434  llvm::Value *Param;
2435  ARCPreciseLifetime_t Precise;
2436 
2437  void Emit(CodeGenFunction &CGF, Flags flags) override {
2438  CGF.EmitARCRelease(Param, Precise);
2439  }
2440  };
2441 } // end anonymous namespace
2442 
2443 /// Emit an alloca (or GlobalValue depending on target)
2444 /// for the specified parameter and set up LocalDeclMap.
2446  unsigned ArgNo) {
2447  bool NoDebugInfo = false;
2448  // FIXME: Why isn't ImplicitParamDecl a ParmVarDecl?
2449  assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
2450  "Invalid argument to EmitParmDecl");
2451 
2452  Arg.getAnyValue()->setName(D.getName());
2453 
2454  QualType Ty = D.getType();
2455 
2456  // Use better IR generation for certain implicit parameters.
2457  if (auto IPD = dyn_cast<ImplicitParamDecl>(&D)) {
2458  // The only implicit argument a block has is its literal.
2459  // This may be passed as an inalloca'ed value on Windows x86.
2460  if (BlockInfo) {
2461  llvm::Value *V = Arg.isIndirect()
2463  : Arg.getDirectValue();
2464  setBlockContextParameter(IPD, ArgNo, V);
2465  return;
2466  }
2467  // Suppressing debug info for ThreadPrivateVar parameters, else it hides
2468  // debug info of TLS variables.
2469  NoDebugInfo =
2470  (IPD->getParameterKind() == ImplicitParamDecl::ThreadPrivateVar);
2471  }
2472 
2473  Address DeclPtr = Address::invalid();
2474  Address AllocaPtr = Address::invalid();
2475  bool DoStore = false;
2476  bool IsScalar = hasScalarEvaluationKind(Ty);
2477  // If we already have a pointer to the argument, reuse the input pointer.
2478  if (Arg.isIndirect()) {
2479  // If we have a prettier pointer type at this point, bitcast to that.
2480  DeclPtr = Arg.getIndirectAddress();
2481  DeclPtr = Builder.CreateElementBitCast(DeclPtr, ConvertTypeForMem(Ty),
2482  D.getName());
2483  // Indirect argument is in alloca address space, which may be different
2484  // from the default address space.
2485  auto AllocaAS = CGM.getASTAllocaAddressSpace();
2486  auto *V = DeclPtr.getPointer();
2487  AllocaPtr = DeclPtr;
2488  auto SrcLangAS = getLangOpts().OpenCL ? LangAS::opencl_private : AllocaAS;
2489  auto DestLangAS =
2491  if (SrcLangAS != DestLangAS) {
2492  assert(getContext().getTargetAddressSpace(SrcLangAS) ==
2493  CGM.getDataLayout().getAllocaAddrSpace());
2494  auto DestAS = getContext().getTargetAddressSpace(DestLangAS);
2495  auto *T = DeclPtr.getElementType()->getPointerTo(DestAS);
2496  DeclPtr = DeclPtr.withPointer(getTargetHooks().performAddrSpaceCast(
2497  *this, V, SrcLangAS, DestLangAS, T, true));
2498  }
2499 
2500  // Push a destructor cleanup for this parameter if the ABI requires it.
2501  // Don't push a cleanup in a thunk for a method that will also emit a
2502  // cleanup.
2503  if (Ty->isRecordType() && !CurFuncIsThunk &&
2505  if (QualType::DestructionKind DtorKind =
2507  assert((DtorKind == QualType::DK_cxx_destructor ||
2508  DtorKind == QualType::DK_nontrivial_c_struct) &&
2509  "unexpected destructor type");
2510  pushDestroy(DtorKind, DeclPtr, Ty);
2511  CalleeDestructedParamCleanups[cast<ParmVarDecl>(&D)] =
2513  }
2514  }
2515  } else {
2516  // Check if the parameter address is controlled by OpenMP runtime.
2517  Address OpenMPLocalAddr =
2518  getLangOpts().OpenMP
2520  : Address::invalid();
2521  if (getLangOpts().OpenMP && OpenMPLocalAddr.isValid()) {
2522  DeclPtr = OpenMPLocalAddr;
2523  AllocaPtr = DeclPtr;
2524  } else {
2525  // Otherwise, create a temporary to hold the value.
2526  DeclPtr = CreateMemTemp(Ty, getContext().getDeclAlign(&D),
2527  D.getName() + ".addr", &AllocaPtr);
2528  }
2529  DoStore = true;
2530  }
2531 
2532  llvm::Value *ArgVal = (DoStore ? Arg.getDirectValue() : nullptr);
2533 
2534  LValue lv = MakeAddrLValue(DeclPtr, Ty);
2535  if (IsScalar) {
2536  Qualifiers qs = Ty.getQualifiers();
2537  if (Qualifiers::ObjCLifetime lt = qs.getObjCLifetime()) {
2538  // We honor __attribute__((ns_consumed)) for types with lifetime.
2539  // For __strong, it's handled by just skipping the initial retain;
2540  // otherwise we have to balance out the initial +1 with an extra
2541  // cleanup to do the release at the end of the function.
2542  bool isConsumed = D.hasAttr<NSConsumedAttr>();
2543 
2544  // If a parameter is pseudo-strong then we can omit the implicit retain.
2545  if (D.isARCPseudoStrong()) {
2546  assert(lt == Qualifiers::OCL_Strong &&
2547  "pseudo-strong variable isn't strong?");
2548  assert(qs.hasConst() && "pseudo-strong variable should be const!");
2550  }
2551 
2552  // Load objects passed indirectly.
2553  if (Arg.isIndirect() && !ArgVal)
2554  ArgVal = Builder.CreateLoad(DeclPtr);
2555 
2556  if (lt == Qualifiers::OCL_Strong) {
2557  if (!isConsumed) {
2558  if (CGM.getCodeGenOpts().OptimizationLevel == 0) {
2559  // use objc_storeStrong(&dest, value) for retaining the
2560  // object. But first, store a null into 'dest' because
2561  // objc_storeStrong attempts to release its old value.
2562  llvm::Value *Null = CGM.EmitNullConstant(D.getType());
2563  EmitStoreOfScalar(Null, lv, /* isInitialization */ true);
2564  EmitARCStoreStrongCall(lv.getAddress(*this), ArgVal, true);
2565  DoStore = false;
2566  }
2567  else
2568  // Don't use objc_retainBlock for block pointers, because we
2569  // don't want to Block_copy something just because we got it
2570  // as a parameter.
2571  ArgVal = EmitARCRetainNonBlock(ArgVal);
2572  }
2573  } else {
2574  // Push the cleanup for a consumed parameter.
2575  if (isConsumed) {
2576  ARCPreciseLifetime_t precise = (D.hasAttr<ObjCPreciseLifetimeAttr>()
2578  EHStack.pushCleanup<ConsumeARCParameter>(getARCCleanupKind(), ArgVal,
2579  precise);
2580  }
2581 
2582  if (lt == Qualifiers::OCL_Weak) {
2583  EmitARCInitWeak(DeclPtr, ArgVal);
2584  DoStore = false; // The weak init is a store, no need to do two.
2585  }
2586  }
2587 
2588  // Enter the cleanup scope.
2589  EmitAutoVarWithLifetime(*this, D, DeclPtr, lt);
2590  }
2591  }
2592 
2593  // Store the initial value into the alloca.
2594  if (DoStore)
2595  EmitStoreOfScalar(ArgVal, lv, /* isInitialization */ true);
2596 
2597  setAddrOfLocalVar(&D, DeclPtr);
2598 
2599  // Emit debug info for param declarations in non-thunk functions.
2600  if (CGDebugInfo *DI = getDebugInfo()) {
2602  !NoDebugInfo) {
2603  llvm::DILocalVariable *DILocalVar = DI->EmitDeclareOfArgVariable(
2604  &D, AllocaPtr.getPointer(), ArgNo, Builder);
2605  if (const auto *Var = dyn_cast_or_null<ParmVarDecl>(&D))
2606  DI->getParamDbgMappings().insert({Var, DILocalVar});
2607  }
2608  }
2609 
2610  if (D.hasAttr<AnnotateAttr>())
2611  EmitVarAnnotations(&D, DeclPtr.getPointer());
2612 
2613  // We can only check return value nullability if all arguments to the
2614  // function satisfy their nullability preconditions. This makes it necessary
2615  // to emit null checks for args in the function body itself.
2616  if (requiresReturnValueNullabilityCheck()) {
2617  auto Nullability = Ty->getNullability(getContext());
2619  SanitizerScope SanScope(this);
2620  RetValNullabilityPrecondition =
2621  Builder.CreateAnd(RetValNullabilityPrecondition,
2622  Builder.CreateIsNotNull(Arg.getAnyValue()));
2623  }
2624  }
2625 }
2626 
2628  CodeGenFunction *CGF) {
2629  if (!LangOpts.OpenMP || (!LangOpts.EmitAllDecls && !D->isUsed()))
2630  return;
2632 }
2633 
2635  CodeGenFunction *CGF) {
2636  if (!LangOpts.OpenMP || LangOpts.OpenMPSimd ||
2637  (!LangOpts.EmitAllDecls && !D->isUsed()))
2638  return;
2640 }
2641 
2644 }
2645 
2647  for (const Expr *E : D->varlists()) {
2648  const auto *DE = cast<DeclRefExpr>(E);
2649  const auto *VD = cast<VarDecl>(DE->getDecl());
2650 
2651  // Skip all but globals.
2652  if (!VD->hasGlobalStorage())
2653  continue;
2654 
2655  // Check if the global has been materialized yet or not. If not, we are done
2656  // as any later generation will utilize the OMPAllocateDeclAttr. However, if
2657  // we already emitted the global we might have done so before the
2658  // OMPAllocateDeclAttr was attached, leading to the wrong address space
2659  // (potentially). While not pretty, common practise is to remove the old IR
2660  // global and generate a new one, so we do that here too. Uses are replaced
2661  // properly.
2662  StringRef MangledName = getMangledName(VD);
2663  llvm::GlobalValue *Entry = GetGlobalValue(MangledName);
2664  if (!Entry)
2665  continue;
2666 
2667  // We can also keep the existing global if the address space is what we
2668  // expect it to be, if not, it is replaced.
2669  QualType ASTTy = VD->getType();
2671  auto TargetAS = getContext().getTargetAddressSpace(GVAS);
2672  if (Entry->getType()->getAddressSpace() == TargetAS)
2673  continue;
2674 
2675  // Make a new global with the correct type / address space.
2676  llvm::Type *Ty = getTypes().ConvertTypeForMem(ASTTy);
2677  llvm::PointerType *PTy = llvm::PointerType::get(Ty, TargetAS);
2678 
2679  // Replace all uses of the old global with a cast. Since we mutate the type
2680  // in place we neeed an intermediate that takes the spot of the old entry
2681  // until we can create the cast.
2682  llvm::GlobalVariable *DummyGV = new llvm::GlobalVariable(
2683  getModule(), Entry->getValueType(), false,
2684  llvm::GlobalValue::CommonLinkage, nullptr, "dummy", nullptr,
2685  llvm::GlobalVariable::NotThreadLocal, Entry->getAddressSpace());
2686  Entry->replaceAllUsesWith(DummyGV);
2687 
2688  Entry->mutateType(PTy);
2689  llvm::Constant *NewPtrForOldDecl =
2690  llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
2691  Entry, DummyGV->getType());
2692 
2693  // Now we have a casted version of the changed global, the dummy can be
2694  // replaced and deleted.
2695  DummyGV->replaceAllUsesWith(NewPtrForOldDecl);
2696  DummyGV->eraseFromParent();
2697  }
2698 }
2699 
2702  if (const auto *AA = VD->getAttr<OMPAllocateDeclAttr>()) {
2703  if (Expr *Alignment = AA->getAlignment()) {
2704  unsigned UserAlign =
2705  Alignment->EvaluateKnownConstInt(getContext()).getExtValue();
2706  CharUnits NaturalAlign =
2708 
2709  // OpenMP5.1 pg 185 lines 7-10
2710  // Each item in the align modifier list must be aligned to the maximum
2711  // of the specified alignment and the type's natural alignment.
2712  return CharUnits::fromQuantity(
2713  std::max<unsigned>(UserAlign, NaturalAlign.getQuantity()));
2714  }
2715  }
2716  return llvm::None;
2717 }
clang::OpenCL
@ OpenCL
Definition: LangStandard.h:62
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:4250
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::emitAutoVarTypeCleanup
void emitAutoVarTypeCleanup(const AutoVarEmission &emission, QualType::DestructionKind dtorKind)
Enter a destroy cleanup for the given local variable.
Definition: CGDecl.cpp:1978
clang::CodeGen::CodeGenFunction::SanitizerScope
RAII object to set/unset CodeGenFunction::IsSanitizerScope.
Definition: CodeGenFunction.h:524
clang::CodeGen::CodeGenFunction::ConvertTypeForMem
llvm::Type * ConvertTypeForMem(QualType T)
Definition: CodeGenFunction.cpp:210
clang::ASTContext::getTypeSizeInChars
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Definition: ASTContext.cpp:2515
clang::CodeGen::CodeGenFunction::destroyCXXObject
static Destroyer destroyCXXObject
Definition: CodeGenFunction.h:2912
clang::CodeGen::CodeGenFunction::EmitARCRetainNonBlock
llvm::Value * EmitARCRetainNonBlock(llvm::Value *value)
Retain the given object, with normal retain semantics.
Definition: CGObjC.cpp:2316
clang::OMPAllocateDecl
This represents '#pragma omp allocate ...' directive.
Definition: DeclOpenMP.h:473
clang::Type::getNullability
Optional< NullabilityKind > getNullability(const ASTContext &context) const
Determine the nullability of the given type.
Definition: Type.cpp:4146
clang::Type::isSamplerT
bool isSamplerT() const
Definition: Type.h:7076
clang::CharUnits::isOne
bool isOne() const
isOne - Test whether the quantity equals one.
Definition: CharUnits.h:119
clang::CodeGen::CodeGenFunction::AutoVarEmission::useLifetimeMarkers
bool useLifetimeMarkers() const
Definition: CodeGenFunction.h:3095
clang::CodeGen::CGBuilderTy::CreateMemCpy
llvm::CallInst * CreateMemCpy(Address Dest, Address Src, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:312
clang::CodeGen::CodeGenModule::addUsedOrCompilerUsedGlobal
void addUsedOrCompilerUsedGlobal(llvm::GlobalValue *GV)
Add a global to a list to be added to the llvm.compiler.used metadata.
Definition: CodeGenModule.cpp:2481
clang::CodeGen::CodeGenFunction::emitByrefStructureInit
void emitByrefStructureInit(const AutoVarEmission &emission)
Initialize the structural components of a __block variable, i.e.
Definition: CGBlocks.cpp:2696
clang::Type::isRecordType
bool isRecordType() const
Definition: Type.h:6973
clang::QualType::DK_nontrivial_c_struct
@ DK_nontrivial_c_struct
Definition: Type.h:1280
clang::LangAS::opencl_private
@ opencl_private
clang::CodeGen::CodeGenFunction::EmitVarDecl
void EmitVarDecl(const VarDecl &D)
EmitVarDecl - Emit a local variable declaration.
Definition: CGDecl.cpp:189
clang::CodeGen::CodeGenTypeCache::SizeTy
llvm::IntegerType * SizeTy
Definition: CodeGenTypeCache.h:50
clang::Dtor_Base
@ Dtor_Base
Base object dtor.
Definition: ABI.h:36
emitStoresForInitAfterBZero
static void emitStoresForInitAfterBZero(CodeGenModule &CGM, llvm::Constant *Init, Address Loc, bool isVolatile, CGBuilderTy &Builder, bool IsAutoInit)
For inits that canEmitInitWithFewStoresAfterBZero returned true for, emit the scalar stores that woul...
Definition: CGDecl.cpp:933
clang::QualType::getObjCLifetime
Qualifiers::ObjCLifetime getObjCLifetime() const
Returns lifetime attribute of this type.
Definition: Type.h:1193
clang::CodeGen::CodeGenTypeCache::Int8PtrTy
llvm::PointerType * Int8PtrTy
Definition: CodeGenTypeCache.h:57
clang::CXXConstructorDecl
Represents a C++ constructor within a class.
Definition: DeclCXX.h:2436
clang::VarDecl::getStorageDuration
StorageDuration getStorageDuration() const
Get the storage duration of this variable, per C++ [basic.stc].
Definition: Decl.h:1176
clang::CodeGen::SanitizerMetadata::reportGlobal
void reportGlobal(llvm::GlobalVariable *GV, const VarDecl &D, bool IsDynInit=false)
Definition: SanitizerMetadata.cpp:78
clang::CharUnits::getAsAlign
llvm::Align getAsAlign() const
getAsAlign - Returns Quantity as a valid llvm::Align, Beware llvm::Align assumes power of two 8-bit b...
Definition: CharUnits.h:183
clang::CodeGen::ConstantEmitter::tryEmitForInitializer
llvm::Constant * tryEmitForInitializer(const VarDecl &D)
Try to emit the initiaizer of the given declaration as an abstract constant.
Definition: CGExprConstant.cpp:1432
clang::interp::APInt
llvm::APInt APInt
Definition: Integral.h:27
clang::CodeGen::CGOpenMPRuntime::getAddressOfLocalVariable
virtual Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD)
Gets the OpenMP-specific address of the local variable.
Definition: CGOpenMPRuntime.cpp:11715
clang::CodeGen::CodeGenModule::EmitNullConstant
llvm::Constant * EmitNullConstant(QualType T)
Return the result of value-initializing the given type, i.e.
Definition: CGExprConstant.cpp:2342
clang::CharUnits::alignmentOfArrayElement
CharUnits alignmentOfArrayElement(CharUnits elementSize) const
Given that this is the alignment of the first element of an array, return the minimum alignment of an...
Definition: CharUnits.h:207
clang::LangOptions::TrivialAutoVarInitKind::Zero
@ Zero
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::CodeGenFunction::initFullExprCleanupWithFlag
void initFullExprCleanupWithFlag(Address ActiveFlag)
Definition: CGCleanup.cpp:307
emitStoresForZeroInit
static void emitStoresForZeroInit(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder)
Definition: CGDecl.cpp:1268
clang::CodeGen::CodeGenFunction::EmitCheckSourceLocation
llvm::Constant * EmitCheckSourceLocation(SourceLocation Loc)
Emit a description of a source location in a format suitable for passing to a runtime sanitizer handl...
Definition: CGExpr.cpp:3125
clang::CodeGen::CodeGenFunction::EmitReferenceBindingToExpr
RValue EmitReferenceBindingToExpr(const Expr *E)
Emits a reference binding to the passed in expression.
Definition: CGExpr.cpp:615
clang::Expr::isLValue
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition: Expr.h:270
clang::ento::Nullability
Nullability
Definition: CheckerHelpers.h:55
CodeGenFunction.h
string
string(SUBSTRING ${CMAKE_CURRENT_BINARY_DIR} 0 ${PATH_LIB_START} PATH_HEAD) string(SUBSTRING $
Definition: CMakeLists.txt:22
clang::DeclContext
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Definition: DeclBase.h:1389
clang::CodeGen::CodeGenFunction::EmitCall
RValue EmitCall(const CGFunctionInfo &CallInfo, const CGCallee &Callee, ReturnValueSlot ReturnValue, const CallArgList &Args, llvm::CallBase **callOrInvoke, bool IsMustTail, SourceLocation Loc)
EmitCall - Generate a call of the given function, expecting the given result type,...
Definition: CGCall.cpp:4726
clang::VarDecl::mightBeUsableInConstantExpressions
bool mightBeUsableInConstantExpressions(const ASTContext &C) const
Determine whether this variable's value might be usable in a constant expression, according to the re...
Definition: Decl.cpp:2389
containsUndef
static bool containsUndef(llvm::Constant *constant)
Definition: CGDecl.cpp:1289
clang::Decl::hasAttr
bool hasAttr() const
Definition: DeclBase.h:560
clang::VarDecl::hasGlobalStorage
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition: Decl.h:1173
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:2469
clang::CodeGen::CodeGenModule::getOpenCLRuntime
CGOpenCLRuntime & getOpenCLRuntime()
Return a reference to the configured OpenCL runtime.
Definition: CodeGenModule.h:624
clang::CodeGen::AggValueSlot::DoesNotNeedGCBarriers
@ DoesNotNeedGCBarriers
Definition: CGValue.h:554
clang::CodeGen::CodeGenFunction::AutoVarEmission::getObjectAddress
Address getObjectAddress(CodeGenFunction &CGF) const
Returns the address of the object within this declaration.
Definition: CodeGenFunction.h:3116
clang::CodeGen::CodeGenFunction::destroyARCStrongPrecise
static Destroyer destroyARCStrongPrecise
Definition: CodeGenFunction.h:4354
clang::CodeGen::Address::getAlignment
CharUnits getAlignment() const
Return the alignment of this pointer.
Definition: Address.h:120
clang::CodeGen::CodeGenFunction::EmitLifetimeEnd
void EmitLifetimeEnd(llvm::Value *Size, llvm::Value *Addr)
Definition: CGDecl.cpp:1351
clang::CodeGen::CodeGenModule::GetAddrOfFunction
llvm::Constant * GetAddrOfFunction(GlobalDecl GD, llvm::Type *Ty=nullptr, bool ForVTable=false, bool DontDefer=false, ForDefinition_t IsForDefinition=NotForDefinition)
Return the address of the given function.
Definition: CodeGenModule.cpp:4119
clang::NamedDecl::isExternallyVisible
bool isExternallyVisible() const
Definition: Decl.h:407
clang::Qualifiers::OCL_Weak
@ OCL_Weak
Reading or writing from this object requires a barrier call.
Definition: Type.h:178
isAccessedBy
static bool isAccessedBy(const VarDecl &var, const Stmt *s)
Definition: CGDecl.cpp:667
llvm::SmallVector
Definition: LLVM.h:38
clang::SourceLocation
Encodes a location in the source.
Definition: SourceLocation.h:86
clang::CodeGen::CodeGenFunction::hasLabelBeenSeenInCurrentScope
bool hasLabelBeenSeenInCurrentScope() const
Return true if a label was seen in the current scope.
Definition: CodeGenFunction.h:367
clang::QualType::getQualifiers
Qualifiers getQualifiers() const
Retrieve the set of qualifiers applied to this type.
Definition: Type.h:6662
clang::TargetCXXABI::isMicrosoft
bool isMicrosoft() const
Is this ABI an MSVC-compatible ABI?
Definition: TargetCXXABI.h:138
clang::CodeGen::LValue::getAddress
Address getAddress(CodeGenFunction &CGF) const
Definition: CGValue.h:341
clang::CodeGen::CodeGenFunction::EmitAutoVarDecl
void EmitAutoVarDecl(const VarDecl &D)
EmitAutoVarDecl - Emit an auto variable declaration.
Definition: CGDecl.cpp:1325
TargetInfo.h
clang::CodeGen::CodeGenFunction::PopCleanupBlock
void PopCleanupBlock(bool FallThroughIsBranchThrough=false)
PopCleanupBlock - Will pop the cleanup entry on the stack and process all branch fixups.
Definition: CGCleanup.cpp:659
clang::QualType::getNonReferenceType
QualType getNonReferenceType() const
If Type is a reference type (e.g., const int&), returns the type that the reference refers to ("const...
Definition: Type.h:6831
clang::CodeGen::CodeGenFunction::emitArrayLength
llvm::Value * emitArrayLength(const ArrayType *arrayType, QualType &baseType, Address &addr)
emitArrayLength - Compute the length of an array, even if it's a VLA, and drill down to the base elem...
Definition: CodeGenFunction.cpp:2068
clang::CodeGen::CodeGenModule::getSize
llvm::ConstantInt * getSize(CharUnits numChars)
Emit the given number of characters as a value of type size_t.
Definition: CodeGenModule.cpp:1092
clang::CodeGen::CodeGenTypeCache::getPointerAlign
CharUnits getPointerAlign() const
Definition: CodeGenTypeCache.h:117
clang::QualType
A (possibly-)qualified type.
Definition: Type.h:737
clang::ASTContext::getTargetAddressSpace
unsigned getTargetAddressSpace(QualType T) const
Definition: ASTContext.cpp:12230
Attr.h
clang::SanitizerSet::has
bool has(SanitizerMask K) const
Check if a certain (single) sanitizer is enabled.
Definition: Sanitizers.h:155
clang::CodeGen::CodeGenModule::getContext
ASTContext & getContext() const
Definition: CodeGenModule.h:722
clang::CodeGen::CGFunctionInfo::arg_begin
const_arg_iterator arg_begin() const
Definition: CGFunctionInfo.h:649
clang::CodeGen::Address::isValid
bool isValid() const
Definition: Address.h:91
createUnnamedGlobalForMemcpyFrom
static Address createUnnamedGlobalForMemcpyFrom(CodeGenModule &CGM, const VarDecl &D, CGBuilderTy &Builder, llvm::Constant *Constant, CharUnits Align)
Definition: CGDecl.cpp:1165
clang::Qualifiers
The collection of all-type qualifiers we support.
Definition: Type.h:147
clang::TargetInfo::getCXXABI
TargetCXXABI getCXXABI() const
Get the C++ ABI currently in use.
Definition: TargetInfo.h:1274
clang::ast_matchers::type
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.
Definition: ASTMatchersInternal.cpp:773
clang::Decl::isUsed
bool isUsed(bool CheckUsedAttr=true) const
Whether any (re-)declaration of the entity was used, meaning that a definition is required.
Definition: DeclBase.cpp:457
emitPartialArrayDestroy
static void emitPartialArrayDestroy(CodeGenFunction &CGF, llvm::Value *begin, llvm::Value *end, QualType type, CharUnits elementAlign, CodeGenFunction::Destroyer *destroyer)
Perform partial array destruction as if in an EH cleanup.
Definition: CGDecl.cpp:2289
canEmitInitWithFewStoresAfterBZero
static bool canEmitInitWithFewStoresAfterBZero(llvm::Constant *Init, unsigned &NumStores)
Decide whether we can emit the non-zero parts of the specified initializer with equal or fewer than N...
Definition: CGDecl.cpp:895
clang::CodeGen::CodeGenFunction::ParamValue::getDirectValue
llvm::Value * getDirectValue() const
Definition: CodeGenFunction.h:3159
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:2428
clang::CodeGen::CodeGenFunction::EmitARCCopyWeak
void EmitARCCopyWeak(Address dst, Address src)
void @objc_copyWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2682
clang::CodeGen::CodeGenFunction::pushIrregularPartialArrayCleanup
void pushIrregularPartialArrayCleanup(llvm::Value *arrayBegin, Address arrayEndPointer, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushIrregularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the ...
Definition: CGDecl.cpp:2378
DeclOpenMP.h
clang::CodeGen::CGBuilderTy::CreateStore
llvm::StoreInst * CreateStore(llvm::Value *Val, Address Addr, bool IsVolatile=false)
Definition: CGBuilder.h:99
clang::CodeGen::LValue::setAddress
void setAddress(Address address)
Definition: CGValue.h:344
clang::codegenoptions::NoDebugInfo
@ NoDebugInfo
Don't generate debug info.
Definition: DebugInfoOptions.h:22
clang::CodeGen::CodeGenModule::GetGlobalVarAddressSpace
LangAS GetGlobalVarAddressSpace(const VarDecl *D)
Return the AST address space of the underlying global variable for D, as determined by its declaratio...
Definition: CodeGenModule.cpp:4604
clang::CodeGen::ARCPreciseLifetime_t
ARCPreciseLifetime_t
Does an ARC strong l-value have precise lifetime?
Definition: CGValue.h:124
clang::Qualifiers::OCL_ExplicitNone
@ OCL_ExplicitNone
This object can be modified without requiring retains or releases.
Definition: Type.h:168
llvm::Optional
Definition: LLVM.h:40
clang::CodeGen::CGDebugInfo::EmitDeclareOfAutoVariable
llvm::DILocalVariable * EmitDeclareOfAutoVariable(const VarDecl *Decl, llvm::Value *AI, CGBuilderTy &Builder, const bool UsePointerValue=false)
Emit call to llvm.dbg.declare for an automatic variable declaration.
Definition: CGDebugInfo.cpp:4681
clang::CodeGen::CodeGenFunction::TCK_NonnullAssign
@ TCK_NonnullAssign
Checking the value assigned to a _Nonnull pointer. Must not be null.
Definition: CodeGenFunction.h:2981
clang::Ctor_Base
@ Ctor_Base
Base object ctor.
Definition: ABI.h:26
SourceManager.h
clang::Decl::getParentFunctionOrMethod
const DeclContext * getParentFunctionOrMethod(bool LexicalParent=false) const
If this decl is defined inside a function/method/block it returns the corresponding DeclContext,...
Definition: DeclBase.cpp:295
clang::CodeGen::CodeGenModule::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenModule.h:723
clang::CodeGen::CodeGenFunction::HaveInsertPoint
bool HaveInsertPoint() const
HaveInsertPoint - True if an insertion point is defined.
Definition: CodeGenFunction.h:2469
clang::LangOptions::GCOnly
@ GCOnly
Definition: LangOptions.h:86
clang::Decl::getAttr
T * getAttr() const
Definition: DeclBase.h:556
clang::CodeGen::CodeGenFunction::getDestroyer
Destroyer * getDestroyer(QualType::DestructionKind destructionKind)
Definition: CGDecl.cpp:2093
clang::OMPAllocateDecl::varlists
varlist_range varlists()
Definition: DeclOpenMP.h:515
clang::CodeGen::EHCleanup
@ EHCleanup
Denotes a cleanup that should run when a scope is exited using exceptional control flow (a throw stat...
Definition: EHScopeStack.h:80
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:3851
clang::CodeGen::CodeGenFunction::CreateTempAlloca
llvm::AllocaInst * CreateTempAlloca(llvm::Type *Ty, const Twine &Name="tmp", llvm::Value *ArraySize=nullptr)
CreateTempAlloca - This creates an alloca and inserts it into the entry block if ArraySize is nullptr...
Definition: CGExpr.cpp:110
clang::CodeGen::CodeGenModule::getTargetCodeGenInfo
const TargetCodeGenInfo & getTargetCodeGenInfo()
Definition: TargetInfo.cpp:12068
clang::SD_Automatic
@ SD_Automatic
Automatic storage duration (most local variables).
Definition: Specifiers.h:313
clang::Qualifiers::hasConst
bool hasConst() const
Definition: Type.h:264
clang::CodeGen::CGBuilderTy
Definition: CGBuilder.h:45
clang::ImplicitTypenameContext::No
@ No
ConstantEmitter.h
clang::CodeGen::CodeGenFunction::pushLifetimeExtendedDestroy
void pushLifetimeExtendedDestroy(CleanupKind kind, Address addr, QualType type, Destroyer *destroyer, bool useEHCleanupForArray)
Definition: CGDecl.cpp:2140
End
SourceLocation End
Definition: USRLocFinder.cpp:167
clang::CodeGen::CodeGenFunction::EmitExprAsInit
void EmitExprAsInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
EmitExprAsInit - Emits the code necessary to initialize a location in memory with the given initializ...
Definition: CGDecl.cpp:1935
clang::Expr::isXValue
bool isXValue() const
Definition: Expr.h:272
clang::CodeGen::CallArgList::add
void add(RValue rvalue, QualType type)
Definition: CGCall.h:283
clang::CodeGen::ConstantEmitter::tryEmitAbstractForInitializer
llvm::Constant * tryEmitAbstractForInitializer(const VarDecl &D)
Try to emit the initializer of the given declaration as an abstract constant.
Definition: CGExprConstant.cpp:1374
clang::CodeGen::CodeGenFunction::EmitStoreThroughLValue
void EmitStoreThroughLValue(RValue Src, LValue Dst, bool isInit=false)
EmitStoreThroughLValue - Store the specified rvalue into the specified lvalue, where both are guarant...
Definition: CGExpr.cpp:2084
clang::CodeGen::CodeGenFunction::Builder
CGBuilderTy Builder
Definition: CodeGenFunction.h:269
CGDebugInfo.h
clang::Dtor_Complete
@ Dtor_Complete
Complete object dtor.
Definition: ABI.h:35
clang::CodeGen::CodeGenModule::getMangledName
StringRef getMangledName(GlobalDecl GD)
Definition: CodeGenModule.cpp:1501
Decl.h
clang::CodeGen::NormalEHLifetimeMarker
@ NormalEHLifetimeMarker
Definition: EHScopeStack.h:89
clang::CodeGen::CodeGenFunction::destroyARCWeak
static Destroyer destroyARCWeak
Definition: CodeGenFunction.h:4355
DeclObjC.h
clang::CodeGen::CodeGenFunction::EmitCXXDestructorCall
void EmitCXXDestructorCall(const CXXDestructorDecl *D, CXXDtorType Type, bool ForVirtualBase, bool Delegating, Address This, QualType ThisTy)
Definition: CGClass.cpp:2505
clang::CodeGen::CodeGenModule::getOpenMPRuntime
CGOpenMPRuntime & getOpenMPRuntime()
Return a reference to the configured OpenMP runtime.
Definition: CodeGenModule.h:630
clang::CodeGen::ARCImpreciseLifetime
@ ARCImpreciseLifetime
Definition: CGValue.h:125
clang::CodeGen::CodeGenFunction::EmitVariablyModifiedType
void EmitVariablyModifiedType(QualType Ty)
EmitVLASize - Capture all the sizes for the VLA expressions in the given variably-modified type and s...
Definition: CodeGenFunction.cpp:2204
clang::CodeGen::CodeGenModule::GetGlobalValue
llvm::GlobalValue * GetGlobalValue(StringRef Ref)
Definition: CodeGenModule.cpp:1584
clang::GlobalDecl
GlobalDecl - represents a global declaration.
Definition: GlobalDecl.h:56
clang::CodeGen::CodeGenFunction::getOverlapForFieldInit
AggValueSlot::Overlap_t getOverlapForFieldInit(const FieldDecl *FD)
Determine whether a field initialization may overlap some other object.
Definition: CGExprAgg.cpp:2017
clang::CodeGen::CodeGenModule::GetGlobalConstantAddressSpace
LangAS GetGlobalConstantAddressSpace() const
Return the AST address space of constant literal, which is used to emit the constant literal as globa...
Definition: CodeGenModule.cpp:4641
clang::IdentifierTable::getOwn
IdentifierInfo & getOwn(StringRef Name)
Gets an IdentifierInfo for the given name without consulting external sources.
Definition: IdentifierTable.h:634
clang::CodeGen::InvariantValue::saved_type
T saved_type
Definition: EHScopeStack.h:57
isCapturedBy
static bool isCapturedBy(const VarDecl &, const Expr *)
Determines whether the given __block variable is potentially captured by the given expression.
Definition: CGDecl.cpp:1657
clang::BlockDecl::captures
ArrayRef< Capture > captures() const
Definition: Decl.h:4410
clang::CodeGen::CodeGenTypeCache::AllocaInt8PtrTy
llvm::PointerType * AllocaInt8PtrTy
Definition: CodeGenTypeCache.h:69
clang::CodeGen::CodeGenModule::stopAutoInit
bool stopAutoInit()
Definition: CodeGenModule.cpp:7011
CodeGenOptions.h
clang::ASTContext::getTypeAlignInChars
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
Definition: ASTContext.cpp:2524
clang::LangOptions::TrivialAutoVarInitKind
TrivialAutoVarInitKind
Definition: LangOptions.h:92
clang::CodeGen::CodeGenFunction::EmitLoadOfScalar
llvm::Value * EmitLoadOfScalar(Address Addr, bool Volatile, QualType Ty, SourceLocation Loc, AlignmentSource Source=AlignmentSource::Type, bool isNontemporal=false)
EmitLoadOfScalar - Load a scalar value from an address, taking care to appropriately convert from the...
Definition: CodeGenFunction.h:3829
clang::CodeGen::CodeGenFunction::ComplexPairTy
std::pair< llvm::Value *, llvm::Value * > ComplexPairTy
Definition: CodeGenFunction.h:267
clang::VarDecl::getFlexibleArrayInitChars
CharUnits getFlexibleArrayInitChars(const ASTContext &Ctx) const
If hasFlexibleArrayInit is true, compute the number of additional bytes necessary to store those elem...
Definition: Decl.cpp:2762
clang::CodeGen::CodeGenModule::getOrCreateStaticVarDecl
llvm::Constant * getOrCreateStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:242
clang::CodeGen::CodeGenTypes::ConvertTypeForMem
llvm::Type * ConvertTypeForMem(QualType T, bool ForBitField=false)
ConvertTypeForMem - Convert type T into a llvm::Type.
Definition: CodeGenTypes.cpp:91
clang::CodeGen::ReturnValueSlot
ReturnValueSlot - Contains the address where the return value of a function can be stored,...
Definition: CGCall.h:357
clang::LangAS::opencl_local
@ opencl_local
clang::QualType::DK_cxx_destructor
@ DK_cxx_destructor
Definition: Type.h:1277
clang::CodeGen::CodeGenFunction::CreateDefaultAlignTempAlloca
Address CreateDefaultAlignTempAlloca(llvm::Type *Ty, const Twine &Name="tmp")
CreateDefaultAlignedTempAlloca - This creates an alloca with the default ABI alignment of the given L...
Definition: CGExpr.cpp:123
V
#define V(N, I)
Definition: ASTContext.h:3237
length
float __ovld __cnfn length(float)
Return the length of vector p, i.e., sqrt(p.x2 + p.y 2 + ...)
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::EmitOMPDeclareMapper
void EmitOMPDeclareMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare mapper construct.
Definition: CGDecl.cpp:2634
clang::CodeGen::CodeGenFunction::BlockInfo
const CodeGen::CGBlockInfo * BlockInfo
Definition: CodeGenFunction.h:609
clang::CodeGen::CodeGenFunction::getVLAElements1D
VlaSizePair getVLAElements1D(const VariableArrayType *vla)
Return the number of elements for a single dimension for the given array type.
Definition: CodeGenFunction.cpp:2197
clang::CodeGen::CodeGenFunction::hasScalarEvaluationKind
static bool hasScalarEvaluationKind(QualType T)
Definition: CodeGenFunction.h:2419
clang::RecordType
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
Definition: Type.h:4816
clang::CompoundStmt
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition: Stmt.h:1410
clang::CodeGen::CodeGenFunction::AutoVarEmission::getOriginalAllocatedAddress
Address getOriginalAllocatedAddress() const
Returns the address for the original alloca instruction.
Definition: CodeGenFunction.h:3111
clang::OMPDeclareReductionDecl
This represents '#pragma omp declare reduction ...' directive.
Definition: DeclOpenMP.h:171
clang::CodeGen::EHScopeStack::Cleanup
Information for lazily generating a cleanup.
Definition: EHScopeStack.h:141
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::ARCPreciseLifetime
@ ARCPreciseLifetime
Definition: CGValue.h:125
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:2194
clang::BlockDecl
Represents a block literal declaration, which is like an unnamed FunctionDecl.
Definition: Decl.h:4283
clang::CodeGen::CodeGenFunction::EmitNullabilityCheck
void EmitNullabilityCheck(LValue LHS, llvm::Value *RHS, SourceLocation Loc)
Given an assignment *LHS = RHS, emit a test that checks if RHS is nonnull, if LHS is marked _Nonnull.
Definition: CGDecl.cpp:755
clang::QualType::getAddressSpace
LangAS getAddressSpace() const
Return the address space of this type.
Definition: Type.h:6756
clang::Decl::getKind
Kind getKind() const
Definition: DeclBase.h:435
clang::QualType::PDIK_Struct
@ PDIK_Struct
The type is a struct containing a field whose type is not PCK_Trivial.
Definition: Type.h:1223
clang::Decl::getNonClosureContext
Decl * getNonClosureContext()
Find the innermost non-closure ancestor of this declaration, walking up through blocks,...
Definition: DeclBase.cpp:1076
getRecordType
static const RecordType * getRecordType(QualType QT)
Checks that the passed in QualType either is of RecordType or points to RecordType.
Definition: SemaDeclAttr.cpp:492
clang::Qualifiers::OCL_Strong
@ OCL_Strong
Assigning into this object requires the old value to be released and the new value to be retained.
Definition: Type.h:175
hlsl::uint64_t
unsigned long uint64_t
Definition: hlsl_basic_types.h:25
clang::Scope
Scope - A scope is a transient data structure that is used while parsing the program.
Definition: Scope.h:40
clang::CodeGen::CodeGenFunction::AutoVarEmission
Definition: CodeGenFunction.h:3052
clang::CodeGen::CodeGenFunction::EmitCheck
void EmitCheck(ArrayRef< std::pair< llvm::Value *, SanitizerMask >> Checked, SanitizerHandler Check, ArrayRef< llvm::Constant * > StaticArgs, ArrayRef< llvm::Value * > DynamicArgs)
Create a basic block that will either trap or call a handler function in the UBSan runtime with the p...
Definition: CGExpr.cpp:3260
clang::syntax::NodeRole::Callee
@ Callee
clang::ASTContext::getAsArrayType
const ArrayType * getAsArrayType(QualType T) const
Type Query functions.
Definition: ASTContext.cpp:6873
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
clang::CodeGen::CodeGenFunction::defaultInitNonTrivialCStructVar
void defaultInitNonTrivialCStructVar(LValue Dst)
Definition: CGNonTrivialStruct.cpp:818
CGOpenMPRuntime.h
clang::CodeGen::CodeGenFunction::EmitARCUnsafeUnretainedScalarExpr
llvm::Value * EmitARCUnsafeUnretainedScalarExpr(const Expr *expr)
EmitARCUnsafeUnretainedScalarExpr - Semantically equivalent to immediately releasing the resut of Emi...
Definition: CGObjC.cpp:3599
clang::CodeGen::CodeGenModule::getCodeGenOpts
const CodeGenOptions & getCodeGenOpts() const
Definition: CodeGenModule.h:731
clang::CodeGen::CodeGenModule::createUnnamedGlobalFrom
Address createUnnamedGlobalFrom(const VarDecl &D, llvm::Constant *Constant, CharUnits Align)
Definition: CGDecl.cpp:1115
CGFunctionInfo.h
clang::CPlusPlus
@ CPlusPlus
Definition: LangStandard.h:53
clang::ArrayType
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition: Type.h:3012
clang::CodeGen::CodeGenFunction::EmitARCStoreWeak
llvm::Value * EmitARCStoreWeak(Address addr, llvm::Value *value, bool ignored)
i8* @objc_storeWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2629
clang::CodeGen::CodeGenFunction::ParamValue::isIndirect
bool isIndirect() const
Definition: CodeGenFunction.h:3156
clang::CodeGen::TEK_Complex
@ TEK_Complex
Definition: CodeGenFunction.h:108
CGCleanup.h
clang::CodeGen::CodeGenFunction::EnsureInsertPoint
void EnsureInsertPoint()
EnsureInsertPoint - Ensure that an insertion point is defined so that emitted IR has a place to go.
Definition: CodeGenFunction.h:2477
clang::Type::getAs
const T * getAs() const
Member-template getAs<specific type>'.
Definition: Type.h:7386
clang::CodeGen::CodeGenFunction::getTarget
const TargetInfo & getTarget() const
Definition: CodeGenFunction.h:2026
clang::QualType::DK_none
@ DK_none
Definition: Type.h:1276
clang::CodeGen::CodeGenFunction::getDebugInfo
CGDebugInfo * getDebugInfo()
Definition: CodeGenFunction.h:1985
clang::ASTContext::getAsVariableArrayType
const VariableArrayType * getAsVariableArrayType(QualType T) const
Definition: ASTContext.h:2729
clang::CodeGen::CodeGenModule::GetAddrOfGlobal
llvm::Constant * GetAddrOfGlobal(GlobalDecl GD, ForDefinition_t IsForDefinition=NotForDefinition)
Definition: CodeGenModule.cpp:4473
clang::Sema::MaximumAlignment
static const uint64_t MaximumAlignment
Definition: Sema.h:398
clang::VarDecl::isEscapingByref
bool isEscapingByref() const
Indicates the capture is a __block variable that is captured by a block that can potentially escape (...
Definition: Decl.cpp:2591
clang::VarDecl::isNRVOVariable
bool isNRVOVariable() const
Determine whether this local variable can be used with the named return value optimization (NRVO).
Definition: Decl.h:1452
clang::CodeGen::CodeGenFunction::pushRegularPartialArrayCleanup
void pushRegularPartialArrayCleanup(llvm::Value *arrayBegin, llvm::Value *arrayEnd, QualType elementType, CharUnits elementAlignment, Destroyer *destroyer)
pushRegularPartialArrayCleanup - Push an EH cleanup to destroy already-constructed elements of the gi...
Definition: CGDecl.cpp:2395
EmitAutoVarWithLifetime
static void EmitAutoVarWithLifetime(CodeGenFunction &CGF, const VarDecl &var, Address addr, Qualifiers::ObjCLifetime lifetime)
EmitAutoVarWithLifetime - Does the setup required for an automatic variable with lifetime.
Definition: CGDecl.cpp:631
clang::Stmt::children
child_range children()
Definition: Stmt.cpp:285
clang::CharUnits::One
static CharUnits One()
One - Construct a CharUnits quantity of one.
Definition: CharUnits.h:58
clang::CodeGen::CodeGenModule::getModule
llvm::Module & getModule() const
Definition: CodeGenModule.h:732
clang::CodeGen::CodeGenFunction::CallLifetimeEnd
Definition: CodeGenFunction.h:625
clang::CodeGen::CodeGenFunction::EmitARCStoreStrongCall
llvm::Value * EmitARCStoreStrongCall(Address addr, llvm::Value *value, bool resultIgnored)
Store into a strong object.
Definition: CGObjC.cpp:2501
clang::CXXDestructorDecl
Represents a C++ destructor within a class.
Definition: DeclCXX.h:2703
clang::CodeGen::CodeGenFunction::AutoVarEmission::getAllocatedAddress
Address getAllocatedAddress() const
Returns the raw, allocated address, which is not necessarily the address of the object itself.
Definition: CodeGenFunction.h:3106
clang::CodeGen::CGDebugInfo
This class gathers all debug information during compilation and is responsible for emitting to llvm g...
Definition: CGDebugInfo.h:55
clang::CodeGen::CGDebugInfo::EmitGlobalVariable
void EmitGlobalVariable(llvm::GlobalVariable *GV, const VarDecl *Decl)
Emit information about a global variable.
Definition: CGDebugInfo.cpp:5254
clang::CodeGen::CodeGenFunction::getCleanupKind
CleanupKind getCleanupKind(QualType::DestructionKind kind)
Definition: CodeGenFunction.h:2092
clang::VarDecl::hasExternalStorage
bool hasExternalStorage() const
Returns true if a variable has extern or private_extern storage.
Definition: Decl.h:1164
clang::CodeGen::LValue::getType
QualType getType() const
Definition: CGValue.h:278
llvm::SmallString
Definition: LLVM.h:37
clang::LangOptions::TrivialAutoVarInitKind::Uninitialized
@ Uninitialized
clang::CodeGen::AggValueSlot::MayOverlap
@ MayOverlap
Definition: CGValue.h:553
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:2077
clang::CodeGen::CodeGenFunction::pushEHDestroy
void pushEHDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushEHDestroy - Push the standard destructor for the given type as an EH-only cleanup.
Definition: CGDecl.cpp:2110
clang::LangAS::opencl_constant
@ opencl_constant
ASTContext.h
clang::interp::Zero
bool Zero(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1122
clang::ASTContext::getIntTypeForBitwidth
QualType getIntTypeForBitwidth(unsigned DestWidth, unsigned Signed) const
getIntTypeForBitwidth - sets integer QualTy according to specified details: bitwidth,...
Definition: ASTContext.cpp:11958
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:906
clang::CodeGen::CodeGenModule::AddGlobalAnnotations
void AddGlobalAnnotations(const ValueDecl *D, llvm::GlobalValue *GV)
Add global annotations that are set on D, for the global GV.
Definition: CodeGenModule.cpp:2893
clang::CodeGen::CodeGenModule::EmitOMPAllocateDecl
void EmitOMPAllocateDecl(const OMPAllocateDecl *D)
Emit a code for the allocate directive.
Definition: CGDecl.cpp:2646
clang::OMPDeclareMapperDecl
This represents '#pragma omp declare mapper ...' directive.
Definition: DeclOpenMP.h:286
IsPattern::Yes
@ Yes
clang::CodeGen::CodeGenModule::getTypes
CodeGenTypes & getTypes()
Definition: CodeGenModule.h:749
clang::CodeGen::CodeGenFunction::createCleanupActiveFlag
Address createCleanupActiveFlag()
Definition: CGCleanup.cpp:292
clang::dataflow::var
static constexpr Variable var(Literal L)
Returns the variable of L.
Definition: WatchedLiteralsSolver.cpp:71
clang::CodeGen::CodeGenFunction::EmitAutoVarInit
void EmitAutoVarInit(const AutoVarEmission &emission)
Definition: CGDecl.cpp:1813
clang::CodeGen::CodeGenFunction::getBlockByrefInfo
const BlockByrefInfo & getBlockByrefInfo(const VarDecl *var)
BuildByrefInfo - This routine changes a __block variable declared as T x into:
Definition: CGBlocks.cpp:2608
clang::VarDecl::isLocalVarDecl
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition: Decl.h:1200
clang::CodeGen::AggValueSlot::IsDestructed
@ IsDestructed
Definition: CGValue.h:551
clang::CodeGen::CodeGenFunction::getVLASize
VlaSizePair getVLASize(const VariableArrayType *vla)
Returns an LLVM value that corresponds to the size, in non-variably-sized elements,...
Definition: CodeGenFunction.cpp:2166
clang::Type::isVariablyModifiedType
bool isVariablyModifiedType() const
Whether this type is a variably-modified type (C99 6.7.5).
Definition: Type.h:2322
constStructWithPadding
static llvm::Constant * constStructWithPadding(CodeGenModule &CGM, IsPattern isPattern, llvm::StructType *STy, llvm::Constant *constant)
Helper function for constWithPadding() to deal with padding in structures.
Definition: CGDecl.cpp:1039
drillIntoBlockVariable
static void drillIntoBlockVariable(CodeGenFunction &CGF, LValue &lvalue, const VarDecl *var)
Definition: CGDecl.cpp:749
clang::CodeGen::CodeGenFunction::EmitAutoVarAlloca
AutoVarEmission EmitAutoVarAlloca(const VarDecl &var)
EmitAutoVarAlloca - Emit the alloca and debug information for a local variable.
Definition: CGDecl.cpp:1426
clang::CodeGen::CodeGenFunction::AddInitializerToStaticVarDecl
llvm::GlobalVariable * AddInitializerToStaticVarDecl(const VarDecl &D, llvm::GlobalVariable *GV)
AddInitializerToStaticVarDecl - Add the initializer for 'D' to the global variable that has already b...
Definition: CGDecl.cpp:337
clang::CodeGen::CodeGenFunction::getContext
ASTContext & getContext() const
Definition: CodeGenFunction.h:1984
clang::CodeGen::NormalCleanup
@ NormalCleanup
Denotes a cleanup that should run when a scope is exited using normal control flow (falling off the e...
Definition: EHScopeStack.h:84
clang::CodeGen::BlockFieldFlags
Definition: CGBlocks.h:102
clang::CodeGen::CGOpenCLRuntime::EmitWorkGroupLocalVarDecl
virtual void EmitWorkGroupLocalVarDecl(CodeGenFunction &CGF, const VarDecl &D)
Emit the IR required for a work-group-local variable declaration, and add an entry to CGF's LocalDecl...
Definition: CGOpenCLRuntime.cpp:28
clang::CodeGen::Address
An aligned address.
Definition: Address.h:74
clang::CodeGen::CGBuilderTy::CreateFlagLoad
llvm::LoadInst * CreateFlagLoad(llvm::Value *Addr, const llvm::Twine &Name="")
Emit a load from an i1 flag variable.
Definition: CGBuilder.h:121
clang::CodeGen::CodeGenFunction::enterByrefCleanup
void enterByrefCleanup(CleanupKind Kind, Address Addr, BlockFieldFlags Flags, bool LoadBlockVarAddr, bool CanThrow)
Enter a cleanup to destroy a __block variable.
Definition: CGBlocks.cpp:2815
Label
std::string Label
Definition: UsingDeclarationsSorter.cpp:69
CodeGenModule.h
clang::RecordDecl::isParamDestroyedInCallee
bool isParamDestroyedInCallee() const
Definition: Decl.h:4145
clang::CodeGen::CodeGenFunction::getARCCleanupKind
CleanupKind getARCCleanupKind()
Retrieves the default cleanup kind for an ARC cleanup.
Definition: CodeGenFunction.h:4288
clang::CodeGen::EHScopeStack::ConditionalCleanup
ConditionalCleanup stores the saved form of its parameters, then restores them and performs the clean...
Definition: EHScopeStack.h:197
clang::CodeGen::CodeGenFunction::SanOpts
SanitizerSet SanOpts
Sanitizers enabled for this function.
Definition: CodeGenFunction.h:518
clang::CodeGen::CodeGenFunction::getEvaluationKind
static TypeEvaluationKind getEvaluationKind(QualType T)
getEvaluationKind - Return the TypeEvaluationKind of QualType T.
Definition: CodeGenFunction.cpp:218
clang::CodeGen::CodeGenFunction::EmitAtomicInit
void EmitAtomicInit(Expr *E, LValue lvalue)
Definition: CGAtomic.cpp:2141
clang::ast_matchers::arrayType
const AstTypeMatcher< ArrayType > arrayType
Matches all kinds of arrays.
Definition: ASTMatchersInternal.cpp:1035
Sema.h
clang::VarDecl::Create
static VarDecl * Create(ASTContext &C, DeclContext *DC, SourceLocation StartLoc, SourceLocation IdLoc, const IdentifierInfo *Id, QualType T, TypeSourceInfo *TInfo, StorageClass S)
Definition: Decl.cpp:2076
constWithPadding
static llvm::Constant * constWithPadding(CodeGenModule &CGM, IsPattern isPattern, llvm::Constant *constant)
Replace all padding bytes in a given constant with either a pattern byte or 0x00.
Definition: CGDecl.cpp:1079
clang::Qualifiers::OCL_Autoreleasing
@ OCL_Autoreleasing
Assigning into this object requires a lifetime extension.
Definition: Type.h:181
IsPattern
IsPattern
Definition: CGDecl.cpp:1024
clang::CodeGen::CGCallee::forDirect
static CGCallee forDirect(llvm::Constant *functionPtr, const CGCalleeInfo &abstractInfo=CGCalleeInfo())
Definition: CGCall.h:130
clang::interp::Null
bool Null(InterpState &S, CodePtr OpPC)
Definition: Interp.h:1128
clang::Qualifiers::ObjCLifetime
ObjCLifetime
Definition: Type.h:162
clang::CodeGen::CodeGenFunction::EmitARCRetainScalarExpr
llvm::Value * EmitARCRetainScalarExpr(const Expr *expr)
EmitARCRetainScalarExpr - Semantically equivalent to EmitARCRetainObject(e->getType(),...
Definition: CGObjC.cpp:3474
clang::CodeGen::CodeGenFunction::isTrivialInitializer
bool isTrivialInitializer(const Expr *Init)
Determine whether the given initializer is trivial in the sense that it requires no code to be genera...
Definition: CGDecl.cpp:1706
clang::QualType::getUnqualifiedType
QualType getUnqualifiedType() const
Retrieve the unqualified variant of the given type, removing as little sugar as possible.
Definition: Type.h:6715
clang::CodeGen::CodeGenFunction::Bypasses
VarBypassDetector Bypasses
Definition: CodeGenFunction.h:273
clang::CodeGen::LValue::getObjCLifetime
Qualifiers::ObjCLifetime getObjCLifetime() const
Definition: CGValue.h:280
CharUnits.h
clang::CodeGen::CodeGenFunction::destroyARCStrongImprecise
static Destroyer destroyARCStrongImprecise
Definition: CodeGenFunction.h:4353
clang::LangAS
LangAS
Defines the address space values used by the address space qualifier of QualType.
Definition: AddressSpaces.h:25
clang::CodeGen::CodeGenFunction::ContainsLabel
static bool ContainsLabel(const Stmt *S, bool IgnoreCaseStmts=false)
ContainsLabel - Return true if the statement contains a label in it.
Definition: CodeGenFunction.cpp:1493
clang::VK_LValue
@ VK_LValue
An l-value expression is a reference to an object with independent storage.
Definition: Specifiers.h:127
clang::CodeGen::CodeGenModule::getDataLayout
const llvm::DataLayout & getDataLayout() const
Definition: CodeGenModule.h:734
clang::Type::castAs
const T * castAs() const
Member-template castAs<specific type>.
Definition: Type.h:7453
clang::Expr::IgnoreParenCasts
Expr * IgnoreParenCasts() LLVM_READONLY
Skip past any parentheses and casts which might surround this expression until reaching a fixed point...
Definition: Expr.cpp:3040
clang::CodeGen::initializationPatternFor
llvm::Constant * initializationPatternFor(CodeGenModule &, llvm::Type *)
Definition: PatternInit.cpp:15
clang::CodeGen::LValue
LValue - This represents an lvalue references.
Definition: CGValue.h:171
clang::CodeGen::CodeGenFunction::EHStack
EHScopeStack EHStack
Definition: CodeGenFunction.h:619
clang::ast_matchers::decl
const internal::VariadicAllOfMatcher< Decl > decl
Matches declarations.
Definition: ASTMatchersInternal.cpp:734
clang::CodeGen::CodeGenFunction::getTargetHooks
const TargetCodeGenInfo & getTargetHooks() const
Definition: CodeGenFunction.h:2028
clang::CodeGen::CodeGenFunction::EmitDeclRefLValue
LValue EmitDeclRefLValue(const DeclRefExpr *E)
Definition: CGExpr.cpp:2718
PatternInit.h
clang::CodeGen::BLOCK_FIELD_IS_BYREF
@ BLOCK_FIELD_IS_BYREF
Definition: CGBlocks.h:92
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:376
clang::CodeGen::CodeGenModule::setStaticLocalDeclAddress
void setStaticLocalDeclAddress(const VarDecl *D, llvm::Constant *C)
Definition: CodeGenModule.h:668
clang::CodeGen::Address::getPointer
llvm::Value * getPointer() const
Definition: Address.h:93
clang::CodeGen::CodeGenModule::ErrorUnsupported
void ErrorUnsupported(const Stmt *S, const char *Type)
Print out an error that codegen doesn't support the specified stmt yet.
Definition: CodeGenModule.cpp:1075
emitStoresForConstant
static void emitStoresForConstant(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder, llvm::Constant *constant, bool IsAutoInit)
Definition: CGDecl.cpp:1174
clang::CodeGen::CodeGenFunction
CodeGenFunction - This class organizes the per-function state that is used while generating LLVM code...
Definition: CodeGenFunction.h:231
clang::ASTContext::getDeclAlign
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
Definition: ASTContext.cpp:1742
clang::ValueDecl
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition: Decl.h:701
clang::ast_matchers::castExpr
const internal::VariadicDynCastAllOfMatcher< Stmt, CastExpr > castExpr
Matches any cast nodes of Clang's AST.
Definition: ASTMatchersInternal.cpp:976
clang::CodeGen::CodeGenFunction::pushDestroy
void pushDestroy(QualType::DestructionKind dtorKind, Address addr, QualType type)
pushDestroy - Push the standard destructor for the given type as at least a normal cleanup.
Definition: CGDecl.cpp:2120
clang::LangOptions::NonGC
@ NonGC
Definition: LangOptions.h:86
shouldSplitConstantStore
static bool shouldSplitConstantStore(CodeGenModule &CGM, uint64_t GlobalByteSize)
Decide whether we want to split a constant structure or array store into a sequence of its fields' st...
Definition: CGDecl.cpp:1013
clang::CodeGen::CGOpenMPRuntime::processRequiresDirective
virtual void processRequiresDirective(const OMPRequiresDecl *D)
Perform check on requires decl to ensure that target architecture supports unified addressing.
Definition: CGOpenMPRuntime.cpp:10419
clang::VarDecl::hasFlexibleArrayInit
bool hasFlexibleArrayInit(const ASTContext &Ctx) const
Whether this variable has a flexible array member initialized with one or more elements.
Definition: Decl.cpp:2747
clang::CodeGen::CodeGenFunction::EmitParmDecl
void EmitParmDecl(const VarDecl &D, ParamValue Arg, unsigned ArgNo)
EmitParmDecl - Emit a ParmVarDecl or an ImplicitParamDecl.
Definition: CGDecl.cpp:2445
clang::CodeGen::AggValueSlot::IsNotAliased
@ IsNotAliased
Definition: CGValue.h:550
clang::ASTContext::Idents
IdentifierTable & Idents
Definition: ASTContext.h:660
clang::CodeGen::CodeGenFunction::EmitARCMoveWeak
void EmitARCMoveWeak(Address dst, Address src)
void @objc_moveWeak(i8** dest, i8** src) Disregards the current value in dest.
Definition: CGObjC.cpp:2673
clang::CodeGen::CodeGenFunction::emitArrayDestroy
void emitArrayDestroy(llvm::Value *begin, llvm::Value *end, QualType elementType, CharUnits elementAlign, Destroyer *destroyer, bool checkZeroLength, bool useEHCleanup)
emitArrayDestroy - Destroys all the elements of the given array, beginning from last to first.
Definition: CGDecl.cpp:2234
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:329
clang::QualType::isConstant
bool isConstant(const ASTContext &Ctx) const
Definition: Type.h:875
clang::CodeGen::CodeGenModule
This class organizes the cross-function state that is used while generating LLVM code.
Definition: CodeGenModule.h:279
clang::CodeGen::Address::withPointer
Address withPointer(llvm::Value *NewPointer) const
Return address with different pointer, but same element type and alignment.
Definition: Address.h:127
clang::QualType::DK_objc_strong_lifetime
@ DK_objc_strong_lifetime
Definition: Type.h:1278
clang::CodeGen::CodeGenFunction::EmitARCRelease
void EmitARCRelease(llvm::Value *value, ARCPreciseLifetime_t precise)
Release the given object.
Definition: CGObjC.cpp:2458
Begin
SourceLocation Begin
Definition: USRLocFinder.cpp:165
clang::BlockExpr
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition: Expr.h:5975
clang::StmtExpr
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition: Expr.h:4354
clang::CodeGen::CodeGenFunction::EmitComplexExpr
ComplexPairTy EmitComplexExpr(const Expr *E, bool IgnoreReal=false, bool IgnoreImag=false)
EmitComplexExpr - Emit the computation of the specified expression of complex type,...
Definition: CGExprComplex.cpp:1291
clang::CodeGen::CodeGenModule::EmitOMPDeclareReduction
void EmitOMPDeclareReduction(const OMPDeclareReductionDecl *D, CodeGenFunction *CGF=nullptr)
Emit a code for declare reduction construct.
Definition: CGDecl.cpp:2627
clang::CodeGen::CodeGenFunction::EmitScalarInit
void EmitScalarInit(const Expr *init, const ValueDecl *D, LValue lvalue, bool capturedByInit)
Definition: CGDecl.cpp:776
clang::ImplicitParamDecl::ThreadPrivateVar
@ ThreadPrivateVar
Parameter for Thread private variable.
Definition: Decl.h:1669
Value
Value
Definition: UninitializedValues.cpp:103
clang::Decl
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:83
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::ApplyDebugLocation::CreateDefaultArtificial
static ApplyDebugLocation CreateDefaultArtificial(CodeGenFunction &CGF, SourceLocation TemporaryLocation)
Apply TemporaryLocation if it is valid.
Definition: CGDebugInfo.h:836
clang::CodeGen::Address::invalid
static Address invalid()
Definition: Address.h:90
patternOrZeroFor
static llvm::Constant * patternOrZeroFor(CodeGenModule &CGM, IsPattern isPattern, llvm::Type *Ty)
Generate a constant filled with either a pattern or zeroes.
Definition: CGDecl.cpp:1027
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::QualType::isObjCGCWeak
bool isObjCGCWeak() const
true when Type is objc's weak.
Definition: Type.h:1183
clang::CodeGen::CodeGenFunction::NRVOFlags
llvm::DenseMap< const VarDecl *, llvm::Value * > NRVOFlags
A mapping from NRVO variables to the flags used to indicate when the NRVO has been applied to this va...
Definition: CodeGenFunction.h:617
clang::QualType::isPODType
bool isPODType(const ASTContext &Context) const
Determine whether this is a Plain Old Data (POD) type (C++ 3.9p10).
Definition: Type.cpp:2361
clang::QualType::DestructionKind
DestructionKind
Definition: Type.h:1275
clang::Type::isConstantSizeType
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition: Type.cpp:2225
clang::CodeGen::CodeGenFunction::ConvertType
llvm::Type * ConvertType(QualType T)
Definition: CodeGenFunction.cpp:214
clang::CodeGen::NormalAndEHCleanup
@ NormalAndEHCleanup
Definition: EHScopeStack.h:86
clang::CodeGenOptions::hasReducedDebugInfo
bool hasReducedDebugInfo() const
Check if type and variable info should be emitted.
Definition: CodeGenOptions.h:482
clang::LangOptions::TrivialAutoVarInitKind::Pattern
@ Pattern
clang::CodeGen::Address::getElementType
llvm::Type * getElementType() const
Return the type of the values stored in this address.
Definition: Address.h:104
clang::VarDecl::getInit
const Expr * getInit() const
Definition: Decl.h:1315
clang::QualType::DK_objc_weak_lifetime
@ DK_objc_weak_lifetime
Definition: Type.h:1279
clang::CodeGen::CodeGenFunction::MakeAddrLValue
LValue MakeAddrLValue(Address Addr, QualType T, AlignmentSource Source=AlignmentSource::Type)
Definition: CodeGenFunction.h:2490
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:85
clang::CodeGen::LValue::getPointer
llvm::Value * getPointer(CodeGenFunction &CGF) const
Definition: CGValue.h:337
clang::CodeGen::CodeGenModule::getNullPointer
llvm::Constant * getNullPointer(llvm::PointerType *T, QualType QT)
Get target specific null pointer.
Definition: CGExprConstant.cpp:1762
emitStoresForPatternInit
static void emitStoresForPatternInit(CodeGenModule &CGM, const VarDecl &D, Address Loc, bool isVolatile, CGBuilderTy &Builder)
Definition: CGDecl.cpp:1278
clang::CodeGen::CGBuilderTy::CreateElementBitCast
Address CreateElementBitCast(Address Addr, llvm::Type *Ty, const llvm::Twine &Name="")
Cast the element type of the given address to a different type, preserving information like the align...
Definition: CGBuilder.h:168
shouldUseBZeroPlusStoresToInitialize
static bool shouldUseBZeroPlusStoresToInitialize(llvm::Constant *Init, uint64_t GlobalSize)
Decide whether we should use bzero plus some stores to initialize a local variable instead of using a...
Definition: CGDecl.cpp:980
IsPattern::No
@ No
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:5041
clang::CodeGen::CodeGenFunction::destroyNonTrivialCStruct
static Destroyer destroyNonTrivialCStruct
Definition: CodeGenFunction.h:4357
clang::NamedDecl::getNameAsString
std::string getNameAsString() const
Get a human-readable name for the declaration, even if it is one of the special kinds of names (C++ c...
Definition: Decl.h:290
clang::CodeGen::CodeGenModule::supportsCOMDAT
bool supportsCOMDAT() const
Definition: TargetInfo.cpp:12064
clang::CodeGen::CodeGenFunction::EmitAndRegisterVariableArrayDimensions
void EmitAndRegisterVariableArrayDimensions(CGDebugInfo *DI, const VarDecl &D, bool EmitDebugInfo)
Emits the alloca and debug information for the size expressions for each dimension of an array.
Definition: CGDecl.cpp:1361
clang::ObjCPropertyAttribute::Kind
Kind
Definition: DeclObjCCommon.h:22
clang::CodeGen::CGFunctionInfo
CGFunctionInfo - Class to encapsulate the information about a function definition.
Definition: CGFunctionInfo.h:546
clang::CodeGen::AggValueSlot::Overlap_t
Overlap_t
Definition: CGValue.h:553
clang::VarDecl::isARCPseudoStrong
bool isARCPseudoStrong() const
Determine whether this variable is an ARC pseudo-__strong variable.
Definition: Decl.h:1487
CGCXXABI.h
clang::CodeGen::BLOCK_FIELD_IS_WEAK
@ BLOCK_FIELD_IS_WEAK
Definition: CGBlocks.h:94
clang::CodeGen::CodeGenFunction::setBlockContextParameter
void setBlockContextParameter(const ImplicitParamDecl *D, unsigned argNum, llvm::Value *ptr)
Definition: CGBlocks.cpp:1405
clang::CodeGen::CodeGenFunction::EmitLifetimeStart
llvm::Value * EmitLifetimeStart(llvm::TypeSize Size, llvm::Value *Addr)
Emit a lifetime.begin marker if some criteria are satisfied.
Definition: CGDecl.cpp:1334
clang::CodeGen::CodeGenFunction::EmitStoreOfComplex
void EmitStoreOfComplex(ComplexPairTy V, LValue dest, bool isInit)
EmitStoreOfComplex - Store a complex number into the specified l-value.
Definition: CGExprComplex.cpp:1310
clang::LangAS::Default
@ Default
clang::Qualifiers::OCL_None
@ OCL_None
There is no lifetime qualification on this type.
Definition: Type.h:164
clang::CodeGen::CodeGenFunction::EmitVarAnnotations
void EmitVarAnnotations(const VarDecl *D, llvm::Value *V)
Emit local annotations for the local variable V, declared by D.
Definition: CodeGenFunction.cpp:2472
clang::CodeGen::CodeGenFunction::OMPBuilderCBHelpers::getAddressOfLocalVariable
static Address getAddressOfLocalVariable(CodeGenFunction &CGF, const VarDecl *VD)
Gets the OpenMP-specific address of the local variable /p VD.
Definition: CGStmtOpenMP.cpp:1610
clang::CodeGen::CodeGenFunction::CGM
CodeGenModule & CGM
Definition: CodeGenFunction.h:261
clang::CodeGen::TEK_Scalar
@ TEK_Scalar
Definition: CodeGenFunction.h:107
clang::CodeGen::CGOpenMPRuntime::emitUserDefinedReduction
virtual void emitUserDefinedReduction(CodeGenFunction *CGF, const OMPDeclareReductionDecl *D)
Emit code for the specified user defined reduction construct.
Definition: CGOpenMPRuntime.cpp:1146
clang::CodeGen::CodeGenFunction::cxxDestructorCanThrow
static bool cxxDestructorCanThrow(QualType T)
Check if T is a C++ class that has a destructor that can throw.
Definition: CGBlocks.cpp:1703
CGBlocks.h
clang::CodeGen::CleanupKind
CleanupKind
Definition: EHScopeStack.h:77
replaceUndef
static llvm::Constant * replaceUndef(CodeGenModule &CGM, IsPattern isPattern, llvm::Constant *constant)
Definition: CGDecl.cpp:1300
clang::Expr::IgnoreParens
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition: Expr.cpp:3031
clang
Definition: CalledOnceCheck.h:17
CGOpenCLRuntime.h
clang::CodeGen::RValue::get
static RValue get(llvm::Value *V)
Definition: CGValue.h:89
clang::Type::isArrayType
bool isArrayType() const
Definition: Type.h:6949
clang::CodeGen::CodeGenFunction::ParamValue::getAnyValue
llvm::Value * getAnyValue() const
Definition: CodeGenFunction.h:3157
clang::Qualifiers::getObjCLifetime
ObjCLifetime getObjCLifetime() const
Definition: Type.h:352
clang::DeclaratorContext::Block
@ Block
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:71
clang::CodeGen::CodeGenFunction::ParamValue::getIndirectAddress
Address getIndirectAddress() const
Definition: CodeGenFunction.h:3164
clang::VarDecl::isConstexpr
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition: Decl.h:1509
clang::CodeGen::CGOpenMPRuntime::emitUserDefinedMapper
void emitUserDefinedMapper(const OMPDeclareMapperDecl *D, CodeGenFunction *CGF=nullptr)
Emit the function for the user defined mapper construct.
Definition: CGOpenMPRuntime.cpp:9441
clang::CodeGen::CGDebugInfo::setLocation
void setLocation(SourceLocation Loc)
Update the current source location.
Definition: CGDebugInfo.cpp:174
clang::CodeGen::CodeGenTypeCache::IntPtrTy
llvm::IntegerType * IntPtrTy
Definition: CodeGenTypeCache.h:49
clang::CodeGen::CodeGenModule::getLLVMLifetimeEndFn
llvm::Function * getLLVMLifetimeEndFn()
Lazily declare the @llvm.lifetime.end intrinsic.
Definition: CGDecl.cpp:2416
clang::CodeGen::CodeGenModule::getNaturalTypeAlignment
CharUnits getNaturalTypeAlignment(QualType T, LValueBaseInfo *BaseInfo=nullptr, TBAAAccessInfo *TBAAInfo=nullptr, bool forPointeeType=false)
Definition: CodeGenModule.cpp:6945
tryEmitARCCopyWeakInit
static bool tryEmitARCCopyWeakInit(CodeGenFunction &CGF, const LValue &destLV, const Expr *init)
Definition: CGDecl.cpp:699
clang::CodeGen::CodeGenFunction::ParamValue
Definition: CodeGenFunction.h:3140
clang::CodeGen::CodeGenTypes::arrangeFunctionDeclaration
const CGFunctionInfo & arrangeFunctionDeclaration(const FunctionDecl *FD)
Free functions are functions that are compatible with an ordinary C function pointer type.
Definition: CGCall.cpp:447
clang::Expr::getType
QualType getType() const
Definition: Expr.h:141
s
__device__ __2f16 float bool s
Definition: __clang_hip_libdevice_declares.h:320
clang::OMPRequiresDecl
This represents '#pragma omp requires...' directive.
Definition: DeclOpenMP.h:416
clang::SC_Auto
@ SC_Auto
Definition: Specifiers.h:244
clang::CodeGen::CodeGenFunction::CurFuncIsThunk
bool CurFuncIsThunk
In C++, whether we are code generating a thunk.
Definition: CodeGenFunction.h:533
clang::CodeGen::CodeGenFunction::ReturnValuePointer
Address ReturnValuePointer
ReturnValuePointer - The temporary alloca to hold a pointer to sret.
Definition: CodeGenFunction.h:360
clang::CodeGen::CodeGenFunction::AutoVarEmission::getSizeForLifetimeMarkers
llvm::Value * getSizeForLifetimeMarkers() const
Definition: CodeGenFunction.h:3098
clang::CodeGen::CGDebugInfo::registerVLASizeExpression
void registerVLASizeExpression(QualType Ty, llvm::Metadata *SizeExpr)
Register VLA size expression debug node with the qualified type.
Definition: CGDebugInfo.h:389
getStaticDeclName
static std::string getStaticDeclName(CodeGenModule &CGM, const VarDecl &D)
Definition: CGDecl.cpp:219
clang::CodeGen::EHScopeStack::stable_begin
stable_iterator stable_begin() const
Create a stable reference to the top of the EH stack.
Definition: EHScopeStack.h:384
clang::CodeGen::CodeGenFunction::Destroyer
void Destroyer(CodeGenFunction &CGF, Address addr, QualType ty)
Definition: CodeGenFunction.h:2036
clang::CompoundStmt::body
body_range body()
Definition: Stmt.h:1468
clang::CodeGen::CodeGenFunction::EmitAutoVarCleanups
void EmitAutoVarCleanups(const AutoVarEmission &emission)
Definition: CGDecl.cpp:2045
clang::CodeGen::CodeGenModule::getLLVMLinkageVarDefinition
llvm::GlobalValue::LinkageTypes getLLVMLinkageVarDefinition(const VarDecl *VD, bool IsConstant)
Returns LLVM linkage for a declarator.
Definition: CodeGenModule.cpp:5154
clang::GlobalDecl::getDecl
const Decl * getDecl() const
Definition: GlobalDecl.h:103
clang::CodeGen::CodeGenFunction::EmitStaticVarDecl
void EmitStaticVarDecl(const VarDecl &D, llvm::GlobalValue::LinkageTypes Linkage)
Definition: CGDecl.cpp:411
clang::CodeGen::CodeGenModule::getBlockMangledName
StringRef getBlockMangledName(GlobalDecl GD, const BlockDecl *BD)
Definition: CodeGenModule.cpp:1553
clang::CodeGen::ConstantEmitter
Definition: ConstantEmitter.h:23
clang::CodeGen::CodeGenFunction::EmitLValue
LValue EmitLValue(const Expr *E)
EmitLValue - Emit code to compute a designator that specifies the location of the expression.
Definition: CGExpr.cpp:1272
clang::CodeGen::CodeGenFunction::EmitARCInitWeak
void EmitARCInitWeak(Address addr, llvm::Value *value)
i8* @objc_initWeak(i8** addr, i8* value) Returns value.
Definition: CGObjC.cpp:2641
clang::ImplicitTypenameContext::Yes
@ Yes
clang::RecordType::getDecl
RecordDecl * getDecl() const
Definition: Type.h:4826
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:2736
clang::ASTContext::getPointerType
QualType getPointerType(QualType T) const
Return the uniqued reference to the type for a pointer to the specified type.
Definition: ASTContext.cpp:3341
clang::CodeGen::TargetCodeGenInfo::performAddrSpaceCast
virtual llvm::Value * performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, llvm::Value *V, LangAS SrcAddr, LangAS DestAddr, llvm::Type *DestTy, bool IsNonNull=false) const
Perform address space cast of an expression of pointer type.
Definition: TargetInfo.cpp:522
clang::CodeGen::CodeGenModule::getOMPAllocateAlignment
llvm::Optional< CharUnits > getOMPAllocateAlignment(const VarDecl *VD)
Return the alignment specified in an allocate directive, if present.
Definition: CGDecl.cpp:2701
clang::CodeGen::CodeGenFunction::pushStackRestore
void pushStackRestore(CleanupKind kind, Address SPMem)
Definition: CGDecl.cpp:2136
getDefinition
static const NamedDecl * getDefinition(const Decl *D)
Definition: SemaDecl.cpp:2973
clang::CharUnits
CharUnits - This is an opaque type for sizes expressed in character units.
Definition: CharUnits.h:38
clang::CodeGen::VarBypassDetector::IsBypassed
bool IsBypassed(const VarDecl *D) const
Returns true if the variable declaration was by bypassed by any goto or switch statement.
Definition: VarBypassDetector.h:57
clang::CodeGen::CodeGenFunction::EmitDecl
void EmitDecl(const Decl &D)
EmitDecl - Emit a declaration.
Definition: CGDecl.cpp:47
clang::CodeGen::CGOpenMPRuntime::DisableAutoDeclareTargetRAII
Allows to disable automatic handling of functions used in target regions as those marked as omp decla...
Definition: CGOpenMPRuntime.h:232
clang::ValueDecl::getType
QualType getType() const
Definition: Decl.h:712
clang::CodeGen::CodeGenFunction::CreateMemTemp
Address CreateMemTemp(QualType T, const Twine &Name="tmp", Address *Alloca=nullptr)
CreateMemTemp - Create a temporary memory object of the given type, with appropriate alignmen and cas...
Definition: CGExpr.cpp:135
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::CodeGen::CodeGenModule::setGVProperties
void setGVProperties(llvm::GlobalValue *GV, GlobalDecl GD) const
Set visibility, dllimport/dllexport and dso_local.
Definition: CodeGenModule.cpp:1256
clang::CXXDefaultInitExpr
A use of a default initializer in a constructor or in aggregate initialization.
Definition: ExprCXX.h:1325
clang::VarDecl::hasLocalStorage
bool hasLocalStorage() const
Returns true if a variable with function scope is a non-static local variable.
Definition: Decl.h:1131
clang::CodeGen::CodeGenTypeCache::Int8Ty
llvm::IntegerType * Int8Ty
i8, i16, i32, and i64
Definition: CodeGenTypeCache.h:37
clang::CodeGen::CodeGenModule::getLLVMLifetimeStartFn
llvm::Function * getLLVMLifetimeStartFn()
Lazily declare the @llvm.lifetime.start intrinsic.
Definition: CGDecl.cpp:2407
clang::CodeGen::CodeGenModule::getIntrinsic
llvm::Function * getIntrinsic(unsigned IID, ArrayRef< llvm::Type * > Tys=None)
Definition: CodeGenModule.cpp:5487
clang::CodeGen::CodeGenFunction::isInConditionalBranch
bool isInConditionalBranch() const
isInConditionalBranch - Return true if we're currently emitting one branch or the other of a conditio...
Definition: CodeGenFunction.h:1212
clang::NullabilityKind::NonNull
@ NonNull
Values of this type can never be null.
clang::VarDecl::isExceptionVariable
bool isExceptionVariable() const
Determine whether this variable is the exception variable in a C++ catch statememt or an Objective-C ...
Definition: Decl.h:1434
clang::Decl::getLocation
SourceLocation getLocation() const
Definition: DeclBase.h:432
clang::CodeGen::CallArgList
CallArgList - Type for representing both the value and type of arguments in a call.
Definition: CGCall.h:259
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1232
clang::CodeGen::CGBuilderTy::CreateMemSet
llvm::CallInst * CreateMemSet(Address Dest, llvm::Value *Value, llvm::Value *Size, bool IsVolatile=false)
Definition: CGBuilder.h:341
shouldUseMemSetToInitialize
static llvm::Value * shouldUseMemSetToInitialize(llvm::Constant *Init, uint64_t GlobalSize, const llvm::DataLayout &DL)
Decide whether we should use memset to initialize a local variable instead of using a memcpy from a c...
Definition: CGDecl.cpp:1001
clang::FunctionDecl
Represents a function declaration or definition.
Definition: Decl.h:1904
clang::CodeGen::CodeGenModule::setTLSMode
void setTLSMode(llvm::GlobalValue *GV, const VarDecl &D) const
Set the TLS mode for the given LLVM GlobalValue for the thread-local variable declaration D.
Definition: CodeGenModule.cpp:1298
clang::CodeGen::ConstantEmitter::finalize
void finalize(llvm::GlobalVariable *global)
Definition: CGExprConstant.cpp:1608
clang::CodeGen::CodeGenFunction::EmitBlock
void EmitBlock(llvm::BasicBlock *BB, bool IsFinished=false)
EmitBlock - Emit the given block.
Definition: CGStmt.cpp:563
clang::diag::kind
unsigned kind
All of the diagnostics that can be emitted by the frontend.
Definition: DiagnosticIDs.h:62
clang::CodeGen::CodeGenModule::getSanitizerMetadata
SanitizerMetadata * getSanitizerMetadata()
Definition: CodeGenModule.h:1366
clang::Expr::isConstantInitializer
bool isConstantInitializer(ASTContext &Ctx, bool ForRef, const Expr **Culprit=nullptr) const
isConstantInitializer - Returns true if this expression can be emitted to IR as a constant,...
Definition: Expr.cpp:3263
clang::CodeGen::CodeGenTypeCache::getASTAllocaAddressSpace
LangAS getASTAllocaAddressSpace() const
Definition: CodeGenTypeCache.h:124
clang::CXXConstructExpr
Represents a call to a C++ constructor.
Definition: ExprCXX.h:1467
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:791
clang::CodeGen::CodeGenModule::getLLVMContext
llvm::LLVMContext & getLLVMContext()
Definition: CodeGenModule.h:743
clang::CharUnits::getQuantity
QuantityType getQuantity() const
getQuantity - Get the raw integer representation of this quantity.
Definition: CharUnits.h:179
clang::CodeGen::CodeGenFunction::getLangOpts
const LangOptions & getLangOpts() const
Definition: CodeGenFunction.h:1997
clang::CodeGen::CodeGenModule::EmitOMPRequiresDecl
void EmitOMPRequiresDecl(const OMPRequiresDecl *D)
Emit a code for requires directive.
Definition: CGDecl.cpp:2642
clang::CodeGen::CodeGenFunction::emitBlockByrefAddress
Address emitBlockByrefAddress(Address baseAddr, const VarDecl *V, bool followForward=true)
BuildBlockByrefAddress - Computes the location of the data in a variable which is declared as __block...
Definition: CGBlocks.cpp:2572
clang::CodeGen::CodeGenFunction::EmitCheckTypeDescriptor
llvm::Constant * EmitCheckTypeDescriptor(QualType T)
Emit a description of a type in a format suitable for passing to a runtime sanitizer handler.
Definition: CGExpr.cpp:3044
clang::CodeGen::CGBuilderTy::CreateLoad
llvm::LoadInst * CreateLoad(Address Addr, const llvm::Twine &Name="")
Definition: CGBuilder.h:71
clang::CodeGen::LValue::setNonGC
void setNonGC(bool Value)
Definition: CGValue.h:291
clang::CodeGen::CodeGenFunction::saveValueInCond
DominatingValue< T >::saved_type saveValueInCond(T value)
Definition: CodeGenFunction.h:710
clang::CodeGen::CodeGenFunction::ReturnValue
Address ReturnValue
ReturnValue - The temporary alloca to hold the return value.
Definition: CodeGenFunction.h:356
clang::Decl::getDeclContext
DeclContext * getDeclContext()
Definition: DeclBase.h:441
clang::CodeGen::AggValueSlot::DoesNotOverlap
@ DoesNotOverlap
Definition: CGValue.h:553
clang::CodeGen::CodeGenFunction::EmitExtendGCLifetime
void EmitExtendGCLifetime(llvm::Value *object)
EmitExtendGCLifetime - Given a pointer to an Objective-C object, make sure it survives garbage collec...
Definition: CGObjC.cpp:3697
clang::CodeGen::CodeGenFunction::EmitARCRetainAutoreleaseScalarExpr
llvm::Value * EmitARCRetainAutoreleaseScalarExpr(const Expr *expr)
Definition: CGObjC.cpp:3489
clang::NamedDecl::getName
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:274